style: split ini parsing to auth and its implementations
This commit is contained in:
parent
29e9935ca7
commit
18c31c0f16
@ -28,6 +28,7 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/fatedier/frp/client"
|
"github.com/fatedier/frp/client"
|
||||||
|
"github.com/fatedier/frp/models/auth"
|
||||||
"github.com/fatedier/frp/models/config"
|
"github.com/fatedier/frp/models/config"
|
||||||
"github.com/fatedier/frp/utils/log"
|
"github.com/fatedier/frp/utils/log"
|
||||||
"github.com/fatedier/frp/utils/version"
|
"github.com/fatedier/frp/utils/version"
|
||||||
@ -157,7 +158,6 @@ func parseClientCommonCfgFromCmd() (cfg config.ClientCommonConf, err error) {
|
|||||||
|
|
||||||
cfg.User = user
|
cfg.User = user
|
||||||
cfg.Protocol = protocol
|
cfg.Protocol = protocol
|
||||||
cfg.Token = token
|
|
||||||
cfg.LogLevel = logLevel
|
cfg.LogLevel = logLevel
|
||||||
cfg.LogFile = logFile
|
cfg.LogFile = logFile
|
||||||
cfg.LogMaxDays = int64(logMaxDays)
|
cfg.LogMaxDays = int64(logMaxDays)
|
||||||
@ -168,6 +168,10 @@ func parseClientCommonCfgFromCmd() (cfg config.ClientCommonConf, err error) {
|
|||||||
}
|
}
|
||||||
cfg.DisableLogColor = disableLogColor
|
cfg.DisableLogColor = disableLogColor
|
||||||
|
|
||||||
|
// Only token authentication is supported in cmd mode
|
||||||
|
cfg.AuthClientConfig = auth.GetDefaultAuthClientConf()
|
||||||
|
cfg.Token = token
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/fatedier/frp/models/auth"
|
||||||
"github.com/fatedier/frp/models/config"
|
"github.com/fatedier/frp/models/config"
|
||||||
"github.com/fatedier/frp/server"
|
"github.com/fatedier/frp/server"
|
||||||
"github.com/fatedier/frp/utils/log"
|
"github.com/fatedier/frp/utils/log"
|
||||||
@ -171,8 +172,11 @@ func parseServerCommonCfgFromCmd() (cfg config.ServerCommonConf, err error) {
|
|||||||
cfg.LogFile = logFile
|
cfg.LogFile = logFile
|
||||||
cfg.LogLevel = logLevel
|
cfg.LogLevel = logLevel
|
||||||
cfg.LogMaxDays = logMaxDays
|
cfg.LogMaxDays = logMaxDays
|
||||||
cfg.Token = token
|
|
||||||
cfg.SubDomainHost = subDomainHost
|
cfg.SubDomainHost = subDomainHost
|
||||||
|
|
||||||
|
// Only token authentication is supported in cmd mode
|
||||||
|
cfg.AuthServerConfig = auth.GetDefaultAuthServerConf()
|
||||||
|
cfg.Token = token
|
||||||
if len(allowPorts) > 0 {
|
if len(allowPorts) > 0 {
|
||||||
// e.g. 1000-2000,2001,2002,3000-4000
|
// e.g. 1000-2000,2001,2002,3000-4000
|
||||||
ports, errRet := util.ParseRangeNumbers(allowPorts)
|
ports, errRet := util.ParseRangeNumbers(allowPorts)
|
||||||
|
@ -15,8 +15,12 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/fatedier/frp/models/consts"
|
"github.com/fatedier/frp/models/consts"
|
||||||
"github.com/fatedier/frp/models/msg"
|
"github.com/fatedier/frp/models/msg"
|
||||||
|
|
||||||
|
"github.com/vaughan0/go-ini"
|
||||||
)
|
)
|
||||||
|
|
||||||
type baseConfig struct {
|
type baseConfig struct {
|
||||||
@ -33,42 +37,83 @@ type baseConfig struct {
|
|||||||
AuthenticateNewWorkConns bool `json:"authenticate_new_work_conns"`
|
AuthenticateNewWorkConns bool `json:"authenticate_new_work_conns"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getDefaultBaseConf() baseConfig {
|
||||||
|
return baseConfig{
|
||||||
|
AuthenticationMethod: "token",
|
||||||
|
AuthenticateHeartBeats: false,
|
||||||
|
AuthenticateNewWorkConns: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshalBaseConfFromIni(conf ini.File) baseConfig {
|
||||||
|
var (
|
||||||
|
tmpStr string
|
||||||
|
ok bool
|
||||||
|
)
|
||||||
|
|
||||||
|
cfg := getDefaultBaseConf()
|
||||||
|
|
||||||
|
if tmpStr, ok = conf.Get("common", "authentication_method"); ok {
|
||||||
|
cfg.AuthenticationMethod = tmpStr
|
||||||
|
}
|
||||||
|
|
||||||
|
if tmpStr, ok = conf.Get("common", "authenticate_heartbeats"); ok && tmpStr == "true" {
|
||||||
|
cfg.AuthenticateHeartBeats = true
|
||||||
|
} else {
|
||||||
|
cfg.AuthenticateHeartBeats = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if tmpStr, ok = conf.Get("common", "authenticate_new_work_conns"); ok && tmpStr == "true" {
|
||||||
|
cfg.AuthenticateNewWorkConns = true
|
||||||
|
} else {
|
||||||
|
cfg.AuthenticateNewWorkConns = false
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
type AuthClientConfig struct {
|
type AuthClientConfig struct {
|
||||||
baseConfig
|
baseConfig
|
||||||
oidcClientConfig
|
oidcClientConfig
|
||||||
tokenConfig
|
tokenConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDefaultClientConf() AuthClientConfig {
|
func GetDefaultAuthClientConf() AuthClientConfig {
|
||||||
return AuthClientConfig{
|
return AuthClientConfig{
|
||||||
baseConfig: baseConfig{
|
baseConfig: getDefaultBaseConf(),
|
||||||
AuthenticationMethod: "token",
|
|
||||||
AuthenticateHeartBeats: false,
|
|
||||||
AuthenticateNewWorkConns: false,
|
|
||||||
},
|
|
||||||
oidcClientConfig: getDefaultOidcClientConf(),
|
oidcClientConfig: getDefaultOidcClientConf(),
|
||||||
tokenConfig: getDefaultTokenConf(),
|
tokenConfig: getDefaultTokenConf(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UnmarshalAuthClientConfFromIni(conf ini.File) (cfg AuthClientConfig) {
|
||||||
|
cfg.baseConfig = unmarshalBaseConfFromIni(conf)
|
||||||
|
cfg.oidcClientConfig = unmarshalOidcClientConfFromIni(conf)
|
||||||
|
cfg.tokenConfig = unmarshalTokenConfFromIni(conf)
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
type AuthServerConfig struct {
|
type AuthServerConfig struct {
|
||||||
baseConfig
|
baseConfig
|
||||||
oidcServerConfig
|
oidcServerConfig
|
||||||
tokenConfig
|
tokenConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDefaultServerConf() AuthServerConfig {
|
func GetDefaultAuthServerConf() AuthServerConfig {
|
||||||
return AuthServerConfig{
|
return AuthServerConfig{
|
||||||
baseConfig: baseConfig{
|
baseConfig: getDefaultBaseConf(),
|
||||||
AuthenticationMethod: "token",
|
|
||||||
AuthenticateHeartBeats: false,
|
|
||||||
AuthenticateNewWorkConns: false,
|
|
||||||
},
|
|
||||||
oidcServerConfig: getDefaultOidcServerConf(),
|
oidcServerConfig: getDefaultOidcServerConf(),
|
||||||
tokenConfig: getDefaultTokenConf(),
|
tokenConfig: getDefaultTokenConf(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UnmarshalAuthServerConfFromIni(conf ini.File) (cfg AuthServerConfig) {
|
||||||
|
cfg.baseConfig = unmarshalBaseConfFromIni(conf)
|
||||||
|
cfg.oidcServerConfig = unmarshalOidcServerConfFromIni(conf)
|
||||||
|
cfg.tokenConfig = unmarshalTokenConfFromIni(conf)
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
type Setter interface {
|
type Setter interface {
|
||||||
SetLogin(*msg.Login) error
|
SetLogin(*msg.Login) error
|
||||||
SetPing(*msg.Ping) error
|
SetPing(*msg.Ping) error
|
||||||
@ -81,6 +126,8 @@ func NewAuthSetter(cfg AuthClientConfig) (authProvider Setter) {
|
|||||||
authProvider = NewTokenAuth(cfg.baseConfig, cfg.tokenConfig)
|
authProvider = NewTokenAuth(cfg.baseConfig, cfg.tokenConfig)
|
||||||
case consts.OidcAuthMethod:
|
case consts.OidcAuthMethod:
|
||||||
authProvider = NewOidcAuthSetter(cfg.baseConfig, cfg.oidcClientConfig)
|
authProvider = NewOidcAuthSetter(cfg.baseConfig, cfg.oidcClientConfig)
|
||||||
|
default:
|
||||||
|
panic(fmt.Sprintf("wrong authentication method: '%s'", cfg.AuthenticationMethod))
|
||||||
}
|
}
|
||||||
|
|
||||||
return authProvider
|
return authProvider
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"github.com/fatedier/frp/models/msg"
|
"github.com/fatedier/frp/models/msg"
|
||||||
|
|
||||||
"github.com/coreos/go-oidc"
|
"github.com/coreos/go-oidc"
|
||||||
|
"github.com/vaughan0/go-ini"
|
||||||
"golang.org/x/oauth2/clientcredentials"
|
"golang.org/x/oauth2/clientcredentials"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -51,6 +52,33 @@ func getDefaultOidcClientConf() oidcClientConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unmarshalOidcClientConfFromIni(conf ini.File) oidcClientConfig {
|
||||||
|
var (
|
||||||
|
tmpStr string
|
||||||
|
ok bool
|
||||||
|
)
|
||||||
|
|
||||||
|
cfg := getDefaultOidcClientConf()
|
||||||
|
|
||||||
|
if tmpStr, ok = conf.Get("common", "oidc_client_id"); ok {
|
||||||
|
cfg.OidcClientId = tmpStr
|
||||||
|
}
|
||||||
|
|
||||||
|
if tmpStr, ok = conf.Get("common", "oidc_client_secret"); ok {
|
||||||
|
cfg.OidcClientSecret = tmpStr
|
||||||
|
}
|
||||||
|
|
||||||
|
if tmpStr, ok = conf.Get("common", "oidc_audience"); ok {
|
||||||
|
cfg.OidcAudience = tmpStr
|
||||||
|
}
|
||||||
|
|
||||||
|
if tmpStr, ok = conf.Get("common", "oidc_token_endpoint_url"); ok {
|
||||||
|
cfg.OidcTokenEndpointUrl = tmpStr
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
type oidcServerConfig struct {
|
type oidcServerConfig struct {
|
||||||
// OidcIssuer specifies the issuer to verify OIDC tokens with. This issuer
|
// OidcIssuer specifies the issuer to verify OIDC tokens with. This issuer
|
||||||
// will be used to load public keys to verify signature and will be compared
|
// will be used to load public keys to verify signature and will be compared
|
||||||
@ -81,6 +109,37 @@ func getDefaultOidcServerConf() oidcServerConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unmarshalOidcServerConfFromIni(conf ini.File) oidcServerConfig {
|
||||||
|
var (
|
||||||
|
tmpStr string
|
||||||
|
ok bool
|
||||||
|
)
|
||||||
|
|
||||||
|
cfg := getDefaultOidcServerConf()
|
||||||
|
|
||||||
|
if tmpStr, ok = conf.Get("common", "oidc_issuer"); ok {
|
||||||
|
cfg.OidcIssuer = tmpStr
|
||||||
|
}
|
||||||
|
|
||||||
|
if tmpStr, ok = conf.Get("common", "oidc_audience"); ok {
|
||||||
|
cfg.OidcAudience = tmpStr
|
||||||
|
}
|
||||||
|
|
||||||
|
if tmpStr, ok = conf.Get("common", "oidc_skip_expiry_check"); ok && tmpStr == "true" {
|
||||||
|
cfg.OidcSkipExpiryCheck = true
|
||||||
|
} else {
|
||||||
|
cfg.OidcSkipExpiryCheck = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if tmpStr, ok = conf.Get("common", "oidc_skip_issuer_check"); ok && tmpStr == "true" {
|
||||||
|
cfg.OidcSkipIssuerCheck = true
|
||||||
|
} else {
|
||||||
|
cfg.OidcSkipIssuerCheck = false
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
type OidcAuthProvider struct {
|
type OidcAuthProvider struct {
|
||||||
baseConfig
|
baseConfig
|
||||||
|
|
||||||
|
@ -19,6 +19,8 @@ import (
|
|||||||
|
|
||||||
"github.com/fatedier/frp/models/msg"
|
"github.com/fatedier/frp/models/msg"
|
||||||
"github.com/fatedier/frp/utils/util"
|
"github.com/fatedier/frp/utils/util"
|
||||||
|
|
||||||
|
"github.com/vaughan0/go-ini"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tokenConfig struct {
|
type tokenConfig struct {
|
||||||
@ -34,6 +36,21 @@ func getDefaultTokenConf() tokenConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unmarshalTokenConfFromIni(conf ini.File) tokenConfig {
|
||||||
|
var (
|
||||||
|
tmpStr string
|
||||||
|
ok bool
|
||||||
|
)
|
||||||
|
|
||||||
|
cfg := getDefaultTokenConf()
|
||||||
|
|
||||||
|
if tmpStr, ok = conf.Get("common", "token"); ok {
|
||||||
|
cfg.Token = tmpStr
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
type TokenAuthSetterVerifier struct {
|
type TokenAuthSetterVerifier struct {
|
||||||
baseConfig
|
baseConfig
|
||||||
|
|
||||||
|
@ -121,7 +121,6 @@ type ClientCommonConf struct {
|
|||||||
// GetDefaultClientConf returns a client configuration with default values.
|
// GetDefaultClientConf returns a client configuration with default values.
|
||||||
func GetDefaultClientConf() ClientCommonConf {
|
func GetDefaultClientConf() ClientCommonConf {
|
||||||
return ClientCommonConf{
|
return ClientCommonConf{
|
||||||
AuthClientConfig: auth.GetDefaultClientConf(),
|
|
||||||
ServerAddr: "0.0.0.0",
|
ServerAddr: "0.0.0.0",
|
||||||
ServerPort: 7000,
|
ServerPort: 7000,
|
||||||
HttpProxy: os.Getenv("http_proxy"),
|
HttpProxy: os.Getenv("http_proxy"),
|
||||||
@ -157,6 +156,8 @@ func UnmarshalClientConfFromIni(content string) (cfg ClientCommonConf, err error
|
|||||||
return ClientCommonConf{}, fmt.Errorf("parse ini conf file error: %v", err)
|
return ClientCommonConf{}, fmt.Errorf("parse ini conf file error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cfg.AuthClientConfig = auth.UnmarshalAuthClientConfFromIni(conf)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tmpStr string
|
tmpStr string
|
||||||
ok bool
|
ok bool
|
||||||
@ -202,42 +203,6 @@ func UnmarshalClientConfFromIni(content string) (cfg ClientCommonConf, err error
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "token"); ok {
|
|
||||||
cfg.Token = tmpStr
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "authentication_method"); ok {
|
|
||||||
cfg.AuthenticationMethod = tmpStr
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "authenticate_heartbeats"); ok && tmpStr == "true" {
|
|
||||||
cfg.AuthenticateHeartBeats = true
|
|
||||||
} else {
|
|
||||||
cfg.AuthenticateHeartBeats = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "authenticate_new_work_conns"); ok && tmpStr == "true" {
|
|
||||||
cfg.AuthenticateNewWorkConns = true
|
|
||||||
} else {
|
|
||||||
cfg.AuthenticateNewWorkConns = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "oidc_client_id"); ok {
|
|
||||||
cfg.OidcClientId = tmpStr
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "oidc_client_secret"); ok {
|
|
||||||
cfg.OidcClientSecret = tmpStr
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "oidc_audience"); ok {
|
|
||||||
cfg.OidcAudience = tmpStr
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "oidc_token_endpoint_url"); ok {
|
|
||||||
cfg.OidcTokenEndpointUrl = tmpStr
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "admin_addr"); ok {
|
if tmpStr, ok = conf.Get("common", "admin_addr"); ok {
|
||||||
cfg.AdminAddr = tmpStr
|
cfg.AdminAddr = tmpStr
|
||||||
}
|
}
|
||||||
|
@ -148,7 +148,6 @@ type ServerCommonConf struct {
|
|||||||
// defaults.
|
// defaults.
|
||||||
func GetDefaultServerConf() ServerCommonConf {
|
func GetDefaultServerConf() ServerCommonConf {
|
||||||
return ServerCommonConf{
|
return ServerCommonConf{
|
||||||
AuthServerConfig: auth.GetDefaultServerConf(),
|
|
||||||
BindAddr: "0.0.0.0",
|
BindAddr: "0.0.0.0",
|
||||||
BindPort: 7000,
|
BindPort: 7000,
|
||||||
BindUdpPort: 0,
|
BindUdpPort: 0,
|
||||||
@ -194,6 +193,8 @@ func UnmarshalServerConfFromIni(content string) (cfg ServerCommonConf, err error
|
|||||||
|
|
||||||
UnmarshalPluginsFromIni(conf, &cfg)
|
UnmarshalPluginsFromIni(conf, &cfg)
|
||||||
|
|
||||||
|
cfg.AuthServerConfig = auth.UnmarshalAuthServerConfFromIni(conf)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tmpStr string
|
tmpStr string
|
||||||
ok bool
|
ok bool
|
||||||
@ -327,44 +328,6 @@ func UnmarshalServerConfFromIni(content string) (cfg ServerCommonConf, err error
|
|||||||
cfg.DetailedErrorsToClient = true
|
cfg.DetailedErrorsToClient = true
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.Token, _ = conf.Get("common", "token")
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "authentication_method"); ok {
|
|
||||||
cfg.AuthenticationMethod = tmpStr
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "authenticate_heartbeats"); ok && tmpStr == "true" {
|
|
||||||
cfg.AuthenticateHeartBeats = true
|
|
||||||
} else {
|
|
||||||
cfg.AuthenticateHeartBeats = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "authenticate_new_work_conns"); ok && tmpStr == "true" {
|
|
||||||
cfg.AuthenticateNewWorkConns = true
|
|
||||||
} else {
|
|
||||||
cfg.AuthenticateNewWorkConns = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "oidc_issuer"); ok {
|
|
||||||
cfg.OidcIssuer = tmpStr
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "oidc_audience"); ok {
|
|
||||||
cfg.OidcAudience = tmpStr
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "oidc_skip_expiry_check"); ok && tmpStr == "true" {
|
|
||||||
cfg.OidcSkipExpiryCheck = true
|
|
||||||
} else {
|
|
||||||
cfg.OidcSkipExpiryCheck = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if tmpStr, ok = conf.Get("common", "oidc_skip_issuer_check"); ok && tmpStr == "true" {
|
|
||||||
cfg.OidcSkipIssuerCheck = true
|
|
||||||
} else {
|
|
||||||
cfg.OidcSkipIssuerCheck = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if allowPortsStr, ok := conf.Get("common", "allow_ports"); ok {
|
if allowPortsStr, ok := conf.Get("common", "allow_ports"); ok {
|
||||||
// e.g. 1000-2000,2001,2002,3000-4000
|
// e.g. 1000-2000,2001,2002,3000-4000
|
||||||
ports, errRet := util.ParseRangeNumbers(allowPortsStr)
|
ports, errRet := util.ParseRangeNumbers(allowPortsStr)
|
||||||
|
Loading…
Reference in New Issue
Block a user