https2http.go 3.2 KB

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