Skip to content

Commit

Permalink
Merge pull request fatedier#1396 from velovix/issue-1387_server-conf-…
Browse files Browse the repository at this point in the history
…as-argument

Pass server configuration as an argument
  • Loading branch information
fatedier authored Aug 20, 2019
2 parents 90a32ab + 5fc7b3c commit f999c8a
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 149 deletions.
84 changes: 40 additions & 44 deletions cmd/frps/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/spf13/cobra"

"github.com/fatedier/frp/g"
"github.com/fatedier/frp/models/config"
"github.com/fatedier/frp/server"
"github.com/fatedier/frp/utils/log"
Expand Down Expand Up @@ -98,23 +97,23 @@ var rootCmd = &cobra.Command{
return nil
}

var cfg config.ServerCommonConf
var err error
if cfgFile != "" {
var content string
content, err = config.GetRenderedConfFromFile(cfgFile)
if err != nil {
return err
}
g.GlbServerCfg.CfgFile = cfgFile
err = parseServerCommonCfg(CfgFileTypeIni, content)
cfg, err = parseServerCommonCfg(CfgFileTypeIni, content)
} else {
err = parseServerCommonCfg(CfgFileTypeCmd, "")
cfg, err = parseServerCommonCfg(CfgFileTypeCmd, "")
}
if err != nil {
return err
}

err = runServer()
err = runServer(cfg)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand All @@ -129,52 +128,51 @@ func Execute() {
}
}

