1
0

https2http.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.PluginHTTPS2HTTP, NewHTTPS2HTTPPlugin)
  33. }
  34. type HTTPS2HTTPPlugin struct {
  35. opts *v1.HTTPS2HTTPPluginOptions
  36. l *Listener
  37. s *http.Server
  38. }
  39. func NewHTTPS2HTTPPlugin(options v1.ClientPluginOptions) (Plugin, error) {
  40. opts := options.(*v1.HTTPS2HTTPPluginOptions)
  41. listener := NewProxyListener()
  42. p := &HTTPS2HTTPPlugin{
  43. opts: opts,
  44. l: listener,
  45. }
  46. rp := &httputil.ReverseProxy{
  47. Rewrite: func(r *httputil.ProxyRequest) {
  48. r.Out.Header["X-Forwarded-For"] = r.In.Header["X-Forwarded-For"]
  49. r.SetXForwarded()
  50. req := r.Out
  51. req.URL.Scheme = "http"
  52. req.URL.Host = p.opts.LocalAddr
  53. if p.opts.HostHeaderRewrite != "" {
  54. req.Host = p.opts.HostHeaderRewrite
  55. }
  56. for k, v := range p.opts.RequestHeaders.Set {
  57. req.Header.Set(k, v)
  58. }
  59. },
  60. BufferPool: pool.NewBuffer(32 * 1024),
  61. ErrorLog: stdlog.New(log.NewWriteLogger(log.WarnLevel, 2), "", 0),
  62. }
  63. var (
  64. tlsConfig *tls.Config
  65. err error
  66. )
  67. if opts.CrtPath != "" || opts.KeyPath != "" {
  68. tlsConfig, err = p.genTLSConfig()
  69. } else {
  70. tlsConfig, err = transport.NewServerTLSConfig("", "", "")
  71. tlsConfig.InsecureSkipVerify = true
  72. }
  73. if err != nil {
  74. return nil, fmt.Errorf("gen TLS config error: %v", err)
  75. }
  76. p.s = &http.Server{
  77. Handler: rp,
  78. ReadHeaderTimeout: 60 * time.Second,
  79. TLSConfig: tlsConfig,
  80. }
  81. go func() {
  82. _ = p.s.ServeTLS(listener, "", "")
  83. }()
  84. return p, nil
  85. }
  86. func (p *HTTPS2HTTPPlugin) genTLSConfig() (*tls.Config, error) {
  87. cert, err := tls.LoadX509KeyPair(p.opts.CrtPath, p.opts.KeyPath)
  88. if err != nil {
  89. return nil, err
  90. }
  91. config := &tls.Config{Certificates: []tls.Certificate{cert}}
  92. return config, nil
  93. }
  94. func (p *HTTPS2HTTPPlugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, extra *ExtraInfo) {
  95. wrapConn := netpkg.WrapReadWriteCloserToConn(conn, realConn)
  96. if extra.SrcAddr != nil {
  97. wrapConn.SetRemoteAddr(extra.SrcAddr)
  98. }
  99. _ = p.l.PutConn(wrapConn)
  100. }
  101. func (p *HTTPS2HTTPPlugin) Name() string {
  102. return v1.PluginHTTPS2HTTP
  103. }
  104. func (p *HTTPS2HTTPPlugin) Close() error {
  105. return p.s.Close()
  106. }