adjust code style about config (#3464)

This commit is contained in:
fatedier 2023-05-30 20:25:22 +08:00 committed by GitHub
parent 341a5e3e3a
commit 9aef3b9944
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 262 additions and 474 deletions

View File

@ -91,7 +91,7 @@ func NewProxyStatusResp(status *proxy.WorkingStatus, serverAddr string) ProxySta
Status: status.Phase, Status: status.Phase,
Err: status.Err, Err: status.Err,
} }
baseCfg := status.Cfg.GetBaseInfo() baseCfg := status.Cfg.GetBaseConfig()
if baseCfg.LocalPort != 0 { if baseCfg.LocalPort != 0 {
psr.LocalAddr = net.JoinHostPort(baseCfg.LocalIP, strconv.Itoa(baseCfg.LocalPort)) psr.LocalAddr = net.JoinHostPort(baseCfg.LocalIP, strconv.Itoa(baseCfg.LocalPort))
} }

View File

@ -109,7 +109,7 @@ func NewControl(
ctl.msgTransporter = transport.NewMessageTransporter(ctl.sendCh) ctl.msgTransporter = transport.NewMessageTransporter(ctl.sendCh)
ctl.pm = proxy.NewManager(ctl.ctx, clientCfg, ctl.msgTransporter) ctl.pm = proxy.NewManager(ctl.ctx, clientCfg, ctl.msgTransporter)
ctl.vm = visitor.NewManager(ctl.ctx, ctl.clientCfg, ctl.connectServer, ctl.msgTransporter) ctl.vm = visitor.NewManager(ctl.ctx, ctl.runID, ctl.clientCfg, ctl.connectServer, ctl.msgTransporter)
ctl.vm.Reload(visitorCfgs) ctl.vm.Reload(visitorCfgs)
return ctl return ctl
} }

View File

@ -54,8 +54,8 @@ func NewProxy(
msgTransporter transport.MessageTransporter, msgTransporter transport.MessageTransporter,
) (pxy Proxy) { ) (pxy Proxy) {
var limiter *rate.Limiter var limiter *rate.Limiter
limitBytes := pxyConf.GetBaseInfo().BandwidthLimit.Bytes() limitBytes := pxyConf.GetBaseConfig().BandwidthLimit.Bytes()
if limitBytes > 0 && pxyConf.GetBaseInfo().BandwidthLimitMode == config.BandwidthLimitModeClient { if limitBytes > 0 && pxyConf.GetBaseConfig().BandwidthLimitMode == config.BandwidthLimitModeClient {
limiter = rate.NewLimiter(rate.Limit(float64(limitBytes)), int(limitBytes)) limiter = rate.NewLimiter(rate.Limit(float64(limitBytes)), int(limitBytes))
} }
@ -148,7 +148,7 @@ func (pxy *TCPProxy) Close() {
} }
func (pxy *TCPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) { func (pxy *TCPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter, HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseConfig(), pxy.limiter,
conn, []byte(pxy.clientCfg.Token), m) conn, []byte(pxy.clientCfg.Token), m)
} }
@ -177,7 +177,7 @@ func (pxy *TCPMuxProxy) Close() {
} }
func (pxy *TCPMuxProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) { func (pxy *TCPMuxProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter, HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseConfig(), pxy.limiter,
conn, []byte(pxy.clientCfg.Token), m) conn, []byte(pxy.clientCfg.Token), m)
} }
@ -206,7 +206,7 @@ func (pxy *HTTPProxy) Close() {
} }
func (pxy *HTTPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) { func (pxy *HTTPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter, HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseConfig(), pxy.limiter,
conn, []byte(pxy.clientCfg.Token), m) conn, []byte(pxy.clientCfg.Token), m)
} }
@ -235,7 +235,7 @@ func (pxy *HTTPSProxy) Close() {
} }
func (pxy *HTTPSProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) { func (pxy *HTTPSProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter, HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseConfig(), pxy.limiter,
conn, []byte(pxy.clientCfg.Token), m) conn, []byte(pxy.clientCfg.Token), m)
} }
@ -264,7 +264,7 @@ func (pxy *STCPProxy) Close() {
} }
func (pxy *STCPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) { func (pxy *STCPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter, HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseConfig(), pxy.limiter,
conn, []byte(pxy.clientCfg.Token), m) conn, []byte(pxy.clientCfg.Token), m)
} }

View File

@ -18,6 +18,7 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"reflect"
"sync" "sync"
"github.com/fatedier/frp/client/event" "github.com/fatedier/frp/client/event"
@ -121,21 +122,18 @@ func (pm *Manager) Reload(pxyCfgs map[string]config.ProxyConf) {
for name, pxy := range pm.proxies { for name, pxy := range pm.proxies {
del := false del := false
cfg, ok := pxyCfgs[name] cfg, ok := pxyCfgs[name]
if !ok { if !ok || !reflect.DeepEqual(pxy.Cfg, cfg) {
del = true
} else if !pxy.Cfg.Compare(cfg) {
del = true del = true
} }
if del { if del {
delPxyNames = append(delPxyNames, name) delPxyNames = append(delPxyNames, name)
delete(pm.proxies, name) delete(pm.proxies, name)
pxy.Stop() pxy.Stop()
} }
} }
if len(delPxyNames) > 0 { if len(delPxyNames) > 0 {
xl.Info("proxy removed: %v", delPxyNames) xl.Info("proxy removed: %s", delPxyNames)
} }
addPxyNames := make([]string, 0) addPxyNames := make([]string, 0)
@ -149,6 +147,6 @@ func (pm *Manager) Reload(pxyCfgs map[string]config.ProxyConf) {
} }
} }
if len(addPxyNames) > 0 { if len(addPxyNames) > 0 {
xl.Info("proxy added: %v", addPxyNames) xl.Info("proxy added: %s", addPxyNames)
} }
} }

View File

@ -91,7 +91,7 @@ func NewWrapper(
eventHandler event.Handler, eventHandler event.Handler,
msgTransporter transport.MessageTransporter, msgTransporter transport.MessageTransporter,
) *Wrapper { ) *Wrapper {
baseInfo := cfg.GetBaseInfo() baseInfo := cfg.GetBaseConfig()
xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(baseInfo.ProxyName) xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(baseInfo.ProxyName)
pw := &Wrapper{ pw := &Wrapper{
WorkingStatus: WorkingStatus{ WorkingStatus: WorkingStatus{

View File

@ -155,7 +155,7 @@ func (pxy *XTCPProxy) listenByKCP(listenConn *net.UDPConn, raddr *net.UDPAddr, s
xl.Error("accept connection error: %v", err) xl.Error("accept connection error: %v", err)
return return
} }
go HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter, go HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseConfig(), pxy.limiter,
muxConn, []byte(pxy.cfg.Sk), startWorkConnMsg) muxConn, []byte(pxy.cfg.Sk), startWorkConnMsg)
} }
} }
@ -194,7 +194,7 @@ func (pxy *XTCPProxy) listenByQUIC(listenConn *net.UDPConn, _ *net.UDPAddr, star
_ = c.CloseWithError(0, "") _ = c.CloseWithError(0, "")
return return
} }
go HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter, go HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseConfig(), pxy.limiter,
utilnet.QuicStreamToNetConn(stream, c), []byte(pxy.cfg.Sk), startWorkConnMsg) utilnet.QuicStreamToNetConn(stream, c), []byte(pxy.cfg.Sk), startWorkConnMsg)
} }
} }

View File

