-
Notifications
You must be signed in to change notification settings - Fork 37
/
ftp.go
133 lines (116 loc) · 2.56 KB
/
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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// author: smallfish <[email protected]>
package ftp
import (
"fmt"
"net"
"strconv"
"strings"
)
type FTP struct {
host string
port int
user string
passwd string
pasv int
cmd string
Code int
Message string
Debug bool
stream []byte
conn net.Conn
Error error
}
func (ftp *FTP) debugInfo(s string) {
if ftp.Debug {
fmt.Println(s)
}
}
func (ftp *FTP) Connect(host string, port int) {
addr := fmt.Sprintf("%s:%d", host, port)
if ftp.conn, ftp.Error = net.Dial("tcp", addr); ftp.Error != nil {
return
}
ftp.Response()
ftp.host = host
ftp.port = port
}
func (ftp *FTP) Login(user, passwd string) {
ftp.Request("USER " + user)
ftp.Request("PASS " + passwd)
ftp.user = user
ftp.passwd = passwd
}
func (ftp *FTP) Response() (code int, message string) {
ret := make([]byte, 1024)
n, _ := ftp.conn.Read(ret)
msg := string(ret[:n])
code, _ = strconv.Atoi(msg[:3])
message = msg[4 : len(msg)-2]
ftp.debugInfo("<*cmd*> " + ftp.cmd)
ftp.debugInfo(fmt.Sprintf("<*code*> %d", code))
ftp.debugInfo("<*message*> " + message)
return
}
func (ftp *FTP) Request(cmd string) {
ftp.conn.Write([]byte(cmd + "\r\n"))
ftp.cmd = cmd
ftp.Code, ftp.Message = ftp.Response()
if cmd == "PASV" {
start, end := strings.Index(ftp.Message, "("), strings.Index(ftp.Message, ")")
s := strings.Split(ftp.Message[start:end], ",")
l1, _ := strconv.Atoi(s[len(s)-2])
l2, _ := strconv.Atoi(s[len(s)-1])
ftp.pasv = l1*256 + l2
}
if (cmd != "PASV") && (ftp.pasv > 0) {
ftp.Message = newRequest(ftp.host, ftp.pasv, ftp.stream)
ftp.debugInfo("<*response*> " + ftp.Message)
ftp.pasv = 0
ftp.stream = nil
ftp.Code, _ = ftp.Response()
}
}
func (ftp *FTP) Pasv() {
ftp.Request("PASV")
}
func (ftp *FTP) Pwd() {
ftp.Request("PWD")
}
func (ftp *FTP) Cwd(path string) {
ftp.Request("CWD " + path)
}
func (ftp *FTP) Mkd(path string) {
ftp.Request("MKD " + path)
}
func (ftp *FTP) Size(path string) (size int) {
ftp.Request("SIZE " + path)
size, _ = strconv.Atoi(ftp.Message)
return
}
func (ftp *FTP) List() {
ftp.Pasv()
ftp.Request("LIST")
}
func (ftp *FTP) Stor(file string, data []byte) {
ftp.Pasv()
if data != nil {
ftp.stream = data
}
ftp.Request("STOR " + file)
}
func (ftp *FTP) Quit() {
ftp.Request("QUIT")
ftp.conn.Close()
}
// new connect to FTP pasv port, return data
func newRequest(host string, port int, b []byte) string {
conn, _ := net.Dial("tcp", fmt.Sprintf("%s:%d", host, port))
defer conn.Close()
if b != nil {
conn.Write(b)
return "OK"
}
ret := make([]byte, 4096)
n, _ := conn.Read(ret)
return string(ret[:n])
}