https2https.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. v1 "github.com/fatedier/frp/pkg/config/v1"
  23. "github.com/fatedier/frp/pkg/transport"
  24. utilnet "github.com/fatedier/frp/pkg/util/net"
  25. )
  26. func init() {
  27. Register(v1.PluginHTTPS2HTTPS, NewHTTPS2HTTPSPlugin)
  28. }
  29. type HTTPS2HTTPSPlugin struct {
  30. opts *v1.HTTPS2HTTPSPluginOptions
  31. l *Listener
  32. s *http.Server
  33. }
  34. func NewHTTPS2HTTPSPlugin(options v1.ClientPluginOptions) (Plugin, error) {
  35. opts := options.(*v1.HTTPS2HTTPSPluginOptions)
  36. listener := NewProxyListener()
  37. p := &HTTPS2HTTPSPlugin{
  38. opts: opts,
  39. l: listener,
  40. }
  41. tr := &http.Transport{
  42. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  43. }
  44. rp := &httputil.ReverseProxy{
  45. Director: func(req *http.Request) {
  46. req.URL.Scheme = "https"
  47. req.URL.Host = p.opts.LocalAddr
  48. if p.opts.HostHeaderRewrite != "" {
  49. req.Host = p.opts.HostHeaderRewrite
  50. }
  51. for k, v := range p.opts.RequestHeaders.Set {
  52. req.Header.Set(k, v)
  53. }
  54. },
  55. Transport: tr,
  56. }
  57. p.s = &http.Server{
  58. Handler: rp,
  59. }
  60. var (
  61. tlsConfig *tls.Config
  62. err error
  63. )
  64. if opts.CrtPath != "" || opts.KeyPath != "" {
  65. tlsConfig, err = p.genTLSConfig()
  66. } else {
  67. tlsConfig, err = transport.NewServerTLSConfig("", "", "")
  68. tlsConfig.InsecureSkipVerify = true
  69. }
  70. if err != nil {
  71. return nil, fmt.Errorf("gen TLS config error: %v", err)
  72. }
  73. ln := tls.NewListener(listener, tlsConfig)
  74. go func() {
  75. _ = p.s.Serve(ln)
  76. }()
  77. return p, nil
  78. }
  79. func (p *HTTPS2HTTPSPlugin) genTLSConfig() (*tls.Config, error) {
  80. cert, err := tls.LoadX509KeyPair(p.opts.CrtPath, p.opts.KeyPath)
  81. if err != nil {
  82. return nil, err
  83. }
  84. config := &tls.Config{Certificates: []tls.Certificate{cert}}
  85. return config, nil
  86. }
  87. func (p *HTTPS2HTTPSPlugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, _ *ExtraInfo) {
  88. wrapConn := utilnet.WrapReadWriteCloserToConn(conn, realConn)
  89. _ = p.l.PutConn(wrapConn)
  90. }
  91. func (p *HTTPS2HTTPSPlugin) Name() string {
  92. return v1.PluginHTTPS2HTTPS
  93. }
  94. func (p *HTTPS2HTTPSPlugin) Close() error {
  95. return p.s.Close()
  96. }