Update by comments

This commit is contained in:
yuyulei 2020-12-28 14:01:21 +08:00
parent 8d27d7f6ee
commit 896add33db
26 changed files with 784 additions and 900 deletions

View File

@ -62,7 +62,7 @@ func (svr *Service) apiReload(w http.ResponseWriter, r *http.Request) {
return return
} }
pxyCfgs, visitorCfgs, err := config.LoadAllConfFromIni(svr.cfg.User, content, newCommonCfg.Start) pxyCfgs, visitorCfgs, err := config.LoadAllProxyConfsFromIni(svr.cfg.User, content, newCommonCfg.Start)
if err != nil { if err != nil {
res.Code = 400 res.Code = 400
res.Msg = err.Error() res.Msg = err.Error()

View File

@ -47,7 +47,7 @@ var httpCmd = &cobra.Command{
Use: "http", Use: "http",
Short: "Run frpc with a single http proxy", Short: "Run frpc with a single http proxy",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, "") clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -43,7 +43,7 @@ var httpsCmd = &cobra.Command{
Use: "https", Use: "https",
Short: "Run frpc with a single https proxy", Short: "Run frpc with a single https proxy",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, "") clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -129,7 +129,7 @@ func handleSignal(svr *client.Service) {
close(kcpDoneCh) close(kcpDoneCh)
} }
func parseClientCommonCfg(fileType int, source interface{}) (cfg config.ClientCommonConf, err error) { func parseClientCommonCfg(fileType int, source []byte) (cfg config.ClientCommonConf, err error) {
if fileType == CfgFileTypeIni { if fileType == CfgFileTypeIni {
cfg, err = config.UnmarshalClientConfFromIni(source) cfg, err = config.UnmarshalClientConfFromIni(source)
} else if fileType == CfgFileTypeCmd { } else if fileType == CfgFileTypeCmd {
@ -194,7 +194,7 @@ func runClient(cfgFilePath string) (err error) {
return return
} }
pxyCfgs, visitorCfgs, err := config.LoadAllConfFromIni(cfg.User, content, cfg.Start) pxyCfgs, visitorCfgs, err := config.LoadAllProxyConfsFromIni(cfg.User, content, cfg.Start)
if err != nil { if err != nil {
return return
} }

View File

@ -45,7 +45,7 @@ var stcpCmd = &cobra.Command{
Use: "stcp", Use: "stcp",
Short: "Run frpc with a single stcp proxy", Short: "Run frpc with a single stcp proxy",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, "") clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -45,7 +45,7 @@ var sudpCmd = &cobra.Command{
Use: "sudp", Use: "sudp",
Short: "Run frpc with a single sudp proxy", Short: "Run frpc with a single sudp proxy",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, "") clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -41,7 +41,7 @@ var tcpCmd = &cobra.Command{
Use: "tcp", Use: "tcp",
Short: "Run frpc with a single tcp proxy", Short: "Run frpc with a single tcp proxy",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, "") clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -44,7 +44,7 @@ var tcpMuxCmd = &cobra.Command{
Use: "tcpmux", Use: "tcpmux",
Short: "Run frpc with a single tcpmux proxy", Short: "Run frpc with a single tcpmux proxy",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, "") clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -41,7 +41,7 @@ var udpCmd = &cobra.Command{
Use: "udp", Use: "udp",
Short: "Run frpc with a single udp proxy", Short: "Run frpc with a single udp proxy",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, "") clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -45,7 +45,7 @@ var xtcpCmd = &cobra.Command{
Use: "xtcp", Use: "xtcp",
Short: "Run frpc with a single xtcp proxy", Short: "Run frpc with a single xtcp proxy",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, "") clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -114,7 +114,7 @@ var rootCmd = &cobra.Command{
cfg, err = parseServerCommonCfg(CfgFileTypeIni, content) cfg, err = parseServerCommonCfg(CfgFileTypeIni, content)
} else { } else {
log.Info("frps uses command line arguments for config") log.Info("frps uses command line arguments for config")
cfg, err = parseServerCommonCfg(CfgFileTypeCmd, "") cfg, err = parseServerCommonCfg(CfgFileTypeCmd, nil)
} }
if err != nil { if err != nil {
return err return err
@ -135,7 +135,7 @@ func Execute() {
} }
} }
func parseServerCommonCfg(fileType int, source interface{}) (cfg config.ServerCommonConf, err error) { func parseServerCommonCfg(fileType int, source []byte) (cfg config.ServerCommonConf, err error) {
if fileType == CfgFileTypeIni { if fileType == CfgFileTypeIni {
cfg, err = config.UnmarshalServerConfFromIni(source) cfg, err = config.UnmarshalServerConfFromIni(source)
} else if fileType == CfgFileTypeCmd { } else if fileType == CfgFileTypeCmd {

View File

@ -26,13 +26,13 @@ type BaseConfig struct {
// authenticate frpc with frps. If "token" is specified - token will be // authenticate frpc with frps. If "token" is specified - token will be
// read into login message. If "oidc" is specified - OIDC (Open ID Connect) // 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". // token will be issued using OIDC settings. By default, this value is "token".
AuthenticationMethod string `ini:"authentication_method",json:"authentication_method"` AuthenticationMethod string `ini:"authentication_method" json:"authentication_method"`
// AuthenticateHeartBeats specifies whether to include authentication token in // AuthenticateHeartBeats specifies whether to include authentication token in
// heartbeats sent to frps. By default, this value is false. // heartbeats sent to frps. By default, this value is false.
AuthenticateHeartBeats bool `ini:"authenticate_heartbeats",json:"authenticate_heartbeats"` AuthenticateHeartBeats bool `ini:"authenticate_heartbeats" json:"authenticate_heartbeats"`
// AuthenticateNewWorkConns specifies whether to include authentication token in // AuthenticateNewWorkConns specifies whether to include authentication token in
// new work connections sent to frps. By default, this value is false. // new work connections sent to frps. By default, this value is false.
AuthenticateNewWorkConns bool `ini:"authenticate_new_work_conns",json:"authenticate_new_work_conns"` AuthenticateNewWorkConns bool `ini:"authenticate_new_work_conns" json:"authenticate_new_work_conns"`
} }
func getDefaultBaseConf() BaseConfig { func getDefaultBaseConf() BaseConfig {
@ -44,9 +44,9 @@ func getDefaultBaseConf() BaseConfig {
} }
type ClientConfig struct { type ClientConfig struct {
BaseConfig `ini:",,,,extends"` BaseConfig `ini:",extends"`
OidcClientConfig `ini:",,,,extends"` OidcClientConfig `ini:",extends"`
TokenConfig `ini:",,,,extends"` TokenConfig `ini:",extends"`
} }
func GetDefaultClientConf() ClientConfig { func GetDefaultClientConf() ClientConfig {
@ -58,9 +58,9 @@ func GetDefaultClientConf() ClientConfig {
} }
type ServerConfig struct { type ServerConfig struct {
BaseConfig `ini:",,,,extends"` BaseConfig `ini:",extends"`
OidcServerConfig `ini:",,,,extends"` OidcServerConfig `ini:",extends"`
TokenConfig `ini:",,,,extends"` TokenConfig `ini:",extends"`
} }
func GetDefaultServerConf() ServerConfig { func GetDefaultServerConf() ServerConfig {

View File

@ -28,18 +28,18 @@ type OidcClientConfig struct {
// OidcClientID specifies the client ID to use to get a token in OIDC // OidcClientID specifies the client ID to use to get a token in OIDC
// authentication if AuthenticationMethod == "oidc". By default, this value // authentication if AuthenticationMethod == "oidc". By default, this value
// is "". // is "".
OidcClientID string `ini:"oidc_client_id",json:"oidc_client_id"` OidcClientID string `ini:"oidc_client_id" json:"oidc_client_id"`
// OidcClientSecret specifies the client secret to use to get a token in OIDC // OidcClientSecret specifies the client secret to use to get a token in OIDC
// authentication if AuthenticationMethod == "oidc". By default, this value // authentication if AuthenticationMethod == "oidc". By default, this value
// is "". // is "".
OidcClientSecret string `ini:"oidc_client_secret",json:"oidc_client_secret"` OidcClientSecret string `ini:"oidc_client_secret" json:"oidc_client_secret"`
// OidcAudience specifies the audience of the token in OIDC authentication // OidcAudience specifies the audience of the token in OIDC authentication
//if AuthenticationMethod == "oidc". By default, this value is "". //if AuthenticationMethod == "oidc". By default, this value is "".
OidcAudience string `ini:"oidc_audience",json:"oidc_audience"` OidcAudience string `ini:"oidc_audience" json:"oidc_audience"`
// OidcTokenEndpointURL specifies the URL which implements OIDC Token Endpoint. // OidcTokenEndpointURL specifies the URL which implements OIDC Token Endpoint.
// It will be used to get an OIDC token if AuthenticationMethod == "oidc". // It will be used to get an OIDC token if AuthenticationMethod == "oidc".
// By default, this value is "". // By default, this value is "".
OidcTokenEndpointURL string `ini:"oidc_token_endpoint_url",json:"oidc_token_endpoint_url"` OidcTokenEndpointURL string `ini:"oidc_token_endpoint_url" json:"oidc_token_endpoint_url"`
} }
func getDefaultOidcClientConf() OidcClientConfig { func getDefaultOidcClientConf() OidcClientConfig {
@ -56,20 +56,20 @@ type OidcServerConfig struct {
// 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
// with the issuer claim in the OIDC token. It will be used if // with the issuer claim in the OIDC token. It will be used if
// AuthenticationMethod == "oidc". By default, this value is "". // AuthenticationMethod == "oidc". By default, this value is "".
OidcIssuer string `ini:"oidc_issuer",json:"oidc_issuer"` OidcIssuer string `ini:"oidc_issuer" json:"oidc_issuer"`
// OidcAudience specifies the audience OIDC tokens should contain when validated. // OidcAudience specifies the audience OIDC tokens should contain when validated.
// If this value is empty, audience ("client ID") verification will be skipped. // If this value is empty, audience ("client ID") verification will be skipped.
// It will be used when AuthenticationMethod == "oidc". By default, this // It will be used when AuthenticationMethod == "oidc". By default, this
// value is "". // value is "".
OidcAudience string `ini:"oidc_audience",json:"oidc_audience"` OidcAudience string `ini:"oidc_audience" json:"oidc_audience"`
// OidcSkipExpiryCheck specifies whether to skip checking if the OIDC token is // OidcSkipExpiryCheck specifies whether to skip checking if the OIDC token is
// expired. It will be used when AuthenticationMethod == "oidc". By default, this // expired. It will be used when AuthenticationMethod == "oidc". By default, this
// value is false. // value is false.
OidcSkipExpiryCheck bool `ini:"oidc_skip_expiry_check",json:"oidc_skip_expiry_check"` OidcSkipExpiryCheck bool `ini:"oidc_skip_expiry_check" json:"oidc_skip_expiry_check"`
// OidcSkipIssuerCheck specifies whether to skip checking if the OIDC token's // OidcSkipIssuerCheck specifies whether to skip checking if the OIDC token's
// issuer claim matches the issuer specified in OidcIssuer. It will be used when // issuer claim matches the issuer specified in OidcIssuer. It will be used when
// AuthenticationMethod == "oidc". By default, this value is false. // AuthenticationMethod == "oidc". By default, this value is false.
OidcSkipIssuerCheck bool `ini:"oidc_skip_issuer_check",json:"oidc_skip_issuer_check"` OidcSkipIssuerCheck bool `ini:"oidc_skip_issuer_check" json:"oidc_skip_issuer_check"`
} }
func getDefaultOidcServerConf() OidcServerConfig { func getDefaultOidcServerConf() OidcServerConfig {

View File

@ -26,7 +26,7 @@ type TokenConfig struct {
// Token specifies the authorization token used to create keys to be sent // Token specifies the authorization token used to create keys to be sent
// to the server. The server must have a matching token for authorization // to the server. The server must have a matching token for authorization
// to succeed. By default, this value is "". // to succeed. By default, this value is "".
Token string `ini:"token",json:"token"` Token string `ini:"token" json:"token"`
} }
func getDefaultTokenConf() TokenConfig { func getDefaultTokenConf() TokenConfig {

12
pkg/config/README.md Normal file
View File

@ -0,0 +1,12 @@
So far, there is no mature Go project that does well in parsing `*.ini` files.
By comparison, we have selected an open source project: `https://github.com/go-ini/ini`.
This library helped us solve most of the key-value matching, but there are still some problems, such as not supporting parsing `map`.
We add our own logic on the basis of this library. In the current situationwhich, we need to complete the entire `Unmarshal` in two steps:
* Step#1, use `go-ini` to complete the basic parameter matching;
* Step#2, parse our custom parameters to realize parsing special structure, like `map`, `array`.
Some of the keywords in `tag`(like inline, extends, etc.) may be different from standard libraries such as `json` and `protobuf` in Go. For details, please refer to the library documentation: https://ini.unknwon.io/docs/intro.

View File

@ -1,121 +0,0 @@
// Copyright 2019 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"encoding/json"
"errors"
"strconv"
"strings"
)
const (
MB = 1024 * 1024
KB = 1024
)
type BandwidthQuantity struct {
s string // MB or KB
i int64 // bytes
}
func NewBandwidthQuantity(s string) (BandwidthQuantity, error) {
q := BandwidthQuantity{}
err := q.UnmarshalString(s)
if err != nil {
return q, err
}
return q, nil
}
func MustBandwidthQuantity(s string) BandwidthQuantity {
q := BandwidthQuantity{}
err := q.UnmarshalString(s)
if err != nil {
panic(err)
}
return q
}
func (q *BandwidthQuantity) Equal(u *BandwidthQuantity) bool {
if q == nil && u == nil {
return true
}
if q != nil && u != nil {
return q.i == u.i
}
return false
}
func (q *BandwidthQuantity) String() string {
return q.s
}
func (q *BandwidthQuantity) UnmarshalString(s string) error {
s = strings.TrimSpace(s)
if s == "" {
return nil
}
var (
base int64
f float64
err error
)
if strings.HasSuffix(s, "MB") {
base = MB
fstr := strings.TrimSuffix(s, "MB")
f, err = strconv.ParseFloat(fstr, 64)
if err != nil {
return err
}
} else if strings.HasSuffix(s, "KB") {
base = KB
fstr := strings.TrimSuffix(s, "KB")
f, err = strconv.ParseFloat(fstr, 64)
if err != nil {
return err
}
} else {
return errors.New("unit not support")
}
q.s = s
q.i = int64(f * float64(base))
return nil
}
func (q *BandwidthQuantity) UnmarshalJSON(b []byte) error {
if len(b) == 4 && string(b) == "null" {
return nil
}
var str string
err := json.Unmarshal(b, &str)
if err != nil {
return err
}
return q.UnmarshalString(str)
}
func (q *BandwidthQuantity) MarshalJSON() ([]byte, error) {
return []byte("\"" + q.s + "\""), nil
}
func (q *BandwidthQuantity) Bytes() int64 {
return q.i
}

View File

@ -25,6 +25,116 @@ import (
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
// ClientCommonConf contains information for a client service. It is
// recommended to use GetDefaultClientConf instead of creating this object
// directly, so that all unspecified fields have reasonable default values.
type ClientCommonConf struct {
auth.ClientConfig `ini:",extends" json:"inline"`
// ServerAddr specifies the address of the server to connect to. By
// default, this value is "0.0.0.0".
ServerAddr string `ini:"server_addr" josn:"server_addr"`
// ServerPort specifies the port to connect to the server on. By default,
// this value is 7000.
ServerPort int `ini:"server_port" json:"server_port"`
// HTTPProxy specifies a proxy address to connect to the server through. If
// this value is "", the server will be connected to directly. By default,
// this value is read from the "http_proxy" environment variable.
HTTPProxy string `ini:"http_proxy" json:"http_proxy"`
// LogFile specifies a file where logs will be written to. This value will
// only be used if LogWay is set appropriately. By default, this value is
// "console".
LogFile string `ini:"log_file" json:"log_file"`
// LogWay specifies the way logging is managed. Valid values are "console"
// or "file". If "console" is used, logs will be printed to stdout. If
// "file" is used, logs will be printed to LogFile. By default, this value
// is "console".
LogWay string `ini:"log_way" json:"log_way"`
// LogLevel specifies the minimum log level. Valid values are "trace",
// "debug", "info", "warn", and "error". By default, this value is "info".
LogLevel string `ini:"log_level" json:"log_level"`
// LogMaxDays specifies the maximum number of days to store log information
// before deletion. This is only used if LogWay == "file". By default, this
// value is 0.
LogMaxDays int64 `ini:"log_max_days" json:"log_max_days"`
// DisableLogColor disables log colors when LogWay == "console" when set to
// true. By default, this value is false.
DisableLogColor bool `ini:"disable_log_color" json:"disable_log_color"`
// AdminAddr specifies the address that the admin server binds to. By
// default, this value is "127.0.0.1".
AdminAddr string `ini:"admin_addr" json:"admin_addr"`
// AdminPort specifies the port for the admin server to listen on. If this
// value is 0, the admin server will not be started. By default, this value
// is 0.
AdminPort int `ini:"admin_port" json:"admin_port"`
// AdminUser specifies the username that the admin server will use for
// login. By default, this value is "admin".
AdminUser string `ini:"admin_user" json:"admin_user"`
// AdminPwd specifies the password that the admin server will use for
// login. By default, this value is "admin".
AdminPwd string `ini:"admin_pwd" json:"admin_pwd"`
// AssetsDir specifies the local directory that the admin server will load
// resources from. If this value is "", assets will be loaded from the
// bundled executable using statik. By default, this value is "".
AssetsDir string `ini:"assets_dir" json:"assets_dir"`
// PoolCount specifies the number of connections the client will make to
// the server in advance. By default, this value is 0.
PoolCount int `ini:"pool_count" json:"pool_count"`
// TCPMux toggles TCP stream multiplexing. This allows multiple requests
// from a client to share a single TCP connection. If this value is true,
// the server must have TCP multiplexing enabled as well. By default, this
// value is true.
TCPMux bool `ini:"tcp_mux" json:"tcp_mux"`
// User specifies a prefix for proxy names to distinguish them from other
// clients. If this value is not "", proxy names will automatically be
// changed to "{user}.{proxy_name}". By default, this value is "".
User string `ini:"user" json:"user"`
// DNSServer specifies a DNS server address for FRPC to use. If this value
// is "", the default DNS will be used. By default, this value is "".
DNSServer string `ini:"dns_server" json:"dns_server"`
// LoginFailExit controls whether or not the client should exit after a
// failed login attempt. If false, the client will retry until a login
// attempt succeeds. By default, this value is true.
LoginFailExit bool `ini:"login_fail_exit" json:"login_fail_exit"`
// Start specifies a set of enabled proxies by name. If this set is empty,
// all supplied proxies are enabled. By default, this value is an empty
// set.
Start []string `ini:"start" json:"start"`
//Start map[string]struct{} `json:"start"`
// Protocol specifies the protocol to use when interacting with the server.
// Valid values are "tcp", "kcp" and "websocket". By default, this value
// is "tcp".
Protocol string `ini:"protocol" json:"protocol"`
// TLSEnable specifies whether or not TLS should be used when communicating
// with the server. If "tls_cert_file" and "tls_key_file" are valid,
// client will load the supplied tls configuration.
TLSEnable bool `ini:"tls_enable" json:"tls_enable"`
// ClientTLSCertPath specifies the path of the cert file that client will
// load. It only works when "tls_enable" is true and "tls_key_file" is valid.
TLSCertFile string `ini:"tls_cert_file" json:"tls_cert_file"`
// ClientTLSKeyPath specifies the path of the secret key file that client
// will load. It only works when "tls_enable" is true and "tls_cert_file"
// are valid.
TLSKeyFile string `ini:"tls_key_file" json:"tls_key_file"`
// TrustedCaFile specifies the path of the trusted ca file that will load.
// It only works when "tls_enable" is valid and tls configuration of server
// has been specified.
TLSTrustedCaFile string `ini:"tls_trusted_ca_file" json:"tls_trusted_ca_file"`
// HeartBeatInterval specifies at what interval heartbeats are sent to the
// server, in seconds. It is not recommended to change this value. By
// default, this value is 30.
HeartbeatInterval int64 `ini:"heartbeat_interval" json:"heartbeat_interval"`
// HeartBeatTimeout specifies the maximum allowed heartbeat response delay
// before the connection is terminated, in seconds. It is not recommended
// to change this value. By default, this value is 90.
HeartbeatTimeout int64 `ini:"heartbeat_timeout" json:"heartbeat_timeout"`
// Client meta info
Metas map[string]string `ini:"-" json:"metas"`
// UDPPacketSize specifies the udp packet size
// By default, this value is 1500
UDPPacketSize int64 `ini:"udp_packet_size" json:"udp_packet_size"`
}
// 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{
@ -101,8 +211,7 @@ func UnmarshalClientConfFromIni(source interface{}) (ClientCommonConf, error) {
s, err := f.GetSection("common") s, err := f.GetSection("common")
if err != nil { if err != nil {
// TODO: add error info return ClientCommonConf{}, fmt.Errorf("invalid configuration file, not found [common] section")
return ClientCommonConf{}, err
} }
common := GetDefaultClientConf() common := GetDefaultClientConf()
@ -118,7 +227,7 @@ func UnmarshalClientConfFromIni(source interface{}) (ClientCommonConf, error) {
// if len(startProxy) is 0, start all // if len(startProxy) is 0, start all
// otherwise just start proxies in startProxy map // otherwise just start proxies in startProxy map
func LoadAllConfFromIni( func LoadAllProxyConfsFromIni(
prefix string, prefix string,
source interface{}, source interface{},
start []string, start []string,
@ -164,9 +273,9 @@ func LoadAllConfFromIni(
} }
for _, section := range rangeSections { for _, section := range rangeSections {
err = appendTemplates(f, section) err = renderRangeProxyTemplates(f, section)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, fmt.Errorf("fail to render range-section[%s] with error: %v", section.Name(), err)
} }
} }
@ -207,7 +316,7 @@ func LoadAllConfFromIni(
return proxyConfs, visitorConfs, nil return proxyConfs, visitorConfs, nil
} }
func appendTemplates(f *ini.File, section *ini.Section) error { func renderRangeProxyTemplates(f *ini.File, section *ini.Section) error {
// Validation // Validation
localPortStr := section.Key("local_port").String() localPortStr := section.Key("local_port").String()
@ -227,11 +336,11 @@ func appendTemplates(f *ini.File, section *ini.Section) error {
} }
if len(localPorts) != len(remotePorts) { if len(localPorts) != len(remotePorts) {
return fmt.Errorf("range section [%s] local ports number should be same with remote ports number", section.Name()) return fmt.Errorf("local ports number should be same with remote ports number")
} }
if len(localPorts) == 0 { if len(localPorts) == 0 {
return fmt.Errorf("range section [%s] local_port and remote_port is necessary", section.Name()) return fmt.Errorf("local_port and remote_port is necessary")
} }
// Templates // Templates

View File

@ -325,10 +325,8 @@ func Test_LoadClientBasicConf(t *testing.T) {
HealthCheckAddr: "127.0.0.9:29", HealthCheckAddr: "127.0.0.9:29",
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 6009, RemotePort: 6009,
}, },
},
testUser + ".ssh_random": &TCPProxyConf{ testUser + ".ssh_random": &TCPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".ssh_random", ProxyName: testUser + ".ssh_random",
@ -338,10 +336,8 @@ func Test_LoadClientBasicConf(t *testing.T) {
LocalPort: 29, LocalPort: 29,
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 9, RemotePort: 9,
}, },
},
testUser + ".tcp_port_0": &TCPProxyConf{ testUser + ".tcp_port_0": &TCPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".tcp_port_0", ProxyName: testUser + ".tcp_port_0",
@ -351,10 +347,8 @@ func Test_LoadClientBasicConf(t *testing.T) {
LocalPort: 6010, LocalPort: 6010,
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 6010, RemotePort: 6010,
}, },
},
testUser + ".tcp_port_1": &TCPProxyConf{ testUser + ".tcp_port_1": &TCPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".tcp_port_1", ProxyName: testUser + ".tcp_port_1",
@ -364,10 +358,8 @@ func Test_LoadClientBasicConf(t *testing.T) {
LocalPort: 6011, LocalPort: 6011,
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 6011, RemotePort: 6011,
}, },
},
testUser + ".tcp_port_2": &TCPProxyConf{ testUser + ".tcp_port_2": &TCPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".tcp_port_2", ProxyName: testUser + ".tcp_port_2",
@ -377,10 +369,8 @@ func Test_LoadClientBasicConf(t *testing.T) {
LocalPort: 6019, LocalPort: 6019,
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 6019, RemotePort: 6019,
}, },
},
testUser + ".dns": &UDPProxyConf{ testUser + ".dns": &UDPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".dns", ProxyName: testUser + ".dns",
@ -392,10 +382,8 @@ func Test_LoadClientBasicConf(t *testing.T) {
LocalPort: 59, LocalPort: 59,
}, },
}, },
UDPProxySpec: UDPProxySpec{
RemotePort: 6009, RemotePort: 6009,
}, },
},
testUser + ".udp_port_0": &UDPProxyConf{ testUser + ".udp_port_0": &UDPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".udp_port_0", ProxyName: testUser + ".udp_port_0",
@ -407,10 +395,8 @@ func Test_LoadClientBasicConf(t *testing.T) {
LocalPort: 6000, LocalPort: 6000,
}, },
}, },
UDPProxySpec: UDPProxySpec{
RemotePort: 6000, RemotePort: 6000,
}, },
},
testUser + ".udp_port_1": &UDPProxyConf{ testUser + ".udp_port_1": &UDPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".udp_port_1", ProxyName: testUser + ".udp_port_1",
@ -422,10 +408,8 @@ func Test_LoadClientBasicConf(t *testing.T) {
LocalPort: 6010, LocalPort: 6010,
}, },
}, },
UDPProxySpec: UDPProxySpec{
RemotePort: 6010, RemotePort: 6010,
}, },
},
testUser + ".udp_port_2": &UDPProxyConf{ testUser + ".udp_port_2": &UDPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".udp_port_2", ProxyName: testUser + ".udp_port_2",
@ -437,10 +421,8 @@ func Test_LoadClientBasicConf(t *testing.T) {
LocalPort: 6011, LocalPort: 6011,
}, },
}, },
UDPProxySpec: UDPProxySpec{
RemotePort: 6011, RemotePort: 6011,
}, },
},
testUser + ".web01": &HTTPProxyConf{ testUser + ".web01": &HTTPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".web01", ProxyName: testUser + ".web01",
@ -459,7 +441,6 @@ func Test_LoadClientBasicConf(t *testing.T) {
HealthCheckURL: "http://127.0.0.9:89/status", HealthCheckURL: "http://127.0.0.9:89/status",
}, },
}, },
HTTPProxySpec: HTTPProxySpec{
DomainConf: DomainConf{ DomainConf: DomainConf{
CustomDomains: []string{"web02.yourdomain.com"}, CustomDomains: []string{"web02.yourdomain.com"},
SubDomain: "web01", SubDomain: "web01",
@ -472,7 +453,6 @@ func Test_LoadClientBasicConf(t *testing.T) {
"X-From-Where": "frp", "X-From-Where": "frp",
}, },
}, },
},
testUser + ".web02": &HTTPSProxyConf{ testUser + ".web02": &HTTPSProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".web02", ProxyName: testUser + ".web02",
@ -485,13 +465,11 @@ func Test_LoadClientBasicConf(t *testing.T) {
}, },
ProxyProtocolVersion: "v2", ProxyProtocolVersion: "v2",
}, },
HTTPSProxySpec: HTTPSProxySpec{
DomainConf: DomainConf{ DomainConf: DomainConf{
CustomDomains: []string{"web02.yourdomain.com"}, CustomDomains: []string{"web02.yourdomain.com"},
SubDomain: "web01", SubDomain: "web01",
}, },
}, },
},
testUser + ".secret_tcp": &STCPProxyConf{ testUser + ".secret_tcp": &STCPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".secret_tcp", ProxyName: testUser + ".secret_tcp",
@ -501,11 +479,9 @@ func Test_LoadClientBasicConf(t *testing.T) {
LocalPort: 22, LocalPort: 22,
}, },
}, },
STCPProxySpec: STCPProxySpec{
Role: "server", Role: "server",
Sk: "abcdefg", Sk: "abcdefg",
}, },
},
testUser + ".p2p_tcp": &XTCPProxyConf{ testUser + ".p2p_tcp": &XTCPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".p2p_tcp", ProxyName: testUser + ".p2p_tcp",
@ -515,11 +491,9 @@ func Test_LoadClientBasicConf(t *testing.T) {
LocalPort: 22, LocalPort: 22,
}, },
}, },
XTCPProxySpec: XTCPProxySpec{
Role: "server", Role: "server",
Sk: "abcdefg", Sk: "abcdefg",
}, },
},
testUser + ".tcpmuxhttpconnect": &TCPMuxProxyConf{ testUser + ".tcpmuxhttpconnect": &TCPMuxProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".tcpmuxhttpconnect", ProxyName: testUser + ".tcpmuxhttpconnect",
@ -529,14 +503,12 @@ func Test_LoadClientBasicConf(t *testing.T) {
LocalPort: 10701, LocalPort: 10701,
}, },
}, },
TCPMuxProxySpec: TCPMuxProxySpec{
DomainConf: DomainConf{ DomainConf: DomainConf{
CustomDomains: []string{"tunnel1"}, CustomDomains: []string{"tunnel1"},
SubDomain: "", SubDomain: "",
}, },
Multiplexer: "httpconnect", Multiplexer: "httpconnect",
}, },
},
testUser + ".plugin_unix_domain_socket": &TCPProxyConf{ testUser + ".plugin_unix_domain_socket": &TCPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".plugin_unix_domain_socket", ProxyName: testUser + ".plugin_unix_domain_socket",
@ -549,10 +521,8 @@ func Test_LoadClientBasicConf(t *testing.T) {
}, },
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 6003, RemotePort: 6003,
}, },
},
testUser + ".plugin_http_proxy": &TCPProxyConf{ testUser + ".plugin_http_proxy": &TCPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".plugin_http_proxy", ProxyName: testUser + ".plugin_http_proxy",
@ -566,10 +536,8 @@ func Test_LoadClientBasicConf(t *testing.T) {
}, },
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 6004, RemotePort: 6004,
}, },
},
testUser + ".plugin_socks5": &TCPProxyConf{ testUser + ".plugin_socks5": &TCPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".plugin_socks5", ProxyName: testUser + ".plugin_socks5",
@ -583,10 +551,8 @@ func Test_LoadClientBasicConf(t *testing.T) {
}, },
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 6005, RemotePort: 6005,
}, },
},
testUser + ".plugin_static_file": &TCPProxyConf{ testUser + ".plugin_static_file": &TCPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".plugin_static_file", ProxyName: testUser + ".plugin_static_file",
@ -602,10 +568,8 @@ func Test_LoadClientBasicConf(t *testing.T) {
}, },
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 6006, RemotePort: 6006,
}, },
},
testUser + ".plugin_https2http": &HTTPSProxyConf{ testUser + ".plugin_https2http": &HTTPSProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".plugin_https2http", ProxyName: testUser + ".plugin_https2http",
@ -622,12 +586,10 @@ func Test_LoadClientBasicConf(t *testing.T) {
}, },
}, },
}, },
HTTPSProxySpec: HTTPSProxySpec{
DomainConf: DomainConf{ DomainConf: DomainConf{
CustomDomains: []string{"test.yourdomain.com"}, CustomDomains: []string{"test.yourdomain.com"},
}, },
}, },
},
testUser + ".plugin_http2https": &HTTPProxyConf{ testUser + ".plugin_http2https": &HTTPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testUser + ".plugin_http2https", ProxyName: testUser + ".plugin_http2https",
@ -642,12 +604,10 @@ func Test_LoadClientBasicConf(t *testing.T) {
}, },
}, },
}, },
HTTPProxySpec: HTTPProxySpec{
DomainConf: DomainConf{ DomainConf: DomainConf{
CustomDomains: []string{"test.yourdomain.com"}, CustomDomains: []string{"test.yourdomain.com"},
}, },
}, },
},
} }
visitorExpected := map[string]VisitorConf{ visitorExpected := map[string]VisitorConf{
@ -675,7 +635,7 @@ func Test_LoadClientBasicConf(t *testing.T) {
}, },
} }
proxyActual, visitorActual, err := LoadAllConfFromIni(testUser, testClientBytesWithFull, nil) proxyActual, visitorActual, err := LoadAllProxyConfsFromIni(testUser, testClientBytesWithFull, nil)
assert.NoError(err) assert.NoError(err)
assert.Equal(proxyExpected, proxyActual) assert.Equal(proxyExpected, proxyActual)
assert.Equal(visitorExpected, visitorActual) assert.Equal(visitorExpected, visitorActual)

