1
0

https2https.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 PluginHTTPS2HTTPS = "https2https"
  27. func init() {
  28. Register(PluginHTTPS2HTTPS, NewHTTPS2HTTPSPlugin)
  29. }
  30. type HTTPS2HTTPSPlugin 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 NewHTTPS2HTTPSPlugin(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 := &HTTPS2HTTPSPlugin{
  58. crtPath: crtPath,
  59. keyPath: keyPath,
  60. localAddr: localAddr,
  61. hostHeaderRewrite: hostHeaderRewrite,
  62. headers: headers,
  63. l: listener,
  64. }
  65. tr := &http.Transport{
  66. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  67. }
  68. rp := &httputil.ReverseProxy{
  69. Director: func(req *http.Request) {
  70. req.URL.Scheme = "https"
  71. req.URL.Host = p.localAddr
  72. if p.hostHeaderRewrite != "" {
  73. req.Host = p.hostHeaderRewrite
  74. }
  75. for k, v := range p.headers {
  76. req.Header.Set(k, v)
  77. }
  78. },
  79. Transport: tr,
  80. }
  81. p.s = &http.Server{
  82. Handler: rp,
  83. }
  84. var (
  85. tlsConfig *tls.Config
  86. err error
  87. )
  88. if crtPath != "" || keyPath != "" {
  89. tlsConfig, err = p.genTLSConfig()
  90. } else {
  91. tlsConfig, err = transport.NewServerTLSConfig("", "", "")
  92. tlsConfig.InsecureSkipVerify = true
  93. }
  94. if err != nil {
  95. return nil, fmt.Errorf("gen TLS config error: %v", err)
  96. }
  97. ln := tls.NewListener(listener, tlsConfig)
  98. go func() {
  99. _ = p.s.Serve(ln)
  100. }()
  101. return p, nil
  102. }
  103. func (p *HTTPS2HTTPSPlugin) genTLSConfig() (*tls.Config, error) {
  104. cert, err := tls.LoadX509KeyPair(p.crtPath, p.keyPath)
  105. if err != nil {
  106. return nil, err
  107. }
  108. config := &tls.Config{Certificates: []tls.Certificate{cert}}
  109. return config, nil
  110. }
  111. func (p *HTTPS2HTTPSPlugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, extraBufToLocal []byte) {
  112. wrapConn := utilnet.WrapReadWriteCloserToConn(conn, realConn)
  113. _ = p.l.PutConn(wrapConn)
  114. }
  115. func (p *HTTPS2HTTPSPlugin) Name() string {
  116. return PluginHTTPS2HTTPS
  117. }
  118. func (p *HTTPS2HTTPSPlugin) Close() error {
  119. if err := p.s.Close(); err != nil {
  120. return err
  121. }
  122. return nil
  123. }