Skip to content

Commit

Permalink
Add recover when executing command (#88)
Browse files Browse the repository at this point in the history
Add recover when executing command

Reviewed-on: https://gitea.com/goftp/server/pulls/88
  • Loading branch information
lunny committed Feb 6, 2020
1 parent 791e89a commit 57b389c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package server

import (
"bufio"
"bytes"
"crypto/rand"
"crypto/sha256"
"crypto/tls"
Expand All @@ -16,6 +17,7 @@ import (
mrand "math/rand"
"net"
"path/filepath"
"runtime"
"strconv"
"strings"
)
Expand Down Expand Up @@ -158,6 +160,25 @@ func (conn *Conn) upgradeToTLS() error {
// receiveLine accepts a single line FTP command and co-ordinates an
// appropriate response.
func (conn *Conn) receiveLine(line string) {
defer func() {
if e := recover(); e != nil {
var buf bytes.Buffer
fmt.Fprintf(&buf, "Handler crashed with error: %v", e)

for i := 1; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
} else {
fmt.Fprintf(&buf, "\n")
}
fmt.Fprintf(&buf, "%v:%v", file, line)
}

conn.logger.Print(conn.sessionID, buf.String())
}
}()

command, param := conn.parseLine(line)
conn.logger.PrintCommand(conn.sessionID, command, param)
cmdObj := commands[strings.ToUpper(command)]
Expand Down

0 comments on commit 57b389c

Please sign in to comment.