https2https.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. v1 "github.com/fatedier/frp/pkg/config/v1"
  27. "github.com/fatedier/frp/pkg/transport"
  28. "github.com/fatedier/frp/pkg/util/log"
  29. netpkg "github.com/fatedier/frp/pkg/util/net"
  30. )
  31. func init() {
  32. Register(v1.PluginHTTPS2HTTPS, NewHTTPS2HTTPSPlugin)
  33. }
  34. type HTTPS2HTTPSPlugin struct {
  35. opts *v1.HTTPS2HTTPSPluginOptions
  36. l *Listener
  37. s *http.Server
  38. }
  39. func NewHTTPS2HTTPSPlugin(options v1.ClientPluginOptions) (Plugin, error) {
  40. opts := options.(*v1.HTTPS2HTTPSPluginOptions)
  41. listener := NewProxyListener()
  42. p := &HTTPS2HTTPSPlugin{
  43. opts: opts,
  44. l: listener,
  45. }
  46. tr := &http.Transport{
  47. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  48. }
  49. rp := &httputil.ReverseProxy{
  50. Rewrite: func(r *httputil.ProxyRequest) {
  51. r.Out.Header["X-Forwarded-For"] = r.In.Header["X-Forwarded-For"]
  52. r.SetXForwarded()
  53. req := r.Out
  54. req.URL.Scheme = "https"
  55. req.URL.Host = p.opts.LocalAddr
  56. if p.opts.HostHeaderRewrite != "" {
  57. req.Host = p.opts.HostHeaderRewrite
  58. }
  59. for k, v := range p.opts.RequestHeaders.Set {
  60. req.Header.Set(k, v)
  61. }
  62. },
  63. Transport: tr,
  64. BufferPool: pool.NewBuffer(32 * 1024),
  65. ErrorLog: stdlog.New(log.NewWriteLogger(log.WarnLevel, 2), "", 0),
  66. }
  67. var (
  68. tlsConfig *tls.Config
  69. err error
  70. )
  71. if opts.CrtPath != "" || opts.KeyPath != "" {
  72. tlsConfig, err = p.genTLSConfig()
  73. } else {
  74. tlsConfig, err = transport.NewServerTLSConfig("", "", "")
  75. tlsConfig.InsecureSkipVerify = true
  76. }
  77. if err != nil {
  78. return nil, fmt.Errorf("gen TLS config error: %v", err)
  79. }
  80. p.s = &http.Server{
  81. Handler: rp,
  82. ReadHeaderTimeout: 60 * time.Second,
  83. TLSConfig: tlsConfig,
  84. }
  85. go func() {
  86. _ = p.s.ServeTLS(listener, "", "")
  87. }()
  88. return p, nil
  89. }
  90. func (p *HTTPS2HTTPSPlugin) genTLSConfig() (*tls.Config, error) {
  91. cert, err := tls.LoadX509KeyPair(p.opts.CrtPath, p.opts.KeyPath)
  92. if err != nil {
  93. return nil, err
  94. }
  95. config := &tls.Config{Certificates: []tls.Certificate{cert}}
  96. return config, nil
  97. }
  98. func (p *HTTPS2HTTPSPlugin) Handle(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. }