From 70fb6d396dd7a6c3838b4453d4b732193e61dbfc Mon Sep 17 00:00:00 2001 From: Dzyanis Kuzmenka Date: Thu, 17 Mar 2022 22:54:04 +0300 Subject: [PATCH] fix gocritic linter warnings --- client/proxy/proxy.go | 4 ++-- client/proxy/proxy_manager.go | 6 ++---- client/service.go | 2 +- client/visitor_manager.go | 6 ++---- pkg/auth/oidc.go | 2 +- pkg/config/client.go | 2 +- pkg/util/net/tls.go | 17 ++++++++++------- pkg/util/util/util.go | 9 +++++---- server/control.go | 6 +++--- 9 files changed, 27 insertions(+), 27 deletions(-) diff --git a/client/proxy/proxy.go b/client/proxy/proxy.go index 0fdececd..7f561df0 100644 --- a/client/proxy/proxy.go +++ b/client/proxy/proxy.go @@ -425,8 +425,8 @@ func (pxy *XTCPProxy) sendDetectMsg(addr string, port int, laddr *net.UDPAddr, c return err } - //uConn := ipv4.NewConn(tConn) - //uConn.SetTTL(3) + // uConn := ipv4.NewConn(tConn) + // uConn.SetTTL(3) tConn.Write(content) tConn.Close() diff --git a/client/proxy/proxy_manager.go b/client/proxy/proxy_manager.go index 98c17faa..c4585946 100644 --- a/client/proxy/proxy_manager.go +++ b/client/proxy/proxy_manager.go @@ -113,10 +113,8 @@ func (pm *Manager) Reload(pxyCfgs map[string]config.ProxyConf) { cfg, ok := pxyCfgs[name] if !ok { del = true - } else { - if !pxy.Cfg.Compare(cfg) { - del = true - } + } else if !pxy.Cfg.Compare(cfg) { + del = true } if del { diff --git a/client/service.go b/client/service.go index 96ddadfd..83a4c9dc 100644 --- a/client/service.go +++ b/client/service.go @@ -181,7 +181,7 @@ func (svr *Service) keepControllerWorking() { xl.Warn("reconnect to server error: %v, wait %v for another retry", err, delayTime) util.RandomSleep(delayTime, 0.9, 1.1) - delayTime = delayTime * 2 + delayTime *= 2 if delayTime > maxDelayTime { delayTime = maxDelayTime } diff --git a/client/visitor_manager.go b/client/visitor_manager.go index 642b21e8..1206d829 100644 --- a/client/visitor_manager.go +++ b/client/visitor_manager.go @@ -99,10 +99,8 @@ func (vm *VisitorManager) Reload(cfgs map[string]config.VisitorConf) { cfg, ok := cfgs[name] if !ok { del = true - } else { - if !oldCfg.Compare(cfg) { - del = true - } + } else if !oldCfg.Compare(cfg) { + del = true } if del { diff --git a/pkg/auth/oidc.go b/pkg/auth/oidc.go index 8a9f3404..ff8e54cc 100644 --- a/pkg/auth/oidc.go +++ b/pkg/auth/oidc.go @@ -34,7 +34,7 @@ type OidcClientConfig struct { // is "". OidcClientSecret string `ini:"oidc_client_secret" json:"oidc_client_secret"` // OidcAudience specifies the audience of the token in OIDC authentication - //if AuthenticationMethod == "oidc". By default, this value is "". + // if AuthenticationMethod == "oidc". By default, this value is "". OidcAudience string `ini:"oidc_audience" json:"oidc_audience"` // OidcTokenEndpointURL specifies the URL which implements OIDC Token Endpoint. // It will be used to get an OIDC token if AuthenticationMethod == "oidc". diff --git a/pkg/config/client.go b/pkg/config/client.go index ce96255b..f73ea386 100644 --- a/pkg/config/client.go +++ b/pkg/config/client.go @@ -110,7 +110,7 @@ type ClientCommonConf struct { // all supplied proxies are enabled. By default, this value is an empty // set. Start []string `ini:"start" json:"start"` - //Start map[string]struct{} `json:"start"` + // Start map[string]struct{} `json:"start"` // Protocol specifies the protocol to use when interacting with the server. // Valid values are "tcp", "kcp" and "websocket". By default, this value // is "tcp". diff --git a/pkg/util/net/tls.go b/pkg/util/net/tls.go index 1bae196a..132dd100 100644 --- a/pkg/util/net/tls.go +++ b/pkg/util/net/tls.go @@ -41,13 +41,16 @@ func CheckAndEnableTLSServerConnWithTimeout( return } - if n == 1 && int(buf[0]) == FRPTLSHeadByte { - out = tls.Server(c, tlsConfig) - isTLS = true - custom = true - } else if n == 1 && int(buf[0]) == 0x16 { - out = tls.Server(sc, tlsConfig) - isTLS = true + if n == 1 { + switch int(buf[0]) { + case FRPTLSHeadByte: + out = tls.Server(c, tlsConfig) + isTLS = true + custom = true + case 0x16: + out = tls.Server(sc, tlsConfig) + isTLS = true + } } else { if tlsOnly { err = fmt.Errorf("non-TLS connection received on a TlsOnly server") diff --git a/pkg/util/util/util.go b/pkg/util/util/util.go index 032675fb..b13f9a77 100644 --- a/pkg/util/util/util.go +++ b/pkg/util/util/util.go @@ -44,7 +44,7 @@ func RandIDWithLen(idLen int) (id string, err error) { } func GetAuthKey(token string, timestamp int64) (key string) { - token = token + fmt.Sprintf("%d", timestamp) + token += fmt.Sprintf("%d", timestamp) md5Ctx := md5.New() md5Ctx.Write([]byte(token)) data := md5Ctx.Sum(nil) @@ -70,7 +70,8 @@ func ParseRangeNumbers(rangeStr string) (numbers []int64, err error) { numArray := strings.Split(numRangeStr, "-") // length: only 1 or 2 is correct rangeType := len(numArray) - if rangeType == 1 { + switch rangeType { + case 1: // single number singleNum, errRet := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64) if errRet != nil { @@ -78,7 +79,7 @@ func ParseRangeNumbers(rangeStr string) (numbers []int64, err error) { return } numbers = append(numbers, singleNum) - } else if rangeType == 2 { + case 2: // range numbers min, errRet := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64) if errRet != nil { @@ -97,7 +98,7 @@ func ParseRangeNumbers(rangeStr string) (numbers []int64, err error) { for i := min; i <= max; i++ { numbers = append(numbers, i) } - } else { + default: err = fmt.Errorf("range number is invalid") return } diff --git a/server/control.go b/server/control.go index 09740611..321e0f4c 100644 --- a/server/control.go +++ b/server/control.go @@ -528,13 +528,13 @@ func (ctl *Control) RegisterProxy(pxyMsg *msg.NewProxy) (remoteAddr string, err err = fmt.Errorf("exceed the max_ports_per_client") return } - ctl.portsUsedNum = ctl.portsUsedNum + pxy.GetUsedPortsNum() + ctl.portsUsedNum += pxy.GetUsedPortsNum() ctl.mu.Unlock() defer func() { if err != nil { ctl.mu.Lock() - ctl.portsUsedNum = ctl.portsUsedNum - pxy.GetUsedPortsNum() + ctl.portsUsedNum -= pxy.GetUsedPortsNum() ctl.mu.Unlock() } }() @@ -570,7 +570,7 @@ func (ctl *Control) CloseProxy(closeMsg *msg.CloseProxy) (err error) { } if ctl.serverCfg.MaxPortsPerClient > 0 { - ctl.portsUsedNum = ctl.portsUsedNum - pxy.GetUsedPortsNum() + ctl.portsUsedNum -= pxy.GetUsedPortsNum() } pxy.Close() ctl.pxyManager.Del(pxy.GetName())