1
0

http2https.go 2.5 KB

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