Pass server configuration as an argument

The ServerCommonConf is now passed around as an argument instead of
being shared between components as a global variable. This allows for
more natural interaction with server.Session as a library and allows for
multiple servers to co-exist within the same process.

Related: #1387
This commit is contained in:
Tyler Compton 2019-08-19 11:07:37 -07:00
parent a415573e45
commit 6277af4790
14 changed files with 117 additions and 146 deletions

View File

@ -20,7 +20,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/fatedier/frp/g"
"github.com/fatedier/frp/models/config" "github.com/fatedier/frp/models/config"
"github.com/fatedier/frp/server" "github.com/fatedier/frp/server"
"github.com/fatedier/frp/utils/log" "github.com/fatedier/frp/utils/log"
@ -98,6 +97,7 @@ var rootCmd = &cobra.Command{
return nil return nil
} }
var cfg config.ServerCommonConf
var err error var err error
if cfgFile != "" { if cfgFile != "" {
var content string var content string
@ -105,16 +105,15 @@ var rootCmd = &cobra.Command{
if err != nil { if err != nil {
return err return err
} }
g.GlbServerCfg.CfgFile = cfgFile cfg, err = parseServerCommonCfg(CfgFileTypeIni, content)
err = parseServerCommonCfg(CfgFileTypeIni, content)
} else { } else {
err = parseServerCommonCfg(CfgFileTypeCmd, "") cfg, err = parseServerCommonCfg(CfgFileTypeCmd, "")
} }
if err != nil { if err != nil {
return err return err
} }
err = runServer() err = runServer(cfg)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
@ -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 { if fileType == CfgFileTypeIni {
err = parseServerCommonCfgFromIni(content) cfg, err = parseServerCommonCfgFromIni(content)
} else if fileType == CfgFileTypeCmd { } else if fileType == CfgFileTypeCmd {
err = parseServerCommonCfgFromCmd() cfg, err = parseServerCommonCfgFromCmd()
} }
if err != nil { if err != nil {
return return
} }
err = g.GlbServerCfg.ServerCommonConf.Check() err = cfg.Check()
if err != nil { if err != nil {
return return
} }
config.InitServerCfg(&g.GlbServerCfg.ServerCommonConf)
return return
} }
func parseServerCommonCfgFromIni(content string) (err error) { func parseServerCommonCfgFromIni(content string) (config.ServerCommonConf, error) {
cfg, err := config.UnmarshalServerConfFromIni(&g.GlbServerCfg.ServerCommonConf, content) cfg, err := config.UnmarshalServerConfFromIni(content)
if err != nil { if err != nil {
return err return config.ServerCommonConf{}, err
} }
g.GlbServerCfg.ServerCommonConf = *cfg return cfg, nil
return
} }
func parseServerCommonCfgFromCmd() (err error) { func parseServerCommonCfgFromCmd() (cfg config.ServerCommonConf, err error) {
g.GlbServerCfg.BindAddr = bindAddr cfg = config.GetDefaultServerConf()
g.GlbServerCfg.BindPort = bindPort
g.GlbServerCfg.BindUdpPort = bindUdpPort cfg.BindAddr = bindAddr
g.GlbServerCfg.KcpBindPort = kcpBindPort cfg.BindPort = bindPort
g.GlbServerCfg.ProxyBindAddr = proxyBindAddr cfg.BindUdpPort = bindUdpPort
g.GlbServerCfg.VhostHttpPort = vhostHttpPort cfg.KcpBindPort = kcpBindPort
g.GlbServerCfg.VhostHttpsPort = vhostHttpsPort cfg.ProxyBindAddr = proxyBindAddr
g.GlbServerCfg.VhostHttpTimeout = vhostHttpTimeout cfg.VhostHttpPort = vhostHttpPort
g.GlbServerCfg.DashboardAddr = dashboardAddr cfg.VhostHttpsPort = vhostHttpsPort
g.GlbServerCfg.DashboardPort = dashboardPort cfg.VhostHttpTimeout = vhostHttpTimeout
g.GlbServerCfg.DashboardUser = dashboardUser cfg.DashboardAddr = dashboardAddr
g.GlbServerCfg.DashboardPwd = dashboardPwd cfg.DashboardPort = dashboardPort
g.GlbServerCfg.LogFile = logFile cfg.DashboardUser = dashboardUser
g.GlbServerCfg.LogLevel = logLevel cfg.DashboardPwd = dashboardPwd
g.GlbServerCfg.LogMaxDays = logMaxDays cfg.LogFile = logFile
g.GlbServerCfg.Token = token cfg.LogLevel = logLevel
g.GlbServerCfg.SubDomainHost = subDomainHost cfg.LogMaxDays = logMaxDays
cfg.Token = token
cfg.SubDomainHost = subDomainHost
if len(allowPorts) > 0 { if len(allowPorts) > 0 {
// e.g. 1000-2000,2001,2002,3000-4000 // e.g. 1000-2000,2001,2002,3000-4000
ports, errRet := util.ParseRangeNumbers(allowPorts) ports, errRet := util.ParseRangeNumbers(allowPorts)
@ -184,24 +182,23 @@ func parseServerCommonCfgFromCmd() (err error) {
} }
for _, port := range ports { 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" { if logFile == "console" {
g.GlbServerCfg.LogWay = "console" cfg.LogWay = "console"
} else { } else {
g.GlbServerCfg.LogWay = "file" cfg.LogWay = "file"
} }
g.GlbServerCfg.DisableLogColor = disableLogColor cfg.DisableLogColor = disableLogColor
return return
} }
func runServer() (err error) { func runServer(cfg config.ServerCommonConf) (err error) {
log.InitLog(g.GlbServerCfg.LogWay, g.GlbServerCfg.LogFile, g.GlbServerCfg.LogLevel, log.InitLog(cfg.LogWay, cfg.LogFile, cfg.LogLevel, cfg.LogMaxDays, cfg.DisableLogColor)
g.GlbServerCfg.LogMaxDays, g.GlbServerCfg.DisableLogColor) svr, err := server.NewService(cfg)
svr, err := server.NewService()
if err != nil { if err != nil {
return err return err
} }

10
g/g.go
View File

@ -6,16 +6,12 @@ import (
var ( var (
GlbClientCfg *ClientCfg GlbClientCfg *ClientCfg
GlbServerCfg *ServerCfg
) )
func init() { func init() {
GlbClientCfg = &ClientCfg{ GlbClientCfg = &ClientCfg{
ClientCommonConf: *config.GetDefaultClientConf(), ClientCommonConf: *config.GetDefaultClientConf(),
} }
GlbServerCfg = &ServerCfg{
ServerCommonConf: *config.GetDefaultServerConf(),
}
} }
type ClientCfg struct { type ClientCfg struct {
@ -24,9 +20,3 @@ type ClientCfg struct {
CfgFile string CfgFile string
ServerUdpPort int // this is configured by login response from frps ServerUdpPort int // this is configured by login response from frps
} }
type ServerCfg struct {
config.ServerCommonConf
CfgFile string
}

2
go.sum
View File

@ -1,5 +1,6 @@
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatedier/beego v0.0.0-20171024143340-6c6a4f5bd5eb h1:wCrNShQidLmvVWn/0PikGmpdP0vtQmnvyRg3ZBEhczw= github.com/fatedier/beego v0.0.0-20171024143340-6c6a4f5bd5eb h1:wCrNShQidLmvVWn/0PikGmpdP0vtQmnvyRg3ZBEhczw=
github.com/fatedier/beego v0.0.0-20171024143340-6c6a4f5bd5eb/go.mod h1:wx3gB6dbIfBRcucp94PI9Bt3I0F2c/MyNEWuhzpWiwk= github.com/fatedier/beego v0.0.0-20171024143340-6c6a4f5bd5eb/go.mod h1:wx3gB6dbIfBRcucp94PI9Bt3I0F2c/MyNEWuhzpWiwk=
@ -27,6 +28,7 @@ github.com/pires/go-proxyproto v0.0.0-20190111085350-4d51b51e3bfc h1:lNOt1SMsgHX
github.com/pires/go-proxyproto v0.0.0-20190111085350-4d51b51e3bfc/go.mod h1:6/gX3+E/IYGa0wMORlSMla999awQFdbaeQCHjSMKIzY= github.com/pires/go-proxyproto v0.0.0-20190111085350-4d51b51e3bfc/go.mod h1:6/gX3+E/IYGa0wMORlSMla999awQFdbaeQCHjSMKIzY=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rakyll/statik v0.1.1 h1:fCLHsIMajHqD5RKigbFXpvX3dN7c80Pm12+NCrI3kvg= github.com/rakyll/statik v0.1.1 h1:fCLHsIMajHqD5RKigbFXpvX3dN7c80Pm12+NCrI3kvg=
github.com/rakyll/statik v0.1.1/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs= github.com/rakyll/statik v0.1.1/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs=

View File

@ -58,11 +58,11 @@ type ProxyConf interface {
UnmarshalFromIni(prefix string, name string, conf ini.Section) error UnmarshalFromIni(prefix string, name string, conf ini.Section) error
MarshalToMsg(pMsg *msg.NewProxy) MarshalToMsg(pMsg *msg.NewProxy)
CheckForCli() error CheckForCli() error
CheckForSvr() error CheckForSvr(serverCfg ServerCommonConf) error
Compare(conf ProxyConf) bool 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 == "" { if pMsg.ProxyType == "" {
pMsg.ProxyType = consts.TcpProxy pMsg.ProxyType = consts.TcpProxy
} }
@ -73,7 +73,7 @@ func NewProxyConfFromMsg(pMsg *msg.NewProxy) (cfg ProxyConf, err error) {
return return
} }
cfg.UnmarshalFromMsg(pMsg) cfg.UnmarshalFromMsg(pMsg)
err = cfg.CheckForSvr() err = cfg.CheckForSvr(serverCfg)
return return
} }
@ -308,21 +308,21 @@ func (cfg *DomainConf) checkForCli() (err error) {
return return
} }
func (cfg *DomainConf) checkForSvr() (err error) { func (cfg *DomainConf) checkForSvr(serverCfg ServerCommonConf) (err error) {
if err = cfg.check(); err != nil { if err = cfg.check(); err != nil {
return return
} }
for _, domain := range cfg.CustomDomains { for _, domain := range cfg.CustomDomains {
if subDomainHost != "" && len(strings.Split(subDomainHost, ".")) < len(strings.Split(domain, ".")) { if serverCfg.SubDomainHost != "" && len(strings.Split(serverCfg.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
if strings.Contains(domain, subDomainHost) { if strings.Contains(domain, serverCfg.SubDomainHost) {
return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, subDomainHost) return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, serverCfg.SubDomainHost)
} }
} }
} }
if cfg.SubDomain != "" { if cfg.SubDomain != "" {
if subDomainHost == "" { if serverCfg.SubDomainHost == "" {
return fmt.Errorf("subdomain is not supported because this feature is not enabled in remote frps") 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, "*") { if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
@ -504,7 +504,7 @@ func (cfg *TcpProxyConf) CheckForCli() (err error) {
return return
} }
func (cfg *TcpProxyConf) CheckForSvr() error { return nil } func (cfg *TcpProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { return nil }
// UDP // UDP
type UdpProxyConf struct { type UdpProxyConf struct {
@ -552,7 +552,7 @@ func (cfg *UdpProxyConf) CheckForCli() (err error) {
return return
} }
func (cfg *UdpProxyConf) CheckForSvr() error { return nil } func (cfg *UdpProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { return nil }
// HTTP // HTTP
type HttpProxyConf struct { type HttpProxyConf struct {
@ -657,11 +657,11 @@ func (cfg *HttpProxyConf) CheckForCli() (err error) {
return return
} }
func (cfg *HttpProxyConf) CheckForSvr() (err error) { func (cfg *HttpProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
if vhostHttpPort == 0 { if serverCfg.VhostHttpPort == 0 {
return fmt.Errorf("type [http] not support when vhost_http_port is not set") 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) err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
return return
} }
@ -717,11 +717,11 @@ func (cfg *HttpsProxyConf) CheckForCli() (err error) {
return return
} }
func (cfg *HttpsProxyConf) CheckForSvr() (err error) { func (cfg *HttpsProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
if vhostHttpsPort == 0 { if serverCfg.VhostHttpsPort == 0 {
return fmt.Errorf("type [https] not support when vhost_https_port is not set") 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) err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
return return
} }
@ -790,7 +790,7 @@ func (cfg *StcpProxyConf) CheckForCli() (err error) {
return return
} }
func (cfg *StcpProxyConf) CheckForSvr() (err error) { func (cfg *StcpProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
return return
} }
@ -857,7 +857,7 @@ func (cfg *XtcpProxyConf) CheckForCli() (err error) {
return return
} }
func (cfg *XtcpProxyConf) CheckForSvr() (err error) { func (cfg *XtcpProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
return return
} }

View File

@ -24,21 +24,6 @@ import (
"github.com/fatedier/frp/utils/util" "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 // common config
type ServerCommonConf struct { type ServerCommonConf struct {
BindAddr string `json:"bind_addr"` BindAddr string `json:"bind_addr"`
@ -79,8 +64,8 @@ type ServerCommonConf struct {
UserConnTimeout int64 `json:"user_conn_timeout"` UserConnTimeout int64 `json:"user_conn_timeout"`
} }
func GetDefaultServerConf() *ServerCommonConf { func GetDefaultServerConf() ServerCommonConf {
return &ServerCommonConf{ return ServerCommonConf{
BindAddr: "0.0.0.0", BindAddr: "0.0.0.0",
BindPort: 7000, BindPort: 7000,
BindUdpPort: 0, BindUdpPort: 0,
@ -111,16 +96,13 @@ func GetDefaultServerConf() *ServerCommonConf {
} }
} }
func UnmarshalServerConfFromIni(defaultCfg *ServerCommonConf, content string) (cfg *ServerCommonConf, err error) { func UnmarshalServerConfFromIni(content string) (cfg ServerCommonConf, err error) {
cfg = defaultCfg cfg = GetDefaultServerConf()
if cfg == nil {
cfg = GetDefaultServerConf()
}
conf, err := ini.Load(strings.NewReader(content)) conf, err := ini.Load(strings.NewReader(content))
if err != nil { if err != nil {
err = fmt.Errorf("parse ini conf file error: %v", err) err = fmt.Errorf("parse ini conf file error: %v", err)
return nil, err return ServerCommonConf{}, err
} }
var ( var (

View File

@ -21,7 +21,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/fatedier/frp/g"
"github.com/fatedier/frp/models/config" "github.com/fatedier/frp/models/config"
"github.com/fatedier/frp/models/consts" "github.com/fatedier/frp/models/consts"
frpErr "github.com/fatedier/frp/models/errors" frpErr "github.com/fatedier/frp/models/errors"
@ -129,10 +128,14 @@ type Control struct {
allShutdown *shutdown.Shutdown allShutdown *shutdown.Shutdown
mu sync.RWMutex mu sync.RWMutex
// Server configuration information
serverCfg config.ServerCommonConf
} }
func NewControl(rc *controller.ResourceController, pxyManager *proxy.ProxyManager, func NewControl(rc *controller.ResourceController, pxyManager *proxy.ProxyManager,
statsCollector stats.Collector, ctlConn net.Conn, loginMsg *msg.Login) *Control { statsCollector stats.Collector, ctlConn net.Conn, loginMsg *msg.Login,
serverCfg config.ServerCommonConf) *Control {
return &Control{ return &Control{
rc: rc, rc: rc,
@ -153,6 +156,7 @@ func NewControl(rc *controller.ResourceController, pxyManager *proxy.ProxyManage
writerShutdown: shutdown.New(), writerShutdown: shutdown.New(),
managerShutdown: shutdown.New(), managerShutdown: shutdown.New(),
allShutdown: shutdown.New(), allShutdown: shutdown.New(),
serverCfg: serverCfg,
} }
} }
@ -161,7 +165,7 @@ func (ctl *Control) Start() {
loginRespMsg := &msg.LoginResp{ loginRespMsg := &msg.LoginResp{
Version: version.Full(), Version: version.Full(),
RunId: ctl.runId, RunId: ctl.runId,
ServerUdpPort: g.GlbServerCfg.BindUdpPort, ServerUdpPort: ctl.serverCfg.BindUdpPort,
Error: "", Error: "",
} }
msg.WriteMsg(ctl.conn, loginRespMsg) msg.WriteMsg(ctl.conn, loginRespMsg)
@ -232,7 +236,7 @@ func (ctl *Control) GetWorkConn() (workConn net.Conn, err error) {
return return
} }
case <-time.After(time.Duration(g.GlbServerCfg.UserConnTimeout) * time.Second): case <-time.After(time.Duration(ctl.serverCfg.UserConnTimeout) * time.Second):
err = fmt.Errorf("timeout trying to get work connection") err = fmt.Errorf("timeout trying to get work connection")
ctl.conn.Warn("%v", err) ctl.conn.Warn("%v", err)
return return
@ -263,7 +267,7 @@ func (ctl *Control) writer() {
defer ctl.allShutdown.Start() defer ctl.allShutdown.Start()
defer ctl.writerShutdown.Done() defer ctl.writerShutdown.Done()
encWriter, err := crypto.NewWriter(ctl.conn, []byte(g.GlbServerCfg.Token)) encWriter, err := crypto.NewWriter(ctl.conn, []byte(ctl.serverCfg.Token))
if err != nil { if err != nil {
ctl.conn.Error("crypto new writer error: %v", err) ctl.conn.Error("crypto new writer error: %v", err)
ctl.allShutdown.Start() ctl.allShutdown.Start()
@ -293,7 +297,7 @@ func (ctl *Control) reader() {
defer ctl.allShutdown.Start() defer ctl.allShutdown.Start()
defer ctl.readerShutdown.Done() defer ctl.readerShutdown.Done()
encReader := crypto.NewReader(ctl.conn, []byte(g.GlbServerCfg.Token)) encReader := crypto.NewReader(ctl.conn, []byte(ctl.serverCfg.Token))
for { for {
if m, err := msg.ReadMsg(encReader); err != nil { if m, err := msg.ReadMsg(encReader); err != nil {
if err == io.EOF { if err == io.EOF {
@ -374,7 +378,7 @@ func (ctl *Control) manager() {
for { for {
select { select {
case <-heartbeat.C: case <-heartbeat.C:
if time.Since(ctl.lastPing) > time.Duration(g.GlbServerCfg.HeartBeatTimeout)*time.Second { if time.Since(ctl.lastPing) > time.Duration(ctl.serverCfg.HeartBeatTimeout)*time.Second {
ctl.conn.Warn("heartbeat timeout") ctl.conn.Warn("heartbeat timeout")
return return
} }
@ -417,22 +421,22 @@ func (ctl *Control) manager() {
func (ctl *Control) RegisterProxy(pxyMsg *msg.NewProxy) (remoteAddr string, err error) { func (ctl *Control) RegisterProxy(pxyMsg *msg.NewProxy) (remoteAddr string, err error) {
var pxyConf config.ProxyConf var pxyConf config.ProxyConf
// Load configures from NewProxy message and check. // Load configures from NewProxy message and check.
pxyConf, err = config.NewProxyConfFromMsg(pxyMsg) pxyConf, err = config.NewProxyConfFromMsg(pxyMsg, ctl.serverCfg)
if err != nil { if err != nil {
return return
} }
// NewProxy will return a interface Proxy. // NewProxy will return a interface Proxy.
// In fact it create different proxies by different proxy type, we just call run() here. // In fact it create different proxies by different proxy type, we just call run() here.
pxy, err := proxy.NewProxy(ctl.runId, ctl.rc, ctl.statsCollector, ctl.poolCount, ctl.GetWorkConn, pxyConf) pxy, err := proxy.NewProxy(ctl.runId, ctl.rc, ctl.statsCollector, ctl.poolCount, ctl.GetWorkConn, pxyConf, ctl.serverCfg)
if err != nil { if err != nil {
return remoteAddr, err return remoteAddr, err
} }
// Check ports used number in each client // Check ports used number in each client
if g.GlbServerCfg.MaxPortsPerClient > 0 { if ctl.serverCfg.MaxPortsPerClient > 0 {
ctl.mu.Lock() ctl.mu.Lock()
if ctl.portsUsedNum+pxy.GetUsedPortsNum() > int(g.GlbServerCfg.MaxPortsPerClient) { if ctl.portsUsedNum+pxy.GetUsedPortsNum() > int(ctl.serverCfg.MaxPortsPerClient) {
ctl.mu.Unlock() ctl.mu.Unlock()
err = fmt.Errorf("exceed the max_ports_per_client") err = fmt.Errorf("exceed the max_ports_per_client")
return return
@ -478,7 +482,7 @@ func (ctl *Control) CloseProxy(closeMsg *msg.CloseProxy) (err error) {
return return
} }
if g.GlbServerCfg.MaxPortsPerClient > 0 { if ctl.serverCfg.MaxPortsPerClient > 0 {
ctl.portsUsedNum = ctl.portsUsedNum - pxy.GetUsedPortsNum() ctl.portsUsedNum = ctl.portsUsedNum - pxy.GetUsedPortsNum()
} }
pxy.Close() pxy.Close()

View File

@ -21,7 +21,6 @@ import (
"time" "time"
"github.com/fatedier/frp/assets" "github.com/fatedier/frp/assets"
"github.com/fatedier/frp/g"
frpNet "github.com/fatedier/frp/utils/net" frpNet "github.com/fatedier/frp/utils/net"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -36,7 +35,7 @@ func (svr *Service) RunDashboardServer(addr string, port int) (err error) {
// url router // url router
router := mux.NewRouter() router := mux.NewRouter()
user, passwd := g.GlbServerCfg.DashboardUser, g.GlbServerCfg.DashboardPwd user, passwd := svr.cfg.DashboardUser, svr.cfg.DashboardPwd
router.Use(frpNet.NewHttpAuthMiddleware(user, passwd).Middleware) router.Use(frpNet.NewHttpAuthMiddleware(user, passwd).Middleware)
// api, see dashboard_api.go // api, see dashboard_api.go

View File

@ -18,7 +18,6 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"github.com/fatedier/frp/g"
"github.com/fatedier/frp/models/config" "github.com/fatedier/frp/models/config"
"github.com/fatedier/frp/models/consts" "github.com/fatedier/frp/models/consts"
"github.com/fatedier/frp/utils/log" "github.com/fatedier/frp/utils/log"
@ -63,19 +62,18 @@ func (svr *Service) ApiServerInfo(w http.ResponseWriter, r *http.Request) {
}() }()
log.Info("Http request: [%s]", r.URL.Path) log.Info("Http request: [%s]", r.URL.Path)
cfg := &g.GlbServerCfg.ServerCommonConf
serverStats := svr.statsCollector.GetServer() serverStats := svr.statsCollector.GetServer()
svrResp := ServerInfoResp{ svrResp := ServerInfoResp{
Version: version.Full(), Version: version.Full(),
BindPort: cfg.BindPort, BindPort: svr.cfg.BindPort,
BindUdpPort: cfg.BindUdpPort, BindUdpPort: svr.cfg.BindUdpPort,
VhostHttpPort: cfg.VhostHttpPort, VhostHttpPort: svr.cfg.VhostHttpPort,
VhostHttpsPort: cfg.VhostHttpsPort, VhostHttpsPort: svr.cfg.VhostHttpsPort,
KcpBindPort: cfg.KcpBindPort, KcpBindPort: svr.cfg.KcpBindPort,
SubdomainHost: cfg.SubDomainHost, SubdomainHost: svr.cfg.SubDomainHost,
MaxPoolCount: cfg.MaxPoolCount, MaxPoolCount: svr.cfg.MaxPoolCount,
MaxPortsPerClient: cfg.MaxPortsPerClient, MaxPortsPerClient: svr.cfg.MaxPortsPerClient,
HeartBeatTimeout: cfg.HeartBeatTimeout, HeartBeatTimeout: svr.cfg.HeartBeatTimeout,
TotalTrafficIn: serverStats.TotalTrafficIn, TotalTrafficIn: serverStats.TotalTrafficIn,
TotalTrafficOut: serverStats.TotalTrafficOut, TotalTrafficOut: serverStats.TotalTrafficOut,

View File

@ -19,7 +19,6 @@ import (
"net" "net"
"strings" "strings"
"github.com/fatedier/frp/g"
"github.com/fatedier/frp/models/config" "github.com/fatedier/frp/models/config"
"github.com/fatedier/frp/server/stats" "github.com/fatedier/frp/server/stats"
frpNet "github.com/fatedier/frp/utils/net" frpNet "github.com/fatedier/frp/utils/net"
@ -88,13 +87,13 @@ func (pxy *HttpProxy) Run() (remoteAddr string, err error) {
pxy.rc.HttpReverseProxy.UnRegister(tmpDomain, tmpLocation) pxy.rc.HttpReverseProxy.UnRegister(tmpDomain, tmpLocation)
}) })
} }
addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, int(g.GlbServerCfg.VhostHttpPort))) addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, int(pxy.serverCfg.VhostHttpPort)))
pxy.Info("http proxy listen for host [%s] location [%s] group [%s]", routeConfig.Domain, routeConfig.Location, pxy.cfg.Group) pxy.Info("http proxy listen for host [%s] location [%s] group [%s]", routeConfig.Domain, routeConfig.Location, pxy.cfg.Group)
} }
} }
if pxy.cfg.SubDomain != "" { if pxy.cfg.SubDomain != "" {
routeConfig.Domain = pxy.cfg.SubDomain + "." + g.GlbServerCfg.SubDomainHost routeConfig.Domain = pxy.cfg.SubDomain + "." + pxy.serverCfg.SubDomainHost
for _, location := range locations { for _, location := range locations {
routeConfig.Location = location routeConfig.Location = location
tmpDomain := routeConfig.Domain tmpDomain := routeConfig.Domain
@ -119,7 +118,7 @@ func (pxy *HttpProxy) Run() (remoteAddr string, err error) {
pxy.rc.HttpReverseProxy.UnRegister(tmpDomain, tmpLocation) pxy.rc.HttpReverseProxy.UnRegister(tmpDomain, tmpLocation)
}) })
} }
addrs = append(addrs, util.CanonicalAddr(tmpDomain, g.GlbServerCfg.VhostHttpPort)) addrs = append(addrs, util.CanonicalAddr(tmpDomain, pxy.serverCfg.VhostHttpPort))
pxy.Info("http proxy listen for host [%s] location [%s] group [%s]", routeConfig.Domain, routeConfig.Location, pxy.cfg.Group) pxy.Info("http proxy listen for host [%s] location [%s] group [%s]", routeConfig.Domain, routeConfig.Location, pxy.cfg.Group)
} }
@ -147,7 +146,7 @@ func (pxy *HttpProxy) GetRealConn(remoteAddr string) (workConn frpNet.Conn, err
var rwc io.ReadWriteCloser = tmpConn var rwc io.ReadWriteCloser = tmpConn
if pxy.cfg.UseEncryption { if pxy.cfg.UseEncryption {
rwc, err = frpIo.WithEncryption(rwc, []byte(g.GlbServerCfg.Token)) rwc, err = frpIo.WithEncryption(rwc, []byte(pxy.serverCfg.Token))
if err != nil { if err != nil {
pxy.Error("create encryption stream error: %v", err) pxy.Error("create encryption stream error: %v", err)
return return

View File

@ -17,7 +17,6 @@ package proxy
import ( import (
"strings" "strings"
"github.com/fatedier/frp/g"
"github.com/fatedier/frp/models/config" "github.com/fatedier/frp/models/config"
"github.com/fatedier/frp/utils/util" "github.com/fatedier/frp/utils/util"
"github.com/fatedier/frp/utils/vhost" "github.com/fatedier/frp/utils/vhost"
@ -51,11 +50,11 @@ func (pxy *HttpsProxy) Run() (remoteAddr string, err error) {
l.AddLogPrefix(pxy.name) l.AddLogPrefix(pxy.name)
pxy.Info("https proxy listen for host [%s]", routeConfig.Domain) pxy.Info("https proxy listen for host [%s]", routeConfig.Domain)
pxy.listeners = append(pxy.listeners, l) pxy.listeners = append(pxy.listeners, l)
addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, g.GlbServerCfg.VhostHttpsPort)) addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, pxy.serverCfg.VhostHttpsPort))
} }
if pxy.cfg.SubDomain != "" { if pxy.cfg.SubDomain != "" {
routeConfig.Domain = pxy.cfg.SubDomain + "." + g.GlbServerCfg.SubDomainHost routeConfig.Domain = pxy.cfg.SubDomain + "." + pxy.serverCfg.SubDomainHost
l, errRet := pxy.rc.VhostHttpsMuxer.Listen(routeConfig) l, errRet := pxy.rc.VhostHttpsMuxer.Listen(routeConfig)
if errRet != nil { if errRet != nil {
err = errRet err = errRet
@ -64,7 +63,7 @@ func (pxy *HttpsProxy) Run() (remoteAddr string, err error) {
l.AddLogPrefix(pxy.name) l.AddLogPrefix(pxy.name)
pxy.Info("https proxy listen for host [%s]", routeConfig.Domain) pxy.Info("https proxy listen for host [%s]", routeConfig.Domain)
pxy.listeners = append(pxy.listeners, l) pxy.listeners = append(pxy.listeners, l)
addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, int(g.GlbServerCfg.VhostHttpsPort))) addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, int(pxy.serverCfg.VhostHttpsPort)))
} }
pxy.startListenHandler(pxy, HandleUserTcpConnection) pxy.startListenHandler(pxy, HandleUserTcpConnection)

View File

@ -21,7 +21,6 @@ import (
"strconv" "strconv"
"sync" "sync"
"github.com/fatedier/frp/g"
"github.com/fatedier/frp/models/config" "github.com/fatedier/frp/models/config"
"github.com/fatedier/frp/models/msg" "github.com/fatedier/frp/models/msg"
"github.com/fatedier/frp/server/controller" "github.com/fatedier/frp/server/controller"
@ -52,6 +51,7 @@ type BaseProxy struct {
usedPortsNum int usedPortsNum int
poolCount int poolCount int
getWorkConnFn GetWorkConnFn getWorkConnFn GetWorkConnFn
serverCfg config.ServerCommonConf
mu sync.RWMutex mu sync.RWMutex
log.Logger log.Logger
@ -126,7 +126,7 @@ func (pxy *BaseProxy) GetWorkConnFromPool(src, dst net.Addr) (workConn frpNet.Co
// startListenHandler start a goroutine handler for each listener. // startListenHandler start a goroutine handler for each listener.
// p: p will just be passed to handler(Proxy, frpNet.Conn). // p: p will just be passed to handler(Proxy, frpNet.Conn).
// handler: each proxy type can set different handler function to deal with connections accepted from listeners. // handler: each proxy type can set different handler function to deal with connections accepted from listeners.
func (pxy *BaseProxy) startListenHandler(p Proxy, handler func(Proxy, frpNet.Conn, stats.Collector)) { func (pxy *BaseProxy) startListenHandler(p Proxy, handler func(Proxy, frpNet.Conn, stats.Collector, config.ServerCommonConf)) {
for _, listener := range pxy.listeners { for _, listener := range pxy.listeners {
go func(l frpNet.Listener) { go func(l frpNet.Listener) {
for { for {
@ -138,14 +138,14 @@ func (pxy *BaseProxy) startListenHandler(p Proxy, handler func(Proxy, frpNet.Con
return return
} }
pxy.Debug("get a user connection [%s]", c.RemoteAddr().String()) pxy.Debug("get a user connection [%s]", c.RemoteAddr().String())
go handler(p, c, pxy.statsCollector) go handler(p, c, pxy.statsCollector, pxy.serverCfg)
} }
}(listener) }(listener)
} }
} }
func NewProxy(runId string, rc *controller.ResourceController, statsCollector stats.Collector, poolCount int, func NewProxy(runId string, rc *controller.ResourceController, statsCollector stats.Collector, poolCount int,
getWorkConnFn GetWorkConnFn, pxyConf config.ProxyConf) (pxy Proxy, err error) { getWorkConnFn GetWorkConnFn, pxyConf config.ProxyConf, serverCfg config.ServerCommonConf) (pxy Proxy, err error) {
basePxy := BaseProxy{ basePxy := BaseProxy{
name: pxyConf.GetBaseInfo().ProxyName, name: pxyConf.GetBaseInfo().ProxyName,
@ -155,6 +155,7 @@ func NewProxy(runId string, rc *controller.ResourceController, statsCollector st
poolCount: poolCount, poolCount: poolCount,
getWorkConnFn: getWorkConnFn, getWorkConnFn: getWorkConnFn,
Logger: log.NewPrefixLogger(runId), Logger: log.NewPrefixLogger(runId),
serverCfg: serverCfg,
} }
switch cfg := pxyConf.(type) { switch cfg := pxyConf.(type) {
case *config.TcpProxyConf: case *config.TcpProxyConf:
@ -198,7 +199,7 @@ func NewProxy(runId string, rc *controller.ResourceController, statsCollector st
// HandleUserTcpConnection is used for incoming tcp user connections. // HandleUserTcpConnection is used for incoming tcp user connections.
// It can be used for tcp, http, https type. // It can be used for tcp, http, https type.
func HandleUserTcpConnection(pxy Proxy, userConn frpNet.Conn, statsCollector stats.Collector) { func HandleUserTcpConnection(pxy Proxy, userConn frpNet.Conn, statsCollector stats.Collector, serverCfg config.ServerCommonConf) {
defer userConn.Close() defer userConn.Close()
// try all connections from the pool // try all connections from the pool
@ -211,7 +212,7 @@ func HandleUserTcpConnection(pxy Proxy, userConn frpNet.Conn, statsCollector sta
var local io.ReadWriteCloser = workConn var local io.ReadWriteCloser = workConn
cfg := pxy.GetConf().GetBaseInfo() cfg := pxy.GetConf().GetBaseInfo()
if cfg.UseEncryption { if cfg.UseEncryption {
local, err = frpIo.WithEncryption(local, []byte(g.GlbServerCfg.Token)) local, err = frpIo.WithEncryption(local, []byte(serverCfg.Token))
if err != nil { if err != nil {
pxy.Error("create encryption stream error: %v", err) pxy.Error("create encryption stream error: %v", err)
return return

View File

@ -17,7 +17,6 @@ package proxy
import ( import (
"fmt" "fmt"
"github.com/fatedier/frp/g"
"github.com/fatedier/frp/models/config" "github.com/fatedier/frp/models/config"
frpNet "github.com/fatedier/frp/utils/net" frpNet "github.com/fatedier/frp/utils/net"
) )
@ -31,7 +30,7 @@ type TcpProxy struct {
func (pxy *TcpProxy) Run() (remoteAddr string, err error) { func (pxy *TcpProxy) Run() (remoteAddr string, err error) {
if pxy.cfg.Group != "" { if pxy.cfg.Group != "" {
l, realPort, errRet := pxy.rc.TcpGroupCtl.Listen(pxy.name, pxy.cfg.Group, pxy.cfg.GroupKey, g.GlbServerCfg.ProxyBindAddr, pxy.cfg.RemotePort) l, realPort, errRet := pxy.rc.TcpGroupCtl.Listen(pxy.name, pxy.cfg.Group, pxy.cfg.GroupKey, pxy.serverCfg.ProxyBindAddr, pxy.cfg.RemotePort)
if errRet != nil { if errRet != nil {
err = errRet err = errRet
return return
@ -56,7 +55,7 @@ func (pxy *TcpProxy) Run() (remoteAddr string, err error) {
pxy.rc.TcpPortManager.Release(pxy.realPort) pxy.rc.TcpPortManager.Release(pxy.realPort)
} }
}() }()
listener, errRet := frpNet.ListenTcp(g.GlbServerCfg.ProxyBindAddr, pxy.realPort) listener, errRet := frpNet.ListenTcp(pxy.serverCfg.ProxyBindAddr, pxy.realPort)
if errRet != nil { if errRet != nil {
err = errRet err = errRet
return return

View File

@ -20,7 +20,6 @@ import (
"net" "net"
"time" "time"
"github.com/fatedier/frp/g"
"github.com/fatedier/frp/models/config" "github.com/fatedier/frp/models/config"
"github.com/fatedier/frp/models/msg" "github.com/fatedier/frp/models/msg"
"github.com/fatedier/frp/models/proto/udp" "github.com/fatedier/frp/models/proto/udp"
@ -67,7 +66,7 @@ func (pxy *UdpProxy) Run() (remoteAddr string, err error) {
remoteAddr = fmt.Sprintf(":%d", pxy.realPort) remoteAddr = fmt.Sprintf(":%d", pxy.realPort)
pxy.cfg.RemotePort = pxy.realPort pxy.cfg.RemotePort = pxy.realPort
addr, errRet := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", g.GlbServerCfg.ProxyBindAddr, pxy.realPort)) addr, errRet := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", pxy.serverCfg.ProxyBindAddr, pxy.realPort))
if errRet != nil { if errRet != nil {
err = errRet err = errRet
return return

View File

@ -29,7 +29,7 @@ import (
"time" "time"
"github.com/fatedier/frp/assets" "github.com/fatedier/frp/assets"
"github.com/fatedier/frp/g" "github.com/fatedier/frp/models/config"
"github.com/fatedier/frp/models/msg" "github.com/fatedier/frp/models/msg"
"github.com/fatedier/frp/models/nathole" "github.com/fatedier/frp/models/nathole"
"github.com/fatedier/frp/server/controller" "github.com/fatedier/frp/server/controller"
@ -86,10 +86,11 @@ type Service struct {
statsCollector stats.Collector statsCollector stats.Collector
tlsConfig *tls.Config tlsConfig *tls.Config
cfg config.ServerCommonConf
} }
func NewService() (svr *Service, err error) { func NewService(cfg config.ServerCommonConf) (svr *Service, err error) {
cfg := &g.GlbServerCfg.ServerCommonConf
svr = &Service{ svr = &Service{
ctlManager: NewControlManager(), ctlManager: NewControlManager(),
pxyManager: proxy.NewProxyManager(), pxyManager: proxy.NewProxyManager(),
@ -100,6 +101,7 @@ func NewService() (svr *Service, err error) {
}, },
httpVhostRouter: vhost.NewVhostRouters(), httpVhostRouter: vhost.NewVhostRouters(),
tlsConfig: generateTLSConfig(), tlsConfig: generateTLSConfig(),
cfg: cfg,
} }
// Init group controller // Init group controller
@ -248,7 +250,7 @@ func (svr *Service) Run() {
if svr.rc.NatHoleController != nil { if svr.rc.NatHoleController != nil {
go svr.rc.NatHoleController.Run() go svr.rc.NatHoleController.Run()
} }
if g.GlbServerCfg.KcpBindPort > 0 { if svr.cfg.KcpBindPort > 0 {
go svr.HandleListener(svr.kcpListener) go svr.HandleListener(svr.kcpListener)
} }
@ -324,7 +326,7 @@ func (svr *Service) HandleListener(l frpNet.Listener) {
} }
} }
if g.GlbServerCfg.TcpMux { if svr.cfg.TcpMux {
fmuxCfg := fmux.DefaultConfig() fmuxCfg := fmux.DefaultConfig()
fmuxCfg.KeepAliveInterval = 20 * time.Second fmuxCfg.KeepAliveInterval = 20 * time.Second
fmuxCfg.LogOutput = ioutil.Discard fmuxCfg.LogOutput = ioutil.Discard
@ -363,7 +365,7 @@ func (svr *Service) RegisterControl(ctlConn frpNet.Conn, loginMsg *msg.Login) (e
} }
// Check auth. // Check auth.
if util.GetAuthKey(g.GlbServerCfg.Token, loginMsg.Timestamp) != loginMsg.PrivilegeKey { if util.GetAuthKey(svr.cfg.Token, loginMsg.Timestamp) != loginMsg.PrivilegeKey {
err = fmt.Errorf("authorization failed") err = fmt.Errorf("authorization failed")
return return
} }
@ -377,7 +379,7 @@ func (svr *Service) RegisterControl(ctlConn frpNet.Conn, loginMsg *msg.Login) (e
} }
} }
ctl := NewControl(svr.rc, svr.pxyManager, svr.statsCollector, ctlConn, loginMsg) ctl := NewControl(svr.rc, svr.pxyManager, svr.statsCollector, ctlConn, loginMsg, svr.cfg)
if oldCtl := svr.ctlManager.Add(loginMsg.RunId, ctl); oldCtl != nil { if oldCtl := svr.ctlManager.Add(loginMsg.RunId, ctl); oldCtl != nil {
oldCtl.allShutdown.WaitDone() oldCtl.allShutdown.WaitDone()