Skip to content

Commit

Permalink
fix config parse logic (#2323)
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier authored Mar 22, 2021
1 parent 6b80861 commit 9a849a2
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 42 deletions.
2 changes: 1 addition & 1 deletion assets/frpc/statik/statik.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/frps/static/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html> <html lang=en> <head> <meta charset=utf-8> <title>frps dashboard</title> <link rel="shortcut icon" href="favicon.ico"></head> <body> <div id=app></div> <script type="text/javascript" src="manifest.js?9cdfcd28c7394b82e47d"></script><script type="text/javascript" src="vendor.js?6aeb7e29d75bc1975785"></script></body> </html>
<!doctype html> <html lang=en> <head> <meta charset=utf-8> <title>frps dashboard</title> <link rel="shortcut icon" href="favicon.ico"></head> <body> <div id=app></div> <script type="text/javascript" src="manifest.js?782d7b1b910e824ac986"></script><script type="text/javascript" src="vendor.js?7f899297af075fb3b085"></script></body> </html>
2 changes: 1 addition & 1 deletion assets/frps/static/manifest.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/frps/static/vendor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/frps/statik/statik.go

Large diffs are not rendered by default.

19 changes: 8 additions & 11 deletions cmd/frpc/sub/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,11 @@ func parseClientCommonCfg(fileType int, source []byte) (cfg config.ClientCommonC
if err != nil {
return
}
if cfg.LogFile == "console" {
cfg.LogWay = "console"
} else {
cfg.LogWay = "file"
}
err = cfg.Check()

cfg.Complete()
err = cfg.Validate()
if err != nil {
err = fmt.Errorf("Parse config error: %v", err)
return
}
return
Expand Down Expand Up @@ -185,21 +183,20 @@ func runClient(cfgFilePath string) (err error) {
var content []byte
content, err = config.GetRenderedConfFromFile(cfgFilePath)
if err != nil {
return
return err
}

cfg, err := parseClientCommonCfg(CfgFileTypeIni, content)
if err != nil {
return
return err
}

pxyCfgs, visitorCfgs, err := config.LoadAllProxyConfsFromIni(cfg.User, content, cfg.Start)
if err != nil {
return
return err
}

err = startService(cfg, pxyCfgs, visitorCfgs, cfgFilePath)
return
return startService(cfg, pxyCfgs, visitorCfgs, cfgFilePath)
}

func startService(
Expand Down
18 changes: 10 additions & 8 deletions cmd/frps/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,13 @@ var rootCmd = &cobra.Command{
var cfg config.ServerCommonConf
var err error
if cfgFile != "" {
log.Info("frps uses config file: %s", cfgFile)
var content []byte
content, err = config.GetRenderedConfFromFile(cfgFile)
if err != nil {
return err
}
cfg, err = parseServerCommonCfg(CfgFileTypeIni, content)
} else {
log.Info("frps uses command line arguments for config")
cfg, err = parseServerCommonCfg(CfgFileTypeCmd, nil)
}
if err != nil {
Expand Down Expand Up @@ -144,13 +142,10 @@ func parseServerCommonCfg(fileType int, source []byte) (cfg config.ServerCommonC
if err != nil {
return
}
if cfg.LogFile == "console" {
cfg.LogWay = "console"
} else {
cfg.LogWay = "file"
}
err = cfg.Check()
cfg.Complete()
err = cfg.Validate()
if err != nil {
err = fmt.Errorf("Parse config error: %v", err)
return
}
return
Expand Down Expand Up @@ -200,6 +195,13 @@ func parseServerCommonCfgFromCmd() (cfg config.ServerCommonConf, err error) {

func runServer(cfg config.ServerCommonConf) (err error) {
log.InitLog(cfg.LogWay, cfg.LogFile, cfg.LogLevel, cfg.LogMaxDays, cfg.DisableLogColor)

if cfgFile != "" {
log.Info("frps uses config file: %s", cfgFile)
} else {
log.Info("frps uses command line arguments for config")
}

svr, err := server.NewService(cfg)
if err != nil {
return err
Expand Down
18 changes: 15 additions & 3 deletions pkg/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,21 @@ func GetDefaultClientConf() ClientCommonConf {
}
}

func (cfg *ClientCommonConf) Check() error {
func (cfg *ClientCommonConf) Complete() {
if cfg.LogFile == "console" {
cfg.LogWay = "console"
} else {
cfg.LogWay = "file"
}
}

func (cfg *ClientCommonConf) Validate() error {
if cfg.HeartbeatInterval <= 0 {
return fmt.Errorf("Parse conf error: invalid heartbeat_interval")
return fmt.Errorf("invalid heartbeat_interval")
}

if cfg.HeartbeatTimeout < cfg.HeartbeatInterval {
return fmt.Errorf("Parse conf error: invalid heartbeat_timeout, heartbeat_timeout is less than heartbeat_interval")
return fmt.Errorf("invalid heartbeat_timeout, heartbeat_timeout is less than heartbeat_interval")
}

if cfg.TLSEnable == false {
Expand All @@ -196,6 +204,10 @@ func (cfg *ClientCommonConf) Check() error {
}
}

if cfg.Protocol != "tcp" && cfg.Protocol != "kcp" && cfg.Protocol != "websocket" {
return fmt.Errorf("invalid protocol")
}

return nil
}

Expand Down
30 changes: 23 additions & 7 deletions pkg/config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type ServerCommonConf struct {
// 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".
// may be the same as BindAddr.
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
Expand Down Expand Up @@ -174,7 +174,7 @@ func GetDefaultServerConf() ServerCommonConf {
BindPort: 7000,
BindUDPPort: 0,
KCPBindPort: 0,
ProxyBindAddr: "0.0.0.0",
ProxyBindAddr: "",
VhostHTTPPort: 0,
VhostHTTPSPort: 0,
TCPMuxHTTPConnectPort: 0,
Expand Down Expand Up @@ -208,10 +208,6 @@ func GetDefaultServerConf() ServerCommonConf {
}
}

func (cfg *ServerCommonConf) Check() error {
return nil
}

func UnmarshalServerConfFromIni(source interface{}) (ServerCommonConf, error) {

f, err := ini.LoadSources(ini.LoadOptions{
Expand Down Expand Up @@ -242,7 +238,7 @@ func UnmarshalServerConfFromIni(source interface{}) (ServerCommonConf, error) {
if allowPortStr != "" {
allowPorts, err := util.ParseRangeNumbers(allowPortStr)
if err != nil {
return ServerCommonConf{}, fmt.Errorf("Parse conf error: allow_ports: %v", err)
return ServerCommonConf{}, fmt.Errorf("invalid allow_ports: %v", err)
}
for _, port := range allowPorts {
common.AllowPorts[int(port)] = struct{}{}
Expand All @@ -269,6 +265,26 @@ func UnmarshalServerConfFromIni(source interface{}) (ServerCommonConf, error) {
return common, nil
}

func (cfg *ServerCommonConf) Complete() {
if cfg.LogFile == "console" {
cfg.LogWay = "console"
} else {
cfg.LogWay = "file"
}

if cfg.ProxyBindAddr == "" {
cfg.ProxyBindAddr = cfg.BindAddr
}

if cfg.TLSTrustedCaFile != "" {
cfg.TLSOnly = true
}
}

func (cfg *ServerCommonConf) Validate() error {
return nil
}

func loadHTTPPluginOpt(section *ini.Section) (*plugin.HTTPPluginOptions, error) {
name := strings.TrimSpace(strings.TrimPrefix(section.Name(), "plugin."))

Expand Down
7 changes: 4 additions & 3 deletions pkg/config/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"testing"

"github.com/fatedier/frp/pkg/auth"
"github.com/fatedier/frp/pkg/plugin/server"
plugin "github.com/fatedier/frp/pkg/plugin/server"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -133,7 +133,7 @@ func Test_LoadServerCommonConf(t *testing.T) {
},
MaxPoolCount: 59,
MaxPortsPerClient: 9,
TLSOnly: false,
TLSOnly: true,
TLSCertFile: "server.crt",
TLSKeyFile: "server.key",
TLSTrustedCaFile: "ca.crt",
Expand Down Expand Up @@ -177,7 +177,7 @@ func Test_LoadServerCommonConf(t *testing.T) {
BindAddr: "0.0.0.9",
BindPort: 7009,
BindUDPPort: 7008,
ProxyBindAddr: "0.0.0.0",
ProxyBindAddr: "0.0.0.9",
VhostHTTPTimeout: 60,
DashboardAddr: "0.0.0.0",
DashboardUser: "admin",
Expand All @@ -202,6 +202,7 @@ func Test_LoadServerCommonConf(t *testing.T) {
for _, c := range testcases {
actual, err := UnmarshalServerConfFromIni(c.source)
assert.NoError(err)
actual.Complete()
assert.Equal(c.expected, actual)
}
}
2 changes: 1 addition & 1 deletion pkg/util/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"strings"
)

var version string = "0.36.1"
var version string = "0.36.2"

func Full() string {
return version
Expand Down
8 changes: 4 additions & 4 deletions web/frps/src/utils/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class HttpProxy extends BaseProxy {
this.custom_domains = proxyStats.conf.custom_domains
this.host_header_rewrite = proxyStats.conf.host_header_rewrite
this.locations = proxyStats.conf.locations
if (proxyStats.conf.sub_domain != "") {
this.subdomain = proxyStats.conf.sub_domain + "." + subdomain_host
if (proxyStats.conf.subdomain != "") {
this.subdomain = proxyStats.conf.subdomain + "." + subdomain_host
} else {
this.subdomain = ""
}
Expand All @@ -75,8 +75,8 @@ class HttpsProxy extends BaseProxy {
this.port = port
if (proxyStats.conf != null) {
this.custom_domains = proxyStats.conf.custom_domains
if (proxyStats.conf.sub_domain != "") {
this.subdomain = proxyStats.conf.sub_domain + "." + subdomain_host
if (proxyStats.conf.subdomain != "") {
this.subdomain = proxyStats.conf.subdomain + "." + subdomain_host
} else {
this.subdomain = ""
}
Expand Down

0 comments on commit 9a849a2

Please sign in to comment.