Skip to content

Commit

Permalink
Moving some code to reduce cyclomatic complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
fclairamb committed Mar 3, 2024
1 parent 4d7c663 commit b8e8124
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 37 deletions.
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 35 additions & 27 deletions client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 17 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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 {
Expand Down

0 comments on commit b8e8124

Please sign in to comment.