Merge pull request #1396 from velovix/issue-1387_server-conf-as-argument

Pass server configuration as an argument
This commit is contained in:
fatedier
2019-08-21 00:30:01 +08:00
committed by GitHub
13 changed files with 115 additions and 149 deletions

View File

@@ -29,7 +29,7 @@ import (
"time"
"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/nathole"
"github.com/fatedier/frp/server/controller"
@@ -51,8 +51,6 @@ const (
connReadTimeout time.Duration = 10 * time.Second
)
var ServerService *Service
// Server service
type Service struct {
// Dispatch connections to different handlers listen on same port
@@ -86,10 +84,11 @@ type Service struct {
statsCollector stats.Collector
tlsConfig *tls.Config
cfg config.ServerCommonConf
}
func NewService() (svr *Service, err error) {
cfg := &g.GlbServerCfg.ServerCommonConf
func NewService(cfg config.ServerCommonConf) (svr *Service, err error) {
svr = &Service{
ctlManager: NewControlManager(),
pxyManager: proxy.NewProxyManager(),
@@ -100,6 +99,7 @@ func NewService() (svr *Service, err error) {
},
httpVhostRouter: vhost.NewVhostRouters(),
tlsConfig: generateTLSConfig(),
cfg: cfg,
}
// Init group controller
@@ -248,7 +248,7 @@ func (svr *Service) Run() {
if svr.rc.NatHoleController != nil {
go svr.rc.NatHoleController.Run()
}
if g.GlbServerCfg.KcpBindPort > 0 {
if svr.cfg.KcpBindPort > 0 {
go svr.HandleListener(svr.kcpListener)
}
@@ -324,7 +324,7 @@ func (svr *Service) HandleListener(l frpNet.Listener) {
}
}
if g.GlbServerCfg.TcpMux {
if svr.cfg.TcpMux {
fmuxCfg := fmux.DefaultConfig()
fmuxCfg.KeepAliveInterval = 20 * time.Second
fmuxCfg.LogOutput = ioutil.Discard
@@ -363,7 +363,7 @@ func (svr *Service) RegisterControl(ctlConn frpNet.Conn, loginMsg *msg.Login) (e
}
// 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")
return
}
@@ -377,7 +377,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 {
oldCtl.allShutdown.WaitDone()