1
0

https2http.go 3.5 KB

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