From 98fa3855bd3b05cbd95c5a6e11bf70917628e1fa Mon Sep 17 00:00:00 2001 From: Guy Lewin Date: Tue, 11 Feb 2020 16:57:38 +0200 Subject: [PATCH] CR: export error string generation to a function --- models/config/server_common.go | 2 +- server/control.go | 7 ++----- server/service.go | 12 ++---------- utils/util/util.go | 8 ++++++++ 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/models/config/server_common.go b/models/config/server_common.go index 6ad2688d..20e92f9c 100644 --- a/models/config/server_common.go +++ b/models/config/server_common.go @@ -174,7 +174,7 @@ func GetDefaultServerConf() ServerCommonConf { AllowPorts: make(map[int]struct{}), MaxPoolCount: 5, MaxPortsPerClient: 0, - TlsOnly: false, + TlsOnly: false, HeartBeatTimeout: 90, UserConnTimeout: 10, Custom404Page: "", diff --git a/server/control.go b/server/control.go index bb06ccf6..4b9227ab 100644 --- a/server/control.go +++ b/server/control.go @@ -31,6 +31,7 @@ import ( "github.com/fatedier/frp/server/controller" "github.com/fatedier/frp/server/proxy" "github.com/fatedier/frp/server/stats" + "github.com/fatedier/frp/utils/util" "github.com/fatedier/frp/utils/version" "github.com/fatedier/frp/utils/xlog" @@ -438,12 +439,8 @@ func (ctl *Control) manager() { ProxyName: m.ProxyName, } if err != nil { - if ctl.serverCfg.DetailedErrorsToClient { - resp.Error = err.Error() - } else { - resp.Error = fmt.Sprintf("new proxy [%s] error", m.ProxyName) - } xl.Warn("new proxy [%s] error: %v", m.ProxyName, err) + resp.Error = util.GenerateResponseErrorString(fmt.Sprintf("new proxy [%s] error", m.ProxyName), err, ctl.serverCfg.DetailedErrorsToClient) } else { resp.RemoteAddr = remoteAddr xl.Info("new proxy [%s] success", m.ProxyName) diff --git a/server/service.go b/server/service.go index 50ebe0fc..1ad7e281 100644 --- a/server/service.go +++ b/server/service.go @@ -320,13 +320,9 @@ func (svr *Service) HandleListener(l net.Listener) { // Otherwise send success message in control's work goroutine. if err != nil { xl.Warn("register control error: %v", err) - errStr := "register control error" - if svr.cfg.DetailedErrorsToClient { - errStr = err.Error() - } msg.WriteMsg(conn, &msg.LoginResp{ Version: version.Full(), - Error: errStr, + Error: util.GenerateResponseErrorString("register control error", err, svr.cfg.DetailedErrorsToClient), }) conn.Close() } @@ -335,13 +331,9 @@ func (svr *Service) HandleListener(l net.Listener) { case *msg.NewVisitorConn: if err = svr.RegisterVisitorConn(conn, m); err != nil { xl.Warn("register visitor conn error: %v", err) - errStr := "register visitor conn error" - if svr.cfg.DetailedErrorsToClient { - errStr = err.Error() - } msg.WriteMsg(conn, &msg.NewVisitorConnResp{ ProxyName: m.ProxyName, - Error: errStr, + Error: util.GenerateResponseErrorString("register visitor conn error", err, svr.cfg.DetailedErrorsToClient), }) conn.Close() } else { diff --git a/utils/util/util.go b/utils/util/util.go index 7ea4e83c..5b30d543 100644 --- a/utils/util/util.go +++ b/utils/util/util.go @@ -101,3 +101,11 @@ func ParseRangeNumbers(rangeStr string) (numbers []int64, err error) { } return } + +func GenerateResponseErrorString(summary string, err error, detailed bool) string { + if detailed { + return err.Error() + } else { + return summary + } +}