View File

@ -25,6 +25,197 @@ import (
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
// Proxy
var (
proxyConfTypeMap = map[string]reflect.Type{
consts.TCPProxy: reflect.TypeOf(TCPProxyConf{}),
consts.TCPMuxProxy: reflect.TypeOf(TCPMuxProxyConf{}),
consts.UDPProxy: reflect.TypeOf(UDPProxyConf{}),
consts.HTTPProxy: reflect.TypeOf(HTTPProxyConf{}),
consts.HTTPSProxy: reflect.TypeOf(HTTPSProxyConf{}),
consts.STCPProxy: reflect.TypeOf(STCPProxyConf{}),
consts.XTCPProxy: reflect.TypeOf(XTCPProxyConf{}),
consts.SUDPProxy: reflect.TypeOf(SUDPProxyConf{}),
}
)
func NewConfByType(proxyType string) ProxyConf {
v, ok := proxyConfTypeMap[proxyType]
if !ok {
return nil
}
cfg := reflect.New(v).Interface().(ProxyConf)
return cfg
}
type ProxyConf interface {
GetBaseInfo() *BaseProxyConf
UnmarshalFromMsg(*msg.NewProxy)
UnmarshalFromIni(string, string, *ini.Section) error
MarshalToMsg(*msg.NewProxy)
CheckForCli() error
CheckForSvr(ServerCommonConf) error
Compare(ProxyConf) bool
}
// LocalSvrConf configures what location the client will to, or what
// plugin will be used.
type LocalSvrConf struct {
// LocalIP specifies the IP address or host name to to.
LocalIP string `ini:"local_ip" json:"local_ip"`
// LocalPort specifies the port to to.
LocalPort int `ini:"local_port" json:"local_port"`
// Plugin specifies what plugin should be used for ng. If this value
// is set, the LocalIp and LocalPort values will be ignored. By default,
// this value is "".
Plugin string `ini:"plugin" json:"plugin"`
// PluginParams specify parameters to be passed to the plugin, if one is
// being used. By default, this value is an empty map.
PluginParams map[string]string `ini:"-"`
}
// HealthCheckConf configures health checking. This can be useful for load
// balancing purposes to detect and remove proxies to failing services.
type HealthCheckConf struct {
// HealthCheckType specifies what protocol to use for health checking.
// Valid values include "tcp", "http", and "". If this value is "", health
// checking will not be performed. By default, this value is "".
//
// If the type is "tcp", a connection will be attempted to the target
// server. If a connection cannot be established, the health check fails.
//
// If the type is "http", a GET request will be made to the endpoint
// specified by HealthCheckURL. If the response is not a 200, the health
// check fails.
HealthCheckType string `ini:"health_check_type" json:"health_check_type"` // tcp | http
// HealthCheckTimeoutS specifies the number of seconds to wait for a health
// check attempt to connect. If the timeout is reached, this counts as a
// health check failure. By default, this value is 3.
HealthCheckTimeoutS int `ini:"health_check_timeout_s" json:"health_check_timeout_s"`
// HealthCheckMaxFailed specifies the number of allowed failures before the
// is stopped. By default, this value is 1.
HealthCheckMaxFailed int `ini:"health_check_max_failed" json:"health_check_max_failed"`
// HealthCheckIntervalS specifies the time in seconds between health
// checks. By default, this value is 10.
HealthCheckIntervalS int `ini:"health_check_interval_s" json:"health_check_interval_s"`
// HealthCheckURL specifies the address to send health checks to if the
// health check type is "http".
HealthCheckURL string `ini:"health_check_url" json:"health_check_url"`
// HealthCheckAddr specifies the address to connect to if the health check
// type is "tcp".
HealthCheckAddr string `ini:"-"`
}
// BaseProxyConf provides configuration info that is common to all types.
type BaseProxyConf struct {
// ProxyName is the name of this
ProxyName string `ini:"name" json:"name"`
// ProxyType specifies the type of this Valid values include "tcp",
// "udp", "http", "https", "stcp", and "xtcp". By default, this value is
// "tcp".
ProxyType string `ini:"type" json:"type"`
// UseEncryption controls whether or not communication with the server will
// be encrypted. Encryption is done using the tokens supplied in the server
// and client configuration. By default, this value is false.
UseEncryption bool `ini:"use_encryption" json:"use_encryption"`
// UseCompression controls whether or not communication with the server
// will be compressed. By default, this value is false.
UseCompression bool `ini:"use_compression" json:"use_compression"`
// Group specifies which group the is a part of. The server will use
// this information to load balance proxies in the same group. If the value
// is "", this will not be in a group. By default, this value is "".
Group string `ini:"group" json:"group"`
// GroupKey specifies a group key, which should be the same among proxies
// of the same group. By default, this value is "".
GroupKey string `ini:"group_key" json:"group_key"`
// ProxyProtocolVersion specifies which protocol version to use. Valid
// values include "v1", "v2", and "". If the value is "", a protocol
// version will be automatically selected. By default, this value is "".
ProxyProtocolVersion string `ini:"proxy_protocol_version" json:"proxy_protocol_version"`
// BandwidthLimit limit the bandwidth
// 0 means no limit
BandwidthLimit BandwidthQuantity `ini:"bandwidth_limit" json:"bandwidth_limit"`
// meta info for each proxy
Metas map[string]string `ini:"-" json:"metas"`
// TODO: LocalSvrConf => LocalAppConf
LocalSvrConf `ini:",extends" json:"inline"`
HealthCheckConf `ini:",extends" json:"inline"`
}
type DomainConf struct {
CustomDomains []string `ini:"custom_domains" json:"custom_domains"`
SubDomain string `ini:"subdomain" json:"subdomain"`
}
// HTTP
type HTTPProxyConf struct {
BaseProxyConf `ini:",extends" json:"inline"`
DomainConf `ini:",extends" json:"inline"`
Locations []string `ini:"locations" json:"locations"`
HTTPUser string `ini:"http_user" json:"http_user"`
HTTPPwd string `ini:"http_pwd" json:"http_pwd"`
HostHeaderRewrite string `ini:"host_header_rewrite" json:"host_header_rewrite"`
Headers map[string]string `ini:"-" json:"headers"`
}
// HTTPS
type HTTPSProxyConf struct {
BaseProxyConf `ini:",extends" json:"inline"`
DomainConf `ini:",extends" json:"inline"`
}
// TCP
type TCPProxyConf struct {
BaseProxyConf `ini:",extends" json:"inline"`
RemotePort int `ini:"remote_port" json:"remote_port"`
}
// TCPMux
type TCPMuxProxyConf struct {
BaseProxyConf `ini:",extends" json:"inline"`
DomainConf `ini:",extends" json:"inline"`
Multiplexer string `ini:"multiplexer"`
}
// STCP
type STCPProxyConf struct {
BaseProxyConf `ini:",extends" json:"inline"`
Role string `ini:"role" json:"role"`
Sk string `ini:"sk" json:"sk"`
}
// XTCP
type XTCPProxyConf struct {
BaseProxyConf `ini:",extends" json:"inline"`
Role string `ini:"role" json:"role"`
Sk string `ini:"sk" json:"sk"`
}
// UDP
type UDPProxyConf struct {
BaseProxyConf `ini:",extends" json:"inline"`
RemotePort int `ini:"remote_port" json:"remote_port"`
}
// SUDP
type SUDPProxyConf struct {
BaseProxyConf `ini:",extends" json:"inline"`
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.
@ -54,23 +245,17 @@ func DefaultProxyConf(proxyType string) ProxyConf {
case consts.STCPProxy: case consts.STCPProxy:
conf = &STCPProxyConf{ conf = &STCPProxyConf{
BaseProxyConf: defaultBaseProxyConf(proxyType), BaseProxyConf: defaultBaseProxyConf(proxyType),
STCPProxySpec: STCPProxySpec{
Role: "server", Role: "server",
},
} }
case consts.XTCPProxy: case consts.XTCPProxy:
conf = &XTCPProxyConf{ conf = &XTCPProxyConf{
BaseProxyConf: defaultBaseProxyConf(proxyType), BaseProxyConf: defaultBaseProxyConf(proxyType),
XTCPProxySpec: XTCPProxySpec{
Role: "server", Role: "server",
},
} }
case consts.SUDPProxy: case consts.SUDPProxy:
conf = &SUDPProxyConf{ conf = &SUDPProxyConf{
BaseProxyConf: defaultBaseProxyConf(proxyType), BaseProxyConf: defaultBaseProxyConf(proxyType),
SUDPProxySpec: SUDPProxySpec{
Role: "server", Role: "server",
},
} }
default: default:
return nil return nil
@ -304,9 +489,21 @@ func (cfg *HealthCheckConf) checkForCli() error {
return nil return nil
} }
// TCP func preUnmarshalFromIni(cfg ProxyConf, prefix string, name string, section *ini.Section) error {
var _ ProxyConf = &TCPProxyConf{} err := section.MapTo(cfg)
if err != nil {
return err
}
err = cfg.GetBaseInfo().decorate(prefix, name, section)
if err != nil {
return err
}
return nil
}
// TCP
func (cfg *TCPProxyConf) Compare(cmp ProxyConf) bool { func (cfg *TCPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*TCPProxyConf) cmpConf, ok := cmp.(*TCPProxyConf)
if !ok { if !ok {
@ -318,7 +515,7 @@ func (cfg *TCPProxyConf) Compare(cmp ProxyConf) bool {
} }
// Add custom logic equal if exists. // Add custom logic equal if exists.
if !reflect.DeepEqual(cfg.TCPProxySpec, cmpConf.TCPProxySpec) { if cfg.RemotePort != cmpConf.RemotePort {
return false return false
} }
@ -333,12 +530,7 @@ func (cfg *TCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
} }
func (cfg *TCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *TCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
err := section.MapTo(cfg) err := preUnmarshalFromIni(cfg, prefix, name, section)
if err != nil {
return err
}
err = cfg.BaseProxyConf.decorate(prefix, name, section)
if err != nil { if err != nil {
return err return err
} }
@ -370,8 +562,6 @@ func (cfg *TCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
} }
// TCPMux // TCPMux
var _ ProxyConf = &TCPMuxProxyConf{}
func (cfg *TCPMuxProxyConf) Compare(cmp ProxyConf) bool { func (cfg *TCPMuxProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*TCPMuxProxyConf) cmpConf, ok := cmp.(*TCPMuxProxyConf)
if !ok { if !ok {
@ -383,7 +573,11 @@ func (cfg *TCPMuxProxyConf) Compare(cmp ProxyConf) bool {
} }
// Add custom logic equal if exists. // Add custom logic equal if exists.
if !reflect.DeepEqual(cfg.TCPMuxProxySpec, cmpConf.TCPMuxProxySpec) { if !reflect.DeepEqual(cfg.DomainConf, cmpConf.DomainConf) {
return false
}
if cfg.Multiplexer != cmpConf.Multiplexer {
return false return false
} }
@ -391,12 +585,7 @@ func (cfg *TCPMuxProxyConf) Compare(cmp ProxyConf) bool {
} }
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 := section.MapTo(cfg) err := preUnmarshalFromIni(cfg, prefix, name, section)
if err != nil {
return err
}
err = cfg.BaseProxyConf.decorate(prefix, name, section)
if err != nil { if err != nil {
return err return err
} }
@ -459,8 +648,6 @@ func (cfg *TCPMuxProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error)
} }
// UDP // UDP
var _ ProxyConf = &UDPProxyConf{}
func (cfg *UDPProxyConf) Compare(cmp ProxyConf) bool { func (cfg *UDPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*UDPProxyConf) cmpConf, ok := cmp.(*UDPProxyConf)
if !ok { if !ok {
@ -472,7 +659,7 @@ func (cfg *UDPProxyConf) Compare(cmp ProxyConf) bool {
} }
// Add custom logic equal if exists. // Add custom logic equal if exists.
if !reflect.DeepEqual(cfg.UDPProxySpec, cmpConf.UDPProxySpec) { if cfg.RemotePort != cmpConf.RemotePort {
return false return false
} }
@ -480,12 +667,7 @@ func (cfg *UDPProxyConf) Compare(cmp ProxyConf) bool {
} }
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 := section.MapTo(cfg) err := preUnmarshalFromIni(cfg, prefix, name, section)
if err != nil {
return err
}
err = cfg.BaseProxyConf.decorate(prefix, name, section)
if err != nil { if err != nil {
return err return err
} }
@ -524,8 +706,6 @@ func (cfg *UDPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
} }
// HTTP // HTTP
var _ ProxyConf = &HTTPProxyConf{}
func (cfg *HTTPProxyConf) Compare(cmp ProxyConf) bool { func (cfg *HTTPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*HTTPProxyConf) cmpConf, ok := cmp.(*HTTPProxyConf)
if !ok { if !ok {
@ -537,7 +717,15 @@ func (cfg *HTTPProxyConf) Compare(cmp ProxyConf) bool {
} }
// Add custom logic equal if exists. // Add custom logic equal if exists.
if !reflect.DeepEqual(cfg.HTTPProxySpec, cmpConf.HTTPProxySpec) { 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 ||
!reflect.DeepEqual(cfg.Headers, cmpConf.Headers) {
return false return false
} }
@ -545,12 +733,7 @@ func (cfg *HTTPProxyConf) Compare(cmp ProxyConf) bool {
} }
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 := section.MapTo(cfg) err := preUnmarshalFromIni(cfg, prefix, name, section)
if err != nil {
return err
}
err = cfg.BaseProxyConf.decorate(prefix, name, section)
if err != nil { if err != nil {
return err return err
} }
@ -614,8 +797,6 @@ func (cfg *HTTPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
} }
// HTTPS // HTTPS
var _ ProxyConf = &HTTPSProxyConf{}
func (cfg *HTTPSProxyConf) Compare(cmp ProxyConf) bool { func (cfg *HTTPSProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*HTTPSProxyConf) cmpConf, ok := cmp.(*HTTPSProxyConf)
if !ok { if !ok {
@ -627,7 +808,7 @@ func (cfg *HTTPSProxyConf) Compare(cmp ProxyConf) bool {
} }
// Add custom logic equal if exists. // Add custom logic equal if exists.
if !reflect.DeepEqual(cfg.HTTPSProxySpec, cmpConf.HTTPSProxySpec) { if !reflect.DeepEqual(cfg.DomainConf, cmpConf.DomainConf) {
return false return false
} }
@ -635,12 +816,7 @@ func (cfg *HTTPSProxyConf) Compare(cmp ProxyConf) bool {
} }
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 := section.MapTo(cfg) err := preUnmarshalFromIni(cfg, prefix, name, section)
if err != nil {
return err
}
err = cfg.BaseProxyConf.decorate(prefix, name, section)
if err != nil { if err != nil {
return err return err
} }
@ -693,8 +869,6 @@ func (cfg *HTTPSProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
} }
// SUDP // SUDP
var _ ProxyConf = &SUDPProxyConf{}
func (cfg *SUDPProxyConf) Compare(cmp ProxyConf) bool { func (cfg *SUDPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*SUDPProxyConf) cmpConf, ok := cmp.(*SUDPProxyConf)
if !ok { if !ok {
@ -706,7 +880,8 @@ func (cfg *SUDPProxyConf) Compare(cmp ProxyConf) bool {
} }
// Add custom logic equal if exists. // Add custom logic equal if exists.
if !reflect.DeepEqual(cfg.SUDPProxySpec, cmpConf.SUDPProxySpec) { if cfg.Role != cmpConf.Role ||
cfg.Sk != cmpConf.Sk {
return false return false
} }
@ -714,12 +889,7 @@ func (cfg *SUDPProxyConf) Compare(cmp ProxyConf) bool {
} }
func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
err := section.MapTo(cfg) err := preUnmarshalFromIni(cfg, prefix, name, section)
if err != nil {
return err
}
err = cfg.BaseProxyConf.decorate(prefix, name, section)
if err != nil { if err != nil {
return err return err
} }
@ -762,8 +932,6 @@ func (cfg *SUDPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
} }
// STCP // STCP
var _ ProxyConf = &STCPProxyConf{}
func (cfg *STCPProxyConf) Compare(cmp ProxyConf) bool { func (cfg *STCPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*STCPProxyConf) cmpConf, ok := cmp.(*STCPProxyConf)
if !ok { if !ok {
@ -775,7 +943,8 @@ func (cfg *STCPProxyConf) Compare(cmp ProxyConf) bool {
} }
// Add custom logic equal if exists. // Add custom logic equal if exists.
if !reflect.DeepEqual(cfg.STCPProxySpec, cmpConf.STCPProxySpec) { if cfg.Role != cmpConf.Role ||
cfg.Sk != cmpConf.Sk {
return false return false
} }
@ -783,12 +952,7 @@ func (cfg *STCPProxyConf) Compare(cmp ProxyConf) bool {
} }
func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
err := section.MapTo(cfg) err := preUnmarshalFromIni(cfg, prefix, name, section)
if err != nil {
return err
}
err = cfg.BaseProxyConf.decorate(prefix, name, section)
if err != nil { if err != nil {
return err return err
} }
@ -834,8 +998,6 @@ func (cfg *STCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
} }
// XTCP // XTCP
var _ ProxyConf = &XTCPProxyConf{}
func (cfg *XTCPProxyConf) Compare(cmp ProxyConf) bool { func (cfg *XTCPProxyConf) Compare(cmp ProxyConf) bool {
cmpConf, ok := cmp.(*XTCPProxyConf) cmpConf, ok := cmp.(*XTCPProxyConf)
if !ok { if !ok {
@ -847,7 +1009,8 @@ func (cfg *XTCPProxyConf) Compare(cmp ProxyConf) bool {
} }
// Add custom logic equal if exists. // Add custom logic equal if exists.
if !reflect.DeepEqual(cfg.XTCPProxySpec, cmpConf.XTCPProxySpec) { if cfg.Role != cmpConf.Role ||
cfg.Sk != cmpConf.Sk {
return false return false
} }
@ -855,12 +1018,7 @@ func (cfg *XTCPProxyConf) Compare(cmp ProxyConf) bool {
} }
func (cfg *XTCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *XTCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
err := section.MapTo(cfg) err := preUnmarshalFromIni(cfg, prefix, name, section)
if err != nil {
return err
}
err = cfg.BaseProxyConf.decorate(prefix, name, section)
if err != nil { if err != nil {
return err return err
} }

View File

@ -35,6 +35,12 @@ var (
testProxyPrefix = "test." testProxyPrefix = "test."
) )
func Test_Proxy_Interface(t *testing.T) {
for name := range proxyConfTypeMap {
NewConfByType(name)
}
}
func Test_Proxy_UnmarshalFromIni(t *testing.T) { func Test_Proxy_UnmarshalFromIni(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
@ -89,11 +95,9 @@ func Test_Proxy_UnmarshalFromIni(t *testing.T) {
HealthCheckAddr: "127.0.0.9:29", HealthCheckAddr: "127.0.0.9:29",
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 6009, RemotePort: 6009,
}, },
}, },
},
{ {
sname: "ssh_random", sname: "ssh_random",
source: []byte(` source: []byte(`
@ -112,11 +116,9 @@ func Test_Proxy_UnmarshalFromIni(t *testing.T) {
LocalPort: 29, LocalPort: 29,
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 9, RemotePort: 9,
}, },
}, },
},
{ {
sname: "dns", sname: "dns",
source: []byte(` source: []byte(`
@ -139,11 +141,9 @@ func Test_Proxy_UnmarshalFromIni(t *testing.T) {
LocalPort: 59, LocalPort: 59,
}, },
}, },
UDPProxySpec: UDPProxySpec{
RemotePort: 6009, RemotePort: 6009,
}, },
}, },
},
{ {
sname: "web01", sname: "web01",
source: []byte(` source: []byte(`
@ -184,7 +184,6 @@ func Test_Proxy_UnmarshalFromIni(t *testing.T) {
HealthCheckURL: "http://127.0.0.9:89/status", HealthCheckURL: "http://127.0.0.9:89/status",
}, },
}, },
HTTPProxySpec: HTTPProxySpec{
DomainConf: DomainConf{ DomainConf: DomainConf{
CustomDomains: []string{"web02.yourdomain.com"}, CustomDomains: []string{"web02.yourdomain.com"},
SubDomain: "web01", SubDomain: "web01",
@ -198,7 +197,6 @@ func Test_Proxy_UnmarshalFromIni(t *testing.T) {
}, },
}, },
}, },
},
{ {
sname: "web02", sname: "web02",
source: []byte(` source: []byte(`
@ -224,14 +222,12 @@ func Test_Proxy_UnmarshalFromIni(t *testing.T) {
}, },
ProxyProtocolVersion: "v2", ProxyProtocolVersion: "v2",
}, },
HTTPSProxySpec: HTTPSProxySpec{
DomainConf: DomainConf{ DomainConf: DomainConf{
CustomDomains: []string{"web02.yourdomain.com"}, CustomDomains: []string{"web02.yourdomain.com"},
SubDomain: "web01", SubDomain: "web01",
}, },
}, },
}, },
},
{ {
sname: "secret_tcp", sname: "secret_tcp",
source: []byte(` source: []byte(`
@ -252,12 +248,10 @@ func Test_Proxy_UnmarshalFromIni(t *testing.T) {
LocalPort: 22, LocalPort: 22,
}, },
}, },
STCPProxySpec: STCPProxySpec{
Role: "server", Role: "server",
Sk: "abcdefg", Sk: "abcdefg",
}, },
}, },
},
{ {
sname: "p2p_tcp", sname: "p2p_tcp",
source: []byte(` source: []byte(`
@ -278,12 +272,10 @@ func Test_Proxy_UnmarshalFromIni(t *testing.T) {
LocalPort: 22, LocalPort: 22,
}, },
}, },
XTCPProxySpec: XTCPProxySpec{
Role: "server", Role: "server",
Sk: "abcdefg", Sk: "abcdefg",
}, },
}, },
},
{ {
sname: "tcpmuxhttpconnect", sname: "tcpmuxhttpconnect",
source: []byte(` source: []byte(`
@ -303,7 +295,6 @@ func Test_Proxy_UnmarshalFromIni(t *testing.T) {
LocalPort: 10701, LocalPort: 10701,
}, },
}, },
TCPMuxProxySpec: TCPMuxProxySpec{
DomainConf: DomainConf{ DomainConf: DomainConf{
CustomDomains: []string{"tunnel1"}, CustomDomains: []string{"tunnel1"},
SubDomain: "", SubDomain: "",
@ -311,7 +302,6 @@ func Test_Proxy_UnmarshalFromIni(t *testing.T) {
Multiplexer: "httpconnect", Multiplexer: "httpconnect",
}, },
}, },
},
} }
for _, c := range testcases { for _, c := range testcases {
@ -359,10 +349,8 @@ func Test_RangeProxy_UnmarshalFromIni(t *testing.T) {
LocalPort: 6010, LocalPort: 6010,
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 6010, RemotePort: 6010,
}, },
},
"tcp_port_1": &TCPProxyConf{ "tcp_port_1": &TCPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testProxyPrefix + "tcp_port_1", ProxyName: testProxyPrefix + "tcp_port_1",
@ -372,10 +360,8 @@ func Test_RangeProxy_UnmarshalFromIni(t *testing.T) {
LocalPort: 6011, LocalPort: 6011,
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 6011, RemotePort: 6011,
}, },
},
"tcp_port_2": &TCPProxyConf{ "tcp_port_2": &TCPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testProxyPrefix + "tcp_port_2", ProxyName: testProxyPrefix + "tcp_port_2",
@ -385,12 +371,10 @@ func Test_RangeProxy_UnmarshalFromIni(t *testing.T) {
LocalPort: 6019, LocalPort: 6019,
}, },
}, },
TCPProxySpec: TCPProxySpec{
RemotePort: 6019, RemotePort: 6019,
}, },
}, },
}, },
},
{ {
sname: "range:udp_port", sname: "range:udp_port",
source: []byte(` source: []byte(`
@ -414,10 +398,8 @@ func Test_RangeProxy_UnmarshalFromIni(t *testing.T) {
LocalPort: 6000, LocalPort: 6000,
}, },
}, },
UDPProxySpec: UDPProxySpec{
RemotePort: 6000, RemotePort: 6000,
}, },
},
"udp_port_1": &UDPProxyConf{ "udp_port_1": &UDPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testProxyPrefix + "udp_port_1", ProxyName: testProxyPrefix + "udp_port_1",
@ -429,10 +411,8 @@ func Test_RangeProxy_UnmarshalFromIni(t *testing.T) {
LocalPort: 6010, LocalPort: 6010,
}, },
}, },
UDPProxySpec: UDPProxySpec{
RemotePort: 6010, RemotePort: 6010,
}, },
},
"udp_port_2": &UDPProxyConf{ "udp_port_2": &UDPProxyConf{
BaseProxyConf: BaseProxyConf{ BaseProxyConf: BaseProxyConf{
ProxyName: testProxyPrefix + "udp_port_2", ProxyName: testProxyPrefix + "udp_port_2",
@ -444,12 +424,10 @@ func Test_RangeProxy_UnmarshalFromIni(t *testing.T) {
LocalPort: 6011, LocalPort: 6011,
}, },
}, },
UDPProxySpec: UDPProxySpec{
RemotePort: 6011, RemotePort: 6011,
}, },
}, },
}, },
},
} }
for _, c := range testcases { for _, c := range testcases {
@ -460,7 +438,7 @@ func Test_RangeProxy_UnmarshalFromIni(t *testing.T) {
actual := make(map[string]ProxyConf) actual := make(map[string]ProxyConf)
s := f.Section(c.sname) s := f.Section(c.sname)
err = appendTemplates(f, s) err = renderRangeProxyTemplates(f, s)
assert.NoError(err) assert.NoError(err)
f.DeleteSection(ini.DefaultSection) f.DeleteSection(ini.DefaultSection)

View File

@ -25,6 +25,146 @@ import (
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
// ServerCommonConf contains information for a server service. It is
// recommended to use GetDefaultServerConf instead of creating this object
// directly, so that all unspecified fields have reasonable default values.
type ServerCommonConf struct {
auth.ServerConfig `ini:",extends" json:"inline"`
// BindAddr specifies the address that the server binds to. By default,
// this value is "0.0.0.0".
BindAddr string `ini:"bind_addr" json:"bind_addr"`
// BindPort specifies the port that the server listens on. By default, this
// value is 7000.
BindPort int `ini:"bind_port" json:"bind_port"`
// BindUDPPort specifies the UDP port that the server listens on. If this
// value is 0, the server will not listen for UDP connections. By default,
// this value is 0
BindUDPPort int `ini:"bind_udp_port" json:"bind_udp_port"`
// KCPBindPort specifies the KCP port that the server listens on. If this
// value is 0, the server will not listen for KCP connections. By default,
// this value is 0.
KCPBindPort int `ini:"kcp_bind_port" json:"kcp_bind_port"`
// ProxyBindAddr specifies the address that the proxy binds to. This value
// may be the same as BindAddr. By default, this value is "0.0.0.0".
ProxyBindAddr string `ini:"proxy_bind_addr" json:"proxy_bind_addr"`
// VhostHTTPPort specifies the port that the server listens for HTTP Vhost
// requests. If this value is 0, the server will not listen for HTTP
// requests. By default, this value is 0.
VhostHTTPPort int `ini:"vhost_http_port" json:"vhost_http_port"`
// VhostHTTPSPort specifies the port that the server listens for HTTPS
// Vhost requests. If this value is 0, the server will not listen for HTTPS
// requests. By default, this value is 0.
VhostHTTPSPort int `ini:"vhost_https_port" json:"vhost_https_port"`
// TCPMuxHTTPConnectPort specifies the port that the server listens for TCP
// HTTP CONNECT requests. If the value is 0, the server will not multiplex TCP
// requests on one single port. If it's not - it will listen on this value for
// HTTP CONNECT requests. By default, this value is 0.
TCPMuxHTTPConnectPort int `ini:"tcpmux_httpconnect_port" json:"tcpmux_httpconnect_port"`
// VhostHTTPTimeout specifies the response header timeout for the Vhost
// HTTP server, in seconds. By default, this value is 60.
VhostHTTPTimeout int64 `ini:"vhost_http_timeout" json:"vhost_http_timeout"`
// DashboardAddr specifies the address that the dashboard binds to. By
// default, this value is "0.0.0.0".
DashboardAddr string `ini:"dashboard_addr" json:"dashboard_addr"`
// DashboardPort specifies the port that the dashboard listens on. If this
// value is 0, the dashboard will not be started. By default, this value is
// 0.
DashboardPort int `ini:"dashboard_port" json:"dashboard_port"`
// DashboardUser specifies the username that the dashboard will use for
// login. By default, this value is "admin".
DashboardUser string `ini:"dashboard_user" json:"dashboard_user"`
// DashboardUser specifies the password that the dashboard will use for
// login. By default, this value is "admin".
DashboardPwd string `ini:"dashboard_pwd" json:"dashboard_pwd"`
// EnablePrometheus will export prometheus metrics on {dashboard_addr}:{dashboard_port}
// in /metrics api.
EnablePrometheus bool `ini:"enable_prometheus" json:"enable_prometheus"`
// AssetsDir specifies the local directory that the dashboard will load
// resources from. If this value is "", assets will be loaded from the
// bundled executable using statik. By default, this value is "".
AssetsDir string `ini:"assets_dir" json:"assets_dir"`
// LogFile specifies a file where logs will be written to. This value will
// only be used if LogWay is set appropriately. By default, this value is
// "console".
LogFile string `ini:"log_file" json:"log_file"`
// LogWay specifies the way logging is managed. Valid values are "console"
// or "file". If "console" is used, logs will be printed to stdout. If
// "file" is used, logs will be printed to LogFile. By default, this value
// is "console".
LogWay string `ini:"log_way" json:"log_way"`
// LogLevel specifies the minimum log level. Valid values are "trace",
// "debug", "info", "warn", and "error". By default, this value is "info".
LogLevel string `ini:"log_level" json:"log_level"`
// LogMaxDays specifies the maximum number of days to store log information
// before deletion. This is only used if LogWay == "file". By default, this
// value is 0.
LogMaxDays int64 `ini:"log_max_days" json:"log_max_days"`
// DisableLogColor disables log colors when LogWay == "console" when set to
// true. By default, this value is false.
DisableLogColor bool `ini:"disable_log_color" json:"disable_log_color"`
// DetailedErrorsToClient defines whether to send the specific error (with
// debug info) to frpc. By default, this value is true.
DetailedErrorsToClient bool `ini:"detailed_errors_to_client" json:"detailed_errors_to_client"`
// SubDomainHost specifies the domain that will be attached to sub-domains
// requested by the client when using Vhost proxying. For example, if this
// value is set to "frps.com" and the client requested the subdomain
// "test", the resulting URL would be "test.frps.com". By default, this
// value is "".
SubDomainHost string `ini:"subdomain_host" json:"subdomain_host"`
// TCPMux toggles TCP stream multiplexing. This allows multiple requests
// from a client to share a single TCP connection. By default, this value
// is true.
TCPMux bool `ini:"tcp_mux" json:"tcp_mux"`
// Custom404Page specifies a path to a custom 404 page to display. If this
// value is "", a default page will be displayed. By default, this value is
// "".
Custom404Page string `ini:"custom_404_page" json:"custom_404_page"`
// AllowPorts specifies a set of ports that clients are able to proxy to.
// If the length of this value is 0, all ports are allowed. By default,
// this value is an empty set.
AllowPorts map[int]struct{} `ini:"-" json:"-"`
// MaxPoolCount specifies the maximum pool size for each proxy. By default,
// this value is 5.
MaxPoolCount int64 `ini:"max_pool_count" json:"max_pool_count"`
// MaxPortsPerClient specifies the maximum number of ports a single client
// may proxy to. If this value is 0, no limit will be applied. By default,
// this value is 0.
MaxPortsPerClient int64 `ini:"max_ports_per_client" json:"max_ports_per_client"`
// TLSOnly specifies whether to only accept TLS-encrypted connections.
// By default, the value is false.
TLSOnly bool `ini:"tls_only" json:"tls_only"`
// TLSCertFile specifies the path of the cert file that the server will
// load. If "tls_cert_file", "tls_key_file" are valid, the server will use this
// supplied tls configuration. Otherwise, the server will use the tls
// configuration generated by itself.
TLSCertFile string `ini:"tls_cert_file" json:"tls_cert_file"`
// TLSKeyFile specifies the path of the secret key that the server will
// load. If "tls_cert_file", "tls_key_file" are valid, the server will use this
// supplied tls configuration. Otherwise, the server will use the tls
// configuration generated by itself.
TLSKeyFile string `ini:"tls_key_file" json:"tls_key_file"`
// TLSTrustedCaFile specifies the paths of the client cert files that the
// server will load. It only works when "tls_only" is true. If
// "tls_trusted_ca_file" is valid, the server will verify each client's
// certificate.
TLSTrustedCaFile string `ini:"tls_trusted_ca_file" json:"tls_trusted_ca_file"`
// HeartBeatTimeout specifies the maximum time to wait for a heartbeat
// before terminating the connection. It is not recommended to change this
// value. By default, this value is 90.
HeartbeatTimeout int64 `ini:"heartbeat_timeout" json:"heartbeat_timeout"`
// UserConnTimeout specifies the maximum time to wait for a work
// connection. By default, this value is 10.
UserConnTimeout int64 `ini:"user_conn_timeout" json:"user_conn_timeout"`
// HTTPPlugins specify the server plugins support HTTP protocol.
HTTPPlugins map[string]plugin.HTTPPluginOptions `ini:"-" json:"http_plugins"`
// UDPPacketSize specifies the UDP packet size
// By default, this value is 1500
UDPPacketSize int64 `ini:"udp_packet_size" json:"udp_packet_size"`
}
// GetDefaultServerConf returns a server configuration with reasonable // GetDefaultServerConf returns a server configuration with reasonable
// defaults. // defaults.
func GetDefaultServerConf() ServerCommonConf { func GetDefaultServerConf() ServerCommonConf {

View File

@ -1,4 +1,4 @@
// Copyright 2020 The frp Authors // Copyright 2019 fatedier, fatedier@gmail.com
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -15,508 +15,107 @@
package config package config
import ( import (
"reflect" "encoding/json"
"errors"
"github.com/fatedier/frp/pkg/auth" "strconv"
"github.com/fatedier/frp/pkg/consts" "strings"
"github.com/fatedier/frp/pkg/msg"
plugin "github.com/fatedier/frp/pkg/plugin/server"
"gopkg.in/ini.v1"
) )
// ClientCommonConf contains information for a client service. It is const (
// recommended to use GetDefaultClientConf instead of creating this object MB = 1024 * 1024
// directly, so that all unspecified fields have reasonable default values. KB = 1024
type ClientCommonConf struct { )
auth.ClientConfig `ini:",,,,extends",json:"inline"`
// ServerAddr specifies the address of the server to connect to. By type BandwidthQuantity struct {
// default, this value is "0.0.0.0". s string // MB or KB
ServerAddr string `ini:"server_addr",josn:"server_addr"`
// ServerPort specifies the port to connect to the server on. By default, i int64 // bytes
// this value is 7000.
ServerPort int `ini:"server_port",json:"server_port"`
// HTTPProxy specifies a proxy address to connect to the server through. If
// this value is "", the server will be connected to directly. By default,
// this value is read from the "http_proxy" environment variable.
HTTPProxy string `ini:"http_proxy",json:"http_proxy"`
// LogFile specifies a file where logs will be written to. This value will
// only be used if LogWay is set appropriately. By default, this value is
// "console".
LogFile string `ini:"log_file",json:"log_file"`
// LogWay specifies the way logging is managed. Valid values are "console"
// or "file". If "console" is used, logs will be printed to stdout. If
// "file" is used, logs will be printed to LogFile. By default, this value
// is "console".
LogWay string `ini:"log_way",json:"log_way"`
// LogLevel specifies the minimum log level. Valid values are "trace",
// "debug", "info", "warn", and "error". By default, this value is "info".
LogLevel string `ini:"log_level",json:"log_level"`
// LogMaxDays specifies the maximum number of days to store log information
// before deletion. This is only used if LogWay == "file". By default, this
// value is 0.
LogMaxDays int64 `ini:"log_max_days",json:"log_max_days"`
// DisableLogColor disables log colors when LogWay == "console" when set to
// true. By default, this value is false.
DisableLogColor bool `ini:"disable_log_color",json:"disable_log_color"`
// AdminAddr specifies the address that the admin server binds to. By
// default, this value is "127.0.0.1".
AdminAddr string `ini:"admin_addr",json:"admin_addr"`
// AdminPort specifies the port for the admin server to listen on. If this
// value is 0, the admin server will not be started. By default, this value
// is 0.
AdminPort int `ini:"admin_port",json:"admin_port"`
// AdminUser specifies the username that the admin server will use for
// login. By default, this value is "admin".
AdminUser string `ini:"admin_user",json:"admin_user"`
// AdminPwd specifies the password that the admin server will use for
// login. By default, this value is "admin".
AdminPwd string `ini:"admin_pwd",json:"admin_pwd"`
// AssetsDir specifies the local directory that the admin server will load
// resources from. If this value is "", assets will be loaded from the
// bundled executable using statik. By default, this value is "".
AssetsDir string `ini:"assets_dir",json:"assets_dir"`
// PoolCount specifies the number of connections the client will make to
// the server in advance. By default, this value is 0.
PoolCount int `ini:"pool_count",json:"pool_count"`
// TCPMux toggles TCP stream multiplexing. This allows multiple requests
// from a client to share a single TCP connection. If this value is true,
// the server must have TCP multiplexing enabled as well. By default, this
// value is true.
TCPMux bool `ini:"tcp_mux",json:"tcp_mux"`
// User specifies a prefix for proxy names to distinguish them from other
// clients. If this value is not "", proxy names will automatically be
// changed to "{user}.{proxy_name}". By default, this value is "".
User string `ini:"user",json:"user"`
// DNSServer specifies a DNS server address for FRPC to use. If this value
// is "", the default DNS will be used. By default, this value is "".
DNSServer string `ini:"dns_server",json:"dns_server"`
// LoginFailExit controls whether or not the client should exit after a
// failed login attempt. If false, the client will retry until a login
// attempt succeeds. By default, this value is true.
LoginFailExit bool `ini:"login_fail_exit",json:"login_fail_exit"`
// Start specifies a set of enabled proxies by name. If this set is empty,
// all supplied proxies are enabled. By default, this value is an empty
// set.
Start []string `ini:"start",json:"start"`
//Start map[string]struct{} `json:"start"`
// Protocol specifies the protocol to use when interacting with the server.
// Valid values are "tcp", "kcp" and "websocket". By default, this value
// is "tcp".
Protocol string `ini:"protocol",json:"protocol"`
// TLSEnable specifies whether or not TLS should be used when communicating
// with the server. If "tls_cert_file" and "tls_key_file" are valid,
// client will load the supplied tls configuration.
TLSEnable bool `ini:"tls_enable",json:"tls_enable"`
// ClientTLSCertPath specifies the path of the cert file that client will
// load. It only works when "tls_enable" is true and "tls_key_file" is valid.
TLSCertFile string `ini:"tls_cert_file",json:"tls_cert_file"`
// ClientTLSKeyPath specifies the path of the secret key file that client
// will load. It only works when "tls_enable" is true and "tls_cert_file"
// are valid.
TLSKeyFile string `ini:"tls_key_file",json:"tls_key_file"`
// TrustedCaFile specifies the path of the trusted ca file that will load.
// It only works when "tls_enable" is valid and tls configuration of server
// has been specified.
TLSTrustedCaFile string `ini:"tls_trusted_ca_file",json:"tls_trusted_ca_file"`
// HeartBeatInterval specifies at what interval heartbeats are sent to the
// server, in seconds. It is not recommended to change this value. By
// default, this value is 30.
HeartbeatInterval int64 `ini:"heartbeat_interval",json:"heartbeat_interval"`
// HeartBeatTimeout specifies the maximum allowed heartbeat response delay
// before the connection is terminated, in seconds. It is not recommended
// to change this value. By default, this value is 90.
HeartbeatTimeout int64 `ini:"heartbeat_timeout",json:"heartbeat_timeout"`
// Client meta info
Metas map[string]string `ini:"-",json:"metas"`
// UDPPacketSize specifies the udp packet size
// By default, this value is 1500
UDPPacketSize int64 `ini:"udp_packet_size",json:"udp_packet_size"`
} }
// ServerCommonConf contains information for a server service. It is func NewBandwidthQuantity(s string) (BandwidthQuantity, error) {
// recommended to use GetDefaultServerConf instead of creating this object q := BandwidthQuantity{}
// directly, so that all unspecified fields have reasonable default values. err := q.UnmarshalString(s)
type ServerCommonConf struct { if err != nil {
auth.ServerConfig `ini:",,,,extends",json:"inline"` return q, err
// BindAddr specifies the address that the server binds to. By default, }
// this value is "0.0.0.0". return q, nil
BindAddr string `ini:"bind_addr",json:"bind_addr"` }
// BindPort specifies the port that the server listens on. By default, this
// value is 7000. func MustBandwidthQuantity(s string) BandwidthQuantity {
BindPort int `ini:"bind_port",json:"bind_port"` q := BandwidthQuantity{}
// BindUDPPort specifies the UDP port that the server listens on. If this err := q.UnmarshalString(s)
// value is 0, the server will not listen for UDP connections. By default, if err != nil {
// this value is 0 panic(err)
BindUDPPort int `ini:"bind_udp_port",json:"bind_udp_port"` }
// KCPBindPort specifies the KCP port that the server listens on. If this return q
// value is 0, the server will not listen for KCP connections. By default, }
// this value is 0.
KCPBindPort int `ini:"kcp_bind_port",json:"kcp_bind_port"` func (q *BandwidthQuantity) Equal(u *BandwidthQuantity) bool {
// ProxyBindAddr specifies the address that the proxy binds to. This value if q == nil && u == nil {
// may be the same as BindAddr. By default, this value is "0.0.0.0". return true
ProxyBindAddr string `ini:"proxy_bind_addr",json:"proxy_bind_addr"` }
// VhostHTTPPort specifies the port that the server listens for HTTP Vhost if q != nil && u != nil {
// requests. If this value is 0, the server will not listen for HTTP return q.i == u.i
// requests. By default, this value is 0. }
VhostHTTPPort int `ini:"vhost_http_port",json:"vhost_http_port"` return false
// VhostHTTPSPort specifies the port that the server listens for HTTPS }
// Vhost requests. If this value is 0, the server will not listen for HTTPS
// requests. By default, this value is 0. func (q *BandwidthQuantity) String() string {
VhostHTTPSPort int `ini:"vhost_https_port",json:"vhost_https_port"` return q.s
// TCPMuxHTTPConnectPort specifies the port that the server listens for TCP }
// HTTP CONNECT requests. If the value is 0, the server will not multiplex TCP
// requests on one single port. If it's not - it will listen on this value for func (q *BandwidthQuantity) UnmarshalString(s string) error {
// HTTP CONNECT requests. By default, this value is 0. s = strings.TrimSpace(s)
TCPMuxHTTPConnectPort int `ini:"tcpmux_httpconnect_port",json:"tcpmux_httpconnect_port"` if s == "" {
// VhostHTTPTimeout specifies the response header timeout for the Vhost return nil
// HTTP server, in seconds. By default, this value is 60.
VhostHTTPTimeout int64 `ini:"vhost_http_timeout",json:"vhost_http_timeout"`
// DashboardAddr specifies the address that the dashboard binds to. By
// default, this value is "0.0.0.0".
DashboardAddr string `ini:"dashboard_addr",json:"dashboard_addr"`
// DashboardPort specifies the port that the dashboard listens on. If this
// value is 0, the dashboard will not be started. By default, this value is
// 0.
DashboardPort int `ini:"dashboard_port",json:"dashboard_port"`
// DashboardUser specifies the username that the dashboard will use for
// login. By default, this value is "admin".
DashboardUser string `ini:"dashboard_user",json:"dashboard_user"`
// DashboardUser specifies the password that the dashboard will use for
// login. By default, this value is "admin".
DashboardPwd string `ini:"dashboard_pwd",json:"dashboard_pwd"`
// EnablePrometheus will export prometheus metrics on {dashboard_addr}:{dashboard_port}
// in /metrics api.
EnablePrometheus bool `ini:"enable_prometheus",json:"enable_prometheus"`
// AssetsDir specifies the local directory that the dashboard will load
// resources from. If this value is "", assets will be loaded from the
// bundled executable using statik. By default, this value is "".
AssetsDir string `ini:"assets_dir",json:"assets_dir"`
// LogFile specifies a file where logs will be written to. This value will
// only be used if LogWay is set appropriately. By default, this value is
// "console".
LogFile string `ini:"log_file",json:"log_file"`
// LogWay specifies the way logging is managed. Valid values are "console"
// or "file". If "console" is used, logs will be printed to stdout. If
// "file" is used, logs will be printed to LogFile. By default, this value
// is "console".
LogWay string `ini:"log_way",json:"log_way"`
// LogLevel specifies the minimum log level. Valid values are "trace",
// "debug", "info", "warn", and "error". By default, this value is "info".
LogLevel string `ini:"log_level",json:"log_level"`
// LogMaxDays specifies the maximum number of days to store log information
// before deletion. This is only used if LogWay == "file". By default, this
// value is 0.
LogMaxDays int64 `ini:"log_max_days",json:"log_max_days"`
// DisableLogColor disables log colors when LogWay == "console" when set to
// true. By default, this value is false.
DisableLogColor bool `ini:"disable_log_color",json:"disable_log_color"`
// DetailedErrorsToClient defines whether to send the specific error (with
// debug info) to frpc. By default, this value is true.
DetailedErrorsToClient bool `ini:"detailed_errors_to_client",json:"detailed_errors_to_client"`
// SubDomainHost specifies the domain that will be attached to sub-domains
// requested by the client when using Vhost proxying. For example, if this
// value is set to "frps.com" and the client requested the subdomain
// "test", the resulting URL would be "test.frps.com". By default, this
// value is "".
SubDomainHost string `ini:"subdomain_host",json:"subdomain_host"`
// TCPMux toggles TCP stream multiplexing. This allows multiple requests
// from a client to share a single TCP connection. By default, this value
// is true.
TCPMux bool `ini:"tcp_mux",json:"tcp_mux"`
// Custom404Page specifies a path to a custom 404 page to display. If this
// value is "", a default page will be displayed. By default, this value is
// "".
Custom404Page string `ini:"custom_404_page",json:"custom_404_page"`
// AllowPorts specifies a set of ports that clients are able to proxy to.
// If the length of this value is 0, all ports are allowed. By default,
// this value is an empty set.
AllowPorts map[int]struct{} `ini:"-",json:"-"`
// MaxPoolCount specifies the maximum pool size for each proxy. By default,
// this value is 5.
MaxPoolCount int64 `ini:"max_pool_count",json:"max_pool_count"`
// MaxPortsPerClient specifies the maximum number of ports a single client
// may proxy to. If this value is 0, no limit will be applied. By default,
// this value is 0.
MaxPortsPerClient int64 `ini:"max_ports_per_client",json:"max_ports_per_client"`
// TLSOnly specifies whether to only accept TLS-encrypted connections.
// By default, the value is false.
TLSOnly bool `ini:"tls_only",json:"tls_only"`
// TLSCertFile specifies the path of the cert file that the server will
// load. If "tls_cert_file", "tls_key_file" are valid, the server will use this
// supplied tls configuration. Otherwise, the server will use the tls
// configuration generated by itself.
TLSCertFile string `ini:"tls_cert_file",json:"tls_cert_file"`
// TLSKeyFile specifies the path of the secret key that the server will
// load. If "tls_cert_file", "tls_key_file" are valid, the server will use this
// supplied tls configuration. Otherwise, the server will use the tls
// configuration generated by itself.
TLSKeyFile string `ini:"tls_key_file",json:"tls_key_file"`
// TLSTrustedCaFile specifies the paths of the client cert files that the
// server will load. It only works when "tls_only" is true. If
// "tls_trusted_ca_file" is valid, the server will verify each client's
// certificate.
TLSTrustedCaFile string `ini:"tls_trusted_ca_file",json:"tls_trusted_ca_file"`
// HeartBeatTimeout specifies the maximum time to wait for a heartbeat
// before terminating the connection. It is not recommended to change this
// value. By default, this value is 90.
HeartbeatTimeout int64 `ini:"heartbeat_timeout",json:"heartbeat_timeout"`
// UserConnTimeout specifies the maximum time to wait for a work
// connection. By default, this value is 10.
UserConnTimeout int64 `ini:"user_conn_timeout",json:"user_conn_timeout"`
// HTTPPlugins specify the server plugins support HTTP protocol.
HTTPPlugins map[string]plugin.HTTPPluginOptions `ini:"-",json:"http_plugins"`
// UDPPacketSize specifies the UDP packet size
// By default, this value is 1500
UDPPacketSize int64 `ini:"udp_packet_size",json:"udp_packet_size"`
} }
// Proxy
var ( var (
ProxyConfTypeMap = map[string]reflect.Type{ base int64
consts.TCPProxy: reflect.TypeOf(TCPProxyConf{}), f float64
consts.TCPMuxProxy: reflect.TypeOf(TCPMuxProxyConf{}), err error
consts.UDPProxy: reflect.TypeOf(UDPProxyConf{}),
consts.HTTPProxy: reflect.TypeOf(HTTPProxyConf{}),
consts.HTTPSProxy: reflect.TypeOf(HTTPSProxyConf{}),
consts.STCPProxy: reflect.TypeOf(STCPProxyConf{}),
consts.XTCPProxy: reflect.TypeOf(XTCPProxyConf{}),
consts.SUDPProxy: reflect.TypeOf(SUDPProxyConf{}),
}
) )
if strings.HasSuffix(s, "MB") {
type ProxyConf interface { base = MB
GetBaseInfo() *BaseProxyConf fstr := strings.TrimSuffix(s, "MB")
UnmarshalFromMsg(*msg.NewProxy) f, err = strconv.ParseFloat(fstr, 64)
UnmarshalFromIni(string, string, *ini.Section) error if err != nil {
MarshalToMsg(*msg.NewProxy) return err
CheckForCli() error }
CheckForSvr(ServerCommonConf) error } else if strings.HasSuffix(s, "KB") {
Compare(ProxyConf) bool base = KB
fstr := strings.TrimSuffix(s, "KB")
f, err = strconv.ParseFloat(fstr, 64)
if err != nil {
return err
}
} else {
return errors.New("unit not support")
} }
// LocalSvrConf configures what location the client will to, or what q.s = s
// plugin will be used. q.i = int64(f * float64(base))
type LocalSvrConf struct { return nil
// LocalIP specifies the IP address or host name to to.
LocalIP string `ini:"local_ip",json:"local_ip"`
// LocalPort specifies the port to to.
LocalPort int `ini:"local_port",json:"local_port"`
// Plugin specifies what plugin should be used for ng. If this value
// is set, the LocalIp and LocalPort values will be ignored. By default,
// this value is "".
Plugin string `ini:"plugin",json:"plugin"`
// PluginParams specify parameters to be passed to the plugin, if one is
// being used. By default, this value is an empty map.
PluginParams map[string]string `ini:"-"`
} }
// HealthCheckConf configures health checking. This can be useful for load func (q *BandwidthQuantity) UnmarshalJSON(b []byte) error {
// balancing purposes to detect and remove proxies to failing services. if len(b) == 4 && string(b) == "null" {
type HealthCheckConf struct { return nil
// HealthCheckType specifies what protocol to use for health checking.
// Valid values include "tcp", "http", and "". If this value is "", health
// checking will not be performed. By default, this value is "".
//
// If the type is "tcp", a connection will be attempted to the target
// server. If a connection cannot be established, the health check fails.
//
// If the type is "http", a GET request will be made to the endpoint
// specified by HealthCheckURL. If the response is not a 200, the health
// check fails.
HealthCheckType string `ini:"health_check_type",json:"health_check_type"` // tcp | http
// HealthCheckTimeoutS specifies the number of seconds to wait for a health
// check attempt to connect. If the timeout is reached, this counts as a
// health check failure. By default, this value is 3.
HealthCheckTimeoutS int `ini:"health_check_timeout_s",json:"health_check_timeout_s"`
// HealthCheckMaxFailed specifies the number of allowed failures before the
// is stopped. By default, this value is 1.
HealthCheckMaxFailed int `ini:"health_check_max_failed",json:"health_check_max_failed"`
// HealthCheckIntervalS specifies the time in seconds between health
// checks. By default, this value is 10.
HealthCheckIntervalS int `ini:"health_check_interval_s",json:"health_check_interval_s"`
// HealthCheckURL specifies the address to send health checks to if the
// health check type is "http".
HealthCheckURL string `ini:"health_check_url",json:"health_check_interval_s"`
// HealthCheckAddr specifies the address to connect to if the health check
// type is "tcp".
HealthCheckAddr string `ini:"-"`
} }
// BaseProxyConf provides configuration info that is common to all types. var str string
type BaseProxyConf struct { err := json.Unmarshal(b, &str)
// ProxyName is the name of this if err != nil {
ProxyName string `ini:"name",json:"name"` return err
// ProxyType specifies the type of this Valid values include "tcp",
// "udp", "http", "https", "stcp", and "xtcp". By default, this value is
// "tcp".
ProxyType string `ini:"type",json:"type"`
// UseEncryption controls whether or not communication with the server will
// be encrypted. Encryption is done using the tokens supplied in the server
// and client configuration. By default, this value is false.
UseEncryption bool `ini:"use_encryption",json:"use_encryption"`
// UseCompression controls whether or not communication with the server
// will be compressed. By default, this value is false.
UseCompression bool `ini:"use_compression",json:"use_compression"`
// Group specifies which group the is a part of. The server will use
// this information to load balance proxies in the same group. If the value
// is "", this will not be in a group. By default, this value is "".
Group string `ini:"group",json:"group"`
// GroupKey specifies a group key, which should be the same among proxies
// of the same group. By default, this value is "".
GroupKey string `ini:"group_key",json:"group_key"`
// ProxyProtocolVersion specifies which protocol version to use. Valid
// values include "v1", "v2", and "". If the value is "", a protocol
// version will be automatically selected. By default, this value is "".
ProxyProtocolVersion string `ini:"proxy_protocol_version",json:"proxy_protocol_version"`
// BandwidthLimit limit the bandwidth
// 0 means no limit
BandwidthLimit BandwidthQuantity `ini:"bandwidth_limit",json:"bandwidth_limit"`
// meta info for each proxy
Metas map[string]string `ini:"-",json:"metas"`
// TODO: LocalSvrConf => LocalAppConf
LocalSvrConf `ini:",,,,extends",json:"inline"`
HealthCheckConf `ini:",,,,extends",json:"inline"`
} }
type DomainConf struct { return q.UnmarshalString(str)
CustomDomains []string `ini:"custom_domains",json:"custom_domains"`
SubDomain string `ini:"subdomain",json:"subdomain"`
} }
// HTTP func (q *BandwidthQuantity) MarshalJSON() ([]byte, error) {
type HTTPProxyConf struct { return []byte("\"" + q.s + "\""), nil
BaseProxyConf `ini:",,,,extends",json:"inline"`
HTTPProxySpec `ini:",,,,extends",json:"inline"`
} }
type HTTPProxySpec struct { func (q *BandwidthQuantity) Bytes() int64 {
DomainConf `ini:",,,,extends",json:"inline"` return q.i
Locations []string `ini:"locations",json:"locations"`
HTTPUser string `ini:"http_user",json:"http_user"`
HTTPPwd string `ini:"http_pwd",json:"http_pwd"`
HostHeaderRewrite string `ini:"host_header_rewrite",json:"host_header_rewrite"`
Headers map[string]string `ini:"-",json:"headers"`
}
// HTTPS
type HTTPSProxyConf struct {
BaseProxyConf `ini:",,,,extends",json:"inline"`
HTTPSProxySpec `ini:",,,,extends",json:"inline"`
}
type HTTPSProxySpec struct {
DomainConf `ini:",,,,extends",json:"inline"`
}
// TCP
type TCPProxyConf struct {
BaseProxyConf `ini:",,,,extends",json:"inline"`
TCPProxySpec `ini:",,,,extends",json:"inline"`
}
type TCPProxySpec struct {
RemotePort int `ini:"remote_port",json:"remote_port"`
}
// TCPMux
type TCPMuxProxyConf struct {
BaseProxyConf `ini:",,,,extends",json:"inline"`
TCPMuxProxySpec `ini:",,,,extends",json:"inline"`
}
type TCPMuxProxySpec struct {
DomainConf `ini:",,,,extends",json:"inline"`
Multiplexer string `ini:"multiplexer"`
}
// STCP
type STCPProxyConf struct {
BaseProxyConf `ini:",,,,extends",json:"inline"`
STCPProxySpec `ini:",,,,extends",json:"inline"`
}
type STCPProxySpec struct {
Role string `ini:"role",json:"role"`
Sk string `ini:"sk",json:"sk"`
}
// XTCP
type XTCPProxyConf struct {
BaseProxyConf `ini:",,,,extends",json:"inline"`
XTCPProxySpec `ini:",,,,extends",json:"inline"`
}
type XTCPProxySpec struct {
Role string `ini:"role",json:"role"`
Sk string `ini:"sk",json:"sk"`
}
// UDP
type UDPProxyConf struct {
BaseProxyConf `ini:",,,,extends",json:"inline"`
UDPProxySpec `ini:",,,,extends",json:"inline"`
}
type UDPProxySpec struct {
RemotePort int `ini:"remote_port",json:"remote_port"`
}
// SUDP
type SUDPProxyConf struct {
BaseProxyConf `ini:",,,,extends",json:"inline"`
SUDPProxySpec `ini:",,,,extends",json:"inline"`
}
type SUDPProxySpec struct {
Role string `ini:"role",json:"role"`
Sk string `ini:"sk",json:"sk"`
}
// Visitor
var (
VisitorConfTypeMap = map[string]reflect.Type{
consts.STCPProxy: reflect.TypeOf(STCPVisitorConf{}),
consts.XTCPProxy: reflect.TypeOf(XTCPVisitorConf{}),
consts.SUDPProxy: reflect.TypeOf(SUDPVisitorConf{}),
}
)
type VisitorConf interface {
GetBaseInfo() *BaseVisitorConf
Compare(cmp VisitorConf) bool
UnmarshalFromIni(prefix string, name string, section *ini.Section) error
Check() error
}
type BaseVisitorConf struct {
ProxyName string `ini:"name",json:"name"`
ProxyType string `ini:"type",json:"type"`
UseEncryption bool `ini:"use_encryption",json:"use_encryption"`
UseCompression bool `ini:"use_compression",json:"use_compression"`
Role string `ini:"role",json:"role"`
Sk string `ini:"sk",json:"sk"`
ServerName string `ini:"server_name",json:"server_name"`
BindAddr string `ini:"bind_addr",json:"bind_addr"`
BindPort int `ini:"bind_port",json:"bind_port"`
}
type SUDPVisitorConf struct {
BaseVisitorConf `ini:",,,,extends",json:"inline"`
}
type STCPVisitorConf struct {
BaseVisitorConf `ini:",,,,extends",json:"inline"`
}
type XTCPVisitorConf struct {
BaseVisitorConf `ini:",,,,extends",json:"inline"`
} }