func parseServerCommonCfg(fileType int, content string) (err error) {
func parseServerCommonCfg(fileType int, content string) (cfg config.ServerCommonConf, err error) {
if fileType == CfgFileTypeIni {
err = parseServerCommonCfgFromIni(content)
cfg, err = parseServerCommonCfgFromIni(content)
} else if fileType == CfgFileTypeCmd {
err = parseServerCommonCfgFromCmd()
cfg, err = parseServerCommonCfgFromCmd()
}
if err != nil {
return
}

err = g.GlbServerCfg.ServerCommonConf.Check()
err = cfg.Check()
if err != nil {
return
}

config.InitServerCfg(&g.GlbServerCfg.ServerCommonConf)
return
}

func parseServerCommonCfgFromIni(content string) (err error) {
cfg, err := config.UnmarshalServerConfFromIni(&g.GlbServerCfg.ServerCommonConf, content)
func parseServerCommonCfgFromIni(content string) (config.ServerCommonConf, error) {
cfg, err := config.UnmarshalServerConfFromIni(content)
if err != nil {
return err
return config.ServerCommonConf{}, err
}
g.GlbServerCfg.ServerCommonConf = *cfg
return
return cfg, nil
}

func parseServerCommonCfgFromCmd() (err error) {
g.GlbServerCfg.BindAddr = bindAddr
g.GlbServerCfg.BindPort = bindPort
g.GlbServerCfg.BindUdpPort = bindUdpPort
g.GlbServerCfg.KcpBindPort = kcpBindPort
g.GlbServerCfg.ProxyBindAddr = proxyBindAddr
g.GlbServerCfg.VhostHttpPort = vhostHttpPort
g.GlbServerCfg.VhostHttpsPort = vhostHttpsPort
g.GlbServerCfg.VhostHttpTimeout = vhostHttpTimeout
g.GlbServerCfg.DashboardAddr = dashboardAddr
g.GlbServerCfg.DashboardPort = dashboardPort
g.GlbServerCfg.DashboardUser = dashboardUser
g.GlbServerCfg.DashboardPwd = dashboardPwd
g.GlbServerCfg.LogFile = logFile
g.GlbServerCfg.LogLevel = logLevel
g.GlbServerCfg.LogMaxDays = logMaxDays
g.GlbServerCfg.Token = token
g.GlbServerCfg.SubDomainHost = subDomainHost
func parseServerCommonCfgFromCmd() (cfg config.ServerCommonConf, err error) {
cfg = config.GetDefaultServerConf()

cfg.BindAddr = bindAddr
cfg.BindPort = bindPort
cfg.BindUdpPort = bindUdpPort
cfg.KcpBindPort = kcpBindPort
cfg.ProxyBindAddr = proxyBindAddr
cfg.VhostHttpPort = vhostHttpPort
cfg.VhostHttpsPort = vhostHttpsPort
cfg.VhostHttpTimeout = vhostHttpTimeout
cfg.DashboardAddr = dashboardAddr
cfg.DashboardPort = dashboardPort
cfg.DashboardUser = dashboardUser
cfg.DashboardPwd = dashboardPwd
cfg.LogFile = logFile
cfg.LogLevel = logLevel
cfg.LogMaxDays = logMaxDays
cfg.Token = token
cfg.SubDomainHost = subDomainHost
if len(allowPorts) > 0 {
// e.g. 1000-2000,2001,2002,3000-4000
ports, errRet := util.ParseRangeNumbers(allowPorts)
Expand All @@ -184,29 +182,27 @@ func parseServerCommonCfgFromCmd() (err error) {
}

for _, port := range ports {
g.GlbServerCfg.AllowPorts[int(port)] = struct{}{}
cfg.AllowPorts[int(port)] = struct{}{}
}
}
g.GlbServerCfg.MaxPortsPerClient = maxPortsPerClient
cfg.MaxPortsPerClient = maxPortsPerClient

if logFile == "console" {
g.GlbServerCfg.LogWay = "console"
cfg.LogWay = "console"
} else {
g.GlbServerCfg.LogWay = "file"
cfg.LogWay = "file"
}
g.GlbServerCfg.DisableLogColor = disableLogColor
cfg.DisableLogColor = disableLogColor
return
}

func runServer() (err error) {
log.InitLog(g.GlbServerCfg.LogWay, g.GlbServerCfg.LogFile, g.GlbServerCfg.LogLevel,
g.GlbServerCfg.LogMaxDays, g.GlbServerCfg.DisableLogColor)
svr, err := server.NewService()
func runServer(cfg config.ServerCommonConf) (err error) {
log.InitLog(cfg.LogWay, cfg.LogFile, cfg.LogLevel, cfg.LogMaxDays, cfg.DisableLogColor)
svr, err := server.NewService(cfg)
if err != nil {
return err
}
log.Info("Start frps success")
server.ServerService = svr
svr.Run()
return
}
10 changes: 0 additions & 10 deletions g/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ import (

var (
GlbClientCfg *ClientCfg
GlbServerCfg *ServerCfg
)

func init() {
GlbClientCfg = &ClientCfg{
ClientCommonConf: *config.GetDefaultClientConf(),
}
GlbServerCfg = &ServerCfg{
ServerCommonConf: *config.GetDefaultServerConf(),
}
}

type ClientCfg struct {
Expand All @@ -24,9 +20,3 @@ type ClientCfg struct {
CfgFile string
ServerUdpPort int // this is configured by login response from frps
}

type ServerCfg struct {
config.ServerCommonConf

CfgFile string
}
36 changes: 18 additions & 18 deletions models/config/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ type ProxyConf interface {
UnmarshalFromIni(prefix string, name string, conf ini.Section) error
MarshalToMsg(pMsg *msg.NewProxy)
CheckForCli() error
CheckForSvr() error
CheckForSvr(serverCfg ServerCommonConf) error
Compare(conf ProxyConf) bool
}

func NewProxyConfFromMsg(pMsg *msg.NewProxy) (cfg ProxyConf, err error) {
func NewProxyConfFromMsg(pMsg *msg.NewProxy, serverCfg ServerCommonConf) (cfg ProxyConf, err error) {
if pMsg.ProxyType == "" {
pMsg.ProxyType = consts.TcpProxy
}
Expand All @@ -73,7 +73,7 @@ func NewProxyConfFromMsg(pMsg *msg.NewProxy) (cfg ProxyConf, err error) {
return
}
cfg.UnmarshalFromMsg(pMsg)
err = cfg.CheckForSvr()
err = cfg.CheckForSvr(serverCfg)
return
}

Expand Down Expand Up @@ -308,21 +308,21 @@ func (cfg *DomainConf) checkForCli() (err error) {
return
}

func (cfg *DomainConf) checkForSvr() (err error) {
func (cfg *DomainConf) checkForSvr(serverCfg ServerCommonConf) (err error) {
if err = cfg.check(); err != nil {
return
}

for _, domain := range cfg.CustomDomains {
if subDomainHost != "" && len(strings.Split(subDomainHost, ".")) < len(strings.Split(domain, ".")) {
if strings.Contains(domain, subDomainHost) {
return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, subDomainHost)
if serverCfg.SubDomainHost != "" && len(strings.Split(serverCfg.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
if strings.Contains(domain, serverCfg.SubDomainHost) {
return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, serverCfg.SubDomainHost)
}
}
}

if cfg.SubDomain != "" {
if subDomainHost == "" {
if serverCfg.SubDomainHost == "" {
return fmt.Errorf("subdomain is not supported because this feature is not enabled in remote frps")
}
if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
Expand Down Expand Up @@ -504,7 +504,7 @@ func (cfg *TcpProxyConf) CheckForCli() (err error) {
return
}

func (cfg *TcpProxyConf) CheckForSvr() error { return nil }
func (cfg *TcpProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { return nil }

// UDP
type UdpProxyConf struct {
Expand Down Expand Up @@ -552,7 +552,7 @@ func (cfg *UdpProxyConf) CheckForCli() (err error) {
return
}

func (cfg *UdpProxyConf) CheckForSvr() error { return nil }
func (cfg *UdpProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { return nil }

// HTTP
type HttpProxyConf struct {
Expand Down Expand Up @@ -657,11 +657,11 @@ func (cfg *HttpProxyConf) CheckForCli() (err error) {
return
}

func (cfg *HttpProxyConf) CheckForSvr() (err error) {
if vhostHttpPort == 0 {
func (cfg *HttpProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
if serverCfg.VhostHttpPort == 0 {
return fmt.Errorf("type [http] not support when vhost_http_port is not set")
}
if err = cfg.DomainConf.checkForSvr(); err != nil {
if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
return
}
Expand Down Expand Up @@ -717,11 +717,11 @@ func (cfg *HttpsProxyConf) CheckForCli() (err error) {
return
}

func (cfg *HttpsProxyConf) CheckForSvr() (err error) {
if vhostHttpsPort == 0 {
func (cfg *HttpsProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
if serverCfg.VhostHttpsPort == 0 {
return fmt.Errorf("type [https] not support when vhost_https_port is not set")
}
if err = cfg.DomainConf.checkForSvr(); err != nil {
if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
return
}
Expand Down Expand Up @@ -790,7 +790,7 @@ func (cfg *StcpProxyConf) CheckForCli() (err error) {
return
}

func (cfg *StcpProxyConf) CheckForSvr() (err error) {
func (cfg *StcpProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
return
}

Expand Down Expand Up @@ -857,7 +857,7 @@ func (cfg *XtcpProxyConf) CheckForCli() (err error) {
return
}

func (cfg *XtcpProxyConf) CheckForSvr() (err error) {
func (cfg *XtcpProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
return
}

Expand Down
28 changes: 5 additions & 23 deletions models/config/server_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,6 @@ import (
"github.com/fatedier/frp/utils/util"
)

var (
// server global configure used for generate proxy conf used in frps
proxyBindAddr string
subDomainHost string
vhostHttpPort int
vhostHttpsPort int
)

func InitServerCfg(cfg *ServerCommonConf) {
proxyBindAddr = cfg.ProxyBindAddr
subDomainHost = cfg.SubDomainHost
vhostHttpPort = cfg.VhostHttpPort
vhostHttpsPort = cfg.VhostHttpsPort
}

// common config
type ServerCommonConf struct {
BindAddr string `json:"bind_addr"`
Expand Down Expand Up @@ -79,8 +64,8 @@ type ServerCommonConf struct {
UserConnTimeout int64 `json:"user_conn_timeout"`
}

func GetDefaultServerConf() *ServerCommonConf {
return &ServerCommonConf{
func GetDefaultServerConf() ServerCommonConf {
return ServerCommonConf{
BindAddr: "0.0.0.0",
BindPort: 7000,
BindUdpPort: 0,
Expand Down Expand Up @@ -111,16 +96,13 @@ func GetDefaultServerConf() *ServerCommonConf {
}
}

func UnmarshalServerConfFromIni(defaultCfg *ServerCommonConf, content string) (cfg *ServerCommonConf, err error) {
cfg = defaultCfg
if cfg == nil {
cfg = GetDefaultServerConf()
}
func UnmarshalServerConfFromIni(content string) (cfg ServerCommonConf, err error) {
cfg = GetDefaultServerConf()

conf, err := ini.Load(strings.NewReader(content))
if err != nil {
err = fmt.Errorf("parse ini conf file error: %v", err)
return nil, err
return ServerCommonConf{}, err
}

var (
Expand Down
Loading

0 comments on commit f999c8a

Please sign in to comment.