-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathftp.go
650 lines (556 loc) · 13 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
package sftps
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/textproto"
"os"
"regexp"
"strconv"
"strings"
"time"
)
type Ftp struct {
rawConn net.Conn
tlsConn *tls.Conn
ctrlConn *textproto.Conn
params *ftpParameters
State int
}
func newFtp(p *ftpParameters) (ftp *Ftp) {
//spew.Dump(p)
ftp = new(Ftp)
ftp.params = p
ftp.State = OFFLINE
return
}
func (this *Ftp) connect() (res *FtpResponse, err error) {
var ipaddr []net.IP
var code int
var msg string
if ipaddr, err = net.LookupIP(this.params.host); err != nil {
return
}
addr := fmt.Sprintf("%s:%d", ipaddr[0], this.params.port)
dialer := new(net.Dialer)
if dialer.Timeout, err = time.ParseDuration(TIMEOUT); err != nil {
return
}
if dialer.KeepAlive, err = time.ParseDuration(KEEPALIVE); err != nil {
return
}
if this.params.secure && this.params.secureMode == IMPLICIT {
var conf *tls.Config
if conf, err = this.getTLSConfig(); err != nil {
return
}
if this.tlsConn, err = tls.DialWithDialer(dialer, "tcp", addr, conf); err != nil {
return
}
this.ctrlConn = textproto.NewConn(this.tlsConn)
} else {
if this.rawConn, err = dialer.Dial("tcp", addr); err != nil {
return
}
this.ctrlConn = textproto.NewConn(this.rawConn)
}
if code, msg, err = this.ctrlConn.ReadResponse(220); err != nil {
return
}
res = &FtpResponse{
command: "",
code: code,
msg: msg,
}
this.State = ONLINE
return
}
func (this *Ftp) secureUpgrade() (err error) {
var conf *tls.Config
if conf, err = this.getTLSConfig(); err != nil {
return
}
this.tlsConn = tls.Client(this.rawConn, conf)
this.ctrlConn = textproto.NewConn(this.tlsConn)
return
}
func (this *Ftp) getTLSConfig() (conf *tls.Config, err error) {
var certPair tls.Certificate
var certPool *x509.CertPool
var rcaPem []byte
conf = new(tls.Config)
conf.ClientAuth = tls.VerifyClientCertIfGiven
conf.CipherSuites = []uint16{
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
}
if this.params.cert != "" && this.params.key != "" {
if certPair, err = tls.LoadX509KeyPair(this.params.cert, this.params.key); err != nil {
return
}
certPool = x509.NewCertPool()
if this.params.rootCA != "" {
if rcaPem, err = ioutil.ReadFile("./cert/rcaPem.pem"); err != nil {
return
}
if this.params.alwaysTrust {
if !certPool.AppendCertsFromPEM(rcaPem) {
panic("Failed to parse the Root Certificate")
}
}
conf.RootCAs = certPool
}
conf.Certificates = make([]tls.Certificate, 1)
conf.Certificates[0] = certPair
conf.ClientCAs = certPool
}
conf.InsecureSkipVerify = this.params.alwaysTrust
return
}
func (this *Ftp) auth() (res []*FtpResponse, err error) {
var r *FtpResponse
res = []*FtpResponse{}
if this.params.secure && this.params.secureMode == EXPLICIT {
if r, err = this.Command("AUTH TLS", 234); err != nil {
return
}
res = append(res, r)
if err = this.secureUpgrade(); err != nil {
return
}
}
if r, err = this.Command(fmt.Sprintf("USER %s", this.params.user), 331); err != nil {
return
}
res = append(res, r)
if r, err = this.Command(fmt.Sprintf("PASS %s", this.params.pass), 230); err != nil {
return
}
res = append(res, r)
return
}
func (this *Ftp) Command(cmd string, code int) (res *FtpResponse, err error) {
var c int
var m string
if _, err = this.ctrlConn.Cmd(cmd); err != nil {
return
}
if c, m, err = this.ctrlConn.ReadResponse(code); err != nil {
return
}
res = &FtpResponse{
command: cmd,
code: c,
msg: m,
}
return
}
func (this *Ftp) options() (res []*FtpResponse, err error) {
var r *FtpResponse
if r, err = this.Command("SYST", 215); err != nil {
return
}
res = []*FtpResponse{}
res = append(res, r)
if r, err = this.Command("FEAT", 211); err != nil {
return
}
res = append(res, r)
if r, err = this.Command("OPTS UTF8 ON", 200); err != nil {
return
}
res = append(res, r)
if this.params.secure {
if r, err = this.Command("PROT P", 200); err != nil {
return
}
res = append(res, r)
}
if r, err = this.Command("TYPE I", 200); err != nil {
return
}
res = append(res, r)
return
}
func (this *Ftp) getLocalIP() (ip string, err error) {
var addrs []net.Addr
if addrs, err = net.InterfaceAddrs(); err != nil {
return
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ip = ipnet.IP.To4().String()
}
}
}
if ip == "" {
err = errors.New("Could not get the Local Address.")
return
}
return
}
func (this *Ftp) ds2h(dec string) (hex string, err error) {
var p int = 0
if p, err = strconv.Atoi(dec); err != nil {
return
}
hex = fmt.Sprintf("%x", p)
return
}
func (this *Ftp) h2i(hex string) (res int, err error) {
if r, e := strconv.ParseInt(hex, 16, 64); e != nil {
return
} else {
res = int(r)
}
return
}
func (this *Ftp) getSplitPorts() (port1 int, port2 int, err error) {
hex := fmt.Sprintf("%x", this.params.listenPort)
switch len(hex) {
case 1, 2:
port1 = 0
if port2, err = this.h2i(hex); err != nil {
return
}
case 3:
p1 := hex[:1]
p2 := hex[1:3]
if port1, err = this.h2i(p1); err != nil {
return
}
if port2, err = this.h2i(p2); err != nil {
return
}
case 4:
p1 := hex[:2]
p2 := hex[2:4]
if port1, err = this.h2i(p1); err != nil {
return
}
if port2, err = this.h2i(p2); err != nil {
return
}
default:
err = errors.New("The Port Number could not converted to the Parameter Format for the Listen Function.")
}
return
}
func (this *Ftp) port() (res *FtpResponse, listener net.Listener, err error) {
var localIP string = ""
if localIP, err = this.getLocalIP(); err != nil {
return
}
ip := strings.Replace(localIP, ".", ",", -1)
var p1, p2 int
if p1, p2, err = this.getSplitPorts(); err != nil {
return
}
cmd := fmt.Sprintf("PORT %s,%d,%d", ip, p1, p2)
if res, err = this.Command(cmd, 200); err != nil {
return
}
if listener, err = net.Listen("tcp", fmt.Sprintf("%s:%d", localIP, this.params.listenPort)); err != nil {
return
}
return
}
func (this *Ftp) pasv() (res *FtpResponse, dataConn net.Conn, err error) {
if res, err = this.Command("PASV", 227); err != nil {
return
}
var ip []net.IP
if ip, err = net.LookupIP(this.params.host); err != nil {
return
}
reg := regexp.MustCompile("([0-9]+?),([0-9]+?),([0-9]+?),([0-9]+?),([0-9]+?),([0-9]+)")
matches := reg.FindAllStringSubmatch(res.msg, -1)
tmp := matches[0]
var hex1 string = ""
var hex2 string = ""
if hex1, err = this.ds2h(tmp[5]); err != nil {
return
}
if hex2, err = this.ds2h(tmp[6]); err != nil {
return
}
var port int
if port, err = this.h2i(fmt.Sprintf("%s%s", hex1, hex2)); err != nil {
return
}
param := fmt.Sprintf("%s:%d", ip[0], port)
dataConn, err = net.Dial("tcp", param)
return
}
func (this *Ftp) readBytes(itf interface{}) (res *FtpResponse, bytes []byte, err error) {
var dataConn net.Conn
if this.params.passive {
if dc, ok := itf.(net.Conn); ok {
dataConn = dc
} else {
err = errors.New("Invalid parameter were bound, net.Conn is not found.")
return
}
} else {
if listener, ok := itf.(net.Listener); ok {
if dataConn, err = listener.Accept(); err != nil {
return
}
} else {
err = errors.New("Invalid parameter were bound, net.Listener is not found.")
return
}
}
defer dataConn.Close()
if this.params.secure {
var conf *tls.Config
if conf, err = this.getTLSConfig(); err != nil {
return
}
dataTLS := tls.Client(dataConn, conf)
defer dataTLS.Close()
if bytes, err = ioutil.ReadAll(dataTLS); err != nil {
return
}
dataTLS.Close() // Important the Buffer flush out.
} else {
if bytes, err = ioutil.ReadAll(dataConn); err != nil {
return
}
dataConn.Close() // Important the Buffer flush out.
}
c, m, e := this.ctrlConn.ReadResponse(226)
if e != nil {
err = e
return
}
res = &FtpResponse{
command: "",
code: c,
msg: m,
}
return
}
func (this *Ftp) quit() (res *FtpResponse, err error) {
defer this.ctrlConn.Close()
if this.params.secure {
defer this.tlsConn.Close()
}
if this.params.secureMode != IMPLICIT {
defer this.rawConn.Close()
}
if res, err = this.Command("QUIT", 221); err != nil {
return
}
return
}
func (this *Ftp) list(p string) (res []*FtpResponse, list string, err error) {
res = []*FtpResponse{}
if !this.params.keepAlive {
defer func () {
var r *FtpResponse
if r, err = this.quit(); err != nil {
return
}
res = append(res, r)
return
}()
}
var itf interface{}
var bytes []byte
var r *FtpResponse
res = []*FtpResponse{}
cmd := fmt.Sprintf("LIST -aL %s", p)
if this.params.passive {
if r, itf, err = this.pasv(); err != nil {
return
}
} else {
if r, itf, err = this.port(); err != nil {
return
}
}
res = append(res, r)
if r, err = this.Command(cmd, 150); err != nil {
return
}
res = append(res, r)
if r, bytes, err = this.readBytes(itf); err != nil {
return
}
res = append(res, r)
list = string(bytes)
return
}
func (this *Ftp) download(local string, remote string) (res []*FtpResponse, len int64, err error) {
res = []*FtpResponse{}
if !this.params.keepAlive {
defer func() {
var r *FtpResponse
if r, err = this.quit(); err != nil {
return
}
res = append(res, r)
return
}()
}
var itf interface{}
var r *FtpResponse
if this.params.passive {
if r, itf, err = this.pasv(); err != nil {
return
}
} else {
if r, itf, err = this.port(); err != nil {
return
}
}
res = append(res, r)
var cmd = fmt.Sprintf("RETR %s", remote)
if r, err = this.Command(cmd, 150); err != nil {
spew.Dump(err)
return
}
res = append(res, r)
if r, len, err = this.fileTransfer(DOWNLOAD, local, itf); err != nil {
return
}
res = append(res, r)
return
}
func (this *Ftp) upload(local string, remote string) (res []*FtpResponse, len int64, err error) {
res = []*FtpResponse{}
if !this.params.keepAlive {
defer func() {
var r *FtpResponse
if r, err = this.quit(); err != nil {
return
}
res = append(res, r)
return
}()
}
var itf interface{}
var r *FtpResponse
if this.params.passive {
if r, itf, err = this.pasv(); err != nil {
return
}
} else {
if r, itf, err = this.port(); err != nil {
return
}
}
res = append(res, r)
var cmd = fmt.Sprintf("STOR %s", remote)
if r, err = this.Command(cmd, 150); err != nil {
spew.Dump(err)
return
}
res = append(res, r)
if r, len, err = this.fileTransfer(UPLOAD, local, itf); err != nil {
return
}
res = append(res, r)
return
}
func (this *Ftp) mkdir(p string) (res *FtpResponse, err error) {
res, err = this.Command(fmt.Sprintf("MKD %s", p), 257)
return
}
func (this *Ftp) rmdir(p string) (res *FtpResponse, err error) {
res, err = this.Command(fmt.Sprintf("RMD %s", p), 250)
return
}
func (this *Ftp) delete(p string) (res *FtpResponse, err error) {
res, err = this.Command(fmt.Sprintf("DELE %s", p), 200)
return
}
func (this *Ftp) rename(old, new string) (res []*FtpResponse, err error) {
r := &FtpResponse{}
if r, err = this.Command(fmt.Sprintf("RNFR %s", old), 350); err != nil {
return
}
res = append(res, r)
if r, err = this.Command(fmt.Sprintf("RNTO %s", new), 250); err != nil {
return
}
res = append(res, r)
return
}
func (this *Ftp) fileTransfer(direction int, uri string, itf interface{}) (res *FtpResponse, len int64, err error) {
var dataConn net.Conn
if this.params.passive {
if c, ok := itf.(net.Conn); ok {
dataConn = c
defer dataConn.Close()
} else {
err = errors.New("Invalid parameter were bound, Value of the argument 'itf' must be the Type 'net.Conn' when the Passive Mode specified by the Parameter.")
return
}
} else {
if listener, ok := itf.(net.Listener); ok {
defer listener.Close()
if dataConn, err = listener.Accept(); err != nil {
return
}
} else {
err = errors.New("Invalid parameter were bound, Value of the argument 'itf' must be the Type 'net.Listener' whern the Active Mode speciffied by the Parameter")
return
}
}
var r io.ReadCloser
var w io.WriteCloser
var rw io.ReadWriteCloser = dataConn
if this.params.secure {
var conf *tls.Config
if conf, err = this.getTLSConfig(); err != nil {
return
}
dataTLS := tls.Client(dataConn, conf)
defer dataTLS.Close()
rw = dataTLS
}
if direction == DOWNLOAD {
if w, err = os.Create(uri); err != nil {
return
}
r = rw
} else if direction == UPLOAD {
spew.Dump("UPLOAD")
if r, err = os.Open(uri); err != nil {
return
}
w = rw
} else {
err = errors.New("The Argument 'direction' must be the either 'DOWNLOAD' or 'UPLOAD'.")
return
}
if len, err = io.Copy(w, r); err != nil {
return
}
r.Close()
w.Close()
var code int
var msg string
if code, msg, err = this.ctrlConn.ReadResponse(226); err != nil {
return
}
res = &FtpResponse{
command: "",
code: code,
msg: msg,
}
return
}