/
client.go
314 lines (286 loc) · 8.93 KB
/
client.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package ftp
import (
"bufio"
"fmt"
"io"
"net"
"os"
"regexp"
"strconv"
"strings"
)
// Knows the control connection where commands are sent to the server.
type Connection struct {
control io.ReadWriteCloser
hostname string
}
var CRLF = "\r\n"
var ASCII = "A"
var BINARY = "I"
var IMAGE = "I" //Synonymous with "Binary"
// Dials up a remote FTP server.
// host should be in the form of address:port e.g. myserver:21 or myserver:ftp
// Returns a pointer to a Connection
func Dial(host string) (*Connection, os.Error) {
if host == "" {
return nil, os.NewError("FTP Connection Error: Host can not be blank!")
}
if !hasPort(host) {
return nil, os.NewError("FTP Connection Error: Host must have a port! e.g. host:21")
}
conn, err := net.Dial("tcp", "", host)
if err != nil {
return nil, err
}
// Upon connect, most servers respond with a welcome message.
// The welcome message contains a status code, just like any other command.
// TODO: Handle servers with no welcome message.
welcomeMsg := make([]byte, 1024)
_, err = conn.Read(welcomeMsg)
if err != nil {
return nil, os.NewError("Couldn't read the server's initital connection information. Error: " + err.String())
}
code, err := strconv.Atoui(string(welcomeMsg[0:3]))
err = checkResponseCode(2, code)
if err != nil {
return nil, os.NewError("Couldn't read the server's Welcome Message. Error: " + err.String())
}
// This doesn't work for IPv6 addresses.
hostParts := strings.Split(host, ":", 2)
return &Connection{conn, hostParts[0]}, nil
}
// Executes an FTP command.
// Sends the command to the server.
// Returns the response code, the response text from the server, and any errors.
// The response code will be zero if an error is encountered.
// The response string will be the empty string if an error is encountered.
func (c *Connection) Cmd(command string, arg string) (code uint, response string, err os.Error) {
// Format command to be sent to the server.
formattedCommand := command + " " + arg + CRLF
// Send command to the server.
_, err = c.control.Write([]byte(formattedCommand))
if err != nil {
return 0, "", err
}
// Process the response.
reader := bufio.NewReader(c.control)
regex := regexp.MustCompile("[0-9][0-9][0-9] ")
for {
ln, err := reader.ReadString('\n')
if err != nil {
return 0, "", err
}
response += ln
if regex.MatchString(ln) {
break
}
}
code, err = strconv.Atoui(response[0:3])
if err != nil {
return 0, response, err
}
return code, response, err
}
// Log into a FTP server using username and password.
func (c *Connection) Login(user string, password string) os.Error {
if user == "" {
return os.NewError("FTP Connection Error: User can not be blank!")
}
if password == "" {
return os.NewError("FTP Connection Error: Password can not be blank!")
}
// TODO: Check the server's response codes.
_, _, err := c.Cmd("USER", user)
_, _, err = c.Cmd("PASS", password)
if err != nil {
return err
}
return nil
}
func (c *Connection) Logout() os.Error {
_, _, err := c.Cmd("QUIT", "")
if err != nil {
return err
}
err = c.control.Close()
if err != nil {
return err
}
return nil
}
// Download a file from a remote server.
// Assumes only passive FTP connections for now.
func (c *Connection) DownloadFile(src, dest, mode string) os.Error {
// Use PASV to set up the data port.
pasvCode, pasvLine, err := c.Cmd("PASV", "")
if err != nil {
return err
}
pasvErr := checkResponseCode(2, pasvCode)
if pasvErr != nil {
msg := fmt.Sprintf("Cannot set PASV. Error: %v", pasvErr)
return os.NewError(msg)
}
dataPort, err := extractDataPort(pasvLine)
/*_, err = extractDataPort(pasvLine)*/
if err != nil {
return err
}
// Set the TYPE (ASCII or Binary)
typeCode, typeLine, err := c.Cmd("TYPE", mode)
if err != nil {
return err
}
typeErr := checkResponseCode(2, typeCode)
if typeErr != nil {
msg := fmt.Sprintf("Cannot set TYPE. Error: '%v'. Line: '%v'", typeErr, typeLine)
return os.NewError(msg)
}
// Can't use Cmd() for RETR because it doesn't return until *after* you've
// downloaded the requested file.
command := []byte("RETR " + src + CRLF)
_, err = c.control.Write(command)
if err != nil {
return err
}
// Open connection to remote data port.
remoteConnectString := c.hostname + ":" + fmt.Sprintf("%d", dataPort)
download_conn, err := net.Dial("tcp", "", remoteConnectString)
defer download_conn.Close()
if err != nil {
msg := fmt.Sprintf("Couldn't connect to server's remote data port. Error: %v", err)
return os.NewError(msg)
}
// Set up the destination file
var filePerms uint32 = 0664
destFile, err := os.Open(dest, os.O_CREAT|os.O_WRONLY, filePerms)
defer destFile.Close()
if err != nil {
msg := fmt.Sprintf("Cannot open destination file, '%s'. %v", dest, err)
return os.NewError(msg)
}
// Buffer for downloading and writing to file
bufLen := 1024
buf := make([]byte, bufLen)
// Read from the server and write the contents to a file
for {
bytesRead, readErr := download_conn.Read(buf)
if bytesRead > 0 {
_, err := destFile.Write(buf[0:bytesRead])
if err != nil {
msg := fmt.Sprintf("Coudn't write to file, '%s'. Error: %v", dest, err)
return os.NewError(msg)
}
}
if readErr == os.EOF {
break
}
if readErr != nil {
return readErr
}
}
return nil
}
func (c *Connection) UploadFile(src, dest, mode string) os.Error {
// Use PASV to set up the data port.
pasvCode, pasvLine, err := c.Cmd("PASV", "")
if err != nil {
return err
}
pasvErr := checkResponseCode(2, pasvCode)
if pasvErr != nil {
msg := fmt.Sprintf("Cannot set PASV. Error: %v", pasvErr)
return os.NewError(msg)
}
dataPort, err := extractDataPort(pasvLine)
if err != nil {
return err
}
// Set the TYPE (ASCII or Binary)
typeCode, typeLine, err := c.Cmd("TYPE", mode)
if err != nil {
return err
}
typeErr := checkResponseCode(2, typeCode)
if typeErr != nil {
msg := fmt.Sprintf("Cannot set TYPE. Error: '%v'. Line: '%v'", typeErr, typeLine)
return os.NewError(msg)
}
// Can't use Cmd() for STOR because it doesn't return until *after* you've
// uploaded the requested file.
command := []byte("STOR " + dest + CRLF)
_, err = c.control.Write(command)
if err != nil {
return err
}
// Open connection to remote data port.
remoteConnectString := c.hostname + ":" + fmt.Sprintf("%d", dataPort)
upload_conn, err := net.Dial("tcp", "", remoteConnectString)
defer upload_conn.Close()
if err != nil {
msg := fmt.Sprintf("Couldn't connect to server's remote data port. Error: %v", err)
return os.NewError(msg)
}
// Open the source file for uploading
sourceFile, err := os.Open(src, os.O_RDONLY, 0644)
defer sourceFile.Close()
if err != nil {
msg := fmt.Sprintf("Cannot open src file, '%s'. %v", src, err)
os.NewError(msg)
}
// Buffer for uploading the file
bufLen := 1024
buf := make([]byte, bufLen)
// Read from the file and write the contents to the server
for {
bytesRead, readErr := sourceFile.Read(buf)
if bytesRead > 0 {
_, writeErr := upload_conn.Write(buf[0:bytesRead])
if writeErr != nil {
msg := fmt.Sprintf("Coudn't write file to server, '%s'. Error: %v", sourceFile, writeErr)
return os.NewError(msg)
}
}
if readErr == os.EOF {
break
}
if readErr != nil {
return readErr
}
}
return nil
}
// Given an prefix, does the response code match the expected code?
func checkResponseCode(expectCode, code uint) os.Error {
if 1 <= expectCode && expectCode < 10 && code/100 != expectCode ||
10 <= expectCode && expectCode < 100 && code/10 != expectCode ||
100 <= expectCode && expectCode < 1000 && code != expectCode {
msg := fmt.Sprintf("Bad response from server. Expected: %d, Got: %d", expectCode, code)
return os.NewError(msg)
}
return nil
}
// Interrogate a server response for the remote port on which to connect.
// Returns the port to be used as the data port for transfers.
func extractDataPort(line string) (port uint, err os.Error) {
// We only care about the last two octets
portPattern:= "[0-9]+,[0-9]+,[0-9]+,[0-9]+,([0-9]+,[0-9]+)"
re, err := regexp.Compile(portPattern)
if err != nil {
return 0, err
}
match := re.FindStringSubmatch(line)
if len(match) == 0 {
msg := "Cannot find data port in server output: " + line
return 0, os.NewError(msg)
}
octets := strings.Split(match[1], ",", 2)
firstOctet, _ := strconv.Atoui(octets[0])
secondOctet, _ := strconv.Atoui(octets[1])
port = firstOctet * 256 + secondOctet
return port, nil
}
// Reused from src/pkg/http/client.go
// Given a string of the form "host", "host:port", or "[ipv6::address]:port",
// return true if the string includes a port.
func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }