ftp.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package ftp
  2. import (
  3. "bufio"
  4. "net"
  5. "net/textproto"
  6. "os"
  7. "fmt"
  8. "strconv"
  9. "strings"
  10. )
  11. const (
  12. EntryTypeFile = iota
  13. EntryTypeFolder
  14. EntryTypeLink
  15. )
  16. type ServerConn struct {
  17. conn *textproto.Conn
  18. host string
  19. }
  20. type Response struct {
  21. conn net.Conn
  22. c *ServerConn
  23. }
  24. type Entry struct {
  25. Name string
  26. EntryType int
  27. Size uint64
  28. }
  29. // Connect to a ftp server and returns a ServerConn handler.
  30. func Connect(host, user, password string) (*ServerConn, os.Error) {
  31. conn, err := textproto.Dial("tcp", host)
  32. if err != nil {
  33. return nil, err
  34. }
  35. a := strings.Split(host, ":", 2)
  36. c := &ServerConn{conn, a[0]}
  37. _, _, err = c.conn.ReadCodeLine(StatusReady)
  38. if err != nil {
  39. c.Close()
  40. return nil, err
  41. }
  42. c.conn.Cmd("USER %s", user)
  43. _, _, err = c.conn.ReadCodeLine(StatusUserOK)
  44. if err != nil {
  45. c.Close()
  46. return nil, err
  47. }
  48. c.conn.Cmd("PASS %s", password)
  49. _, _, err = c.conn.ReadCodeLine(StatusLoggedIn)
  50. if err != nil {
  51. c.Close()
  52. return nil, err
  53. }
  54. return c, nil
  55. }
  56. // Like Connect() but with anonymous credentials.
  57. func ConnectAnonymous(host string) (*ServerConn, os.Error) {
  58. return Connect(host, "anonymous", "anonymous")
  59. }
  60. // Enter extended passive mode
  61. func (c *ServerConn) epsv() (port int, err os.Error) {
  62. c.conn.Cmd("EPSV")
  63. _, line, err := c.conn.ReadCodeLine(StatusExtendedPassiveMode)
  64. if err != nil {
  65. return
  66. }
  67. start := strings.Index(line, "|||")
  68. end := strings.LastIndex(line, "|")
  69. if start == -1 || end == -1 {
  70. err = os.NewError("Invalid EPSV response format")
  71. return
  72. }
  73. port, err = strconv.Atoi(line[start+3 : end])
  74. return
  75. }
  76. // Open a new data connection using extended passive mode
  77. func (c *ServerConn) openDataConnection() (r *Response, err os.Error) {
  78. port, err := c.epsv()
  79. if err != nil {
  80. return
  81. }
  82. // Build the new net address string
  83. addr := fmt.Sprintf("%s:%d", c.host, port)
  84. conn, err := net.Dial("tcp", addr)
  85. if err != nil {
  86. return
  87. }
  88. r = &Response{conn, c}
  89. return
  90. }
  91. func parseListLine(line string) (*Entry, os.Error) {
  92. fields := strings.Fields(line)
  93. if len(fields) < 9 {
  94. return nil, os.NewError("Unsupported LIST line")
  95. }
  96. e := &Entry{}
  97. switch fields[0][0] {
  98. case '-':
  99. e.EntryType = EntryTypeFile
  100. case 'd':
  101. e.EntryType = EntryTypeFolder
  102. case 'l':
  103. e.EntryType = EntryTypeLink
  104. default:
  105. return nil, os.NewError("Unknown entry type")
  106. }
  107. e.Name = strings.Join(fields[8:], " ")
  108. return e, nil
  109. }
  110. func (c *ServerConn) List() (entries []*Entry, err os.Error) {
  111. r, err := c.openDataConnection()
  112. if err != nil {
  113. return
  114. }
  115. defer r.Close()
  116. c.conn.Cmd("LIST")
  117. _, _, err = c.conn.ReadCodeLine(StatusAboutToSend)
  118. if err != nil {
  119. return
  120. }
  121. bio := bufio.NewReader(r)
  122. for {
  123. line, e := bio.ReadString('\n')
  124. if e == os.EOF {
  125. break
  126. } else if e != nil {
  127. return nil, e
  128. }
  129. entry, err := parseListLine(line)
  130. if err == nil {
  131. entries = append(entries, entry)
  132. }
  133. }
  134. return
  135. }
  136. func (c *ServerConn) ChangeDir(path string) (err os.Error) {
  137. c.conn.Cmd("CWD %s", path);
  138. _, _, err = c.conn.ReadCodeLine(StatusRequestedFileActionOK)
  139. return
  140. }
  141. func (c *ServerConn) Get(path string) (r *Response, err os.Error) {
  142. r, err = c.openDataConnection()
  143. if err != nil {
  144. return
  145. }
  146. c.conn.Cmd("RETR %s", path)
  147. _, _, err = c.conn.ReadCodeLine(StatusAboutToSend)
  148. return
  149. }
  150. func (c *ServerConn) Close() {
  151. c.conn.Cmd("QUIT")
  152. c.conn.Close()
  153. }
  154. func (r *Response) Read(buf []byte) (int, os.Error) {
  155. n, err := r.conn.Read(buf)
  156. if err == os.EOF {
  157. _, _, err2 := r.c.conn.ReadCodeLine(StatusClosingDataConnection)
  158. if err2 != nil {
  159. err = err2
  160. }
  161. }
  162. return n, err
  163. }
  164. func (r *Response) Close() os.Error {
  165. return r.conn.Close()
  166. }