https2http.go 2.5 KB

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