62 lines
915 B
Go
62 lines
915 B
Go
package ftp
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
)
|
|
|
|
type Conn struct {
|
|
conn net.Conn
|
|
dataType dataType
|
|
dataPort *dataPort
|
|
rootDir string
|
|
workDir string
|
|
}
|
|
|
|
func NewConn(conn net.Conn, rootDir string) *Conn {
|
|
return &Conn{
|
|
conn: conn,
|
|
rootDir: rootDir,
|
|
workDir: "/",
|
|
}
|
|
}
|
|
|
|
func Serve (c *Conn) {
|
|
c.respond(status220)
|
|
|
|
s := bufio.NewScanner(c.conn)
|
|
for s.Scan() {
|
|
input := strings.Fields(s.Text())
|
|
if len(input) == 0 {
|
|
continue
|
|
}
|
|
command, args := input[0], input[1:]
|
|
fmt.Printf("<< %s %v", command, args)
|
|
|
|
switch command {
|
|
case "CWD": // cd
|
|
c.cwd(args)
|
|
case "LIST": // ls
|
|
c.list(args)
|
|
case "PORT":
|
|
c.port(args)
|
|
case "USER":
|
|
c.user(args)
|
|
case "QUIT": // close
|
|
c.respond(status221)
|
|
return
|
|
case "RETR": // get
|
|
c.retr(args)
|
|
case "TYPE":
|
|
c.setDataType(args)
|
|
default:
|
|
c.respond(status502)
|
|
}
|
|
}
|
|
if s.Err() != nil {
|
|
fmt.Print(s.Err())
|
|
}
|
|
|
|
} |