View File

@ -18,13 +18,57 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"github.com/fatedier/frp/pkg/consts"
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
// Visitor
var (
visitorConfTypeMap = map[string]reflect.Type{
consts.STCPProxy: reflect.TypeOf(STCPVisitorConf{}),
consts.XTCPProxy: reflect.TypeOf(XTCPVisitorConf{}),
consts.SUDPProxy: reflect.TypeOf(SUDPVisitorConf{}),
}
)
type VisitorConf interface {
GetBaseInfo() *BaseVisitorConf
Compare(cmp VisitorConf) bool
UnmarshalFromIni(prefix string, name string, section *ini.Section) error
Check() error
}
type BaseVisitorConf struct {
ProxyName string `ini:"name" json:"name"`
ProxyType string `ini:"type" json:"type"`
UseEncryption bool `ini:"use_encryption" json:"use_encryption"`
UseCompression bool `ini:"use_compression" json:"use_compression"`
Role string `ini:"role" json:"role"`
Sk string `ini:"sk" json:"sk"`
ServerName string `ini:"server_name" json:"server_name"`
BindAddr string `ini:"bind_addr" json:"bind_addr"`
BindPort int `ini:"bind_port" json:"bind_port"`
}
type SUDPVisitorConf struct {
BaseVisitorConf `ini:",extends" json:"inline"`
}
type STCPVisitorConf struct {
BaseVisitorConf `ini:",extends" json:"inline"`
}
type XTCPVisitorConf struct {
BaseVisitorConf `ini:",extends" json:"inline"`
}
// DefaultVisitorConf creates a empty VisitorConf object by visitorType. // DefaultVisitorConf creates a empty VisitorConf object by visitorType.
// If visitorType doesn't exist, return nil. // If visitorType doesn't exist, return nil.
func DefaultVisitorConf(visitorType string) VisitorConf { func DefaultVisitorConf(visitorType string) VisitorConf {
v, ok := VisitorConfTypeMap[visitorType] v, ok := visitorConfTypeMap[visitorType]
if !ok { if !ok {
return nil return nil
} }
@ -93,8 +137,9 @@ func (cfg *BaseVisitorConf) check() (err error) {
return return
} }
func (cfg *BaseVisitorConf) decorate(prefix string, name string, section *ini.Section) error { func (cfg *BaseVisitorConf) unmarshalFromIni(prefix string, name string, section *ini.Section) error {
// Custom decoration after basic unmarshal:
// proxy name // proxy name
cfg.ProxyName = prefix + name cfg.ProxyName = prefix + name
@ -109,6 +154,20 @@ func (cfg *BaseVisitorConf) decorate(prefix string, name string, section *ini.Se
return nil return nil
} }
func preVisitorUnmarshalFromIni(cfg VisitorConf, prefix string, name string, section *ini.Section) error {
err := section.MapTo(cfg)
if err != nil {
return err
}
err = cfg.GetBaseInfo().unmarshalFromIni(prefix, name, section)
if err != nil {
return err
}
return nil
}
// SUDP // SUDP
var _ VisitorConf = &SUDPVisitorConf{} var _ VisitorConf = &SUDPVisitorConf{}
@ -127,20 +186,15 @@ func (cfg *SUDPVisitorConf) Compare(cmp VisitorConf) bool {
return true return true
} }
func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *SUDPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
err := section.MapTo(cfg) err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
if err != nil { if err != nil {
return err return
}
err = cfg.BaseVisitorConf.decorate(prefix, name, section)
if err != nil {
return err
} }
// Add custom logic unmarshal, if exists // Add custom logic unmarshal, if exists
return nil return
} }
func (cfg *SUDPVisitorConf) Check() (err error) { func (cfg *SUDPVisitorConf) Check() (err error) {
@ -171,20 +225,15 @@ func (cfg *STCPVisitorConf) Compare(cmp VisitorConf) bool {
return true return true
} }
func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *STCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
err := section.MapTo(cfg) err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
if err != nil { if err != nil {
return err return
}
err = cfg.BaseVisitorConf.decorate(prefix, name, section)
if err != nil {
return err
} }
// Add custom logic unmarshal, if exists // Add custom logic unmarshal, if exists
return nil return
} }
func (cfg *STCPVisitorConf) Check() (err error) { func (cfg *STCPVisitorConf) Check() (err error) {
@ -215,20 +264,15 @@ func (cfg *XTCPVisitorConf) Compare(cmp VisitorConf) bool {
return true return true
} }
func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error { func (cfg *XTCPVisitorConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) (err error) {
err := section.MapTo(cfg) err = preVisitorUnmarshalFromIni(cfg, prefix, name, section)
if err != nil { if err != nil {
return err return
}
err = cfg.BaseVisitorConf.decorate(prefix, name, section)
if err != nil {
return err
} }
// Add custom logic unmarshal, if exists // Add custom logic unmarshal, if exists
return nil return
} }
func (cfg *XTCPVisitorConf) Check() (err error) { func (cfg *XTCPVisitorConf) Check() (err error) {

View File

@ -17,15 +17,20 @@ package config
import ( import (
"testing" "testing"
"gopkg.in/ini.v1"
"github.com/fatedier/frp/pkg/consts" "github.com/fatedier/frp/pkg/consts"
"gopkg.in/ini.v1"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
const testVisitorPrefix = "test." const testVisitorPrefix = "test."
func Test_Visitor_Interface(t *testing.T) {
for name := range visitorConfTypeMap {
DefaultVisitorConf(name)
}
}
func Test_Visitor_UnmarshalFromIni(t *testing.T) { func Test_Visitor_UnmarshalFromIni(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)