tcpmux.go 2.4 KB

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