1
0

http2https.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. Director: func(req *http.Request) {
  45. req.URL.Scheme = "https"
  46. req.URL.Host = p.opts.LocalAddr
  47. if p.opts.HostHeaderRewrite != "" {
  48. req.Host = p.opts.HostHeaderRewrite
  49. }
  50. for k, v := range p.opts.RequestHeaders.Set {
  51. req.Header.Set(k, v)
  52. }
  53. },
  54. Transport: tr,
  55. }
  56. p.s = &http.Server{
  57. Handler: rp,
  58. ReadHeaderTimeout: 0,
  59. }
  60. go func() {
  61. _ = p.s.Serve(listener)
  62. }()
  63. return p, nil
  64. }
  65. func (p *HTTP2HTTPSPlugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, _ *ExtraInfo) {
  66. wrapConn := netpkg.WrapReadWriteCloserToConn(conn, realConn)
  67. _ = p.l.PutConn(wrapConn)
  68. }
  69. func (p *HTTP2HTTPSPlugin) Name() string {
  70. return v1.PluginHTTP2HTTPS
  71. }
  72. func (p *HTTP2HTTPSPlugin) Close() error {
  73. return p.s.Close()
  74. }