43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package services
|
|
|
|
import (
|
|
"bufio"
|
|
"net"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func NewFTPHandler(log LoggerFunc) Handler {
|
|
return func(conn net.Conn) {
|
|
defer conn.Close()
|
|
remote := conn.RemoteAddr().String()
|
|
_, _ = conn.Write([]byte("220 Welcome to FTP Server\r\n"))
|
|
conn.SetDeadline(time.Now().Add(5 * time.Minute))
|
|
scanner := bufio.NewScanner(conn)
|
|
var username string
|
|
for scanner.Scan() {
|
|
line := strings.TrimSpace(scanner.Text())
|
|
if line == "" {
|
|
continue
|
|
}
|
|
parts := strings.SplitN(line, " ", 2)
|
|
cmd := strings.ToUpper(parts[0])
|
|
arg := ""
|
|
if len(parts) > 1 { arg = parts[1] }
|
|
switch cmd {
|
|
case "USER":
|
|
username = arg
|
|
log(Record{Timestamp: Now(), RemoteAddr: remoteIP(remote), RemotePort: remotePort(remote), Service: "ftp", Details: map[string]string{"event":"username_attempt","username":username}})
|
|
_, _ = conn.Write([]byte("331 Password required for "+username+"\r\n"))
|
|
case "PASS":
|
|
log(Record{Timestamp: Now(), RemoteAddr: remoteIP(remote), RemotePort: remotePort(remote), Service: "ftp", Details: map[string]string{"event":"password_attempt","username":username,"password":arg}})
|
|
_, _ = conn.Write([]byte("530 Login incorrect\r\n"))
|
|
case "QUIT":
|
|
_, _ = conn.Write([]byte("221 Goodbye\r\n")); return
|
|
default:
|
|
_, _ = conn.Write([]byte("502 Command not implemented\r\n"))
|
|
}
|
|
}
|
|
}
|
|
}
|