-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_cmd.go
76 lines (71 loc) · 1.31 KB
/
service_cmd.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
/*
* Handles the FTP service commands as defined by RFC 959
*/
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
func handleRetr(c *client) {
if c.pasv == nil {
sendMessage(c, 425)
return
}
defer c.pasv.Close()
if len(c.input) < 2 {
sendMessage(c, 500)
return
}
sendMessage(c, 150)
// TODO: deal with encoding and type specifics.
fp := filepath.Join(append(c.path, c.input[1])...)
fd, err := os.Open(fp)
if err != nil {
sendMessage(c, 550)
return
}
defer fd.Close()
buff := make([]byte, 512)
for {
_, err := fd.Read(buff)
if err == io.EOF {
break
}
c.pasv.Write(buff)
}
sendMessage(c, 226)
}
/**
* Sends list of dir and files in present folder over pasv connection.
*/
func handleNlst(c *client) {
if c.pasv == nil {
sendMessage(c, 425)
return
}
defer c.pasv.Close()
fp := filepath.Join(c.path...)
fmt.Println(fp)
files, _ := ioutil.ReadDir(fp)
writer := bufio.NewWriter(c.pasv)
sendMessage(c, 150)
for _, file := range files {
s := []byte(fmt.Sprintf("%s %d\r\n", file.Name(), file.Size()))
_, err := writer.Write(s)
if err != nil {
// TODO: check error code.
sendMessage(c, 500)
break
}
}
writer.Flush()
sendMessage(c, 226)
}
// Noop should do nothing but specify an okay.
func handleNoop(c *client) {
sendMessage(c, 200)
}