1
0

https2https.go 3.2 KB

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