tcpmux.go 3.1 KB

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