├── LICENSE ├── README.md ├── doc.go ├── ftp.go ├── ftp_test.go ├── status.go └── upload.go /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | goftp 2 | ===== 3 | 4 | Golang FTP library with Walk support. 5 | 6 | ## Features 7 | 8 | * AUTH TLS support 9 | * Walk 10 | 11 | ## Sample 12 | ```go 13 | package main 14 | 15 | import ( 16 | "crypto/sha256" 17 | "crypto/tls" 18 | "fmt" 19 | "io" 20 | "os" 21 | 22 | "encoding/hex" 23 | "gopkg.in/dutchcoders/goftp.v1" 24 | ) 25 | 26 | func main() { 27 | var err error 28 | var ftp *goftp.FTP 29 | 30 | // For debug messages: goftp.ConnectDbg("ftp.server.com:21") 31 | if ftp, err = goftp.Connect("ftp.server.com:21"); err != nil { 32 | panic(err) 33 | } 34 | 35 | defer ftp.Close() 36 | fmt.Println("Successfully connected to", server) 37 | 38 | // TLS client authentication 39 | config := tls.Config{ 40 | InsecureSkipVerify: true, 41 | ClientAuth: tls.RequestClientCert, 42 | } 43 | 44 | if err = ftp.AuthTLS(config); err != nil { 45 | panic(err) 46 | } 47 | 48 | // Username / password authentication 49 | if err = ftp.Login("username", "password"); err != nil { 50 | panic(err) 51 | } 52 | 53 | if err = ftp.Cwd("/"); err != nil { 54 | panic(err) 55 | } 56 | 57 | var curpath string 58 | if curpath, err = ftp.Pwd(); err != nil { 59 | panic(err) 60 | } 61 | 62 | fmt.Printf("Current path: %s", curpath) 63 | 64 | // Get directory listing 65 | var files []string 66 | if files, err = ftp.List(""); err != nil { 67 | panic(err) 68 | } 69 | fmt.Println("Directory listing:", files) 70 | 71 | // Upload a file 72 | var file *os.File 73 | if file, err = os.Open("/tmp/test.txt"); err != nil { 74 | panic(err) 75 | } 76 | 77 | if err := ftp.Stor("/test.txt", file); err != nil { 78 | panic(err) 79 | } 80 | 81 | // Download each file into local memory, and calculate it's sha256 hash 82 | err = ftp.Walk("/", func(path string, info os.FileMode, err error) error { 83 | _, err = ftp.Retr(path, func(r io.Reader) error { 84 | var hasher = sha256.New() 85 | if _, err = io.Copy(hasher, r); err != nil { 86 | return err 87 | } 88 | 89 | hash := fmt.Sprintf("%s %x", path, hex.EncodeToString(hasher.Sum(nil))) 90 | fmt.Println(hash) 91 | 92 | return err 93 | }) 94 | 95 | return nil 96 | }) 97 | } 98 | ```` 99 | 100 | ## Contributions 101 | 102 | Contributions are welcome. 103 | 104 | * Sourav Datta: for his work on the anonymous user login and multiline return status. 105 | * Vincenzo La Spesa: for his work on resolving login issues with specific ftp servers 106 | 107 | 108 | ## Creators 109 | 110 | **Remco Verhoef** 111 | - 112 | - 113 | 114 | ## Copyright and license 115 | 116 | Code and documentation copyright 2011-2014 Remco Verhoef. 117 | Code released under [the MIT license](LICENSE). 118 | 119 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2014 DutchCoders [https://github.com/dutchcoders/] 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | // ftp client library with Walk and TLS support 26 | 27 | package goftp 28 | -------------------------------------------------------------------------------- /ftp.go: -------------------------------------------------------------------------------- 1 | package goftp 2 | 3 | import ( 4 | "bufio" 5 | "crypto/tls" 6 | "errors" 7 | "fmt" 8 | "io" 9 | "log" 10 | "net" 11 | "os" 12 | "regexp" 13 | "strconv" 14 | "strings" 15 | "time" 16 | ) 17 | 18 | // RePwdPath is the default expression for matching files in the current working directory 19 | var RePwdPath = regexp.MustCompile(`\"(.*)\"`) 20 | 21 | // FTP is a session for File Transfer Protocol 22 | type FTP struct { 23 | conn net.Conn 24 | 25 | addr string 26 | 27 | debug bool 28 | tlsconfig *tls.Config 29 | 30 | reader *bufio.Reader 31 | writer *bufio.Writer 32 | } 33 | 34 | // Close ends the FTP connection 35 | func (ftp *FTP) Close() error { 36 | return ftp.conn.Close() 37 | } 38 | 39 | type ( 40 | // WalkFunc is called on each path in a Walk. Errors are filtered through WalkFunc 41 | WalkFunc func(path string, info os.FileMode, err error) error 42 | 43 | // RetrFunc is passed to Retr and is the handler for the stream received for a given path 44 | RetrFunc func(r io.Reader) error 45 | ) 46 | 47 | func parseLine(line string) (perm string, t string, filename string) { 48 | for _, v := range strings.Split(line, ";") { 49 | v2 := strings.Split(v, "=") 50 | 51 | switch v2[0] { 52 | case "perm": 53 | perm = v2[1] 54 | case "type": 55 | t = v2[1] 56 | default: 57 | filename = v[1 : len(v)-2] 58 | } 59 | } 60 | return 61 | } 62 | 63 | // Walk walks recursively through path and call walkfunc for each file 64 | func (ftp *FTP) Walk(path string, walkFn WalkFunc) (err error) { 65 | /* 66 | if err = walkFn(path, os.ModeDir, nil); err != nil { 67 | if err == filepath.SkipDir { 68 | return nil 69 | } 70 | } 71 | */ 72 | if ftp.debug { 73 | log.Printf("Walking: '%s'\n", path) 74 | } 75 | 76 | var lines []string 77 | 78 | if lines, err = ftp.List(path); err != nil { 79 | return 80 | } 81 | 82 | for _, line := range lines { 83 | _, t, subpath := parseLine(line) 84 | 85 | switch t { 86 | case "dir": 87 | if subpath == "." { 88 | } else if subpath == ".." { 89 | } else { 90 | if err = ftp.Walk(path+subpath+"/", walkFn); err != nil { 91 | return 92 | } 93 | } 94 | case "file": 95 | if err = walkFn(path+subpath, os.FileMode(0), nil); err != nil { 96 | return 97 | } 98 | } 99 | } 100 | 101 | return 102 | } 103 | 104 | // Quit sends quit to the server and close the connection. No need to Close after this. 105 | func (ftp *FTP) Quit() (err error) { 106 | if _, err := ftp.cmd(StatusConnectionClosing, "QUIT"); err != nil { 107 | return err 108 | } 109 | 110 | ftp.conn.Close() 111 | ftp.conn = nil 112 | 113 | return nil 114 | } 115 | 116 | // Noop will send a NOOP (no operation) to the server 117 | func (ftp *FTP) Noop() (err error) { 118 | _, err = ftp.cmd(StatusOK, "NOOP") 119 | return 120 | } 121 | 122 | // RawCmd sends raw commands to the remote server. Returns response code as int and response as string. 123 | func (ftp *FTP) RawCmd(command string, args ...interface{}) (code int, line string) { 124 | if ftp.debug { 125 | log.Printf("Raw-> %s\n", fmt.Sprintf(command, args...)) 126 | } 127 | 128 | code = -1 129 | var err error 130 | if err = ftp.send(command, args...); err != nil { 131 | return code, "" 132 | } 133 | if line, err = ftp.receive(); err != nil { 134 | return code, "" 135 | } 136 | code, err = strconv.Atoi(line[:3]) 137 | if ftp.debug { 138 | log.Printf("Raw<- <- %d \n", code) 139 | } 140 | return code, line 141 | } 142 | 143 | // private function to send command and compare return code with expects 144 | func (ftp *FTP) cmd(expects string, command string, args ...interface{}) (line string, err error) { 145 | if err = ftp.send(command, args...); err != nil { 146 | return 147 | } 148 | 149 | if line, err = ftp.receive(); err != nil { 150 | return 151 | } 152 | 153 | if !strings.HasPrefix(line, expects) { 154 | err = errors.New(line) 155 | return 156 | } 157 | 158 | return 159 | } 160 | 161 | // Rename file on the remote host 162 | func (ftp *FTP) Rename(from string, to string) (err error) { 163 | if _, err = ftp.cmd(StatusActionPending, "RNFR %s", from); err != nil { 164 | return 165 | } 166 | 167 | if _, err = ftp.cmd(StatusActionOK, "RNTO %s", to); err != nil { 168 | return 169 | } 170 | 171 | return 172 | } 173 | 174 | // Mkd makes a directory on the remote host 175 | func (ftp *FTP) Mkd(path string) error { 176 | _, err := ftp.cmd(StatusPathCreated, "MKD %s", path) 177 | return err 178 | } 179 | 180 | // Rmd remove directory 181 | func (ftp *FTP) Rmd(path string) (err error) { 182 | _, err = ftp.cmd(StatusActionOK, "RMD %s", path) 183 | return 184 | } 185 | 186 | // Pwd gets current path on the remote host 187 | func (ftp *FTP) Pwd() (path string, err error) { 188 | var line string 189 | if line, err = ftp.cmd(StatusPathCreated, "PWD"); err != nil { 190 | return 191 | } 192 | 193 | res := RePwdPath.FindAllStringSubmatch(line[4:], -1) 194 | 195 | path = res[0][1] 196 | return 197 | } 198 | 199 | // Cwd changes current working directory on remote host to path 200 | func (ftp *FTP) Cwd(path string) (err error) { 201 | _, err = ftp.cmd(StatusActionOK, "CWD %s", path) 202 | return 203 | } 204 | 205 | // Dele deletes path on remote host 206 | func (ftp *FTP) Dele(path string) (err error) { 207 | if err = ftp.send("DELE %s", path); err != nil { 208 | return 209 | } 210 | 211 | var line string 212 | if line, err = ftp.receive(); err != nil { 213 | return 214 | } 215 | 216 | if !strings.HasPrefix(line, StatusActionOK) { 217 | return errors.New(line) 218 | } 219 | 220 | return 221 | } 222 | 223 | // AuthTLS secures the ftp connection by using TLS 224 | func (ftp *FTP) AuthTLS(config *tls.Config) error { 225 | if _, err := ftp.cmd("234", "AUTH TLS"); err != nil { 226 | return err 227 | } 228 | 229 | // wrap tls on existing connection 230 | ftp.tlsconfig = config 231 | 232 | ftp.conn = tls.Client(ftp.conn, config) 233 | ftp.writer = bufio.NewWriter(ftp.conn) 234 | ftp.reader = bufio.NewReader(ftp.conn) 235 | 236 | if _, err := ftp.cmd(StatusOK, "PBSZ 0"); err != nil { 237 | return err 238 | } 239 | 240 | if _, err := ftp.cmd(StatusOK, "PROT P"); err != nil { 241 | return err 242 | } 243 | 244 | return nil 245 | } 246 | 247 | // ReadAndDiscard reads all the buffered bytes and returns the number of bytes 248 | // that cleared from the buffer 249 | func (ftp *FTP) ReadAndDiscard() (int, error) { 250 | var i int 251 | bufferSize := ftp.reader.Buffered() 252 | for i = 0; i < bufferSize; i++ { 253 | if _, err := ftp.reader.ReadByte(); err != nil { 254 | return i, err 255 | } 256 | } 257 | return i, nil 258 | } 259 | 260 | // Type changes transfer type. 261 | func (ftp *FTP) Type(t TypeCode) error { 262 | _, err := ftp.cmd(StatusOK, "TYPE %s", t) 263 | return err 264 | } 265 | 266 | // TypeCode for the representation types 267 | type TypeCode string 268 | 269 | const ( 270 | // TypeASCII for ASCII 271 | TypeASCII = "A" 272 | // TypeEBCDIC for EBCDIC 273 | TypeEBCDIC = "E" 274 | // TypeImage for an Image 275 | TypeImage = "I" 276 | // TypeLocal for local byte size 277 | TypeLocal = "L" 278 | ) 279 | 280 | func (ftp *FTP) receiveLine() (string, error) { 281 | line, err := ftp.reader.ReadString('\n') 282 | 283 | if ftp.debug { 284 | log.Printf("< %s", line) 285 | } 286 | 287 | return line, err 288 | } 289 | 290 | func (ftp *FTP) receive() (string, error) { 291 | line, err := ftp.receiveLine() 292 | 293 | if err != nil { 294 | return line, err 295 | } 296 | 297 | if (len(line) >= 4) && (line[3] == '-') { 298 | //Multiline response 299 | closingCode := line[:3] + " " 300 | for { 301 | str, err := ftp.receiveLine() 302 | line = line + str 303 | if err != nil { 304 | return line, err 305 | } 306 | if len(str) < 4 { 307 | if ftp.debug { 308 | log.Println("Uncorrectly terminated response") 309 | } 310 | break 311 | } else { 312 | if str[:4] == closingCode { 313 | break 314 | } 315 | } 316 | } 317 | } 318 | ftp.ReadAndDiscard() 319 | //fmt.Println(line) 320 | return line, err 321 | } 322 | 323 | func (ftp *FTP) receiveNoDiscard() (string, error) { 324 | line, err := ftp.receiveLine() 325 | 326 | if err != nil { 327 | return line, err 328 | } 329 | 330 | if (len(line) >= 4) && (line[3] == '-') { 331 | //Multiline response 332 | closingCode := line[:3] + " " 333 | for { 334 | str, err := ftp.receiveLine() 335 | line = line + str 336 | if err != nil { 337 | return line, err 338 | } 339 | if len(str) < 4 { 340 | if ftp.debug { 341 | log.Println("Uncorrectly terminated response") 342 | } 343 | break 344 | } else { 345 | if str[:4] == closingCode { 346 | break 347 | } 348 | } 349 | } 350 | } 351 | //ftp.ReadAndDiscard() 352 | //fmt.Println(line) 353 | return line, err 354 | } 355 | 356 | func (ftp *FTP) send(command string, arguments ...interface{}) error { 357 | if ftp.debug { 358 | log.Printf("> %s", fmt.Sprintf(command, arguments...)) 359 | } 360 | 361 | command = fmt.Sprintf(command, arguments...) 362 | command += "\r\n" 363 | 364 | if _, err := ftp.writer.WriteString(command); err != nil { 365 | return err 366 | } 367 | 368 | if err := ftp.writer.Flush(); err != nil { 369 | return err 370 | } 371 | 372 | return nil 373 | } 374 | 375 | // Pasv enables passive data connection and returns port number 376 | 377 | func (ftp *FTP) Pasv() (port int, err error) { 378 | doneChan := make(chan int, 1) 379 | go func() { 380 | defer func() { 381 | doneChan <- 1 382 | }() 383 | var line string 384 | if line, err = ftp.cmd("227", "PASV"); err != nil { 385 | return 386 | } 387 | re := regexp.MustCompile(`\((.*)\)`) 388 | res := re.FindAllStringSubmatch(line, -1) 389 | if len(res) == 0 || len(res[0]) < 2 { 390 | err = errors.New("PasvBadAnswer") 391 | return 392 | } 393 | s := strings.Split(res[0][1], ",") 394 | if len(s) < 2 { 395 | err = errors.New("PasvBadAnswer") 396 | return 397 | } 398 | l1, _ := strconv.Atoi(s[len(s)-2]) 399 | l2, _ := strconv.Atoi(s[len(s)-1]) 400 | 401 | port = l1<<8 + l2 402 | 403 | return 404 | }() 405 | 406 | select { 407 | case _ = <-doneChan: 408 | 409 | case <-time.After(time.Second * 10): 410 | err = errors.New("PasvTimeout") 411 | ftp.Close() 412 | } 413 | 414 | return 415 | } 416 | 417 | // open new data connection 418 | func (ftp *FTP) newConnection(port int) (conn net.Conn, err error) { 419 | addr := fmt.Sprintf("%s:%d", strings.Split(ftp.addr, ":")[0], port) 420 | 421 | if ftp.debug { 422 | log.Printf("Connecting to %s\n", addr) 423 | } 424 | 425 | if conn, err = net.Dial("tcp", addr); err != nil { 426 | return 427 | } 428 | 429 | if ftp.tlsconfig != nil { 430 | conn = tls.Client(conn, ftp.tlsconfig) 431 | } 432 | 433 | return 434 | } 435 | 436 | // Stor uploads file to remote host path, from r 437 | func (ftp *FTP) Stor(path string, r io.Reader) (err error) { 438 | if err = ftp.Type(TypeImage); err != nil { 439 | return 440 | } 441 | 442 | var port int 443 | if port, err = ftp.Pasv(); err != nil { 444 | return 445 | } 446 | 447 | if err = ftp.send("STOR %s", path); err != nil { 448 | return 449 | } 450 | 451 | var pconn net.Conn 452 | if pconn, err = ftp.newConnection(port); err != nil { 453 | return 454 | } 455 | defer pconn.Close() 456 | 457 | var line string 458 | if line, err = ftp.receive(); err != nil { 459 | return 460 | } 461 | 462 | if !strings.HasPrefix(line, StatusFileOK) { 463 | err = errors.New(line) 464 | return 465 | } 466 | 467 | if _, err = io.Copy(pconn, r); err != nil { 468 | return 469 | } 470 | pconn.Close() 471 | 472 | if line, err = ftp.receive(); err != nil { 473 | return 474 | } 475 | 476 | if !strings.HasPrefix(line, StatusClosingDataConnection) { 477 | err = errors.New(line) 478 | return 479 | } 480 | 481 | return 482 | 483 | } 484 | 485 | // Syst returns the system type of the remote host 486 | func (ftp *FTP) Syst() (line string, err error) { 487 | if err := ftp.send("SYST"); err != nil { 488 | return "", err 489 | } 490 | if line, err = ftp.receive(); err != nil { 491 | return 492 | } 493 | if !strings.HasPrefix(line, StatusSystemType) { 494 | err = errors.New(line) 495 | return 496 | } 497 | 498 | return strings.SplitN(strings.TrimSpace(line), " ", 2)[1], nil 499 | } 500 | 501 | // System types from Syst 502 | var ( 503 | SystemTypeUnixL8 = "UNIX Type: L8" 504 | SystemTypeWindowsNT = "Windows_NT" 505 | ) 506 | 507 | var reSystStatus = map[string]*regexp.Regexp{ 508 | SystemTypeUnixL8: regexp.MustCompile(""), 509 | SystemTypeWindowsNT: regexp.MustCompile(""), 510 | } 511 | 512 | // Stat gets the status of path from the remote host 513 | func (ftp *FTP) Stat(path string) ([]string, error) { 514 | if err := ftp.send("STAT %s", path); err != nil { 515 | return nil, err 516 | } 517 | 518 | stat, err := ftp.receive() 519 | if err != nil { 520 | return nil, err 521 | } 522 | if !strings.HasPrefix(stat, StatusFileStatus) && 523 | !strings.HasPrefix(stat, StatusDirectoryStatus) && 524 | !strings.HasPrefix(stat, StatusSystemStatus) { 525 | return nil, errors.New(stat) 526 | } 527 | if strings.HasPrefix(stat, StatusSystemStatus) { 528 | return strings.Split(stat, "\n"), nil 529 | } 530 | lines := []string{} 531 | for _, line := range strings.Split(stat, "\n") { 532 | if strings.HasPrefix(line, StatusFileStatus) { 533 | continue 534 | } 535 | //fmt.Printf("%v\n", re.FindAllStringSubmatch(line, -1)) 536 | lines = append(lines, strings.TrimSpace(line)) 537 | 538 | } 539 | // TODO(vbatts) parse this line for SystemTypeWindowsNT 540 | //"213-status of /remfdata/all.zip:\r\n 09-12-15 04:07AM 37192705 all.zip\r\n213 End of status.\r\n" 541 | 542 | // and this for SystemTypeUnixL8 543 | // "-rw-r--r-- 22 4015 4015 17976 Jun 10 1994 COPYING" 544 | // "drwxr-xr-x 6 4015 4015 4096 Aug 21 17:25 kernels" 545 | return lines, nil 546 | } 547 | 548 | // Retr retrieves file from remote host at path, using retrFn to read from the remote file. 549 | func (ftp *FTP) Retr(path string, retrFn RetrFunc) (s string, err error) { 550 | if err = ftp.Type(TypeImage); err != nil { 551 | return 552 | } 553 | 554 | var port int 555 | if port, err = ftp.Pasv(); err != nil { 556 | return 557 | } 558 | 559 | if err = ftp.send("RETR %s", path); err != nil { 560 | return 561 | } 562 | 563 | var pconn net.Conn 564 | if pconn, err = ftp.newConnection(port); err != nil { 565 | return 566 | } 567 | defer pconn.Close() 568 | 569 | var line string 570 | if line, err = ftp.receiveNoDiscard(); err != nil { 571 | return 572 | } 573 | 574 | if !strings.HasPrefix(line, StatusFileOK) { 575 | err = errors.New(line) 576 | return 577 | } 578 | 579 | if err = retrFn(pconn); err != nil { 580 | return 581 | } 582 | 583 | pconn.Close() 584 | 585 | if line, err = ftp.receive(); err != nil { 586 | return 587 | } 588 | 589 | if !strings.HasPrefix(line, StatusClosingDataConnection) { 590 | err = errors.New(line) 591 | return 592 | } 593 | 594 | return 595 | } 596 | 597 | /*func GetFilesList(path string) (files []string, err error) { 598 | 599 | }*/ 600 | 601 | // List lists the path (or current directory) 602 | func (ftp *FTP) List(path string) (files []string, err error) { 603 | if err = ftp.Type(TypeASCII); err != nil { 604 | return 605 | } 606 | 607 | var port int 608 | if port, err = ftp.Pasv(); err != nil { 609 | return 610 | } 611 | 612 | // check if MLSD works 613 | if err = ftp.send("MLSD %s", path); err != nil { 614 | } 615 | 616 | var pconn net.Conn 617 | if pconn, err = ftp.newConnection(port); err != nil { 618 | return 619 | } 620 | defer pconn.Close() 621 | 622 | var line string 623 | if line, err = ftp.receiveNoDiscard(); err != nil { 624 | return 625 | } 626 | 627 | if !strings.HasPrefix(line, StatusFileOK) { 628 | // MLSD failed, lets try LIST 629 | if err = ftp.send("LIST %s", path); err != nil { 630 | return 631 | } 632 | 633 | if line, err = ftp.receiveNoDiscard(); err != nil { 634 | return 635 | } 636 | 637 | if !strings.HasPrefix(line, StatusFileOK) { 638 | // Really list is not working here 639 | err = errors.New(line) 640 | return 641 | } 642 | } 643 | 644 | reader := bufio.NewReader(pconn) 645 | 646 | for { 647 | line, err = reader.ReadString('\n') 648 | if err == io.EOF { 649 | break 650 | } else if err != nil { 651 | return 652 | } 653 | 654 | files = append(files, string(line)) 655 | } 656 | // Must close for vsftp tlsed conenction otherwise does not receive connection 657 | pconn.Close() 658 | 659 | if line, err = ftp.receive(); err != nil { 660 | return 661 | } 662 | 663 | if !strings.HasPrefix(line, StatusClosingDataConnection) { 664 | err = errors.New(line) 665 | return 666 | } 667 | 668 | return 669 | } 670 | 671 | /* 672 | 673 | 674 | // login on server with strange login behavior 675 | func (ftp *FTP) SmartLogin(username string, password string) (err error) { 676 | var code int 677 | // Maybe the server has some useless words to say. Make him talk 678 | code, _ = ftp.RawCmd("NOOP") 679 | 680 | if code == 220 || code == 530 { 681 | // Maybe with another Noop the server will ask us to login? 682 | code, _ = ftp.RawCmd("NOOP") 683 | if code == 530 { 684 | // ok, let's login 685 | code, _ = ftp.RawCmd("USER %s", username) 686 | code, _ = ftp.RawCmd("NOOP") 687 | if code == 331 { 688 | // user accepted, password required 689 | code, _ = ftp.RawCmd("PASS %s", password) 690 | code, _ = ftp.RawCmd("PASS %s", password) 691 | if code == 230 { 692 | code, _ = ftp.RawCmd("NOOP") 693 | return 694 | } 695 | } 696 | } 697 | 698 | } 699 | // Nothing strange... let's try a normal login 700 | return ftp.Login(username, password) 701 | } 702 | 703 | */ 704 | 705 | // Login to the server with provided username and password. 706 | // Typical default may be ("anonymous",""). 707 | func (ftp *FTP) Login(username string, password string) (err error) { 708 | if _, err = ftp.cmd("331", "USER %s", username); err != nil { 709 | if strings.HasPrefix(err.Error(), "230") { 710 | // Ok, probably anonymous server 711 | // but login was fine, so return no error 712 | err = nil 713 | } else { 714 | return 715 | } 716 | } 717 | 718 | if _, err = ftp.cmd("230", "PASS %s", password); err != nil { 719 | return 720 | } 721 | 722 | return 723 | } 724 | 725 | // Connect to server at addr (format "host:port"). debug is OFF 726 | func Connect(addr string) (*FTP, error) { 727 | var err error 728 | var conn net.Conn 729 | 730 | if conn, err = net.Dial("tcp", addr); err != nil { 731 | return nil, err 732 | } 733 | 734 | writer := bufio.NewWriter(conn) 735 | reader := bufio.NewReader(conn) 736 | 737 | //reader.ReadString('\n') 738 | object := &FTP{conn: conn, addr: addr, reader: reader, writer: writer, debug: false} 739 | object.receive() 740 | 741 | return object, nil 742 | } 743 | 744 | // ConnectDbg to server at addr (format "host:port"). debug is ON 745 | func ConnectDbg(addr string) (*FTP, error) { 746 | var err error 747 | var conn net.Conn 748 | 749 | if conn, err = net.Dial("tcp", addr); err != nil { 750 | return nil, err 751 | } 752 | 753 | writer := bufio.NewWriter(conn) 754 | reader := bufio.NewReader(conn) 755 | 756 | var line string 757 | 758 | object := &FTP{conn: conn, addr: addr, reader: reader, writer: writer, debug: true} 759 | line, _ = object.receive() 760 | 761 | log.Print(line) 762 | 763 | return object, nil 764 | } 765 | 766 | // Size returns the size of a file. 767 | func (ftp *FTP) Size(path string) (size int, err error) { 768 | line, err := ftp.cmd("213", "SIZE %s", path) 769 | 770 | if err != nil { 771 | return 0, err 772 | } 773 | 774 | return strconv.Atoi(line[4 : len(line)-2]) 775 | } 776 | 777 | -------------------------------------------------------------------------------- /ftp_test.go: -------------------------------------------------------------------------------- 1 | package goftp 2 | 3 | import "testing" 4 | 5 | //import "fmt" 6 | 7 | var goodServer string 8 | var uglyServer string 9 | var badServer string 10 | 11 | func init() { 12 | //ProFTPD 1.3.5 Server (Debian) 13 | goodServer = "bo.mirror.garr.it:21" 14 | 15 | //Symantec EMEA FTP Server 16 | badServer = "ftp.packardbell.com:21" 17 | 18 | //Unknown server 19 | uglyServer = "ftp.musicbrainz.org:21" 20 | } 21 | 22 | func standard(host string) (msg string) { 23 | var err error 24 | var connection *FTP 25 | 26 | if connection, err = Connect(host); err != nil { 27 | return "Can't connect ->" + err.Error() 28 | } 29 | if err = connection.Login("anonymous", "anonymous"); err != nil { 30 | return "Can't login ->" + err.Error() 31 | } 32 | if _, err = connection.List(""); err != nil { 33 | return "Can't list ->" + err.Error() 34 | } 35 | connection.Close() 36 | return "" 37 | } 38 | 39 | func TestLogin_good(t *testing.T) { 40 | str := standard(goodServer) 41 | if len(str) > 0 { 42 | t.Error(str) 43 | } 44 | } 45 | 46 | func TestLogin_bad(t *testing.T) { 47 | str := standard(badServer) 48 | if len(str) > 0 { 49 | t.Error(str) 50 | } 51 | } 52 | 53 | func TestLogin_ugly(t *testing.T) { 54 | str := standard(uglyServer) 55 | if len(str) > 0 { 56 | t.Error(str) 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /status.go: -------------------------------------------------------------------------------- 1 | package goftp 2 | 3 | // FTP Status codes, defined in RFC 959 4 | const ( 5 | StatusFileOK = "150" 6 | StatusOK = "200" 7 | StatusSystemStatus = "211" 8 | StatusDirectoryStatus = "212" 9 | StatusFileStatus = "213" 10 | StatusConnectionClosing = "221" 11 | StatusSystemType = "215" 12 | StatusClosingDataConnection = "226" 13 | StatusActionOK = "250" 14 | StatusPathCreated = "257" 15 | StatusActionPending = "350" 16 | ) 17 | 18 | var statusText = map[string]string{ 19 | StatusFileOK: "File status okay; about to open data connection", 20 | StatusOK: "Command okay", 21 | StatusSystemStatus: "System status, or system help reply", 22 | StatusDirectoryStatus: "Directory status", 23 | StatusFileStatus: "File status", 24 | StatusConnectionClosing: "Service closing control connection", 25 | StatusSystemType: "System Type", 26 | StatusClosingDataConnection: "Closing data connection. Requested file action successful.", 27 | StatusActionOK: "Requested file action okay, completed", 28 | StatusPathCreated: "Pathname Created", 29 | StatusActionPending: "Requested file action pending further information", 30 | } 31 | 32 | // StatusText returns a text for the FTP status code. It returns the empty 33 | // string if the code is unknown. 34 | func StatusText(code string) string { 35 | return statusText[code] 36 | } 37 | -------------------------------------------------------------------------------- /upload.go: -------------------------------------------------------------------------------- 1 | // Package goftp upload helper 2 | package goftp 3 | 4 | import ( 5 | "os" 6 | "path/filepath" 7 | ) 8 | 9 | func (ftp *FTP) copyDir(localPath string) error { 10 | fullPath, err := filepath.Abs(localPath) 11 | if err != nil { 12 | return err 13 | } 14 | 15 | pwd, err := ftp.Pwd() 16 | if err != nil { 17 | return err 18 | } 19 | 20 | walkFunc := func(path string, fi os.FileInfo, err error) error { 21 | // Stop upon error 22 | if err != nil { 23 | return err 24 | } 25 | relPath, err := filepath.Rel(fullPath, path) 26 | if err != nil { 27 | return err 28 | } 29 | switch { 30 | case fi.IsDir(): 31 | // Walk calls walkFn on root as well 32 | if path == fullPath { 33 | return nil 34 | } 35 | if err = ftp.Mkd(relPath); err != nil { 36 | if _, err = ftp.List(relPath + "/"); err != nil { 37 | return err 38 | } 39 | } 40 | case fi.Mode()&os.ModeSymlink == os.ModeSymlink: 41 | fInfo, err := os.Stat(path) 42 | if err != nil { 43 | return err 44 | } 45 | if fInfo.IsDir() { 46 | err = ftp.Mkd(relPath) 47 | return err 48 | } else if fInfo.Mode()&os.ModeType != 0 { 49 | // ignore other special files 50 | return nil 51 | } 52 | fallthrough 53 | case fi.Mode()&os.ModeType == 0: 54 | if err = ftp.copyFile(path, pwd+"/"+relPath); err != nil { 55 | return err 56 | } 57 | default: 58 | // Ignore other special files 59 | } 60 | 61 | return nil 62 | } 63 | 64 | return filepath.Walk(fullPath, walkFunc) 65 | } 66 | 67 | func (ftp *FTP) copyFile(localPath, serverPath string) (err error) { 68 | var file *os.File 69 | if file, err = os.Open(localPath); err != nil { 70 | return err 71 | } 72 | defer file.Close() 73 | if err := ftp.Stor(serverPath, file); err != nil { 74 | return err 75 | } 76 | 77 | return nil 78 | } 79 | 80 | // Upload a file, or recursively upload a directory. 81 | // Only normal files and directories are uploaded. 82 | // Symlinks are not kept but treated as normal files/directories if targets are so. 83 | func (ftp *FTP) Upload(localPath string) (err error) { 84 | fInfo, err := os.Stat(localPath) 85 | if err != nil { 86 | return err 87 | } 88 | 89 | switch { 90 | case fInfo.IsDir(): 91 | return ftp.copyDir(localPath) 92 | case fInfo.Mode()&os.ModeType == 0: 93 | return ftp.copyFile(localPath, filepath.Base(localPath)) 94 | default: 95 | // Ignore other special files 96 | } 97 | 98 | return nil 99 | } 100 | --------------------------------------------------------------------------------