tcpmux.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2020 guylewin, [email protected]
  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. "strings"
  19. "github.com/fatedier/frp/pkg/config"
  20. "github.com/fatedier/frp/pkg/consts"
  21. "github.com/fatedier/frp/pkg/util/util"
  22. "github.com/fatedier/frp/pkg/util/vhost"
  23. )
  24. type TCPMuxProxy struct {
  25. *BaseProxy
  26. cfg *config.TCPMuxProxyConf
  27. }
  28. func (pxy *TCPMuxProxy) httpConnectListen(domain string, addrs []string) (_ []string, err error) {
  29. var l net.Listener
  30. if pxy.cfg.Group != "" {
  31. l, err = pxy.rc.TCPMuxGroupCtl.Listen(pxy.ctx, pxy.cfg.Multiplexer, pxy.cfg.Group, pxy.cfg.GroupKey, domain)
  32. } else {
  33. routeConfig := &vhost.RouteConfig{
  34. Domain: domain,
  35. }
  36. l, err = pxy.rc.TCPMuxHTTPConnectMuxer.Listen(pxy.ctx, routeConfig)
  37. }
  38. if err != nil {
  39. return nil, err
  40. }
  41. pxy.xl.Info("tcpmux httpconnect multiplexer listens for host [%s]", domain)
  42. pxy.listeners = append(pxy.listeners, l)
  43. return append(addrs, util.CanonicalAddr(domain, pxy.serverCfg.TCPMuxHTTPConnectPort)), nil
  44. }
  45. func (pxy *TCPMuxProxy) httpConnectRun() (remoteAddr string, err error) {
  46. addrs := make([]string, 0)
  47. for _, domain := range pxy.cfg.CustomDomains {
  48. if domain == "" {
  49. continue
  50. }
  51. addrs, err = pxy.httpConnectListen(domain, addrs)
  52. if err != nil {
  53. return "", err
  54. }
  55. }
  56. if pxy.cfg.SubDomain != "" {
  57. addrs, err = pxy.httpConnectListen(pxy.cfg.SubDomain+"."+pxy.serverCfg.SubDomainHost, addrs)
  58. if err != nil {
  59. return "", err
  60. }
  61. }
  62. pxy.startListenHandler(pxy, HandleUserTCPConnection)
  63. remoteAddr = strings.Join(addrs, ",")
  64. return remoteAddr, err
  65. }
  66. func (pxy *TCPMuxProxy) Run() (remoteAddr string, err error) {
  67. switch pxy.cfg.Multiplexer {
  68. case consts.HTTPConnectTCPMultiplexer:
  69. remoteAddr, err = pxy.httpConnectRun()
  70. default:
  71. err = fmt.Errorf("unknown multiplexer [%s]", pxy.cfg.Multiplexer)
  72. }
  73. if err != nil {
  74. pxy.Close()
  75. }
  76. return remoteAddr, err
  77. }
  78. func (pxy *TCPMuxProxy) GetConf() config.ProxyConf {
  79. return pxy.cfg
  80. }
  81. func (pxy *TCPMuxProxy) Close() {
  82. pxy.BaseProxy.Close()
  83. }