1
0

http2https.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "io"
  19. "net"
  20. "net/http"
  21. "net/http/httputil"
  22. v1 "github.com/fatedier/frp/pkg/config/v1"
  23. netpkg "github.com/fatedier/frp/pkg/util/net"
  24. )
  25. func init() {
  26. Register(v1.PluginHTTP2HTTPS, NewHTTP2HTTPSPlugin)
  27. }
  28. type HTTP2HTTPSPlugin struct {
  29. opts *v1.HTTP2HTTPSPluginOptions
  30. l *Listener
  31. s *http.Server
  32. }
  33. func NewHTTP2HTTPSPlugin(options v1.ClientPluginOptions) (Plugin, error) {
  34. opts := options.(*v1.HTTP2HTTPSPluginOptions)
  35. listener := NewProxyListener()
  36. p := &HTTP2HTTPSPlugin{
  37. opts: opts,
  38. l: listener,
  39. }
  40. tr := &http.Transport{
  41. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  42. }
  43. rp := &httputil.ReverseProxy{
  44. Rewrite: func(r *httputil.ProxyRequest) {
  45. r.Out.Header["X-Forwarded-For"] = r.In.Header["X-Forwarded-For"]
  46. r.Out.Header["X-Forwarded-Host"] = r.In.Header["X-Forwarded-Host"]
  47. r.Out.Header["X-Forwarded-Proto"] = r.In.Header["X-Forwarded-Proto"]
  48. req := r.Out
  49. req.URL.Scheme = "https"
  50. req.URL.Host = p.opts.LocalAddr
  51. if p.opts.HostHeaderRewrite != "" {
  52. req.Host = p.opts.HostHeaderRewrite
  53. }
  54. for k, v := range p.opts.RequestHeaders.Set {
  55. req.Header.Set(k, v)
  56. }
  57. },
  58. Transport: tr,
  59. }
  60. p.s = &http.Server{
  61. Handler: rp,
  62. ReadHeaderTimeout: 0,
  63. }
  64. go func() {
  65. _ = p.s.Serve(listener)
  66. }()
  67. return p, nil
  68. }
  69. func (p *HTTP2HTTPSPlugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, _ *ExtraInfo) {
  70. wrapConn := netpkg.WrapReadWriteCloserToConn(conn, realConn)
  71. _ = p.l.PutConn(wrapConn)
  72. }
  73. func (p *HTTP2HTTPSPlugin) Name() string {
  74. return v1.PluginHTTP2HTTPS
  75. }
  76. func (p *HTTP2HTTPSPlugin) Close() error {
  77. return p.s.Close()
  78. }