mirror of
https://github.com/fatedier/frp.git
synced 2025-07-27 07:35:07 +00:00
code optimization (#3625)
This commit is contained in:
@@ -165,7 +165,7 @@ type AuthClientConfig struct {
|
||||
// authenticate frpc with frps. If "token" is specified - token will be
|
||||
// read into login message. If "oidc" is specified - OIDC (Open ID Connect)
|
||||
// token will be issued using OIDC settings. By default, this value is "token".
|
||||
Method string `json:"method,omitempty"`
|
||||
Method AuthMethod `json:"method,omitempty"`
|
||||
// Specify whether to include auth info in additional scope.
|
||||
// Current supported scopes are: "HeartBeats", "NewWorkConns".
|
||||
AdditionalScopes []AuthScope `json:"additionalScopes,omitempty"`
|
||||
|
@@ -26,7 +26,7 @@ func TestClientConfigComplete(t *testing.T) {
|
||||
c := &ClientConfig{}
|
||||
c.Complete()
|
||||
|
||||
require.Equal("token", c.Auth.Method)
|
||||
require.EqualValues("token", c.Auth.Method)
|
||||
require.Equal(true, lo.FromPtr(c.Transport.TCPMux))
|
||||
require.Equal(true, lo.FromPtr(c.LoginFailExit))
|
||||
require.Equal(true, lo.FromPtr(c.Transport.TLS.Enable))
|
||||
|
@@ -25,6 +25,13 @@ const (
|
||||
AuthScopeNewWorkConns AuthScope = "NewWorkConns"
|
||||
)
|
||||
|
||||
type AuthMethod string
|
||||
|
||||
const (
|
||||
AuthMethodToken AuthMethod = "token"
|
||||
AuthMethodOIDC AuthMethod = "oidc"
|
||||
)
|
||||
|
||||
// QUIC protocol options
|
||||
type QUICOptions struct {
|
||||
KeepalivePeriod int `json:"quicKeepalivePeriod,omitempty" validate:"gte=0"`
|
||||
|
@@ -23,7 +23,6 @@ import (
|
||||
"github.com/samber/lo"
|
||||
|
||||
"github.com/fatedier/frp/pkg/config/types"
|
||||
"github.com/fatedier/frp/pkg/consts"
|
||||
"github.com/fatedier/frp/pkg/msg"
|
||||
"github.com/fatedier/frp/pkg/util/util"
|
||||
)
|
||||
@@ -174,7 +173,7 @@ func (c *TypedProxyConfig) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
|
||||
c.Type = typeStruct.Type
|
||||
configurer := NewProxyConfigurerByType(typeStruct.Type)
|
||||
configurer := NewProxyConfigurerByType(ProxyType(typeStruct.Type))
|
||||
if configurer == nil {
|
||||
return fmt.Errorf("unknown proxy type: %s", typeStruct.Type)
|
||||
}
|
||||
@@ -196,18 +195,31 @@ type ProxyConfigurer interface {
|
||||
UnmarshalFromMsg(*msg.NewProxy)
|
||||
}
|
||||
|
||||
var proxyConfigTypeMap = map[string]reflect.Type{
|
||||
consts.TCPProxy: reflect.TypeOf(TCPProxyConfig{}),
|
||||
consts.UDPProxy: reflect.TypeOf(UDPProxyConfig{}),
|
||||
consts.HTTPProxy: reflect.TypeOf(HTTPProxyConfig{}),
|
||||
consts.HTTPSProxy: reflect.TypeOf(HTTPSProxyConfig{}),
|
||||
consts.TCPMuxProxy: reflect.TypeOf(TCPMuxProxyConfig{}),
|
||||
consts.STCPProxy: reflect.TypeOf(STCPProxyConfig{}),
|
||||
consts.XTCPProxy: reflect.TypeOf(XTCPProxyConfig{}),
|
||||
consts.SUDPProxy: reflect.TypeOf(SUDPProxyConfig{}),
|
||||
type ProxyType string
|
||||
|
||||
const (
|
||||
ProxyTypeTCP ProxyType = "tcp"
|
||||
ProxyTypeUDP ProxyType = "udp"
|
||||
ProxyTypeTCPMUX ProxyType = "tcpmux"
|
||||
ProxyTypeHTTP ProxyType = "http"
|
||||
ProxyTypeHTTPS ProxyType = "https"
|
||||
ProxyTypeSTCP ProxyType = "stcp"
|
||||
ProxyTypeXTCP ProxyType = "xtcp"
|
||||
ProxyTypeSUDP ProxyType = "sudp"
|
||||
)
|
||||
|
||||
var proxyConfigTypeMap = map[ProxyType]reflect.Type{
|
||||
ProxyTypeTCP: reflect.TypeOf(TCPProxyConfig{}),
|
||||
ProxyTypeUDP: reflect.TypeOf(UDPProxyConfig{}),
|
||||
ProxyTypeHTTP: reflect.TypeOf(HTTPProxyConfig{}),
|
||||
ProxyTypeHTTPS: reflect.TypeOf(HTTPSProxyConfig{}),
|
||||
ProxyTypeTCPMUX: reflect.TypeOf(TCPMuxProxyConfig{}),
|
||||
ProxyTypeSTCP: reflect.TypeOf(STCPProxyConfig{}),
|
||||
ProxyTypeXTCP: reflect.TypeOf(XTCPProxyConfig{}),
|
||||
ProxyTypeSUDP: reflect.TypeOf(SUDPProxyConfig{}),
|
||||
}
|
||||
|
||||
func NewProxyConfigurerByType(proxyType string) ProxyConfigurer {
|
||||
func NewProxyConfigurerByType(proxyType ProxyType) ProxyConfigurer {
|
||||
v, ok := proxyConfigTypeMap[proxyType]
|
||||
if !ok {
|
||||
return nil
|
||||
@@ -316,6 +328,12 @@ func (c *HTTPSProxyConfig) UnmarshalFromMsg(m *msg.NewProxy) {
|
||||
c.SubDomain = m.SubDomain
|
||||
}
|
||||
|
||||
type TCPMultiplexerType string
|
||||
|
||||
const (
|
||||
TCPMultiplexerHTTPConnect TCPMultiplexerType = "httpconnect"
|
||||
)
|
||||
|
||||
var _ ProxyConfigurer = &TCPMuxProxyConfig{}
|
||||
|
||||
type TCPMuxProxyConfig struct {
|
||||
|
@@ -120,7 +120,7 @@ func (c *ServerConfig) Complete() {
|
||||
}
|
||||
|
||||
type AuthServerConfig struct {
|
||||
Method string `json:"method,omitempty"`
|
||||
Method AuthMethod `json:"method,omitempty"`
|
||||
AdditionalScopes []AuthScope `json:"additionalScopes,omitempty"`
|
||||
Token string `json:"token,omitempty"`
|
||||
OIDC AuthOIDCServerConfig `json:"oidc,omitempty"`
|
||||
|
@@ -26,7 +26,7 @@ func TestServerConfigComplete(t *testing.T) {
|
||||
c := &ServerConfig{}
|
||||
c.Complete()
|
||||
|
||||
require.Equal("token", c.Auth.Method)
|
||||
require.EqualValues("token", c.Auth.Method)
|
||||
require.Equal(true, lo.FromPtr(c.Transport.TCPMux))
|
||||
require.Equal(true, lo.FromPtr(c.DetailedErrorsToClient))
|
||||
}
|
||||
|
@@ -22,7 +22,6 @@ import (
|
||||
"github.com/samber/lo"
|
||||
|
||||
v1 "github.com/fatedier/frp/pkg/config/v1"
|
||||
"github.com/fatedier/frp/pkg/consts"
|
||||
)
|
||||
|
||||
func validateProxyBaseConfigForClient(c *v1.ProxyBaseConfig) error {
|
||||
@@ -134,7 +133,7 @@ func validateTCPMuxProxyConfigForClient(c *v1.TCPMuxProxyConfig) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if !lo.Contains([]string{consts.HTTPConnectTCPMultiplexer}, c.Multiplexer) {
|
||||
if !lo.Contains([]string{string(v1.TCPMultiplexerHTTPConnect)}, c.Multiplexer) {
|
||||
return fmt.Errorf("not support multiplexer: %s", c.Multiplexer)
|
||||
}
|
||||
return nil
|
||||
@@ -197,7 +196,7 @@ func validateUDPProxyConfigForServer(c *v1.UDPProxyConfig, s *v1.ServerConfig) e
|
||||
}
|
||||
|
||||
func validateTCPMuxProxyConfigForServer(c *v1.TCPMuxProxyConfig, s *v1.ServerConfig) error {
|
||||
if c.Multiplexer == consts.HTTPConnectTCPMultiplexer &&
|
||||
if c.Multiplexer == string(v1.TCPMultiplexerHTTPConnect) &&
|
||||
s.TCPMuxHTTPConnectPort == 0 {
|
||||
return fmt.Errorf("tcpmux with multiplexer httpconnect not supported because this feature is not enabled in server")
|
||||
}
|
||||
|
@@ -30,7 +30,7 @@ var (
|
||||
"wss",
|
||||
}
|
||||
|
||||
SupportedAuthMethods = []string{
|
||||
SupportedAuthMethods = []v1.AuthMethod{
|
||||
"token",
|
||||
"oidc",
|
||||
}
|
||||
|
@@ -22,7 +22,6 @@ import (
|
||||
|
||||
"github.com/samber/lo"
|
||||
|
||||
"github.com/fatedier/frp/pkg/consts"
|
||||
"github.com/fatedier/frp/pkg/util/util"
|
||||
)
|
||||
|
||||
@@ -73,10 +72,18 @@ type VisitorConfigurer interface {
|
||||
GetBaseConfig() *VisitorBaseConfig
|
||||
}
|
||||
|
||||
var visitorConfigTypeMap = map[string]reflect.Type{
|
||||
consts.STCPProxy: reflect.TypeOf(STCPVisitorConfig{}),
|
||||
consts.XTCPProxy: reflect.TypeOf(XTCPVisitorConfig{}),
|
||||
consts.SUDPProxy: reflect.TypeOf(SUDPVisitorConfig{}),
|
||||
type VisitorType string
|
||||
|
||||
const (
|
||||
VisitorTypeSTCP VisitorType = "stcp"
|
||||
VisitorTypeXTCP VisitorType = "xtcp"
|
||||
VisitorTypeSUDP VisitorType = "sudp"
|
||||
)
|
||||
|
||||
var visitorConfigTypeMap = map[VisitorType]reflect.Type{
|
||||
VisitorTypeSTCP: reflect.TypeOf(STCPVisitorConfig{}),
|
||||
VisitorTypeXTCP: reflect.TypeOf(XTCPVisitorConfig{}),
|
||||
VisitorTypeSUDP: reflect.TypeOf(SUDPVisitorConfig{}),
|
||||
}
|
||||
|
||||
type TypedVisitorConfig struct {
|
||||
@@ -97,7 +104,7 @@ func (c *TypedVisitorConfig) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
|
||||
c.Type = typeStruct.Type
|
||||
configurer := NewVisitorConfigurerByType(typeStruct.Type)
|
||||
configurer := NewVisitorConfigurerByType(VisitorType(typeStruct.Type))
|
||||
if configurer == nil {
|
||||
return fmt.Errorf("unknown visitor type: %s", typeStruct.Type)
|
||||
}
|
||||
@@ -108,7 +115,7 @@ func (c *TypedVisitorConfig) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewVisitorConfigurerByType(t string) VisitorConfigurer {
|
||||
func NewVisitorConfigurerByType(t VisitorType) VisitorConfigurer {
|
||||
v, ok := visitorConfigTypeMap[t]
|
||||
if !ok {
|
||||
return nil
|
||||
|
Reference in New Issue
Block a user