mirror of
https://github.com/fatedier/frp.git
synced 2025-07-27 16:21:53 +07:00
support yaml/json/toml configuration format, make ini deprecated (#3599)
This commit is contained in:
@ -27,7 +27,8 @@ import (
|
||||
libio "github.com/fatedier/golib/io"
|
||||
"golang.org/x/time/rate"
|
||||
|
||||
"github.com/fatedier/frp/pkg/config"
|
||||
"github.com/fatedier/frp/pkg/config/types"
|
||||
v1 "github.com/fatedier/frp/pkg/config/v1"
|
||||
"github.com/fatedier/frp/pkg/msg"
|
||||
plugin "github.com/fatedier/frp/pkg/plugin/server"
|
||||
"github.com/fatedier/frp/pkg/util/limit"
|
||||
@ -37,9 +38,9 @@ import (
|
||||
"github.com/fatedier/frp/server/metrics"
|
||||
)
|
||||
|
||||
var proxyFactoryRegistry = map[reflect.Type]func(*BaseProxy, config.ProxyConf) Proxy{}
|
||||
var proxyFactoryRegistry = map[reflect.Type]func(*BaseProxy) Proxy{}
|
||||
|
||||
func RegisterProxyFactory(proxyConfType reflect.Type, factory func(*BaseProxy, config.ProxyConf) Proxy) {
|
||||
func RegisterProxyFactory(proxyConfType reflect.Type, factory func(*BaseProxy) Proxy) {
|
||||
proxyFactoryRegistry[proxyConfType] = factory
|
||||
}
|
||||
|
||||
@ -49,7 +50,7 @@ type Proxy interface {
|
||||
Context() context.Context
|
||||
Run() (remoteAddr string, err error)
|
||||
GetName() string
|
||||
GetConf() config.ProxyConf
|
||||
GetConfigurer() v1.ProxyConfigurer
|
||||
GetWorkConnFromPool(src, dst net.Addr) (workConn net.Conn, err error)
|
||||
GetUsedPortsNum() int
|
||||
GetResourceController() *controller.ResourceController
|
||||
@ -66,11 +67,11 @@ type BaseProxy struct {
|
||||
usedPortsNum int
|
||||
poolCount int
|
||||
getWorkConnFn GetWorkConnFn
|
||||
serverCfg config.ServerCommonConf
|
||||
serverCfg *v1.ServerConfig
|
||||
limiter *rate.Limiter
|
||||
userInfo plugin.UserInfo
|
||||
loginMsg *msg.Login
|
||||
pxyConf config.ProxyConf
|
||||
configurer v1.ProxyConfigurer
|
||||
|
||||
mu sync.RWMutex
|
||||
xl *xlog.Logger
|
||||
@ -105,6 +106,10 @@ func (pxy *BaseProxy) GetLimiter() *rate.Limiter {
|
||||
return pxy.limiter
|
||||
}
|
||||
|
||||
func (pxy *BaseProxy) GetConfigurer() v1.ProxyConfigurer {
|
||||
return pxy.configurer
|
||||
}
|
||||
|
||||
func (pxy *BaseProxy) Close() {
|
||||
xl := xlog.FromContextSafe(pxy.ctx)
|
||||
xl.Info("proxy closing")
|
||||
@ -209,13 +214,13 @@ func (pxy *BaseProxy) handleUserTCPConnection(userConn net.Conn) {
|
||||
defer userConn.Close()
|
||||
|
||||
serverCfg := pxy.serverCfg
|
||||
cfg := pxy.pxyConf.GetBaseConfig()
|
||||
cfg := pxy.configurer.GetBaseConfig()
|
||||
// server plugin hook
|
||||
rc := pxy.GetResourceController()
|
||||
content := &plugin.NewUserConnContent{
|
||||
User: pxy.GetUserInfo(),
|
||||
ProxyName: pxy.GetName(),
|
||||
ProxyType: cfg.ProxyType,
|
||||
ProxyType: cfg.Type,
|
||||
RemoteAddr: userConn.RemoteAddr().String(),
|
||||
}
|
||||
_, err := rc.PluginManager.NewUserConn(content)
|
||||
@ -232,15 +237,16 @@ func (pxy *BaseProxy) handleUserTCPConnection(userConn net.Conn) {
|
||||
defer workConn.Close()
|
||||
|
||||
var local io.ReadWriteCloser = workConn
|
||||
xl.Trace("handler user tcp connection, use_encryption: %t, use_compression: %t", cfg.UseEncryption, cfg.UseCompression)
|
||||
if cfg.UseEncryption {
|
||||
local, err = libio.WithEncryption(local, []byte(serverCfg.Token))
|
||||
xl.Trace("handler user tcp connection, use_encryption: %t, use_compression: %t",
|
||||
cfg.Transport.UseEncryption, cfg.Transport.UseCompression)
|
||||
if cfg.Transport.UseEncryption {
|
||||
local, err = libio.WithEncryption(local, []byte(serverCfg.Auth.Token))
|
||||
if err != nil {
|
||||
xl.Error("create encryption stream error: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if cfg.UseCompression {
|
||||
if cfg.Transport.UseCompression {
|
||||
var recycleFn func()
|
||||
local, recycleFn = libio.WithCompressionFromPool(local)
|
||||
defer recycleFn()
|
||||
@ -256,7 +262,7 @@ func (pxy *BaseProxy) handleUserTCPConnection(userConn net.Conn) {
|
||||
workConn.RemoteAddr().String(), userConn.LocalAddr().String(), userConn.RemoteAddr().String())
|
||||
|
||||
name := pxy.GetName()
|
||||
proxyType := cfg.ProxyType
|
||||
proxyType := cfg.Type
|
||||
metrics.Server.OpenConnection(name, proxyType)
|
||||
inCount, outCount, _ := libio.Join(local, userConn)
|
||||
metrics.Server.CloseConnection(name, proxyType)
|
||||
@ -266,18 +272,18 @@ func (pxy *BaseProxy) handleUserTCPConnection(userConn net.Conn) {
|
||||
}
|
||||
|
||||
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, configurer v1.ProxyConfigurer, serverCfg *v1.ServerConfig, loginMsg *msg.Login,
|
||||
) (pxy Proxy, err error) {
|
||||
xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(pxyConf.GetBaseConfig().ProxyName)
|
||||
xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(configurer.GetBaseConfig().Name)
|
||||
|
||||
var limiter *rate.Limiter
|
||||
limitBytes := pxyConf.GetBaseConfig().BandwidthLimit.Bytes()
|
||||
if limitBytes > 0 && pxyConf.GetBaseConfig().BandwidthLimitMode == config.BandwidthLimitModeServer {
|
||||
limitBytes := configurer.GetBaseConfig().Transport.BandwidthLimit.Bytes()
|
||||
if limitBytes > 0 && configurer.GetBaseConfig().Transport.BandwidthLimitMode == types.BandwidthLimitModeServer {
|
||||
limiter = rate.NewLimiter(rate.Limit(float64(limitBytes)), int(limitBytes))
|
||||
}
|
||||
|
||||
basePxy := BaseProxy{
|
||||
name: pxyConf.GetBaseConfig().ProxyName,
|
||||
name: configurer.GetBaseConfig().Name,
|
||||
rc: rc,
|
||||
listeners: make([]net.Listener, 0),
|
||||
poolCount: poolCount,
|
||||
@ -288,14 +294,14 @@ func NewProxy(ctx context.Context, userInfo plugin.UserInfo, rc *controller.Reso
|
||||
ctx: xlog.NewContext(ctx, xl),
|
||||
userInfo: userInfo,
|
||||
loginMsg: loginMsg,
|
||||
pxyConf: pxyConf,
|
||||
configurer: configurer,
|
||||
}
|
||||
|
||||
factory := proxyFactoryRegistry[reflect.TypeOf(pxyConf)]
|
||||
factory := proxyFactoryRegistry[reflect.TypeOf(configurer)]
|
||||
if factory == nil {
|
||||
return pxy, fmt.Errorf("proxy type not support")
|
||||
}
|
||||
pxy = factory(&basePxy, pxyConf)
|
||||
pxy = factory(&basePxy)
|
||||
if pxy == nil {
|
||||
return nil, fmt.Errorf("proxy not created")
|
||||
}
|
||||
|
Reference in New Issue
Block a user