https2https.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 plugin
  16. import (
  17. "context"
  18. "crypto/tls"
  19. "fmt"
  20. "io"
  21. stdlog "log"
  22. "net"
  23. "net/http"
  24. "net/http/httputil"
  25. "time"
  26. "github.com/fatedier/golib/pool"
  27. "github.com/samber/lo"
  28. v1 "github.com/fatedier/frp/pkg/config/v1"
  29. "github.com/fatedier/frp/pkg/transport"
  30. httppkg "github.com/fatedier/frp/pkg/util/http"
  31. "github.com/fatedier/frp/pkg/util/log"
  32. netpkg "github.com/fatedier/frp/pkg/util/net"
  33. )
  34. func init() {
  35. Register(v1.PluginHTTPS2HTTPS, NewHTTPS2HTTPSPlugin)
  36. }
  37. type HTTPS2HTTPSPlugin struct {
  38. opts *v1.HTTPS2HTTPSPluginOptions
  39. l *Listener
  40. s *http.Server
  41. }
  42. func NewHTTPS2HTTPSPlugin(options v1.ClientPluginOptions) (Plugin, error) {
  43. opts := options.(*v1.HTTPS2HTTPSPluginOptions)
  44. listener := NewProxyListener()
  45. p := &HTTPS2HTTPSPlugin{
  46. opts: opts,
  47. l: listener,
  48. }
  49. tr := &http.Transport{
  50. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  51. }
  52. rp := &httputil.ReverseProxy{
  53. Rewrite: func(r *httputil.ProxyRequest) {
  54. r.Out.Header["X-Forwarded-For"] = r.In.Header["X-Forwarded-For"]
  55. r.SetXForwarded()
  56. req := r.Out
  57. req.URL.Scheme = "https"
  58. req.URL.Host = p.opts.LocalAddr
  59. if p.opts.HostHeaderRewrite != "" {
  60. req.Host = p.opts.HostHeaderRewrite
  61. }
  62. for k, v := range p.opts.RequestHeaders.Set {
  63. req.Header.Set(k, v)
  64. }
  65. },
  66. Transport: tr,
  67. BufferPool: pool.NewBuffer(32 * 1024),
  68. ErrorLog: stdlog.New(log.NewWriteLogger(log.WarnLevel, 2), "", 0),
  69. }
  70. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  71. if r.TLS != nil {
  72. tlsServerName, _ := httppkg.CanonicalHost(r.TLS.ServerName)
  73. host, _ := httppkg.CanonicalHost(r.Host)
  74. if tlsServerName != "" && tlsServerName != host {
  75. w.WriteHeader(http.StatusMisdirectedRequest)
  76. return
  77. }
  78. }
  79. rp.ServeHTTP(w, r)
  80. })
  81. tlsConfig, err := transport.NewServerTLSConfig(p.opts.CrtPath, p.opts.KeyPath, "")
  82. if err != nil {
  83. return nil, fmt.Errorf("gen TLS config error: %v", err)
  84. }
  85. p.s = &http.Server{
  86. Handler: handler,
  87. ReadHeaderTimeout: 60 * time.Second,
  88. TLSConfig: tlsConfig,
  89. }
  90. if !lo.FromPtr(opts.EnableHTTP2) {
  91. p.s.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
  92. }
  93. go func() {
  94. _ = p.s.ServeTLS(listener, "", "")
  95. }()
  96. return p, nil
  97. }
  98. func (p *HTTPS2HTTPSPlugin) Handle(_ context.Context, conn io.ReadWriteCloser, realConn net.Conn, extra *ExtraInfo) {
  99. wrapConn := netpkg.WrapReadWriteCloserToConn(conn, realConn)
  100. if extra.SrcAddr != nil {
  101. wrapConn.SetRemoteAddr(extra.SrcAddr)
  102. }
  103. _ = p.l.PutConn(wrapConn)
  104. }
  105. func (p *HTTPS2HTTPSPlugin) Name() string {
  106. return v1.PluginHTTPS2HTTPS
  107. }
  108. func (p *HTTPS2HTTPSPlugin) Close() error {
  109. return p.s.Close()
  110. }