1
0

tcpmux.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2020 guylewin, guy@lewin.co.il
  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 proxy
  15. import (
  16. "fmt"
  17. "net"
  18. "reflect"
  19. "strings"
  20. v1 "github.com/fatedier/frp/pkg/config/v1"
  21. "github.com/fatedier/frp/pkg/consts"
  22. "github.com/fatedier/frp/pkg/util/util"
  23. "github.com/fatedier/frp/pkg/util/vhost"
  24. )
  25. func init() {
  26. RegisterProxyFactory(reflect.TypeOf(&v1.TCPMuxProxyConfig{}), NewTCPMuxProxy)
  27. }
  28. type TCPMuxProxy struct {
  29. *BaseProxy
  30. cfg *v1.TCPMuxProxyConfig
  31. }
  32. func NewTCPMuxProxy(baseProxy *BaseProxy) Proxy {
  33. unwrapped, ok := baseProxy.GetConfigurer().(*v1.TCPMuxProxyConfig)
  34. if !ok {
  35. return nil
  36. }
  37. return &TCPMuxProxy{
  38. BaseProxy: baseProxy,
  39. cfg: unwrapped,
  40. }
  41. }
  42. func (pxy *TCPMuxProxy) httpConnectListen(
  43. domain, routeByHTTPUser, httpUser, httpPwd string, addrs []string) ([]string, error,
  44. ) {
  45. var l net.Listener
  46. var err error
  47. routeConfig := &vhost.RouteConfig{
  48. Domain: domain,
  49. RouteByHTTPUser: routeByHTTPUser,
  50. Username: httpUser,
  51. Password: httpPwd,
  52. }
  53. if pxy.cfg.LoadBalancer.Group != "" {
  54. l, err = pxy.rc.TCPMuxGroupCtl.Listen(pxy.ctx, pxy.cfg.Multiplexer,
  55. pxy.cfg.LoadBalancer.Group, pxy.cfg.LoadBalancer.GroupKey, *routeConfig)
  56. } else {
  57. l, err = pxy.rc.TCPMuxHTTPConnectMuxer.Listen(pxy.ctx, routeConfig)
  58. }
  59. if err != nil {
  60. return nil, err
  61. }
  62. pxy.xl.Info("tcpmux httpconnect multiplexer listens for host [%s], group [%s] routeByHTTPUser [%s]",
  63. domain, pxy.cfg.LoadBalancer.Group, pxy.cfg.RouteByHTTPUser)
  64. pxy.listeners = append(pxy.listeners, l)
  65. return append(addrs, util.CanonicalAddr(domain, pxy.serverCfg.TCPMuxHTTPConnectPort)), nil
  66. }
  67. func (pxy *TCPMuxProxy) httpConnectRun() (remoteAddr string, err error) {
  68. addrs := make([]string, 0)
  69. for _, domain := range pxy.cfg.CustomDomains {
  70. if domain == "" {
  71. continue
  72. }
  73. addrs, err = pxy.httpConnectListen(domain, pxy.cfg.RouteByHTTPUser, pxy.cfg.HTTPUser, pxy.cfg.HTTPPassword, addrs)
  74. if err != nil {
  75. return "", err
  76. }
  77. }
  78. if pxy.cfg.SubDomain != "" {
  79. addrs, err = pxy.httpConnectListen(pxy.cfg.SubDomain+"."+pxy.serverCfg.SubDomainHost,
  80. pxy.cfg.RouteByHTTPUser, pxy.cfg.HTTPUser, pxy.cfg.HTTPPassword, addrs)
  81. if err != nil {
  82. return "", err
  83. }
  84. }
  85. pxy.startCommonTCPListenersHandler()
  86. remoteAddr = strings.Join(addrs, ",")
  87. return remoteAddr, err
  88. }
  89. func (pxy *TCPMuxProxy) Run() (remoteAddr string, err error) {
  90. switch pxy.cfg.Multiplexer {
  91. case consts.HTTPConnectTCPMultiplexer:
  92. remoteAddr, err = pxy.httpConnectRun()
  93. default:
  94. err = fmt.Errorf("unknown multiplexer [%s]", pxy.cfg.Multiplexer)
  95. }
  96. if err != nil {
  97. pxy.Close()
  98. }
  99. return remoteAddr, err
  100. }
  101. func (pxy *TCPMuxProxy) Close() {
  102. pxy.BaseProxy.Close()
  103. }