-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclient.go
1031 lines (876 loc) · 25.7 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
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package ftp implements an FTP client.
package ftp4go
import (
"bufio"
"errors"
"fmt"
"golang.org/x/net/proxy"
"io"
"log"
"net"
"net/textproto"
"net/url"
"os"
"strconv"
"strings"
"time"
)
// The default constants
const (
DefaultFtpPort = 21
DefaultTimeoutInMsec = 20 * time.Second
CRLF = "\r\n"
BLOCK_SIZE = 8192
)
// FTP command strings
type FtpCmd int
const (
NONE_FTP_CMD FtpCmd = 0
USER_FTP_CMD FtpCmd = 1
PASSWORD_FTP_CMD FtpCmd = 2
ACCT_FTP_CMD FtpCmd = 3
ABORT_FTP_CMD FtpCmd = 4
PORT_FTP_CMD FtpCmd = 5
PASV_FTP_CMD FtpCmd = 6
TYPE_A_FTP_CMD FtpCmd = 7
NLST_FTP_CMD FtpCmd = 8
LIST_FTP_CMD FtpCmd = 9
FEAT_FTP_CMD FtpCmd = 10
OPTS_FTP_CMD FtpCmd = 11
RETR_FTP_CMD FtpCmd = 12
TYPE_I_FTP_CMD FtpCmd = 13
STORE_FTP_CMD FtpCmd = 14
RENAMEFROM_FTP_CMD FtpCmd = 15
RENAMETO_FTP_CMD FtpCmd = 16
DELETE_FTP_CMD FtpCmd = 17
CWD_FTP_CMD FtpCmd = 18
SIZE_FTP_CMD FtpCmd = 19
MKDIR_FTP_CMD FtpCmd = 20
RMDIR_FTP_CMD FtpCmd = 21
PWDIR_FTP_CMD FtpCmd = 22
CDUP_FTP_CMD FtpCmd = 23
QUIT_FTP_CMD FtpCmd = 24
MLSD_FTP_CMD FtpCmd = 25
REST_FTP_CMD FtpCmd = 26
)
const MSG_OOB = 0x1 //Process data out of band
var ftpCmdStrings = map[FtpCmd]string{
NONE_FTP_CMD: "",
USER_FTP_CMD: "USER",
PASSWORD_FTP_CMD: "PASS",
ACCT_FTP_CMD: "ACCT",
ABORT_FTP_CMD: "ABOR",
PORT_FTP_CMD: "PORT",
PASV_FTP_CMD: "PASV",
TYPE_A_FTP_CMD: "TYPE A",
NLST_FTP_CMD: "NLST",
LIST_FTP_CMD: "LIST",
MLSD_FTP_CMD: "MLSD",
FEAT_FTP_CMD: "FEAT",
OPTS_FTP_CMD: "OPTS",
RETR_FTP_CMD: "RETR",
TYPE_I_FTP_CMD: "TYPE I",
STORE_FTP_CMD: "STOR",
RENAMEFROM_FTP_CMD: "RNFR",
RENAMETO_FTP_CMD: "RNTO",
DELETE_FTP_CMD: "DELE",
CWD_FTP_CMD: "CWD",
SIZE_FTP_CMD: "SIZE",
MKDIR_FTP_CMD: "MKD",
RMDIR_FTP_CMD: "RMD",
PWDIR_FTP_CMD: "PWD",
CDUP_FTP_CMD: "CDUP",
QUIT_FTP_CMD: "QUIT",
REST_FTP_CMD: "REST",
}
// The FTP client structure containing:
// - host, user, password, acct, timeout
type FTP struct {
debugging int
Host string
Port int
file string
welcome string
passiveserver bool
logger *log.Logger
timeoutInMsec time.Duration
textprotoConn *textproto.Conn
dialer proxy.Dialer
conn net.Conn
encoding string
stop chan bool
}
type NameFactsLine struct {
Name string
Facts map[string]string
}
func getTimeoutInMsec(msec int64) time.Time {
return time.Now().Add(time.Duration(msec) * time.Millisecond)
}
func (i FtpCmd) String() string {
if cmd, ok := ftpCmdStrings[i]; ok {
return cmd
}
panic("No cmd found")
}
func (i FtpCmd) AppendParameters(pars ...string) string {
allPars := make([]string, len(pars)+1)
allPars[0] = i.String()
var k int = 1
for _, par := range pars {
if p := strings.TrimSpace(par); len(p) > 0 {
allPars[k] = p
k++
}
}
return strings.Join(allPars[:k], " ")
}
func (ftp *FTP) writeInfo(params ...interface{}) {
if ftp.debugging >= 1 {
log.Println(params...)
}
}
func (ftp *FTP) Stop() {
ftp.stop = make(chan bool)
ftp.stop <- true
}
// NewFTP creates a new FTP client using a debug level, default is 0, which is disabled.
// The FTP server uses the passive tranfer mode by default.
//
// Debuglevel:
// 0 -> disabled
// 1 -> information
// 2 -> verbose
//
func NewFTP(debuglevel int) *FTP {
logger := log.New(os.Stdout, "", log.LstdFlags) //syslog.NewLogger(syslog.LOG_ERR, 999)
ftp := &FTP{
debugging: debuglevel,
Port: DefaultFtpPort,
logger: logger,
//timeoutInMsec: DefaultTimeoutInMsec,
passiveserver: true,
}
return ftp
}
//Set ftp dial timeout
func (ftp *FTP) SetFTPTimeout(timeout time.Duration) error {
if ftp == nil {
return errors.New("ftp client is nil")
}
if timeout <= 0 {
return errors.New("timeout must be greater than 0")
}
ftp.timeoutInMsec = timeout
return nil
}
// Connect connects to the host by using the specified port or the default one if the value is <=0.
func (ftp *FTP) Connect(host string, port int, socks5ProxyUrl string) (resp *Response, err error) {
if len(host) == 0 {
return nil, errors.New("The host must be specified")
}
ftp.Host = host
if port <= 0 {
port = DefaultFtpPort
} else {
ftp.Port = port
}
addr := fmt.Sprintf("%s:%d", ftp.Host, ftp.Port)
// use the system proxy if emtpy
if socks5ProxyUrl == "" {
ftp.writeInfo("using environment proxy, url: ", os.Getenv("all_proxy"))
ftp.dialer = proxy.FromEnvironment()
} else {
ftp.dialer = proxy.Direct
if u, err1 := url.Parse(socks5ProxyUrl); err1 == nil {
p, err2 := proxy.FromURL(u, proxy.Direct)
if err2 == nil {
ftp.dialer = p
}
}
}
err = ftp.NewConn(addr)
if err != nil {
return
}
ftp.writeInfo("host:", ftp.Host, " port:", strconv.Itoa(ftp.Port), " proxy enabled:", ftp.dialer != proxy.Direct)
// NOTE: this is an absolute time that needs refreshing after each READ/WRITE net operation
//ftp.conn.conn.SetDeadline(getTimeoutInMsec(ftp.timeoutInMsec))
if resp, err = ftp.Read(NONE_FTP_CMD); err != nil {
return
}
ftp.welcome = resp.Message
ftp.writeInfo("Successfully connected on local address:", ftp.conn.LocalAddr())
return
}
// SetPassive sets the mode to passive or active for data transfers.
// With a false statement use the normal PORT mode.
// With a true statement use the PASV command.
func (ftp *FTP) SetPassive(ispassive bool) {
ftp.passiveserver = ispassive
}
// Login logs on to the server.
func (ftp *FTP) Login(username, password string, acct string) (response *Response, err error) {
//Login, default anonymous.
if len(username) == 0 {
username = "anonymous"
}
if len(password) == 0 {
password = ""
}
if username == "anonymous" && len(password) == 0 {
// If there is no anonymous ftp password specified
// then we'll just use anonymous@
// We don't send any other thing because:
// - We want to remain anonymous
// - We want to stop SPAM
// - We don't want to let ftp sites to discriminate by the user,
// host or country.
password = password + "anonymous@"
}
ftp.writeInfo("username:", username)
tempResponse, err := ftp.SendAndRead(USER_FTP_CMD, username)
if err != nil {
return
}
// if tempResponse.getFirstChar() == "3" {
if tempResponse.Code == StatusUserOK {
tempResponse, err = ftp.SendAndRead(PASSWORD_FTP_CMD, password)
if err != nil {
return
}
}
if tempResponse.getFirstChar() == "3" {
tempResponse, err = ftp.SendAndRead(ACCT_FTP_CMD, acct)
if err != nil {
return
}
}
// if tempResponse.getFirstChar() != "2" {
if tempResponse.Code != StatusLoggedIn {
err = NewErrReply(errors.New(tempResponse.Message))
return
}
return tempResponse, err
}
// Abort interrupts a file transfer, which uses out-of-band data.
// This does not follow the procedure from the RFC to send Telnet IP and Synch;
// that does not seem to work with all servers. Instead just send the ABOR command as OOB data.
func (ftp *FTP) Abort() (response *Response, err error) {
return ftp.SendAndRead(ABORT_FTP_CMD)
}
// SendPort sends a PORT command with the current host and given port number
func (ftp *FTP) SendPort(host string, port int) (response *Response, err error) {
hbytes := strings.Split(host, ".") // return all substrings
pbytes := []string{strconv.Itoa(port / 256), strconv.Itoa(port % 256)}
bytes := strings.Join(append(hbytes, pbytes...), ",")
return ftp.SendAndRead(PORT_FTP_CMD, bytes)
}
// makePasv sends a PASV command and returns the host and port number to be used for the data transfer connection.
func (ftp *FTP) makePasv() (host string, port int, err error) {
var resp *Response
resp, err = ftp.SendAndRead(PASV_FTP_CMD)
if err != nil {
return
}
return parse227(resp)
}
// Acct sends an ACCT command.
func (ftp *FTP) Acct() (response *Response, err error) {
return ftp.SendAndRead(ACCT_FTP_CMD)
}
// Mlsd lists a directory in a standardized format by using MLSD
// command (RFC-3659). If path is omitted the current directory
// is assumed. "facts" is a list of strings representing the type
// of information desired (e.g. ["type", "size", "perm"]).
// Return a generator object yielding a tuple of two elements
// for every file found in path.
// First element is the file name, the second one is a dictionary
// including a variable number of "facts" depending on the server
// and whether "facts" argument has been provided.
func (ftp *FTP) Mlsd(path string, facts []string) (ls []*NameFactsLine, err error) {
if len(facts) > 0 {
if _, err = ftp.Opts("MLST", strings.Join(facts, ";")+";"); err != nil {
return nil, err
}
}
sw := &stringSliceWriter{make([]string, 0, 50)}
if err = ftp.GetLines(MLSD_FTP_CMD, sw, path); err != nil {
return nil, err
}
ls = make([]*NameFactsLine, len(sw.s))
for _, l := range sw.s {
tkns := strings.Split(strings.TrimSpace(l), " ")
name := tkns[0]
facts := strings.Split(tkns[1], ";")
ftp.writeInfo("Found facts:", facts)
vals := make(map[string]string, len(facts)-1)
for i := 0; i < len(facts)-1; i++ {
fpair := strings.Split(facts[i], "=")
vals[fpair[0]] = fpair[1]
}
ls = append(ls, &NameFactsLine{strings.ToLower(name), vals})
}
return
}
// Feat lists all new FTP features that the server supports beyond those described in RFC 959.
func (ftp *FTP) Feat(params ...string) (fts []string, err error) {
var r *Response
if r, err = ftp.SendAndRead(FEAT_FTP_CMD); err != nil {
return
}
return parse211(r)
}
// Nlst returns a list of file in a directory, by default the current.
func (ftp *FTP) Nlst(params ...string) (filelist []string, err error) {
return ftp.getList(NLST_FTP_CMD, params...)
}
// Dir returns a list of file in a directory in long form, by default the current.
func (ftp *FTP) Dir(params ...string) (filelist []string, err error) {
return ftp.getList(LIST_FTP_CMD, params...)
}
func (ftp *FTP) getList(cmd FtpCmd, params ...string) (filelist []string, err error) {
files := make([]string, 0, 50)
sw := &stringSliceWriter{files}
if err = ftp.GetLines(cmd, sw, params...); err != nil {
return nil, err
}
return sw.s, nil
}
// Rename renames a file.
func (ftp *FTP) Rename(fromname string, toname string) (response *Response, err error) {
tempResponse, err := ftp.SendAndRead(RENAMEFROM_FTP_CMD, fromname)
if err != nil {
return nil, err
}
if tempResponse.getFirstChar() != "3" {
err = NewErrReply(errors.New(tempResponse.Message))
return nil, err
}
return ftp.SendAndRead(RENAMETO_FTP_CMD, toname)
}
// Delete deletes a file.
func (ftp *FTP) Delete(filename string) (response *Response, err error) {
tempResponse, err := ftp.SendAndRead(DELETE_FTP_CMD, filename)
if err != nil {
return nil, err
}
if c := tempResponse.Code; c == StatusRequestedFileActionOK || c == StatusCommandOK {
return tempResponse, nil
} else {
return nil, NewErrReply(errors.New(tempResponse.Message))
}
return
}
// Cwd changes to current directory.
func (ftp *FTP) Cwd(dirname string) (response *Response, err error) {
if dirname == ".." {
return ftp.SendAndRead(CDUP_FTP_CMD)
} else if dirname == "" {
dirname = "."
}
return ftp.SendAndRead(CWD_FTP_CMD, dirname)
}
// Size retrieves the size of a file.
func (ftp *FTP) Size(filename string) (size int, err error) {
response, err := ftp.SendAndRead(SIZE_FTP_CMD, filename)
if err != nil {
return
}
if response.Code == StatusFile {
//size, _ = strconv.Atoi(strings.TrimSpace(response.Message[3:]))
size, _ = strconv.Atoi(response.Message)
return size, err
}
return
}
// Mkd creates a directory and returns its full pathname.
func (ftp *FTP) Mkd(dirname string) (dname string, err error) {
var response *Response
response, err = ftp.SendAndRead(MKDIR_FTP_CMD, dirname)
if err != nil {
return
}
// fix around non-compliant implementations such as IIS shipped
// with Windows server 2003
if response.Code != StatusPathCreated {
return "", nil
}
return parse257(response)
}
// Rmd removes a directory.
func (ftp *FTP) Rmd(dirname string) (response *Response, err error) {
return ftp.SendAndRead(RMDIR_FTP_CMD, dirname)
}
// Pwd returns the current working directory.
func (ftp *FTP) Pwd() (dirname string, err error) {
response, err := ftp.SendAndRead(PWDIR_FTP_CMD)
// fix around non-compliant implementations such as IIS shipped
// with Windows server 2003
if err != nil {
return "", err
}
if response.Code != 257 {
return "", nil
}
return parse257(response)
}
// Quits sends a QUIT command and closes the connection.
func (ftp *FTP) Quit() (response *Response, err error) {
response, err = ftp.SendAndRead(QUIT_FTP_CMD)
if ftp.conn != nil {
ftp.conn.Close()
ftp.conn = nil
}
return
}
// DownloadFile downloads a file and stores it locally.
// There are two modes:
// - binary, useLineMode = false
// - line by line (text), useLineMode = true
func (ftp *FTP) DownloadFile(remotename string, localpath string, useLineMode bool) (err error) {
// remove local file
os.Remove(localpath)
var f *os.File
f, err = os.OpenFile(localpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
defer f.Close()
if err != nil {
return
}
if useLineMode {
w := newTextFileWriter(f)
defer w.bw.Flush() // remember to flush
if err = ftp.GetLines(RETR_FTP_CMD, w, remotename); err != nil {
return err
}
} else {
if err = ftp.GetBytes(RETR_FTP_CMD, f, BLOCK_SIZE, remotename); err != nil {
return err
}
}
return err
}
// UploadFile uploads a file from a local path to the current folder (see Cwd too) on the FTP server.
// A remotename needs to be specified.
// There are two modes set via the useLineMode flag:
// - binary, useLineMode = false
// - line by line (text), useLineMode = true
func (ftp *FTP) UploadFile(remotename string, localpath string, useLineMode bool, callback Callback) (err error) {
var f *os.File
f, err = os.Open(localpath)
defer f.Close()
if err != nil {
return
}
if useLineMode {
if err = ftp.StoreLines(STORE_FTP_CMD, f, remotename, localpath, callback); err != nil {
return err
}
} else {
if err = ftp.StoreBytes(STORE_FTP_CMD, f, BLOCK_SIZE, remotename, localpath, callback); err != nil {
return err
}
}
return err
}
// Opts returns a list of file in a directory in long form, by default the current.
func (ftp *FTP) Opts(params ...string) (response *Response, err error) {
return ftp.SendAndRead(OPTS_FTP_CMD, params...)
}
// GetLines retrieves data in line mode.
// Args:
// cmd: A RETR, LIST, NLST, or MLSD command.
// writer: of interface type io.Writer that is called for each line with the trailing CRLF stripped.
//
// returns:
// The response code.
func (ftp *FTP) GetLines(cmd FtpCmd, writer io.Writer, params ...string) (err error) {
var conn net.Conn
if _, err = ftp.SendAndRead(TYPE_A_FTP_CMD); err != nil {
return
}
// wrap this code up to guarantee the connection disposal via a defer
separateCall := func() error {
if conn, _, err = ftp.transferCmd(cmd, params...); err != nil {
return err
}
defer conn.Close() // close the connection on exit
ftpReader := textproto.NewConn(conn)
ftp.writeInfo("Try and get lines via connection for remote address:", conn.RemoteAddr().String())
for {
line, err := ftpReader.ReadLineBytes()
if err != nil {
if err == io.EOF {
ftp.writeInfo("Reached end of buffer with line:", line)
break
}
return err
}
if _, err1 := writer.Write(line); err1 != nil {
return err1
}
}
return nil
}
if err := separateCall(); err != nil {
return err
}
ftp.writeInfo("Reading final empty line")
_, err = ftp.Read(cmd)
return
}
// GetBytes retrieves data in binary mode.
// Args:
// cmd: A RETR command.
// callback: A single parameter callable to be called on each
// block of data read.
// blocksize: The maximum number of bytes to read from the
// socket at one time. [default: 8192]
//
//Returns:
// The response code.
func (ftp *FTP) GetBytes(cmd FtpCmd, writer io.Writer, blocksize int, params ...string) (err error) {
var conn net.Conn
if _, err = ftp.SendAndRead(TYPE_I_FTP_CMD); err != nil {
return
}
// wrap this code up to guarantee the connection disposal via a defer
separateCall := func() error {
if conn, _, err = ftp.transferCmd(cmd, params...); err != nil {
return err
}
defer conn.Close() // close the connection on exit
bufReader := bufio.NewReaderSize(conn, blocksize)
ftp.writeInfo("Try and get bytes via connection for remote address:", conn.RemoteAddr().String())
s := make([]byte, blocksize)
var n int
for {
n, err = bufReader.Read(s)
ftp.writeInfo("GETBYTES: Number of bytes read:", n)
if _, err1 := writer.Write(s[:n]); err1 != nil {
return err1
}
if tmpfile, ok := writer.(*os.File); ok {
tmpfile.Sync()
}
if err != nil {
if err == io.EOF {
break
}
return err
}
}
return nil
}
if err := separateCall(); err != nil {
return err
}
_, err = ftp.Read(cmd)
return
}
func (ftp *FTP) DownloadResumeFile(remotename string, localpath string, useLineMode bool) (err error) {
// remove local file
// os.Remove(localpath)
var f *os.File
f, err = os.OpenFile(localpath, os.O_WRONLY|os.O_CREATE, 0644)
defer f.Close()
if err != nil {
return
}
if useLineMode {
w := newTextFileWriter(f)
defer w.bw.Flush() // remember to flush
if err = ftp.GetLines(RETR_FTP_CMD, w, remotename); err != nil {
return err
}
} else {
var stat os.FileInfo
stat, err = f.Stat()
if err != nil {
return
}
offset := stat.Size()
if err = ftp.ResumeFile(RETR_FTP_CMD, f, offset, BLOCK_SIZE, remotename); err != nil {
return err
}
}
return err
}
func (ftp *FTP) ResumeFile(cmd FtpCmd, writer *os.File, offset int64, blocksize int, params ...string) (err error) {
var conn net.Conn
if _, err = ftp.SendAndRead(TYPE_I_FTP_CMD); err != nil {
return
}
// wrap this code up to guarantee the connection disposal via a defer
separateCall := func() error {
if offset != 0 {
if res, err := ftp.SendAndRead(REST_FTP_CMD, fmt.Sprintf("%d", offset)); err != nil {
return err
} else {
fmt.Println(res)
}
}
if conn, _, err = ftp.transferCmd(cmd, params...); err != nil {
return err
}
defer conn.Close() // close the connection on exit
bufReader := bufio.NewReaderSize(conn, blocksize)
ftp.writeInfo("Try and get bytes via connection for remote address:", conn.RemoteAddr().String())
s := make([]byte, blocksize)
var n int
for {
if ftp.stop != nil {
select {
case <-ftp.stop:
return NewErrStop
default:
n, err = bufReader.Read(s)
ftp.writeInfo("GETBYTES: Number of bytes read:", n)
if _, err1 := writer.WriteAt(s[:n], offset); err1 != nil {
return err1
}
if err2 := writer.Sync(); err2 != nil {
return err2
}
offset += int64(n)
if err != nil {
if err == io.EOF {
break
}
return err
}
}
} else {
n, err = bufReader.Read(s)
ftp.writeInfo("GETBYTES: Number of bytes read:", n)
if _, err1 := writer.WriteAt(s[:n], offset); err1 != nil {
return err1
}
if err2 := writer.Sync(); err2 != nil {
return err2
}
offset += int64(n)
if err != nil {
if err == io.EOF {
break
}
return err
}
}
}
return nil
}
if err := separateCall(); err != nil {
return err
}
_, err = ftp.Read(cmd)
return
}
// StoreLines stores a file in line mode.
//
// Args:
// cmd: A STOR command.
// reader: A reader object with a ReadLine() method.
// callback: An optional single parameter callable that is called on
// on each line after it is sent. [default: None]
//
// Returns:
// The response code.
func (ftp *FTP) StoreLines(cmd FtpCmd, reader io.Reader, remotename string, filename string, callback Callback) (err error) {
var conn net.Conn
if _, err = ftp.SendAndRead(TYPE_A_FTP_CMD); err != nil {
return
}
// wrap this code up to guarantee the connection disposal via a defer
separateCall := func() error {
if conn, _, err = ftp.transferCmd(cmd, remotename); err != nil {
return err
}
defer conn.Close() // close the connection on exit
ftp.writeInfo("Try and write lines via connection for remote address:", conn.RemoteAddr().String())
//lineReader := bufio.NewReader(reader)
lineReader := bufio.NewReader(reader)
var tot int64
for {
var n int
var eof bool
line, _, err := lineReader.ReadLine()
if err != nil {
eof = err == io.EOF
if !eof {
return err
}
}
// !Remember to convert to string (UTF-8 encoding)
if !eof {
n, err = fmt.Fprintln(conn, string(line))
if err != nil {
return err
}
}
if callback != nil {
tot += int64(n)
callback(&CallbackInfo{remotename, filename, tot, eof})
}
if eof {
break
}
}
return nil
}
if err := separateCall(); err != nil {
return err
}
ftp.writeInfo("Reading final empty line")
_, err = ftp.Read(cmd)
return
}
// StoreBytes uploads bytes in chunks defined by the blocksize parameter.
// It uses an io.Reader to read the input data.
func (ftp *FTP) StoreBytes(cmd FtpCmd, reader io.Reader, blocksize int, remotename string, filename string, callback Callback) (err error) {
var conn net.Conn
if _, err = ftp.SendAndRead(TYPE_I_FTP_CMD); err != nil {
return
}
// wrap this code up to guarantee the connection disposal via a defer
separateCall := func() error {
if conn, _, err = ftp.transferCmd(cmd, remotename); err != nil {
return err
}
defer conn.Close() // close the connection on exit
bufReader := bufio.NewReaderSize(reader, blocksize)
ftp.writeInfo("Try and store bytes via connection for remote address:", conn.RemoteAddr().String())
s := make([]byte, blocksize)
var tot int64
for {
var nr, nw int
var eof bool
nr, err = bufReader.Read(s)
eof = err == io.EOF
if err != nil && !eof {
return err
}
if nw, err = conn.Write(s[:nr]); err != nil {
return err
}
if callback != nil {
tot += int64(nw)
callback(&CallbackInfo{remotename, filename, tot, eof})
}
if eof {
break
}
}
return nil
}
if err := separateCall(); err != nil {
return err
}
_, err = ftp.Read(cmd)
return
}
// transferCmd initializes a tranfer over the data connection.
//
// If the transfer is active, send a port command and the tranfer command
// then accept the connection. If the server is passive, send a pasv command, connect to it
// and start the tranfer command. Either way return the connection and the expected size of the transfer.
// The expected size may be none if it could be not be determined.
func (ftp *FTP) transferCmd(cmd FtpCmd, params ...string) (conn net.Conn, size int, err error) {
var listener net.Listener
ftp.writeInfo("Server is passive:", ftp.passiveserver)
if ftp.passiveserver {
host, port, error := ftp.makePasv()
if ftp.conn.LocalAddr().Network() != host {
ftp.writeInfo("The remote server answered with a different host address, which is", host, ", using the orginal host instead:", ftp.Host)
host = ftp.Host
}
if error != nil {
return nil, -1, error
}
addr := fmt.Sprintf("%s:%d", host, port)
if ftp.timeoutInMsec > 0 {
if conn, err = net.DialTimeout("tcp", addr, ftp.timeoutInMsec); err != nil {
ftp.writeInfo("Dial error, address:", addr, "error:", err)
return
}
} else {
if conn, err = ftp.dialer.Dial("tcp", addr); err != nil {
ftp.writeInfo("Dial error, address:", addr, "error:", err, "proxy enabled:", ftp.dialer != proxy.Direct)
return
}
}
} else {
if listener, err = ftp.makePort(); err != nil {
return
}
ftp.writeInfo("Listener created for non-passive mode")
}
var resp *Response
if resp, err = ftp.SendAndRead(cmd, params...); err != nil {
resp = nil
return
}
// Some servers apparently send a 200 reply to
// a LIST or STOR command, before the 150 reply
// (and way before the 226 reply). This seems to
// be in violation of the protocol (which only allows
// 1xx or error messages for LIST), so we just discard
// this response.
if resp.getFirstChar() == "2" {
resp, err = ftp.Read(cmd)
}
if resp.getFirstChar() != "1" {
err = NewErrReply(errors.New(resp.Message))
return
}
// not passive, open connection and close it then
if listener != nil {
ftp.writeInfo("Preparing to listen for non-passive mode.")
if conn, err = listener.Accept(); err != nil {
conn = nil
return
}
ftp.writeInfo("Trying to communicate with local host: ", conn.LocalAddr())
defer listener.Close() // close after getting the connection
}
if resp.Code == 150 {
// this is conditional in case we received a 125
ftp.writeInfo("Parsing return code 150")
size, err = parse150ForSize(resp)
}
return conn, size, err
}
// makePort creates a new communication port and return a listener for this.
func (ftp *FTP) makePort() (listener net.Listener, err error) {
tcpAddr := ftp.conn.LocalAddr()
network := tcpAddr.Network()
var la *net.TCPAddr
if la, err = net.ResolveTCPAddr(network, tcpAddr.String()); err != nil {
return
}
// get the new address