forked from mushorg/glutton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp.go
46 lines (42 loc) · 994 Bytes
/
ftp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package glutton
import (
"bufio"
"context"
"fmt"
"net"
"strings"
)
func readFTP(conn net.Conn, g *Glutton) (msg string, err error) {
msg, err = bufio.NewReader(conn).ReadString('\n')
if err != nil {
g.logger.Error(fmt.Sprintf("[ftp ] error: %v", err))
}
g.logger.Info(fmt.Sprintf("[ftp ] recv: %q", msg))
return
}
// HandleFTP takes a net.Conn and does basic FTP communication
func (g *Glutton) HandleFTP(ctx context.Context, conn net.Conn) (err error) {
defer func() {
err = conn.Close()
if err != nil {
g.logger.Error(fmt.Sprintf("[ftp ] error: %v", err))
}
}()
conn.Write([]byte("220 Welcome!\r\n"))
for {
g.updateConnectionTimeout(ctx, conn)
msg, err := readFTP(conn, g)
if len(msg) < 4 || err != nil {
break
}
cmd := strings.ToUpper(msg[:4])
if cmd == "USER" {
conn.Write([]byte("331 OK.\r\n"))
} else if cmd == "PASS" {
conn.Write([]byte("230 OK.\r\n"))
} else {
conn.Write([]byte("200 OK.\r\n"))
}
}
return nil
}