https2http.go 3.0 KB

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