https2http.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 PluginHTTPS2HTTP = "https2http"
  26. func init() {
  27. Register(PluginHTTPS2HTTP, NewHTTPS2HTTPPlugin)
  28. }
  29. type HTTPS2HTTPPlugin struct {
  30. crtPath string
  31. keyPath string
  32. hostHeaderRewrite string
  33. localAddr string
  34. headers map[string]string
  35. l *Listener
  36. s *http.Server
  37. }
  38. func NewHTTPS2HTTPPlugin(params map[string]string) (Plugin, error) {
  39. crtPath := params["plugin_crt_path"]
  40. keyPath := params["plugin_key_path"]
  41. localAddr := params["plugin_local_addr"]
  42. hostHeaderRewrite := params["plugin_host_header_rewrite"]
  43. headers := make(map[string]string)
  44. for k, v := range params {
  45. if !strings.HasPrefix(k, "plugin_header_") {
  46. continue
  47. }
  48. if k = strings.TrimPrefix(k, "plugin_header_"); k != "" {
  49. headers[k] = v
  50. }
  51. }
  52. if crtPath == "" {
  53. return nil, fmt.Errorf("plugin_crt_path is required")
  54. }
  55. if keyPath == "" {
  56. return nil, fmt.Errorf("plugin_key_path is required")
  57. }
  58. if localAddr == "" {
  59. return nil, fmt.Errorf("plugin_local_addr is required")
  60. }
  61. listener := NewProxyListener()
  62. p := &HTTPS2HTTPPlugin{
  63. crtPath: crtPath,
  64. keyPath: keyPath,
  65. localAddr: localAddr,
  66. hostHeaderRewrite: hostHeaderRewrite,
  67. headers: headers,
  68. l: listener,
  69. }
  70. rp := &httputil.ReverseProxy{
  71. Director: func(req *http.Request) {
  72. req.URL.Scheme = "http"
  73. req.URL.Host = p.localAddr
  74. if p.hostHeaderRewrite != "" {
  75. req.Host = p.hostHeaderRewrite
  76. }
  77. for k, v := range p.headers {
  78. req.Header.Set(k, v)
  79. }
  80. },
  81. }
  82. p.s = &http.Server{
  83. Handler: rp,
  84. }
  85. tlsConfig, err := p.genTLSConfig()
  86. if err != nil {
  87. return nil, fmt.Errorf("gen TLS config error: %v", err)
  88. }
  89. ln := tls.NewListener(listener, tlsConfig)
  90. go p.s.Serve(ln)
  91. return p, nil
  92. }
  93. func (p *HTTPS2HTTPPlugin) genTLSConfig() (*tls.Config, error) {
  94. cert, err := tls.LoadX509KeyPair(p.crtPath, p.keyPath)
  95. if err != nil {
  96. return nil, err
  97. }
  98. config := &tls.Config{Certificates: []tls.Certificate{cert}}
  99. return config, nil
  100. }
  101. func (p *HTTPS2HTTPPlugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, extraBufToLocal []byte) {
  102. wrapConn := frpNet.WrapReadWriteCloserToConn(conn, realConn)
  103. p.l.PutConn(wrapConn)
  104. }
  105. func (p *HTTPS2HTTPPlugin) Name() string {
  106. return PluginHTTPS2HTTP
  107. }
  108. func (p *HTTPS2HTTPPlugin) Close() error {
  109. if err := p.s.Close(); err != nil {
  110. return err
  111. }
  112. return nil
  113. }