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