1
0

https2http.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. //go:build !frps
  15. package plugin
  16. import (
  17. "crypto/tls"
  18. "fmt"
  19. "io"
  20. "net"
  21. "net/http"
  22. "net/http/httputil"
  23. v1 "github.com/fatedier/frp/pkg/config/v1"
  24. "github.com/fatedier/frp/pkg/transport"
  25. netpkg "github.com/fatedier/frp/pkg/util/net"
  26. )
  27. func init() {
  28. Register(v1.PluginHTTPS2HTTP, NewHTTPS2HTTPPlugin)
  29. }
  30. type HTTPS2HTTPPlugin struct {
  31. opts *v1.HTTPS2HTTPPluginOptions
  32. l *Listener
  33. s *http.Server
  34. }
  35. func NewHTTPS2HTTPPlugin(options v1.ClientPluginOptions) (Plugin, error) {
  36. opts := options.(*v1.HTTPS2HTTPPluginOptions)
  37. listener := NewProxyListener()
  38. p := &HTTPS2HTTPPlugin{
  39. opts: opts,
  40. l: listener,
  41. }
  42. rp := &httputil.ReverseProxy{
  43. Rewrite: func(r *httputil.ProxyRequest) {
  44. req := r.Out
  45. req.URL.Scheme = "http"
  46. req.URL.Host = p.opts.LocalAddr
  47. if p.opts.HostHeaderRewrite != "" {
  48. req.Host = p.opts.HostHeaderRewrite
  49. }
  50. for k, v := range p.opts.RequestHeaders.Set {
  51. req.Header.Set(k, v)
  52. }
  53. },
  54. }
  55. p.s = &http.Server{
  56. Handler: rp,
  57. }
  58. var (
  59. tlsConfig *tls.Config
  60. err error
  61. )
  62. if opts.CrtPath != "" || opts.KeyPath != "" {
  63. tlsConfig, err = p.genTLSConfig()
  64. } else {
  65. tlsConfig, err = transport.NewServerTLSConfig("", "", "")
  66. tlsConfig.InsecureSkipVerify = true
  67. }
  68. if err != nil {
  69. return nil, fmt.Errorf("gen TLS config error: %v", err)
  70. }
  71. ln := tls.NewListener(listener, tlsConfig)
  72. go func() {
  73. _ = p.s.Serve(ln)
  74. }()
  75. return p, nil
  76. }
  77. func (p *HTTPS2HTTPPlugin) genTLSConfig() (*tls.Config, error) {
  78. cert, err := tls.LoadX509KeyPair(p.opts.CrtPath, p.opts.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 net.Conn, _ *ExtraInfo) {
  86. wrapConn := netpkg.WrapReadWriteCloserToConn(conn, realConn)
  87. _ = p.l.PutConn(wrapConn)
  88. }
  89. func (p *HTTPS2HTTPPlugin) Name() string {
  90. return v1.PluginHTTPS2HTTP
  91. }
  92. func (p *HTTPS2HTTPPlugin) Close() error {
  93. return p.s.Close()
  94. }