http2https.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "io"
  20. stdlog "log"
  21. "net"
  22. "net/http"
  23. "net/http/httputil"
  24. "github.com/fatedier/golib/pool"
  25. v1 "github.com/fatedier/frp/pkg/config/v1"
  26. "github.com/fatedier/frp/pkg/util/log"
  27. netpkg "github.com/fatedier/frp/pkg/util/net"
  28. )
  29. func init() {
  30. Register(v1.PluginHTTP2HTTPS, NewHTTP2HTTPSPlugin)
  31. }
  32. type HTTP2HTTPSPlugin struct {
  33. opts *v1.HTTP2HTTPSPluginOptions
  34. l *Listener
  35. s *http.Server
  36. }
  37. func NewHTTP2HTTPSPlugin(options v1.ClientPluginOptions) (Plugin, error) {
  38. opts := options.(*v1.HTTP2HTTPSPluginOptions)
  39. listener := NewProxyListener()
  40. p := &HTTP2HTTPSPlugin{
  41. opts: opts,
  42. l: listener,
  43. }
  44. tr := &http.Transport{
  45. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  46. }
  47. rp := &httputil.ReverseProxy{
  48. Rewrite: func(r *httputil.ProxyRequest) {
  49. r.Out.Header["X-Forwarded-For"] = r.In.Header["X-Forwarded-For"]
  50. r.Out.Header["X-Forwarded-Host"] = r.In.Header["X-Forwarded-Host"]
  51. r.Out.Header["X-Forwarded-Proto"] = r.In.Header["X-Forwarded-Proto"]
  52. req := r.Out
  53. req.URL.Scheme = "https"
  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. Transport: tr,
  63. BufferPool: pool.NewBuffer(32 * 1024),
  64. ErrorLog: stdlog.New(log.NewWriteLogger(log.WarnLevel, 2), "", 0),
  65. }
  66. p.s = &http.Server{
  67. Handler: rp,
  68. ReadHeaderTimeout: 0,
  69. }
  70. go func() {
  71. _ = p.s.Serve(listener)
  72. }()
  73. return p, nil
  74. }
  75. func (p *HTTP2HTTPSPlugin) Handle(_ context.Context, conn io.ReadWriteCloser, realConn net.Conn, _ *ExtraInfo) {
  76. wrapConn := netpkg.WrapReadWriteCloserToConn(conn, realConn)
  77. _ = p.l.PutConn(wrapConn)
  78. }
  79. func (p *HTTP2HTTPSPlugin) Name() string {
  80. return v1.PluginHTTP2HTTPS
  81. }
  82. func (p *HTTP2HTTPSPlugin) Close() error {
  83. return p.s.Close()
  84. }