-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimpleFTP.go
executable file
·162 lines (134 loc) · 2.81 KB
/
simpleFTP.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
// test project main.go
package main
import (
"bytes"
"fmt"
"io"
"net"
"os"
"path"
"regexp"
"strconv"
"strings"
"time"
)
const (
BUFF_SIZE = 512
READ_DEADLINE = 1
)
func check(err error) {
if err != nil {
panic(err)
}
}
func pass227(str string) string {
re := regexp.MustCompile(".*[(](.*)[)].*")
sp := strings.Split(re.FindStringSubmatch(str)[1], ",")
p1, _ := strconv.Atoi(sp[4])
p2, _ := strconv.Atoi(sp[5])
addr := fmt.Sprintf("%s.%s.%s.%s:%d", sp[0], sp[1], sp[2], sp[3], p1*256+p2)
fmt.Println("Server is listen on ", addr)
return addr
}
func recv(r io.Reader) string {
var err error
var cnt, n int
data := bytes.NewBuffer(nil)
buff := make([]byte, BUFF_SIZE)
for true {
n, err = r.Read(buff[0:])
cnt += n
data.Write(buff[:n])
if n < BUFF_SIZE {
break
}
if err != nil {
fmt.Println(err)
}
}
trim := strings.TrimSpace(data.String())
fmt.Printf("Response:size=%v data=%v\n", cnt, trim)
return trim
}
func (ftp *FTP) Open(addr string) net.Conn {
c, err := net.Dial("tcp", addr)
check(err)
return c
}
type FTP struct {
Conn net.Conn
err error
}
func (ftp *FTP) SendCmd(cmd string) (string, int) {
fmt.Printf("Request:%s\n", cmd)
fmt.Fprintf(ftp.Conn, "%s\r\n", cmd)
ftp.Conn.SetReadDeadline(time.Now().Add(READ_DEADLINE * time.Second))
res := recv(ftp.Conn)
code, _ := strconv.Atoi(res[0:3])
return res, code
}
func (ftp *FTP) SetPasv() net.Conn {
res, code := ftp.SendCmd("PASV")
if 227 == code {
c := ftp.Open(pass227(res))
fmt.Println("Parse PASV 227 code done")
return c
} else {
panic("Cannot get 227 request")
return nil
}
}
func (ftp *FTP) List(dir string) {
c := ftp.SetPasv()
ftp.SendCmd("LIST " + dir)
recv(c)
defer func() {
c.Close()
}()
}
func (ftp *FTP) GetFile(file string) {
ftp.SendCmd("SIZE " + file)
ftp.SendCmd("TYPE I")
c := ftp.SetPasv()
ftp.SendCmd("RETR " + file)
out, _ := os.Create(path.Base(file))
n, err := io.Copy(out, c)
check(err)
fmt.Printf("Store %s[%d]\n", path.Base(file), n)
defer func() {
out.Close()
c.Close()
}()
}
func (ftp *FTP) New(args ...string) {
var addr, user, pass string
switch len(args) {
case 1:
addr = args[0] + ":21"
user = "anonymous"
pass = ""
case 3:
addr = args[0] + ":21"
user = args[1]
pass = args[2]
default:
panic("error,please specify the FTP server host at least")
}
ftp.Conn = ftp.Open(addr)
recv(ftp.Conn)
ftp.SendCmd("USER " + user)
ftp.SendCmd("PASS " + pass)
}
func (ftp *FTP) Close() {
ftp.SendCmd("QUIT")
ftp.Conn.Close()
}
func main() {
ftp := FTP{}
ftp.New("10.64.70.73")
ftp.List("/")
ftp.Close()
ftp.New("10.64.70.73")
ftp.GetFile("/pub/atop-1.27-3.tar.gz")
ftp.Close()
}