1
0

https2https.go 3.6 KB

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