@ -80,7 +80,7 @@ func (sv *STCPVisitor) handleConn(userConn net.Conn) {
defer userConn.Close() defer userConn.Close()
xl.Debug("get a new stcp user connection") xl.Debug("get a new stcp user connection")
visitorConn, err := sv.connectServer() visitorConn, err := sv.helper.ConnectServer()
if err != nil { if err != nil {
return return
} }
@ -88,6 +88,7 @@ func (sv *STCPVisitor) handleConn(userConn net.Conn) {
now := time.Now().Unix() now := time.Now().Unix()
newVisitorConnMsg := &msg.NewVisitorConn{ newVisitorConnMsg := &msg.NewVisitorConn{
RunID: sv.helper.RunID(),
ProxyName: sv.cfg.ServerName, ProxyName: sv.cfg.ServerName,
SignKey: util.GetAuthKey(sv.cfg.Sk, now), SignKey: util.GetAuthKey(sv.cfg.Sk, now),
Timestamp: now, Timestamp: now,

View File

@ -199,13 +199,14 @@ func (sv *SUDPVisitor) worker(workConn net.Conn, firstPacket *msg.UDPPacket) {
func (sv *SUDPVisitor) getNewVisitorConn() (net.Conn, error) { func (sv *SUDPVisitor) getNewVisitorConn() (net.Conn, error) {
xl := xlog.FromContextSafe(sv.ctx) xl := xlog.FromContextSafe(sv.ctx)
visitorConn, err := sv.connectServer() visitorConn, err := sv.helper.ConnectServer()
if err != nil { if err != nil {
return nil, fmt.Errorf("frpc connect frps error: %v", err) return nil, fmt.Errorf("frpc connect frps error: %v", err)
} }
now := time.Now().Unix() now := time.Now().Unix()
newVisitorConnMsg := &msg.NewVisitorConn{ newVisitorConnMsg := &msg.NewVisitorConn{
RunID: sv.helper.RunID(),
ProxyName: sv.cfg.ServerName, ProxyName: sv.cfg.ServerName,
SignKey: util.GetAuthKey(sv.cfg.Sk, now), SignKey: util.GetAuthKey(sv.cfg.Sk, now),
Timestamp: now, Timestamp: now,

View File

@ -25,6 +25,19 @@ import (
"github.com/fatedier/frp/pkg/util/xlog" "github.com/fatedier/frp/pkg/util/xlog"
) )
// Helper wrapps some functions for visitor to use.
type Helper interface {
// ConnectServer directly connects to the frp server.
ConnectServer() (net.Conn, error)
// TransferConn transfers the connection to another visitor.
TransferConn(string, net.Conn) error
// MsgTransporter returns the message transporter that is used to send and receive messages
// to the frp server through the controller.
MsgTransporter() transport.MessageTransporter
// RunID returns the run id of current controller.
RunID() string
}
// Visitor is used for forward traffics from local port tot remote service. // Visitor is used for forward traffics from local port tot remote service.
type Visitor interface { type Visitor interface {
Run() error Run() error
@ -36,18 +49,14 @@ func NewVisitor(
ctx context.Context, ctx context.Context,
cfg config.VisitorConf, cfg config.VisitorConf,
clientCfg config.ClientCommonConf, clientCfg config.ClientCommonConf,
connectServer func() (net.Conn, error), helper Helper,
transferConn func(string, net.Conn) error,
msgTransporter transport.MessageTransporter,
) (visitor Visitor) { ) (visitor Visitor) {
xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(cfg.GetBaseInfo().ProxyName) xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(cfg.GetBaseConfig().ProxyName)
baseVisitor := BaseVisitor{ baseVisitor := BaseVisitor{
clientCfg: clientCfg, clientCfg: clientCfg,
connectServer: connectServer, helper: helper,
transferConn: transferConn, ctx: xlog.NewContext(ctx, xl),
msgTransporter: msgTransporter, internalLn: utilnet.NewInternalListener(),
ctx: xlog.NewContext(ctx, xl),
internalLn: utilnet.NewInternalListener(),
} }
switch cfg := cfg.(type) { switch cfg := cfg.(type) {
case *config.STCPVisitorConf: case *config.STCPVisitorConf:
@ -72,12 +81,10 @@ func NewVisitor(
} }
type BaseVisitor struct { type BaseVisitor struct {
clientCfg config.ClientCommonConf clientCfg config.ClientCommonConf
connectServer func() (net.Conn, error) helper Helper
transferConn func(string, net.Conn) error l net.Listener
msgTransporter transport.MessageTransporter internalLn *utilnet.InternalListener
l net.Listener
internalLn *utilnet.InternalListener
mu sync.RWMutex mu sync.RWMutex
ctx context.Context ctx context.Context

View File

@ -18,6 +18,7 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"reflect"
"sync" "sync"
"time" "time"
@ -27,11 +28,10 @@ import (
) )
type Manager struct { type Manager struct {
clientCfg config.ClientCommonConf clientCfg config.ClientCommonConf
connectServer func() (net.Conn, error) cfgs map[string]config.VisitorConf
msgTransporter transport.MessageTransporter visitors map[string]Visitor
cfgs map[string]config.VisitorConf helper Helper
visitors map[string]Visitor
checkInterval time.Duration checkInterval time.Duration
@ -43,20 +43,26 @@ type Manager struct {
func NewManager( func NewManager(
ctx context.Context, ctx context.Context,
runID string,
clientCfg config.ClientCommonConf, clientCfg config.ClientCommonConf,
connectServer func() (net.Conn, error), connectServer func() (net.Conn, error),
msgTransporter transport.MessageTransporter, msgTransporter transport.MessageTransporter,
) *Manager { ) *Manager {
return &Manager{ m := &Manager{
clientCfg: clientCfg, clientCfg: clientCfg,
connectServer: connectServer, cfgs: make(map[string]config.VisitorConf),
msgTransporter: msgTransporter, visitors: make(map[string]Visitor),
cfgs: make(map[string]config.VisitorConf), checkInterval: 10 * time.Second,
visitors: make(map[string]Visitor), ctx: ctx,
checkInterval: 10 * time.Second, stopCh: make(chan struct{}),
ctx: ctx,
stopCh: make(chan struct{}),
} }
m.helper = &visitorHelperImpl{
connectServerFn: connectServer,
msgTransporter: msgTransporter,
transferConnFn: m.TransferConn,
runID: runID,
}
return m
} }
func (vm *Manager) Run() { func (vm *Manager) Run() {
@ -73,7 +79,7 @@ func (vm *Manager) Run() {
case <-ticker.C: case <-ticker.C:
vm.mu.Lock() vm.mu.Lock()
for _, cfg := range vm.cfgs { for _, cfg := range vm.cfgs {
name := cfg.GetBaseInfo().ProxyName name := cfg.GetBaseConfig().ProxyName
if _, exist := vm.visitors[name]; !exist { if _, exist := vm.visitors[name]; !exist {
xl.Info("try to start visitor [%s]", name) xl.Info("try to start visitor [%s]", name)
_ = vm.startVisitor(cfg) _ = vm.startVisitor(cfg)
@ -100,8 +106,8 @@ func (vm *Manager) Close() {
// Hold lock before calling this function. // Hold lock before calling this function.
func (vm *Manager) startVisitor(cfg config.VisitorConf) (err error) { func (vm *Manager) startVisitor(cfg config.VisitorConf) (err error) {
xl := xlog.FromContextSafe(vm.ctx) xl := xlog.FromContextSafe(vm.ctx)
name := cfg.GetBaseInfo().ProxyName name := cfg.GetBaseConfig().ProxyName
visitor := NewVisitor(vm.ctx, cfg, vm.clientCfg, vm.connectServer, vm.TransferConn, vm.msgTransporter) visitor := NewVisitor(vm.ctx, cfg, vm.clientCfg, vm.helper)
err = visitor.Run() err = visitor.Run()
if err != nil { if err != nil {
xl.Warn("start error: %v", err) xl.Warn("start error: %v", err)
@ -121,9 +127,7 @@ func (vm *Manager) Reload(cfgs map[string]config.VisitorConf) {
for name, oldCfg := range vm.cfgs { for name, oldCfg := range vm.cfgs {
del := false del := false
cfg, ok := cfgs[name] cfg, ok := cfgs[name]
if !ok { if !ok || !reflect.DeepEqual(oldCfg, cfg) {
del = true
} else if !oldCfg.Compare(cfg) {
del = true del = true
} }
@ -163,3 +167,26 @@ func (vm *Manager) TransferConn(name string, conn net.Conn) error {
} }
return v.AcceptConn(conn) return v.AcceptConn(conn)
} }
type visitorHelperImpl struct {
connectServerFn func() (net.Conn, error)
msgTransporter transport.MessageTransporter
transferConnFn func(name string, conn net.Conn) error
runID string
}
func (v *visitorHelperImpl) ConnectServer() (net.Conn, error) {
return v.connectServerFn()
}
func (v *visitorHelperImpl) TransferConn(name string, conn net.Conn) error {
return v.transferConnFn(name, conn)
}
func (v *visitorHelperImpl) MsgTransporter() transport.MessageTransporter {
return v.msgTransporter
}
func (v *visitorHelperImpl) RunID() string {
return v.runID
}

View File

@ -183,7 +183,7 @@ func (sv *XTCPVisitor) handleConn(userConn net.Conn) {
} }
xl.Debug("try to transfer connection to visitor: %s", sv.cfg.FallbackTo) xl.Debug("try to transfer connection to visitor: %s", sv.cfg.FallbackTo)
if err := sv.transferConn(sv.cfg.FallbackTo, userConn); err != nil { if err := sv.helper.TransferConn(sv.cfg.FallbackTo, userConn); err != nil {
xl.Error("transfer connection to visitor %s error: %v", sv.cfg.FallbackTo, err) xl.Error("transfer connection to visitor %s error: %v", sv.cfg.FallbackTo, err)
return return
} }
@ -266,7 +266,7 @@ func (sv *XTCPVisitor) getTunnelConn() (net.Conn, error) {
// 4. Create a tunnel session using an underlying UDP connection. // 4. Create a tunnel session using an underlying UDP connection.
func (sv *XTCPVisitor) makeNatHole() { func (sv *XTCPVisitor) makeNatHole() {
xl := xlog.FromContextSafe(sv.ctx) xl := xlog.FromContextSafe(sv.ctx)
if err := nathole.PreCheck(sv.ctx, sv.msgTransporter, sv.cfg.ServerName, 5*time.Second); err != nil { if err := nathole.PreCheck(sv.ctx, sv.helper.MsgTransporter(), sv.cfg.ServerName, 5*time.Second); err != nil {
xl.Warn("nathole precheck error: %v", err) xl.Warn("nathole precheck error: %v", err)
return return
} }
@ -294,7 +294,7 @@ func (sv *XTCPVisitor) makeNatHole() {
AssistedAddrs: prepareResult.AssistedAddrs, AssistedAddrs: prepareResult.AssistedAddrs,
} }
natHoleRespMsg, err := nathole.ExchangeInfo(sv.ctx, sv.msgTransporter, transactionID, natHoleVisitorMsg, 5*time.Second) natHoleRespMsg, err := nathole.ExchangeInfo(sv.ctx, sv.helper.MsgTransporter(), transactionID, natHoleVisitorMsg, 5*time.Second)
if err != nil { if err != nil {
listenConn.Close() listenConn.Close()
xl.Warn("nathole exchange info error: %v", err) xl.Warn("nathole exchange info error: %v", err)

View File

@ -79,7 +79,7 @@ var httpCmd = &cobra.Command{
} }
cfg.BandwidthLimitMode = bandwidthLimitMode cfg.BandwidthLimitMode = bandwidthLimitMode
err = cfg.CheckForCli() err = cfg.ValidateForClient()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -71,7 +71,7 @@ var httpsCmd = &cobra.Command{
} }
cfg.BandwidthLimitMode = bandwidthLimitMode cfg.BandwidthLimitMode = bandwidthLimitMode
err = cfg.CheckForCli() err = cfg.ValidateForClient()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -78,7 +78,7 @@ var stcpCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
cfg.BandwidthLimitMode = bandwidthLimitMode cfg.BandwidthLimitMode = bandwidthLimitMode
err = cfg.CheckForCli() err = cfg.ValidateForClient()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
@ -95,7 +95,7 @@ var stcpCmd = &cobra.Command{
cfg.ServerName = serverName cfg.ServerName = serverName
cfg.BindAddr = bindAddr cfg.BindAddr = bindAddr
cfg.BindPort = bindPort cfg.BindPort = bindPort
err = cfg.Check() err = cfg.Validate()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -78,7 +78,7 @@ var sudpCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
cfg.BandwidthLimitMode = bandwidthLimitMode cfg.BandwidthLimitMode = bandwidthLimitMode
err = cfg.CheckForCli() err = cfg.ValidateForClient()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
@ -95,7 +95,7 @@ var sudpCmd = &cobra.Command{
cfg.ServerName = serverName cfg.ServerName = serverName
cfg.BindAddr = bindAddr cfg.BindAddr = bindAddr
cfg.BindPort = bindPort cfg.BindPort = bindPort
err = cfg.Check() err = cfg.Validate()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -68,7 +68,7 @@ var tcpCmd = &cobra.Command{
} }
cfg.BandwidthLimitMode = bandwidthLimitMode cfg.BandwidthLimitMode = bandwidthLimitMode
err = cfg.CheckForCli() err = cfg.ValidateForClient()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -73,7 +73,7 @@ var tcpMuxCmd = &cobra.Command{
} }
cfg.BandwidthLimitMode = bandwidthLimitMode cfg.BandwidthLimitMode = bandwidthLimitMode
err = cfg.CheckForCli() err = cfg.ValidateForClient()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -68,7 +68,7 @@ var udpCmd = &cobra.Command{
} }
cfg.BandwidthLimitMode = bandwidthLimitMode cfg.BandwidthLimitMode = bandwidthLimitMode
err = cfg.CheckForCli() err = cfg.ValidateForClient()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -78,7 +78,7 @@ var xtcpCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
cfg.BandwidthLimitMode = bandwidthLimitMode cfg.BandwidthLimitMode = bandwidthLimitMode
err = cfg.CheckForCli() err = cfg.ValidateForClient()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
@ -95,7 +95,7 @@ var xtcpCmd = &cobra.Command{
cfg.ServerName = serverName cfg.ServerName = serverName
cfg.BindAddr = bindAddr cfg.BindAddr = bindAddr
cfg.BindPort = bindPort cfg.BindPort = bindPort
err = cfg.Check() err = cfg.Validate()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -326,6 +326,9 @@ local_ip = 127.0.0.1
local_port = 22 local_port = 22
use_encryption = false use_encryption = false
use_compression = false use_compression = false
# If not empty, only visitors from specified users can connect.
# Otherwise, visitors from same user can connect. '*' means allow all users.
allow_users = *
# user of frpc should be same in both stcp server and stcp visitor # user of frpc should be same in both stcp server and stcp visitor
[secret_tcp_visitor] [secret_tcp_visitor]
@ -350,10 +353,15 @@ local_ip = 127.0.0.1
local_port = 22 local_port = 22
use_encryption = false use_encryption = false
use_compression = false use_compression = false
# If not empty, only visitors from specified users can connect.
# Otherwise, visitors from same user can connect. '*' means allow all users.
allow_users = user1, user2
[p2p_tcp_visitor] [p2p_tcp_visitor]
role = visitor role = visitor
type = xtcp type = xtcp
# if the server user is not set, it defaults to the current user
server_user = user1
server_name = p2p_tcp server_name = p2p_tcp
sk = abcdefg sk = abcdefg
bind_addr = 127.0.0.1 bind_addr = 127.0.0.1

View File

@ -500,8 +500,10 @@ func Test_LoadClientBasicConf(t *testing.T) {
}, },
BandwidthLimitMode: BandwidthLimitModeClient, BandwidthLimitMode: BandwidthLimitModeClient,
}, },
Role: "server", RoleServerCommonConf: RoleServerCommonConf{
Sk: "abcdefg", Role: "server",
Sk: "abcdefg",
},
}, },
testUser + ".p2p_tcp": &XTCPProxyConf{ testUser + ".p2p_tcp": &XTCPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
@ -513,8 +515,10 @@ func Test_LoadClientBasicConf(t *testing.T) {
}, },
BandwidthLimitMode: BandwidthLimitModeClient, BandwidthLimitMode: BandwidthLimitModeClient,
}, },
Role: "server", RoleServerCommonConf: RoleServerCommonConf{
Sk: "abcdefg", Role: "server",
Sk: "abcdefg",
},
}, },
testUser + ".tcpmuxhttpconnect": &TCPMuxProxyConf{ testUser + ".tcpmuxhttpconnect": &TCPMuxProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{

View File

@ -51,13 +51,23 @@ func NewConfByType(proxyType string) ProxyConf {
} }
type ProxyConf interface { type ProxyConf interface {
GetBaseInfo() *BaseProxyConf // GetBaseConfig returns the BaseProxyConf for this config.
GetBaseConfig() *BaseProxyConf
// SetDefaultValues sets the default values for this config.
SetDefaultValues()
// UnmarshalFromMsg unmarshals a msg.NewProxy message into this config.
// This function will be called on the frps side.
UnmarshalFromMsg(*msg.NewProxy) UnmarshalFromMsg(*msg.NewProxy)
// UnmarshalFromIni unmarshals a ini.Section into this config. This function
// will be called on the frpc side.
UnmarshalFromIni(string, string, *ini.Section) error UnmarshalFromIni(string, string, *ini.Section) error
// MarshalToMsg marshals this config into a msg.NewProxy message. This
// function will be called on the frpc side.
MarshalToMsg(*msg.NewProxy) MarshalToMsg(*msg.NewProxy)
CheckForCli() error // ValidateForClient checks that the config is valid for the frpc side.
CheckForSvr(ServerCommonConf) error ValidateForClient() error
Compare(ProxyConf) bool // ValidateForServer checks that the config is valid for the frps side.
ValidateForServer(ServerCommonConf) error
} }
// LocalSvrConf configures what location the client will to, or what // LocalSvrConf configures what location the client will to, or what
@ -158,6 +168,16 @@ type DomainConf struct {
SubDomain string `ini:"subdomain" json:"subdomain"` SubDomain string `ini:"subdomain" json:"subdomain"`
} }
type RoleServerCommonConf struct {
Role string `ini:"role" json:"role"`
Sk string `ini:"sk" json:"sk"`
AllowUsers []string `ini:"allow_users" json:"allow_users"`
}
func (cfg *RoleServerCommonConf) setDefaultValues() {
cfg.Role = "server"
}
// HTTP // HTTP
type HTTPProxyConf struct { type HTTPProxyConf struct {
BaseProxyConf `ini:",extends"` BaseProxyConf `ini:",extends"`
@ -203,73 +223,30 @@ type TCPMuxProxyConf struct {
// STCP // STCP
type STCPProxyConf struct { type STCPProxyConf struct {
BaseProxyConf `ini:",extends"` BaseProxyConf `ini:",extends"`
RoleServerCommonConf `ini:",extends"`
Role string `ini:"role" json:"role"`
Sk string `ini:"sk" json:"sk"`
} }
// XTCP // XTCP
type XTCPProxyConf struct { type XTCPProxyConf struct {
BaseProxyConf `ini:",extends"` BaseProxyConf `ini:",extends"`
RoleServerCommonConf `ini:",extends"`
Role string `ini:"role" json:"role"`
Sk string `ini:"sk" json:"sk"`
} }
// SUDP // SUDP
type SUDPProxyConf struct { type SUDPProxyConf struct {
BaseProxyConf `ini:",extends"` BaseProxyConf `ini:",extends"`
RoleServerCommonConf `ini:",extends"`
Role string `ini:"role" json:"role"`
Sk string `ini:"sk" json:"sk"`
} }
// Proxy Conf Loader // Proxy Conf Loader
// DefaultProxyConf creates a empty ProxyConf object by proxyType. // DefaultProxyConf creates a empty ProxyConf object by proxyType.
// If proxyType doesn't exist, return nil. // If proxyType doesn't exist, return nil.
func DefaultProxyConf(proxyType string) ProxyConf { func DefaultProxyConf(proxyType string) ProxyConf {
var conf ProxyConf conf := NewConfByType(proxyType)
switch proxyType { if conf != nil {
case consts.TCPProxy: conf.SetDefaultValues()
conf = &TCPProxyConf{
BaseProxyConf: defaultBaseProxyConf(proxyType),
}
case consts.TCPMuxProxy:
conf = &TCPMuxProxyConf{
BaseProxyConf: defaultBaseProxyConf(proxyType),
}
case consts.UDPProxy:
conf = &UDPProxyConf{
BaseProxyConf: defaultBaseProxyConf(proxyType),
}
case consts.HTTPProxy:
conf = &HTTPProxyConf{
BaseProxyConf: defaultBaseProxyConf(proxyType),
}
case consts.HTTPSProxy:
conf = &HTTPSProxyConf{
BaseProxyConf: defaultBaseProxyConf(proxyType),
}
case consts.STCPProxy:
conf = &STCPProxyConf{
BaseProxyConf: defaultBaseProxyConf(proxyType),
Role: "server",
}
case consts.XTCPProxy:
conf = &XTCPProxyConf{
BaseProxyConf: defaultBaseProxyConf(proxyType),
Role: "server",
}
case consts.SUDPProxy:
conf = &SUDPProxyConf{
BaseProxyConf: defaultBaseProxyConf(proxyType),
Role: "server",
}
default:
return nil
} }
return conf return conf
} }
@ -290,10 +267,9 @@ func NewProxyConfFromIni(prefix, name string, section *ini.Section) (ProxyConf,
return nil, err return nil, err
} }
if err := conf.CheckForCli(); err != nil { if err := conf.ValidateForClient(); err != nil {
return nil, err return nil, err
} }
return conf, nil return conf, nil
} }
@ -310,7 +286,7 @@ func NewProxyConfFromMsg(pMsg *msg.NewProxy, serverCfg ServerCommonConf) (ProxyC
conf.UnmarshalFromMsg(pMsg) conf.UnmarshalFromMsg(pMsg)
err := conf.CheckForSvr(serverCfg) err := conf.ValidateForServer(serverCfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -319,42 +295,15 @@ func NewProxyConfFromMsg(pMsg *msg.NewProxy, serverCfg ServerCommonConf) (ProxyC
} }
// Base // Base
func defaultBaseProxyConf(proxyType string) BaseProxyConf { func (cfg *BaseProxyConf) GetBaseConfig() *BaseProxyConf {
return BaseProxyConf{
ProxyType: proxyType,
LocalSvrConf: LocalSvrConf{
LocalIP: "127.0.0.1",
},
BandwidthLimitMode: BandwidthLimitModeClient,
}
}
func (cfg *BaseProxyConf) GetBaseInfo() *BaseProxyConf {
return cfg return cfg
} }
func (cfg *BaseProxyConf) compare(cmp *BaseProxyConf) bool { func (cfg *BaseProxyConf) SetDefaultValues() {
if cfg.ProxyName != cmp.ProxyName || cfg.LocalSvrConf = LocalSvrConf{
cfg.ProxyType != cmp.ProxyType || LocalIP: "127.0.0.1",
cfg.UseEncryption != cmp.UseEncryption ||
cfg.UseCompression != cmp.UseCompression ||
cfg.Group != cmp.Group ||
cfg.GroupKey != cmp.GroupKey ||
cfg.ProxyProtocolVersion != cmp.ProxyProtocolVersion ||
!cfg.BandwidthLimit.Equal(&cmp.BandwidthLimit) ||
cfg.BandwidthLimitMode != cmp.BandwidthLimitMode ||
!reflect.DeepEqual(cfg.Metas, cmp.Metas) {
return false
} }
cfg.BandwidthLimitMode = BandwidthLimitModeClient
if !reflect.DeepEqual(cfg.LocalSvrConf, cmp.LocalSvrConf) {
return false
}
if !reflect.DeepEqual(cfg.HealthCheckConf, cmp.HealthCheckConf) {
return false
}
return true
} }
// BaseProxyConf apply custom logic changes. // BaseProxyConf apply custom logic changes.
@ -423,7 +372,7 @@ func (cfg *BaseProxyConf) unmarshalFromMsg(pMsg *msg.NewProxy) {
cfg.Metas = pMsg.Metas cfg.Metas = pMsg.Metas
} }
func (cfg *BaseProxyConf) checkForCli() (err error) { func (cfg *BaseProxyConf) validateForClient() (err error) {
if cfg.ProxyProtocolVersion != "" { if cfg.ProxyProtocolVersion != "" {
if cfg.ProxyProtocolVersion != "v1" && cfg.ProxyProtocolVersion != "v2" { if cfg.ProxyProtocolVersion != "v1" && cfg.ProxyProtocolVersion != "v2" {
return fmt.Errorf("no support proxy protocol version: %s", cfg.ProxyProtocolVersion) return fmt.Errorf("no support proxy protocol version: %s", cfg.ProxyProtocolVersion)
@ -434,16 +383,16 @@ func (cfg *BaseProxyConf) checkForCli() (err error) {
return fmt.Errorf("bandwidth_limit_mode should be client or server") return fmt.Errorf("bandwidth_limit_mode should be client or server")
} }
if err = cfg.LocalSvrConf.checkForCli(); err != nil { if err = cfg.LocalSvrConf.validateForClient(); err != nil {
return return
} }
if err = cfg.HealthCheckConf.checkForCli(); err != nil { if err = cfg.HealthCheckConf.validateForClient(); err != nil {
return return
} }
return nil return nil
} }
func (cfg *BaseProxyConf) checkForSvr() (err error) { func (cfg *BaseProxyConf) validateForServer() (err error) {
if cfg.BandwidthLimitMode != "client" && cfg.BandwidthLimitMode != "server" { if cfg.BandwidthLimitMode != "client" && cfg.BandwidthLimitMode != "server" {
return fmt.Errorf("bandwidth_limit_mode should be client or server") return fmt.Errorf("bandwidth_limit_mode should be client or server")
} }
@ -459,14 +408,14 @@ func (cfg *DomainConf) check() (err error) {
return return
} }
func (cfg *DomainConf) checkForCli() (err error) { func (cfg *DomainConf) validateForClient() (err error) {
if err = cfg.check(); err != nil { if err = cfg.check(); err != nil {
return return
} }
return return
} }
func (cfg *DomainConf) checkForSvr(serverCfg ServerCommonConf) (err error) { func (cfg *DomainConf) validateForServer(serverCfg ServerCommonConf) (err error) {
if err = cfg.check(); err != nil { if err = cfg.check(); err != nil {
return return
} }
@ -491,7 +440,7 @@ func (cfg *DomainConf) checkForSvr(serverCfg ServerCommonConf) (err error) {
} }
// LocalSvrConf // LocalSvrConf
func (cfg *LocalSvrConf) checkForCli() (err error) { func (cfg *LocalSvrConf) validateForClient() (err error) {
if cfg.Plugin == "" { if cfg.Plugin == "" {
if cfg.LocalIP == "" { if cfg.LocalIP == "" {
err = fmt.Errorf("local ip or plugin is required") err = fmt.Errorf("local ip or plugin is required")
@ -506,7 +455,7 @@ func (cfg *LocalSvrConf) checkForCli() (err error) {
} }
// HealthCheckConf // HealthCheckConf
func (cfg *HealthCheckConf) checkForCli() error { func (cfg *HealthCheckConf) validateForClient() error {
if cfg.HealthCheckType != "" && cfg.HealthCheckType != "tcp" && cfg.HealthCheckType != "http" { if cfg.HealthCheckType != "" && cfg.HealthCheckType != "tcp" && cfg.HealthCheckType != "http" {
return fmt.Errorf("unsupport health check type") return fmt.Errorf("unsupport health check type")
} }
@ -524,7 +473,7 @@ func preUnmarshalFromIni(cfg ProxyConf, prefix string, name string, section *ini
return err return err
} }
err = cfg.GetBaseInfo().decorate(prefix, name, section) err = cfg.GetBaseConfig().decorate(prefix, name, section)
if err != nil { if err != nil {
return err return err
} }
@ -533,24 +482,6 @@ func preUnmarshalFromIni(cfg ProxyConf, prefix string, name string, section *ini
} }
// TCP // TCP
func (cfg *TCPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*TCPProxyConf)
if !ok {
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
return false
}
// Add custom logic equal if exists.
if cfg.RemotePort != cmpConf.RemotePort {
return false
}
return true
}
func (cfg *TCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) { func (cfg *TCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
cfg.BaseProxyConf.unmarshalFromMsg(pMsg) cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
@ -576,8 +507,8 @@ func (cfg *TCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
pMsg.RemotePort = cfg.RemotePort pMsg.RemotePort = cfg.RemotePort
} }
func (cfg *TCPProxyConf) CheckForCli() (err error) { func (cfg *TCPProxyConf) ValidateForClient() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil { if err = cfg.BaseProxyConf.validateForClient(); err != nil {
return return
} }
@ -586,39 +517,14 @@ func (cfg *TCPProxyConf) CheckForCli() (err error) {
return return
} }
func (cfg *TCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { func (cfg *TCPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
if err := cfg.BaseProxyConf.checkForSvr(); err != nil { if err := cfg.BaseProxyConf.validateForServer(); err != nil {
return err return err
} }
return nil return nil
} }
// TCPMux // TCPMux
func (cfg *TCPMuxProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*TCPMuxProxyConf)
if !ok {
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
return false
}
// Add custom logic equal if exists.
if !reflect.DeepEqual(cfg.DomainConf, cmpConf.DomainConf) {
return false
}
if cfg.Multiplexer != cmpConf.Multiplexer ||
cfg.HTTPUser != cmpConf.HTTPUser ||
cfg.HTTPPwd != cmpConf.HTTPPwd ||
cfg.RouteByHTTPUser != cmpConf.RouteByHTTPUser {
return false
}
return true
}
func (cfg *TCPMuxProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *TCPMuxProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
err := preUnmarshalFromIni(cfg, prefix, name, section) err := preUnmarshalFromIni(cfg, prefix, name, section)
if err != nil { if err != nil {
@ -654,13 +560,13 @@ func (cfg *TCPMuxProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
pMsg.RouteByHTTPUser = cfg.RouteByHTTPUser pMsg.RouteByHTTPUser = cfg.RouteByHTTPUser
} }
func (cfg *TCPMuxProxyConf) CheckForCli() (err error) { func (cfg *TCPMuxProxyConf) ValidateForClient() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil { if err = cfg.BaseProxyConf.validateForClient(); err != nil {
return return
} }
// Add custom logic check if exists // Add custom logic check if exists
if err = cfg.DomainConf.checkForCli(); err != nil { if err = cfg.DomainConf.validateForClient(); err != nil {
return return
} }
@ -671,8 +577,8 @@ func (cfg *TCPMuxProxyConf) CheckForCli() (err error) {
return return
} }
func (cfg *TCPMuxProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) { func (cfg *TCPMuxProxyConf) ValidateForServer(serverCfg ServerCommonConf) (err error) {
if err := cfg.BaseProxyConf.checkForSvr(); err != nil { if err := cfg.BaseProxyConf.validateForServer(); err != nil {
return err return err
} }
@ -684,7 +590,7 @@ func (cfg *TCPMuxProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error)
return fmt.Errorf("proxy [%s] type [tcpmux] with multiplexer [httpconnect] requires tcpmux_httpconnect_port configuration", cfg.ProxyName) return fmt.Errorf("proxy [%s] type [tcpmux] with multiplexer [httpconnect] requires tcpmux_httpconnect_port configuration", cfg.ProxyName)
} }
if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil { if err = cfg.DomainConf.validateForServer(serverCfg); err != nil {
err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err) err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
return return
} }
@ -693,24 +599,6 @@ func (cfg *TCPMuxProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error)
} }
// UDP // UDP
func (cfg *UDPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*UDPProxyConf)
if !ok {
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
return false
}
// Add custom logic equal if exists.
if cfg.RemotePort != cmpConf.RemotePort {
return false
}
return true
}
func (cfg *UDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *UDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
err := preUnmarshalFromIni(cfg, prefix, name, section) err := preUnmarshalFromIni(cfg, prefix, name, section)
if err != nil { if err != nil {
@ -736,8 +624,8 @@ func (cfg *UDPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
pMsg.RemotePort = cfg.RemotePort pMsg.RemotePort = cfg.RemotePort
} }
func (cfg *UDPProxyConf) CheckForCli() (err error) { func (cfg *UDPProxyConf) ValidateForClient() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil { if err = cfg.BaseProxyConf.validateForClient(); err != nil {
return return
} }
@ -746,41 +634,14 @@ func (cfg *UDPProxyConf) CheckForCli() (err error) {
return return
} }
func (cfg *UDPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { func (cfg *UDPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
if err := cfg.BaseProxyConf.checkForSvr(); err != nil { if err := cfg.BaseProxyConf.validateForServer(); err != nil {
return err return err
} }
return nil return nil
} }
// HTTP // HTTP
func (cfg *HTTPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*HTTPProxyConf)
if !ok {
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
return false
}
// Add custom logic equal if exists.
if !reflect.DeepEqual(cfg.DomainConf, cmpConf.DomainConf) {
return false
}
if !reflect.DeepEqual(cfg.Locations, cmpConf.Locations) ||
cfg.HTTPUser != cmpConf.HTTPUser ||
cfg.HTTPPwd != cmpConf.HTTPPwd ||
cfg.HostHeaderRewrite != cmpConf.HostHeaderRewrite ||
cfg.RouteByHTTPUser != cmpConf.RouteByHTTPUser ||
!reflect.DeepEqual(cfg.Headers, cmpConf.Headers) {
return false
}
return true
}
func (cfg *HTTPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *HTTPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
err := preUnmarshalFromIni(cfg, prefix, name, section) err := preUnmarshalFromIni(cfg, prefix, name, section)
if err != nil { if err != nil {
@ -789,7 +650,6 @@ func (cfg *HTTPProxyConf) UnmarshalFromIni(prefix string, name string, section *
// Add custom logic unmarshal if exists // Add custom logic unmarshal if exists
cfg.Headers = GetMapWithoutPrefix(section.KeysHash(), "header_") cfg.Headers = GetMapWithoutPrefix(section.KeysHash(), "header_")
return nil return nil
} }
@ -821,21 +681,21 @@ func (cfg *HTTPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
pMsg.RouteByHTTPUser = cfg.RouteByHTTPUser pMsg.RouteByHTTPUser = cfg.RouteByHTTPUser
} }
func (cfg *HTTPProxyConf) CheckForCli() (err error) { func (cfg *HTTPProxyConf) ValidateForClient() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil { if err = cfg.BaseProxyConf.validateForClient(); err != nil {
return return
} }
// Add custom logic check if exists // Add custom logic check if exists
if err = cfg.DomainConf.checkForCli(); err != nil { if err = cfg.DomainConf.validateForClient(); err != nil {
return return
} }
return return
} }
func (cfg *HTTPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) { func (cfg *HTTPProxyConf) ValidateForServer(serverCfg ServerCommonConf) (err error) {
if err := cfg.BaseProxyConf.checkForSvr(); err != nil { if err := cfg.BaseProxyConf.validateForServer(); err != nil {
return err return err
} }
@ -843,7 +703,7 @@ func (cfg *HTTPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
return fmt.Errorf("type [http] not support when vhost_http_port is not set") return fmt.Errorf("type [http] not support when vhost_http_port is not set")
} }
if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil { if err = cfg.DomainConf.validateForServer(serverCfg); err != nil {
err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err) err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
return return
} }
@ -852,24 +712,6 @@ func (cfg *HTTPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
} }
// HTTPS // HTTPS
func (cfg *HTTPSProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*HTTPSProxyConf)
if !ok {
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
return false
}
// Add custom logic equal if exists.
if !reflect.DeepEqual(cfg.DomainConf, cmpConf.DomainConf) {
return false
}
return true
}
func (cfg *HTTPSProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *HTTPSProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
err := preUnmarshalFromIni(cfg, prefix, name, section) err := preUnmarshalFromIni(cfg, prefix, name, section)
if err != nil { if err != nil {
@ -877,7 +719,6 @@ func (cfg *HTTPSProxyConf) UnmarshalFromIni(prefix string, name string, section
} }
// Add custom logic unmarshal if exists // Add custom logic unmarshal if exists
return nil return nil
} }
@ -897,21 +738,20 @@ func (cfg *HTTPSProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
pMsg.SubDomain = cfg.SubDomain pMsg.SubDomain = cfg.SubDomain
} }
func (cfg *HTTPSProxyConf) CheckForCli() (err error) { func (cfg *HTTPSProxyConf) ValidateForClient() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil { if err = cfg.BaseProxyConf.validateForClient(); err != nil {
return return
} }
// Add custom logic check if exists // Add custom logic check if exists
if err = cfg.DomainConf.checkForCli(); err != nil { if err = cfg.DomainConf.validateForClient(); err != nil {
return return
} }
return return
} }
func (cfg *HTTPSProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) { func (cfg *HTTPSProxyConf) ValidateForServer(serverCfg ServerCommonConf) (err error) {
if err := cfg.BaseProxyConf.checkForSvr(); err != nil { if err := cfg.BaseProxyConf.validateForServer(); err != nil {
return err return err
} }
@ -919,7 +759,7 @@ func (cfg *HTTPSProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
return fmt.Errorf("type [https] not support when vhost_https_port is not set") return fmt.Errorf("type [https] not support when vhost_https_port is not set")
} }
if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil { if err = cfg.DomainConf.validateForServer(serverCfg); err != nil {
err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err) err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
return return
} }
@ -928,23 +768,9 @@ func (cfg *HTTPSProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
} }
// SUDP // SUDP
func (cfg *SUDPProxyConf) Compare(cmp ProxyConf) bool { func (cfg *SUDPProxyConf) SetDefaultValues() {
cmpConf, ok := cmp.(*SUDPProxyConf) cfg.BaseProxyConf.SetDefaultValues()
if !ok { cfg.RoleServerCommonConf.setDefaultValues()
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
return false
}
// Add custom logic equal if exists.
if cfg.Role != cmpConf.Role ||
cfg.Sk != cmpConf.Sk {
return false
}
return true
} }
func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
@ -954,7 +780,6 @@ func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section *
} }
// Add custom logic unmarshal if exists // Add custom logic unmarshal if exists
return nil return nil
} }
@ -973,8 +798,8 @@ func (cfg *SUDPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
pMsg.Sk = cfg.Sk pMsg.Sk = cfg.Sk
} }
func (cfg *SUDPProxyConf) CheckForCli() (err error) { func (cfg *SUDPProxyConf) ValidateForClient() (err error) {
if err := cfg.BaseProxyConf.checkForCli(); err != nil { if err := cfg.BaseProxyConf.validateForClient(); err != nil {
return err return err
} }
@ -986,31 +811,17 @@ func (cfg *SUDPProxyConf) CheckForCli() (err error) {
return nil return nil
} }
func (cfg *SUDPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { func (cfg *SUDPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
if err := cfg.BaseProxyConf.checkForSvr(); err != nil { if err := cfg.BaseProxyConf.validateForServer(); err != nil {
return err return err
} }
return nil return nil
} }
// STCP // STCP
func (cfg *STCPProxyConf) Compare(cmp ProxyConf) bool { func (cfg *STCPProxyConf) SetDefaultValues() {
cmpConf, ok := cmp.(*STCPProxyConf) cfg.BaseProxyConf.SetDefaultValues()
if !ok { cfg.RoleServerCommonConf.setDefaultValues()
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
return false
}
// Add custom logic equal if exists.
if cfg.Role != cmpConf.Role ||
cfg.Sk != cmpConf.Sk {
return false
}
return true
} }
func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
@ -1023,7 +834,6 @@ func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section *
if cfg.Role == "" { if cfg.Role == "" {
cfg.Role = "server" cfg.Role = "server"
} }
return nil return nil
} }
@ -1042,8 +852,8 @@ func (cfg *STCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
pMsg.Sk = cfg.Sk pMsg.Sk = cfg.Sk
} }
func (cfg *STCPProxyConf) CheckForCli() (err error) { func (cfg *STCPProxyConf) ValidateForClient() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil { if err = cfg.BaseProxyConf.validateForClient(); err != nil {
return return
} }
@ -1055,30 +865,17 @@ func (cfg *STCPProxyConf) CheckForCli() (err error) {
return return
} }
func (cfg *STCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { func (cfg *STCPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
if err := cfg.BaseProxyConf.checkForSvr(); err != nil { if err := cfg.BaseProxyConf.validateForServer(); err != nil {
return err return err
} }
return nil return nil
} }
// XTCP // XTCP
func (cfg *XTCPProxyConf) Compare(cmp ProxyConf) bool { func (cfg *XTCPProxyConf) SetDefaultValues() {
cmpConf, ok := cmp.(*XTCPProxyConf) cfg.BaseProxyConf.SetDefaultValues()
if !ok { cfg.RoleServerCommonConf.setDefaultValues()
return false
}
if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
return false
}
// Add custom logic equal if exists.
if cfg.Role != cmpConf.Role ||
cfg.Sk != cmpConf.Sk {
return false
}
return true
} }
func (cfg *XTCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *XTCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
@ -1109,8 +906,8 @@ func (cfg *XTCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
pMsg.Sk = cfg.Sk pMsg.Sk = cfg.Sk
} }
func (cfg *XTCPProxyConf) CheckForCli() (err error) { func (cfg *XTCPProxyConf) ValidateForClient() (err error) {
if err = cfg.BaseProxyConf.checkForCli(); err != nil { if err = cfg.BaseProxyConf.validateForClient(); err != nil {
return return
} }
@ -1121,8 +918,8 @@ func (cfg *XTCPProxyConf) CheckForCli() (err error) {
return return
} }
func (cfg *XTCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { func (cfg *XTCPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
if err := cfg.BaseProxyConf.checkForSvr(); err != nil { if err := cfg.BaseProxyConf.validateForServer(); err != nil {
return err return err
} }
return nil return nil

View File

@ -254,8 +254,10 @@ func Test_Proxy_UnmarshalFromIni(t *testing.T) {
}, },
BandwidthLimitMode: BandwidthLimitModeClient, BandwidthLimitMode: BandwidthLimitModeClient,
}, },
Role: "server", RoleServerCommonConf: RoleServerCommonConf{
Sk: "abcdefg", Role: "server",
Sk: "abcdefg",
},
}, },
}, },
{ {
@ -279,8 +281,10 @@ func Test_Proxy_UnmarshalFromIni(t *testing.T) {
}, },
BandwidthLimitMode: BandwidthLimitModeClient, BandwidthLimitMode: BandwidthLimitModeClient,
}, },
Role: "server", RoleServerCommonConf: RoleServerCommonConf{
Sk: "abcdefg", Role: "server",
Sk: "abcdefg",
},
}, },
}, },
{ {

View File

@ -34,10 +34,12 @@ var (
) )
type VisitorConf interface { type VisitorConf interface {
GetBaseInfo() *BaseVisitorConf // GetBaseConfig returns the base config of visitor.
Compare(cmp VisitorConf) bool GetBaseConfig() *BaseVisitorConf
// UnmarshalFromIni unmarshals config from ini.
UnmarshalFromIni(prefix string, name string, section *ini.Section) error UnmarshalFromIni(prefix string, name string, section *ini.Section) error
Check() error // Validate validates config.
Validate() error
} }
type BaseVisitorConf struct { type BaseVisitorConf struct {
@ -47,8 +49,10 @@ type BaseVisitorConf struct {
UseCompression bool `ini:"use_compression" json:"use_compression"` UseCompression bool `ini:"use_compression" json:"use_compression"`
Role string `ini:"role" json:"role"` Role string `ini:"role" json:"role"`
Sk string `ini:"sk" json:"sk"` Sk string `ini:"sk" json:"sk"`
ServerName string `ini:"server_name" json:"server_name"` // if the server user is not set, it defaults to the current user
BindAddr string `ini:"bind_addr" json:"bind_addr"` ServerUser string `ini:"server_user" json:"server_user"`
ServerName string `ini:"server_name" json:"server_name"`
BindAddr string `ini:"bind_addr" json:"bind_addr"`
// BindPort is the port that visitor listens on. // BindPort is the port that visitor listens on.
// It can be less than 0, it means don't bind to the port and only receive connections redirected from // It can be less than 0, it means don't bind to the port and only receive connections redirected from
// other visitors. (This is not supported for SUDP now) // other visitors. (This is not supported for SUDP now)
@ -81,7 +85,6 @@ func DefaultVisitorConf(visitorType string) VisitorConf {
if !ok { if !ok {
return nil return nil
} }
return reflect.New(v).Interface().(VisitorConf) return reflect.New(v).Interface().(VisitorConf)
} }
@ -103,7 +106,7 @@ func NewVisitorConfFromIni(prefix string, name string, section *ini.Section) (Vi
return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType) return nil, fmt.Errorf("visitor [%s] type [%s] error", name, visitorType)
} }
if err := conf.Check(); err != nil { if err := conf.Validate(); err != nil {
return nil, err return nil, err
} }
@ -111,26 +114,11 @@ func NewVisitorConfFromIni(prefix string, name string, section *ini.Section) (Vi
} }
// Base // Base
func (cfg *BaseVisitorConf) GetBaseInfo() *BaseVisitorConf { func (cfg *BaseVisitorConf) GetBaseConfig() *BaseVisitorConf {
return cfg return cfg
} }
func (cfg *BaseVisitorConf) compare(cmp *BaseVisitorConf) bool { func (cfg *BaseVisitorConf) validate() (err error) {
if cfg.ProxyName != cmp.ProxyName ||
cfg.ProxyType != cmp.ProxyType ||
cfg.UseEncryption != cmp.UseEncryption ||
cfg.UseCompression != cmp.UseCompression ||
cfg.Role != cmp.Role ||
cfg.Sk != cmp.Sk ||
cfg.ServerName != cmp.ServerName ||
cfg.BindAddr != cmp.BindAddr ||
cfg.BindPort != cmp.BindPort {
return false
}
return true
}
func (cfg *BaseVisitorConf) check() (err error) {
if cfg.Role != "visitor" { if cfg.Role != "visitor" {
err = fmt.Errorf("invalid role") err = fmt.Errorf("invalid role")
return return
@ -156,7 +144,11 @@ func (cfg *BaseVisitorConf) unmarshalFromIni(prefix string, name string, section
cfg.ProxyName = prefix + name cfg.ProxyName = prefix + name
// server_name // server_name
cfg.ServerName = prefix + cfg.ServerName if cfg.ServerUser == "" {
cfg.ServerName = prefix + cfg.ServerName
} else {
cfg.ServerName = cfg.ServerUser + "." + cfg.ServerName
}
// bind_addr // bind_addr
if cfg.BindAddr == "" { if cfg.BindAddr == "" {
@ -171,7 +163,7 @@ func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, sec
return err return err
} }
err = cfg.GetBaseInfo().unmarshalFromIni(prefix, name, section) err = cfg.GetBaseConfig().unmarshalFromIni(prefix, name, section)
if err != nil { if err != nil {
return err return err
} }
@ -181,21 +173,6 @@ func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, sec
// SUDP // SUDP
var _ VisitorConf = &SUDPVisitorConf{} var _ VisitorConf = &SUDPVisitorConf{}
func (cfg *SUDPVisitorConf) Compare(cmp VisitorConf) bool {
cmpConf, ok := cmp.(*SUDPVisitorConf)
if !ok {
return false
}
if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
return false
}
// Add custom login equal, if exists
return true
}
func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) { func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
err = preVisitorUnmarshalFromIni(cfg, prefix, name, section) err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
if err != nil { if err != nil {
@ -207,8 +184,8 @@ func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section
return return
} }
func (cfg *SUDPVisitorConf) Check() (err error) { func (cfg *SUDPVisitorConf) Validate() (err error) {
if err = cfg.BaseVisitorConf.check(); err != nil { if err = cfg.BaseVisitorConf.validate(); err != nil {
return return
} }
@ -220,21 +197,6 @@ func (cfg *SUDPVisitorConf) Check() (err error) {
// STCP // STCP
var _ VisitorConf = &STCPVisitorConf{} var _ VisitorConf = &STCPVisitorConf{}
func (cfg *STCPVisitorConf) Compare(cmp VisitorConf) bool {
cmpConf, ok := cmp.(*STCPVisitorConf)
if !ok {
return false
}
if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
return false
}
// Add custom login equal, if exists
return true
}
func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) { func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
err = preVisitorUnmarshalFromIni(cfg, prefix, name, section) err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
if err != nil { if err != nil {
@ -246,8 +208,8 @@ func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section
return return
} }
func (cfg *STCPVisitorConf) Check() (err error) { func (cfg *STCPVisitorConf) Validate() (err error) {
if err = cfg.BaseVisitorConf.check(); err != nil { if err = cfg.BaseVisitorConf.validate(); err != nil {
return return
} }
@ -259,28 +221,6 @@ func (cfg *STCPVisitorConf) Check() (err error) {
// XTCP // XTCP
var _ VisitorConf = &XTCPVisitorConf{} var _ VisitorConf = &XTCPVisitorConf{}
func (cfg *XTCPVisitorConf) Compare(cmp VisitorConf) bool {
cmpConf, ok := cmp.(*XTCPVisitorConf)
if !ok {
return false
}
if !cfg.BaseVisitorConf.compare(&cmpConf.BaseVisitorConf) {
return false
}
// Add custom login equal, if exists
if cfg.Protocol != cmpConf.Protocol ||
cfg.KeepTunnelOpen != cmpConf.KeepTunnelOpen ||
cfg.MaxRetriesAnHour != cmpConf.MaxRetriesAnHour ||
cfg.MinRetryInterval != cmpConf.MinRetryInterval ||
cfg.FallbackTo != cmpConf.FallbackTo ||
cfg.FallbackTimeoutMs != cmpConf.FallbackTimeoutMs {
return false
}
return true
}
func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) { func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
err = preVisitorUnmarshalFromIni(cfg, prefix, name, section) err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
if err != nil { if err != nil {
@ -303,8 +243,8 @@ func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section
return return
} }
func (cfg *XTCPVisitorConf) Check() (err error) { func (cfg *XTCPVisitorConf) Validate() (err error) {
if err = cfg.BaseVisitorConf.check(); err != nil { if err = cfg.BaseVisitorConf.validate(); err != nil {
return return
} }

View File

@ -145,6 +145,7 @@ type StartWorkConn struct {
} }
type NewVisitorConn struct { type NewVisitorConn struct {
RunID string `json:"run_id,omitempty"`
ProxyName string `json:"proxy_name,omitempty"` ProxyName string `json:"proxy_name,omitempty"`
SignKey string `json:"sign_key,omitempty"` SignKey string `json:"sign_key,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"` Timestamp int64 `json:"timestamp,omitempty"`

View File

@ -394,7 +394,7 @@ func (ctl *Control) stoper() {
for _, pxy := range ctl.proxies { for _, pxy := range ctl.proxies {
pxy.Close() pxy.Close()
ctl.pxyManager.Del(pxy.GetName()) ctl.pxyManager.Del(pxy.GetName())
metrics.Server.CloseProxy(pxy.GetName(), pxy.GetConf().GetBaseInfo().ProxyType) metrics.Server.CloseProxy(pxy.GetName(), pxy.GetConf().GetBaseConfig().ProxyType)
notifyContent := &plugin.CloseProxyContent{ notifyContent := &plugin.CloseProxyContent{
User: plugin.UserInfo{ User: plugin.UserInfo{
@ -614,7 +614,7 @@ func (ctl *Control) CloseProxy(closeMsg *msg.CloseProxy) (err error) {
delete(ctl.proxies, closeMsg.ProxyName) delete(ctl.proxies, closeMsg.ProxyName)
ctl.mu.Unlock() ctl.mu.Unlock()
metrics.Server.CloseProxy(pxy.GetName(), pxy.GetConf().GetBaseInfo().ProxyType) metrics.Server.CloseProxy(pxy.GetName(), pxy.GetConf().GetBaseConfig().ProxyType)
notifyContent := &plugin.CloseProxyContent{ notifyContent := &plugin.CloseProxyContent{
User: plugin.UserInfo{ User: plugin.UserInfo{

View File

@ -175,13 +175,13 @@ func (pxy *HTTPProxy) GetRealConn(remoteAddr string) (workConn net.Conn, err err
workConn = utilnet.WrapReadWriteCloserToConn(rwc, tmpConn) workConn = utilnet.WrapReadWriteCloserToConn(rwc, tmpConn)
workConn = utilnet.WrapStatsConn(workConn, pxy.updateStatsAfterClosedConn) workConn = utilnet.WrapStatsConn(workConn, pxy.updateStatsAfterClosedConn)
metrics.Server.OpenConnection(pxy.GetName(), pxy.GetConf().GetBaseInfo().ProxyType) metrics.Server.OpenConnection(pxy.GetName(), pxy.GetConf().GetBaseConfig().ProxyType)
return return
} }
func (pxy *HTTPProxy) updateStatsAfterClosedConn(totalRead, totalWrite int64) { func (pxy *HTTPProxy) updateStatsAfterClosedConn(totalRead, totalWrite int64) {
name := pxy.GetName() name := pxy.GetName()
proxyType := pxy.GetConf().GetBaseInfo().ProxyType proxyType := pxy.GetConf().GetBaseConfig().ProxyType
metrics.Server.CloseConnection(name, proxyType) metrics.Server.CloseConnection(name, proxyType)
metrics.Server.AddTrafficIn(name, proxyType, totalWrite) metrics.Server.AddTrafficIn(name, proxyType, totalWrite)
metrics.Server.AddTrafficOut(name, proxyType, totalRead) metrics.Server.AddTrafficOut(name, proxyType, totalRead)

View File

@ -196,16 +196,16 @@ func (pxy *BaseProxy) startListenHandler(p Proxy, handler func(Proxy, net.Conn,
func NewProxy(ctx context.Context, userInfo plugin.UserInfo, rc *controller.ResourceController, poolCount int, func NewProxy(ctx context.Context, userInfo plugin.UserInfo, rc *controller.ResourceController, poolCount int,
getWorkConnFn GetWorkConnFn, pxyConf config.ProxyConf, serverCfg config.ServerCommonConf, loginMsg *msg.Login, getWorkConnFn GetWorkConnFn, pxyConf config.ProxyConf, serverCfg config.ServerCommonConf, loginMsg *msg.Login,
) (pxy Proxy, err error) { ) (pxy Proxy, err error) {
xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(pxyConf.GetBaseInfo().ProxyName) xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(pxyConf.GetBaseConfig().ProxyName)
var limiter *rate.Limiter var limiter *rate.Limiter
limitBytes := pxyConf.GetBaseInfo().BandwidthLimit.Bytes() limitBytes := pxyConf.GetBaseConfig().BandwidthLimit.Bytes()
if limitBytes > 0 && pxyConf.GetBaseInfo().BandwidthLimitMode == config.BandwidthLimitModeServer { if limitBytes > 0 && pxyConf.GetBaseConfig().BandwidthLimitMode == config.BandwidthLimitModeServer {
limiter = rate.NewLimiter(rate.Limit(float64(limitBytes)), int(limitBytes)) limiter = rate.NewLimiter(rate.Limit(float64(limitBytes)), int(limitBytes))
} }
basePxy := BaseProxy{ basePxy := BaseProxy{
name: pxyConf.GetBaseInfo().ProxyName, name: pxyConf.GetBaseConfig().ProxyName,
rc: rc, rc: rc,
listeners: make([]net.Listener, 0), listeners: make([]net.Listener, 0),
poolCount: poolCount, poolCount: poolCount,
@ -277,7 +277,7 @@ func HandleUserTCPConnection(pxy Proxy, userConn net.Conn, serverCfg config.Serv
content := &plugin.NewUserConnContent{ content := &plugin.NewUserConnContent{
User: pxy.GetUserInfo(), User: pxy.GetUserInfo(),
ProxyName: pxy.GetName(), ProxyName: pxy.GetName(),
ProxyType: pxy.GetConf().GetBaseInfo().ProxyType, ProxyType: pxy.GetConf().GetBaseConfig().ProxyType,
RemoteAddr: userConn.RemoteAddr().String(), RemoteAddr: userConn.RemoteAddr().String(),
} }
_, err := rc.PluginManager.NewUserConn(content) _, err := rc.PluginManager.NewUserConn(content)
@ -294,7 +294,7 @@ func HandleUserTCPConnection(pxy Proxy, userConn net.Conn, serverCfg config.Serv
defer workConn.Close() defer workConn.Close()
var local io.ReadWriteCloser = workConn var local io.ReadWriteCloser = workConn
cfg := pxy.GetConf().GetBaseInfo() cfg := pxy.GetConf().GetBaseConfig()
xl.Trace("handler user tcp connection, use_encryption: %t, use_compression: %t", cfg.UseEncryption, cfg.UseCompression) xl.Trace("handler user tcp connection, use_encryption: %t, use_compression: %t", cfg.UseEncryption, cfg.UseCompression)
if cfg.UseEncryption { if cfg.UseEncryption {
local, err = libio.WithEncryption(local, []byte(serverCfg.Token)) local, err = libio.WithEncryption(local, []byte(serverCfg.Token))
@ -317,7 +317,7 @@ func HandleUserTCPConnection(pxy Proxy, userConn net.Conn, serverCfg config.Serv
workConn.RemoteAddr().String(), userConn.LocalAddr().String(), userConn.RemoteAddr().String()) workConn.RemoteAddr().String(), userConn.LocalAddr().String(), userConn.RemoteAddr().String())
name := pxy.GetName() name := pxy.GetName()
proxyType := pxy.GetConf().GetBaseInfo().ProxyType proxyType := pxy.GetConf().GetBaseConfig().ProxyType
metrics.Server.OpenConnection(name, proxyType) metrics.Server.OpenConnection(name, proxyType)
inCount, outCount, _ := libio.Join(local, userConn) inCount, outCount, _ := libio.Join(local, userConn)
metrics.Server.CloseConnection(name, proxyType) metrics.Server.CloseConnection(name, proxyType)

View File

@ -124,7 +124,7 @@ func (pxy *UDPProxy) Run() (remoteAddr string, err error) {
pxy.readCh <- m pxy.readCh <- m
metrics.Server.AddTrafficOut( metrics.Server.AddTrafficOut(
pxy.GetName(), pxy.GetName(),
pxy.GetConf().GetBaseInfo().ProxyType, pxy.GetConf().GetBaseConfig().ProxyType,
int64(len(m.Content)), int64(len(m.Content)),
) )
}); errRet != nil { }); errRet != nil {
@ -154,7 +154,7 @@ func (pxy *UDPProxy) Run() (remoteAddr string, err error) {
xl.Trace("send message to udp workConn: %s", udpMsg.Content) xl.Trace("send message to udp workConn: %s", udpMsg.Content)
metrics.Server.AddTrafficIn( metrics.Server.AddTrafficIn(
pxy.GetName(), pxy.GetName(),
pxy.GetConf().GetBaseInfo().ProxyType, pxy.GetConf().GetBaseConfig().ProxyType,
int64(len(udpMsg.Content)), int64(len(udpMsg.Content)),
) )
continue continue