http2https.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. package plugin
  15. import (
  16. "crypto/tls"
  17. "fmt"
  18. "io"
  19. "net"
  20. "net/http"
  21. "net/http/httputil"
  22. "strings"
  23. frpNet "github.com/fatedier/frp/pkg/util/net"
  24. )
  25. const PluginHTTP2HTTPS = "http2https"
  26. func init() {
  27. Register(PluginHTTP2HTTPS, NewHTTP2HTTPSPlugin)
  28. }
  29. type HTTP2HTTPSPlugin struct {
  30. hostHeaderRewrite string
  31. localAddr string
  32. headers map[string]string
  33. l *Listener
  34. s *http.Server
  35. }
  36. func NewHTTP2HTTPSPlugin(params map[string]string) (Plugin, error) {
  37. localAddr := params["plugin_local_addr"]
  38. hostHeaderRewrite := params["plugin_host_header_rewrite"]
  39. headers := make(map[string]string)
  40. for k, v := range params {
  41. if !strings.HasPrefix(k, "plugin_header_") {
  42. continue
  43. }
  44. if k = strings.TrimPrefix(k, "plugin_header_"); k != "" {
  45. headers[k] = v
  46. }
  47. }
  48. if localAddr == "" {
  49. return nil, fmt.Errorf("plugin_local_addr is required")
  50. }
  51. listener := NewProxyListener()
  52. p := &HTTP2HTTPSPlugin{
  53. localAddr: localAddr,
  54. hostHeaderRewrite: hostHeaderRewrite,
  55. headers: headers,
  56. l: listener,
  57. }
  58. tr := &http.Transport{
  59. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  60. }
  61. rp := &httputil.ReverseProxy{
  62. Director: func(req *http.Request) {
  63. req.URL.Scheme = "https"
  64. req.URL.Host = p.localAddr
  65. if p.hostHeaderRewrite != "" {
  66. req.Host = p.hostHeaderRewrite
  67. }
  68. for k, v := range p.headers {
  69. req.Header.Set(k, v)
  70. }
  71. },
  72. Transport: tr,
  73. }
  74. p.s = &http.Server{
  75. Handler: rp,
  76. }
  77. go p.s.Serve(listener)
  78. return p, nil
  79. }
  80. func (p *HTTP2HTTPSPlugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, extraBufToLocal []byte) {
  81. wrapConn := frpNet.WrapReadWriteCloserToConn(conn, realConn)
  82. p.l.PutConn(wrapConn)
  83. }
  84. func (p *HTTP2HTTPSPlugin) Name() string {
  85. return PluginHTTP2HTTPS
  86. }
  87. func (p *HTTP2HTTPSPlugin) Close() error {
  88. if err := p.s.Close(); err != nil {
  89. return err
  90. }
  91. return nil
  92. }