https2http.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.PluginHTTPS2HTTP, NewHTTPS2HTTPPlugin)
  36. }
  37. type HTTPS2HTTPPlugin struct {
  38. opts *v1.HTTPS2HTTPPluginOptions
  39. l *Listener
  40. s *http.Server
  41. }
  42. func NewHTTPS2HTTPPlugin(options v1.ClientPluginOptions) (Plugin, error) {
  43. opts := options.(*v1.HTTPS2HTTPPluginOptions)
  44. listener := NewProxyListener()
  45. p := &HTTPS2HTTPPlugin{
  46. opts: opts,
  47. l: listener,
  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 = "http"
  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. BufferPool: pool.NewBuffer(32 * 1024),
  64. ErrorLog: stdlog.New(log.NewWriteLogger(log.WarnLevel, 2), "", 0),
  65. }
  66. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  67. if r.TLS != nil {
  68. tlsServerName, _ := httppkg.CanonicalHost(r.TLS.ServerName)
  69. host, _ := httppkg.CanonicalHost(r.Host)
  70. if tlsServerName != "" && tlsServerName != host {
  71. w.WriteHeader(http.StatusMisdirectedRequest)
  72. return
  73. }
  74. }
  75. rp.ServeHTTP(w, r)
  76. })
  77. tlsConfig, err := transport.NewServerTLSConfig(p.opts.CrtPath, p.opts.KeyPath, "")
  78. if err != nil {
  79. return nil, fmt.Errorf("gen TLS config error: %v", err)
  80. }
  81. p.s = &http.Server{
  82. Handler: handler,
  83. ReadHeaderTimeout: 60 * time.Second,
  84. TLSConfig: tlsConfig,
  85. }
  86. if !lo.FromPtr(opts.EnableHTTP2) {
  87. p.s.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
  88. }
  89. go func() {
  90. _ = p.s.ServeTLS(listener, "", "")
  91. }()
  92. return p, nil
  93. }
  94. func (p *HTTPS2HTTPPlugin) Handle(_ context.Context, 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. }