2020-03-05 14:47:49 +01:00
|
|
|
// Copyright 2020 guylewin, guy@lewin.co.il
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-20 07:35:47 +02:00
|
|
|
"net"
|
2020-03-05 14:47:49 +01:00
|
|
|
"strings"
|
|
|
|
|
2020-09-23 07:49:14 +02:00
|
|
|
"github.com/fatedier/frp/pkg/config"
|
|
|
|
"github.com/fatedier/frp/pkg/consts"
|
|
|
|
"github.com/fatedier/frp/pkg/util/util"
|
|
|
|
"github.com/fatedier/frp/pkg/util/vhost"
|
2020-03-05 14:47:49 +01:00
|
|
|
)
|
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
type TCPMuxProxy struct {
|
2020-03-05 14:47:49 +01:00
|
|
|
*BaseProxy
|
2020-05-24 11:48:37 +02:00
|
|
|
cfg *config.TCPMuxProxyConf
|
2020-03-05 14:47:49 +01:00
|
|
|
}
|
|
|
|
|
2022-05-26 17:57:30 +02:00
|
|
|
func (pxy *TCPMuxProxy) httpConnectListen(domain, routeByHTTPUser string, addrs []string) ([]string, error) {
|
2020-04-20 07:35:47 +02:00
|
|
|
var l net.Listener
|
2022-05-26 17:57:30 +02:00
|
|
|
var err error
|
|
|
|
routeConfig := &vhost.RouteConfig{
|
|
|
|
Domain: domain,
|
|
|
|
RouteByHTTPUser: routeByHTTPUser,
|
|
|
|
}
|
2020-04-20 07:35:47 +02:00
|
|
|
if pxy.cfg.Group != "" {
|
2022-05-26 17:57:30 +02:00
|
|
|
l, err = pxy.rc.TCPMuxGroupCtl.Listen(pxy.ctx, pxy.cfg.Multiplexer, pxy.cfg.Group, pxy.cfg.GroupKey, *routeConfig)
|
2020-04-20 07:35:47 +02:00
|
|
|
} else {
|
2020-05-24 11:48:37 +02:00
|
|
|
l, err = pxy.rc.TCPMuxHTTPConnectMuxer.Listen(pxy.ctx, routeConfig)
|
2020-03-05 14:47:49 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-26 17:57:30 +02:00
|
|
|
pxy.xl.Info("tcpmux httpconnect multiplexer listens for host [%s], group [%s] routeByHTTPUser [%s]",
|
|
|
|
domain, pxy.cfg.Group, pxy.cfg.RouteByHTTPUser)
|
2020-03-05 14:47:49 +01:00
|
|
|
pxy.listeners = append(pxy.listeners, l)
|
2020-05-24 11:48:37 +02:00
|
|
|
return append(addrs, util.CanonicalAddr(domain, pxy.serverCfg.TCPMuxHTTPConnectPort)), nil
|
2020-03-05 14:47:49 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
func (pxy *TCPMuxProxy) httpConnectRun() (remoteAddr string, err error) {
|
2020-03-05 14:47:49 +01:00
|
|
|
addrs := make([]string, 0)
|
|
|
|
for _, domain := range pxy.cfg.CustomDomains {
|
|
|
|
if domain == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-26 17:57:30 +02:00
|
|
|
addrs, err = pxy.httpConnectListen(domain, pxy.cfg.RouteByHTTPUser, addrs)
|
2020-03-05 14:47:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if pxy.cfg.SubDomain != "" {
|
2022-05-26 17:57:30 +02:00
|
|
|
addrs, err = pxy.httpConnectListen(pxy.cfg.SubDomain+"."+pxy.serverCfg.SubDomainHost, pxy.cfg.RouteByHTTPUser, addrs)
|
2020-03-05 14:47:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
pxy.startListenHandler(pxy, HandleUserTCPConnection)
|
2020-03-05 14:47:49 +01:00
|
|
|
remoteAddr = strings.Join(addrs, ",")
|
|
|
|
return remoteAddr, err
|
|
|
|
}
|
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
func (pxy *TCPMuxProxy) Run() (remoteAddr string, err error) {
|
2020-03-05 14:47:49 +01:00
|
|
|
switch pxy.cfg.Multiplexer {
|
2020-05-24 11:48:37 +02:00
|
|
|
case consts.HTTPConnectTCPMultiplexer:
|
2020-03-05 14:47:49 +01:00
|
|
|
remoteAddr, err = pxy.httpConnectRun()
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unknown multiplexer [%s]", pxy.cfg.Multiplexer)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
pxy.Close()
|
|
|
|
}
|
|
|
|
return remoteAddr, err
|
|
|
|
}
|
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
func (pxy *TCPMuxProxy) GetConf() config.ProxyConf {
|
2020-03-05 14:47:49 +01:00
|
|
|
return pxy.cfg
|
|
|
|
}
|
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
func (pxy *TCPMuxProxy) Close() {
|
2020-03-05 14:47:49 +01:00
|
|
|
pxy.BaseProxy.Close()
|
|
|
|
}
|