1
0

https2http.go 2.7 KB

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