https2http.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2019 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build !frps
  15. package client
  16. import (
  17. "context"
  18. "crypto/tls"
  19. "fmt"
  20. stdlog "log"
  21. "net/http"
  22. "net/http/httputil"
  23. "time"
  24. "github.com/fatedier/golib/pool"
  25. "github.com/samber/lo"
  26. v1 "github.com/fatedier/frp/pkg/config/v1"
  27. "github.com/fatedier/frp/pkg/transport"
  28. httppkg "github.com/fatedier/frp/pkg/util/http"
  29. "github.com/fatedier/frp/pkg/util/log"
  30. netpkg "github.com/fatedier/frp/pkg/util/net"
  31. )
  32. func init() {
  33. Register(v1.PluginHTTPS2HTTP, NewHTTPS2HTTPPlugin)
  34. }
  35. type HTTPS2HTTPPlugin struct {
  36. opts *v1.HTTPS2HTTPPluginOptions
  37. l *Listener
  38. s *http.Server
  39. }
  40. func NewHTTPS2HTTPPlugin(_ PluginContext, options v1.ClientPluginOptions) (Plugin, error) {
  41. opts := options.(*v1.HTTPS2HTTPPluginOptions)
  42. listener := NewProxyListener()
  43. p := &HTTPS2HTTPPlugin{
  44. opts: opts,
  45. l: listener,
  46. }
  47. rp := &httputil.ReverseProxy{
  48. Rewrite: func(r *httputil.ProxyRequest) {
  49. r.Out.Header["X-Forwarded-For"] = r.In.Header["X-Forwarded-For"]
  50. r.SetXForwarded()
  51. req := r.Out
  52. req.URL.Scheme = "http"
  53. req.URL.Host = p.opts.LocalAddr
  54. if p.opts.HostHeaderRewrite != "" {
  55. req.Host = p.opts.HostHeaderRewrite
  56. }
  57. for k, v := range p.opts.RequestHeaders.Set {
  58. req.Header.Set(k, v)
  59. }
  60. },
  61. BufferPool: pool.NewBuffer(32 * 1024),
  62. ErrorLog: stdlog.New(log.NewWriteLogger(log.WarnLevel, 2), "", 0),
  63. }
  64. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  65. if r.TLS != nil {
  66. tlsServerName, _ := httppkg.CanonicalHost(r.TLS.ServerName)
  67. host, _ := httppkg.CanonicalHost(r.Host)
  68. if tlsServerName != "" && tlsServerName != host {
  69. w.WriteHeader(http.StatusMisdirectedRequest)
  70. return
  71. }
  72. }
  73. rp.ServeHTTP(w, r)
  74. })
  75. tlsConfig, err := transport.NewServerTLSConfig(p.opts.CrtPath, p.opts.KeyPath, "")
  76. if err != nil {
  77. return nil, fmt.Errorf("gen TLS config error: %v", err)
  78. }
  79. p.s = &http.Server{
  80. Handler: handler,
  81. ReadHeaderTimeout: 60 * time.Second,
  82. TLSConfig: tlsConfig,
  83. }
  84. if !lo.FromPtr(opts.EnableHTTP2) {
  85. p.s.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
  86. }
  87. go func() {
  88. _ = p.s.ServeTLS(listener, "", "")
  89. }()
  90. return p, nil
  91. }
  92. func (p *HTTPS2HTTPPlugin) Handle(_ context.Context, connInfo *ConnectionInfo) {
  93. wrapConn := netpkg.WrapReadWriteCloserToConn(connInfo.Conn, connInfo.UnderlyingConn)
  94. if connInfo.SrcAddr != nil {
  95. wrapConn.SetRemoteAddr(connInfo.SrcAddr)
  96. }
  97. _ = p.l.PutConn(wrapConn)
  98. }
  99. func (p *HTTPS2HTTPPlugin) Name() string {
  100. return v1.PluginHTTPS2HTTP
  101. }
  102. func (p *HTTPS2HTTPPlugin) Close() error {
  103. return p.s.Close()
  104. }