From b8e81240fd38c5409bfed73e3541a24d913f0a33 Mon Sep 17 00:00:00 2001 From: fclairamb Date: Sun, 3 Mar 2024 13:49:21 +0100 Subject: [PATCH] Moving some code to reduce cyclomatic complexity --- .golangci.yml | 6 +++++ client_handler.go | 62 ++++++++++++++++++++++++++--------------------- server.go | 27 +++++++++++++-------- 3 files changed, 58 insertions(+), 37 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index f5647385..9d2d0256 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -161,8 +161,14 @@ linters: disable-all: true enable: - asciicheck + - asasalint + - bidichk - bodyclose # - deadcode -> unused + - containedctx + # - contextcheck -> creates an odd error here: https://github.com/fclairamb/ftpserverlib/blob/4d7c663e9e0b2650673fc2e0fcdb443895f2a1b9/server.go#L234 + # - copyloopvar -> unknown in v1.56.2 ??? + - cyclop - depguard - dogsled - dupl diff --git a/client_handler.go b/client_handler.go index d3313295..48193b1c 100644 --- a/client_handler.go +++ b/client_handler.go @@ -410,47 +410,55 @@ func (c *clientHandler) HandleCommands() { } for { - if c.reader == nil { - if c.debug { - c.logger.Debug("Client disconnected", "clean", true) - } - + if c.readCommand() { return } + } +} - // florent(2018-01-14): #58: IDLE timeout: Preparing the deadline before we read - if c.server.settings.IdleTimeout > 0 { - if err := c.conn.SetDeadline( - time.Now().Add(time.Duration(time.Second.Nanoseconds() * int64(c.server.settings.IdleTimeout)))); err != nil { - c.logger.Error("Network error", "err", err) - } +func (c *clientHandler) readCommand() bool { + if c.reader == nil { + if c.debug { + c.logger.Debug("Client disconnected", "clean", true) } - lineSlice, isPrefix, err := c.reader.ReadLine() - - if isPrefix { - if c.debug { - c.logger.Warn("Received line too long, disconnecting client", - "size", len(lineSlice)) - } + return true + } - return + // florent(2018-01-14): #58: IDLE timeout: Preparing the deadline before we read + if c.server.settings.IdleTimeout > 0 { + if err := c.conn.SetDeadline( + time.Now().Add(time.Duration(time.Second.Nanoseconds() * int64(c.server.settings.IdleTimeout)))); err != nil { + c.logger.Error("Network error", "err", err) } + } - if err != nil { - c.handleCommandsStreamError(err) + lineSlice, isPrefix, err := c.reader.ReadLine() - return + if isPrefix { + if c.debug { + c.logger.Warn("Received line too long, disconnecting client", + "size", len(lineSlice)) } - line := string(lineSlice) + return true + } - if c.debug { - c.logger.Debug("Received line", "line", line) - } + if err != nil { + c.handleCommandsStreamError(err) - c.handleCommand(line) + return true } + + line := string(lineSlice) + + if c.debug { + c.logger.Debug("Received line", "line", line) + } + + c.handleCommand(line) + + return false } func (c *clientHandler) handleCommandsStreamError(err error) { diff --git a/server.go b/server.go index 0462b219..5e876adc 100644 --- a/server.go +++ b/server.go @@ -118,17 +118,10 @@ func (server *FtpServer) loadSettings() error { } if s.PublicHost != "" { - parsedIP := net.ParseIP(s.PublicHost) - if parsedIP == nil { - return &ipValidationError{error: fmt.Sprintf("invalid passive IP %#v", s.PublicHost)} - } - - parsedIP = parsedIP.To4() - if parsedIP == nil { - return &ipValidationError{error: fmt.Sprintf("invalid IPv4 passive IP %#v", s.PublicHost)} + s.PublicHost, err = parseIPv4(s.PublicHost) + if err != nil { + return err } - - s.PublicHost = parsedIP.String() } if s.Listener == nil && s.ListenAddr == "" { @@ -153,6 +146,20 @@ func (server *FtpServer) loadSettings() error { return nil } +func parseIPv4(publicHost string) (string, error) { + parsedIP := net.ParseIP(publicHost) + if parsedIP == nil { + return "", &ipValidationError{error: fmt.Sprintf("invalid passive IP %#v", publicHost)} + } + + parsedIP = parsedIP.To4() + if parsedIP == nil { + return "", &ipValidationError{error: fmt.Sprintf("invalid IPv4 passive IP %#v", publicHost)} + } + + return parsedIP.String(), nil +} + // Listen starts the listening // It's not a blocking call func (server *FtpServer) Listen() error {