-
Notifications
You must be signed in to change notification settings - Fork 63
/
ftp.go
410 lines (363 loc) · 10.9 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
package ftp
import (
"bufio"
"bytes"
"fmt"
"go/types"
"io"
"net"
"strconv"
"strings"
"sync"
"time"
"github.com/li4n0/revsuit/internal/database"
"github.com/li4n0/revsuit/internal/recycler"
"github.com/li4n0/revsuit/internal/rule"
"github.com/patrickmn/go-cache"
"github.com/pkg/errors"
log "unknwon.dev/clog/v2"
)
type Server struct {
Config
pasvIP string
rules []*Rule
rulesLock sync.RWMutex
livingLock sync.Mutex
dataChannel chan map[string]interface{}
}
type Status string
const (
CRASHED Status = "CRASHED"
FINISHED Status = "FINISHED"
)
type Method = string
const (
DOWNLOAD Method = "DOWNLOAD"
UPLOAD Method = "UPLOAD"
)
var (
server *Server
once sync.Once
rebindingCache = cache.New(5*time.Second, 10*time.Second)
)
func GetServer() *Server {
once.Do(func() {
server = &Server{rulesLock: sync.RWMutex{}, dataChannel: make(chan map[string]interface{}, 10)}
})
return server
}
func (s *Server) SetPasvIP(ip string) *Server {
s.pasvIP = ip
return s
}
func (s *Server) getRules() []*Rule {
defer s.rulesLock.RUnlock()
s.rulesLock.RLock()
return s.rules
}
func (s *Server) UpdateRules() error {
db := database.DB.Model(new(Rule))
defer s.rulesLock.Unlock()
s.rulesLock.Lock()
return errors.Wrap(db.Order("base_rank desc").Find(&s.rules).Error, "FTP update rules error")
}
func getClientPasvConnAddress(ip, port string) string {
dataPort, _ := strconv.Atoi(port)
return fmt.Sprintf("%s:%d", ip, dataPort+1)
}
func (s *Server) authenticate(user, password string) (_rule *Rule, flag, flagGroup string, vars map[string]string) {
for _, _rule := range s.getRules() {
for _, s := range []string{user, password} {
flag, flagGroup, vars = _rule.Match(s)
if flag != "" {
vars["user"] = user
vars["password"] = password
return _rule, flag, flagGroup, vars
}
}
}
return _rule, flag, flagGroup, vars
}
func (s *Server) getPasvAddressFromCache(ip, pasvAddressTpl string) (pasvAddress string) {
pasvAddress = pasvAddressTpl
if strings.Contains(pasvAddressTpl, ",") {
values, ok := rebindingCache.Get(ip)
if !ok {
rebindingCache.Set(ip, strings.Split(pasvAddressTpl, ","), cache.DefaultExpiration)
values = strings.Split(pasvAddressTpl, ",")
}
//Choose and delete first address
pasvAddress = values.([]string)[0]
if len(values.([]string)) > 1 {
rebindingCache.Set(ip, values.([]string)[1:len(values.([]string))], cache.DefaultExpiration)
} else {
rebindingCache.Delete(ip)
}
}
return pasvAddress
}
const (
NeedAccount = "332 Need account for login.\r\n"
PasswordPlease = "331 password please - version check\r\n"
PasswordError = "331 please specify the password\r\n"
UserLogged = "230 User logged in\r\n"
NoSuchFile = "550 %s: No such file or directory.\r\n"
CommandNotFound = "500 '%s': command not understood.\r\n"
EnteringPassiveMode = "227 Entering Passive Mode (%s,%v,%d)\r\n"
OpeningBinaryMode = "150 Opening BINARY mode data connection for '%s' (%d bytes).\r\n"
OpeningBinaryModeUpload = "150 Opening BINARY mode data connection for '%s'.\r\n"
TransferComplete = "226 Transfer complete.\r\n"
Goodbye = "221 Goodbye.\r\n"
DirectoryChanged = "250 Directory successfully changed.\r\n"
CurrentDirectory = "257 \"%s\" is the current directory\r\n"
)
func (s *Server) handleConnection(conn net.Conn) {
defer func() {
if err := recover(); err != nil {
recycler.Recycle(err)
}
}()
log.Trace("New FTP connection from addr [%s]", conn.RemoteAddr())
defer func() {
_ = conn.Close()
if err := recover(); err != nil {
recycler.Recycle(err)
}
}()
if err := conn.SetDeadline(time.Now().Add(time.Second * 30)); err != nil {
log.Warn("FTP set connection deadline error:%v", err)
}
if _, err := conn.Write([]byte("220 (vsFTPd 3.0.2)\r\n")); err != nil {
log.Warn("FTP write connection error:%v", err)
}
ip, port, _ := net.SplitHostPort(conn.RemoteAddr().String())
clientPasvConnAddress := getClientPasvConnAddress(ip, port)
buf := &bytes.Buffer{}
connBuf := bufio.NewWriter(conn)
var user, password, method, flag, flagGroup, pasvAddress, filename string
status := CRASHED
path := "/"
uploadData := make([]byte, 0)
var _rule *Rule
var vars map[string]string
var isRedirect bool
loop:
for {
data := make([]byte, 2048)
n, err := conn.Read(data)
if err != nil {
break
}
buf.Write(data[:n])
if buf.Len() > 4 {
frags := strings.SplitN(strings.TrimRight(buf.String(), "\r\n"), " ", 2)
var cmd = frags[0]
var args string
if len(frags) > 1 {
args = frags[1]
}
log.Trace("FTP connection[%s] exec command: %s", conn.RemoteAddr(), strings.TrimRight(buf.String(), "\r\n"))
if _rule == nil && cmd != "USER" && cmd != "PASS" {
_, _ = connBuf.WriteString(NeedAccount)
break loop
}
switch cmd {
case "USER":
user = args
_, _ = connBuf.WriteString(PasswordPlease)
case "PASS":
password = args
if _rule, flag, flagGroup, vars = s.authenticate(user, password); _rule == nil {
_, _ = connBuf.WriteString(PasswordError)
break loop
}
log.Trace("FTP connection[%s] matched rule[rule_name: %s, flag: %s]", conn.RemoteAddr(), _rule.Name, flag)
_, _ = connBuf.WriteString(UserLogged)
if pasvAddress = s.getPasvAddressFromCache(ip, _rule.PasvAddress); pasvAddress == "" {
pasvAddress = fmt.Sprintf("%s:%d", s.pasvIP, s.PasvPort)
}
pasvAddress = rule.CompileTpl(pasvAddress, vars)
isRedirect = pasvAddress != fmt.Sprintf("%s:%d", s.pasvIP, s.PasvPort)
case "SIZE":
path += strings.TrimLeft(args, "/")
if _rule == nil || isRedirect || len(_rule.Data) == 0 {
_, _ = connBuf.WriteString(fmt.Sprintf(NoSuchFile, args))
break
}
_, _ = connBuf.WriteString(fmt.Sprintf("213 %d\r\n", len(_rule.Data)))
case "EPSV", "EPRT", "PORT":
// refuse to use EPSV/EPRT/PORT in order to make the client to use PASV mode.
_, _ = connBuf.WriteString(fmt.Sprintf(CommandNotFound, cmd))
case "PASV":
//Just so that ide does not prompt that there may be a nil value
if _rule != nil {
pasvIP, pasvPort, err := net.SplitHostPort(pasvAddress)
if err != nil {
log.Warn("FTP failed to split rule[id:%d] pasv_address(%s) :%s", _rule.ID, pasvAddress, err)
break
}
port, err := strconv.Atoi(pasvPort)
if err != nil {
log.Warn("FTP failed to convert rule[id:%d] pasv_port(%s) :%s", _rule.ID, pasvPort, err)
break
}
ret := fmt.Sprintf(EnteringPassiveMode, strings.ReplaceAll(pasvIP, ".", ","), float64(port/256), port%256)
_, _ = connBuf.WriteString(ret)
if isRedirect {
log.Trace("FTP connection[%s] will be redirect[pasv_address: %s]", conn.RemoteAddr(), pasvAddress)
}
}
case "RETR":
//Just so that ide does not prompt that there may be a nil value
if _rule != nil {
filename = args
method = DOWNLOAD
//send data to client
_, _ = connBuf.WriteString(fmt.Sprintf(OpeningBinaryMode, filename, len(_rule.Data)))
_ = connBuf.Flush()
s.dataChannel <- map[string]interface{}{clientPasvConnAddress: []byte(rule.CompileTpl(_rule.Data, vars))}
_, _ = connBuf.WriteString(TransferComplete)
}
case "STOR":
filename = args
method = UPLOAD
_, _ = connBuf.WriteString(fmt.Sprintf(OpeningBinaryModeUpload, filename))
_ = connBuf.Flush()
//only could read data send to local pasv server.
if !isRedirect {
dataChannel := make(chan []byte)
s.dataChannel <- map[string]interface{}{clientPasvConnAddress: dataChannel}
uploadData = <-dataChannel
log.Trace("FTP connection[%s] uploaded %d bytes", conn.RemoteAddr(), len(uploadData))
}
_, _ = connBuf.WriteString(TransferComplete)
case "QUIT":
_, _ = connBuf.WriteString(Goodbye)
status = FINISHED
break loop
case "CWD":
_, _ = connBuf.WriteString(DirectoryChanged)
path += strings.TrimRight(args, "\r\n") + "/"
case "PWD":
_, _ = connBuf.WriteString(fmt.Sprintf(CurrentDirectory, path))
default:
_, _ = conn.Write([]byte("230 more data please!\r\n"))
}
_ = connBuf.Flush()
}
buf = &bytes.Buffer{}
}
if _rule != nil {
createRecord(_rule, flag, flagGroup, user, password, method, path, filename, ip, uploadData, status)
}
}
func (s *Server) handlePasvConnection(conn net.Conn, data map[string]interface{}) {
defer func() {
if err := recover(); err != nil {
recycler.Recycle(err)
}
}()
remoteAddress := conn.RemoteAddr().String()
switch v := data[remoteAddress].(type) {
case types.Nil:
s.dataChannel <- data
return
case []byte:
_, err := conn.Write(v)
if err != nil {
log.Warn("FTP PASV server sent data to connection[%s] failed with error: %s", remoteAddress, err)
}
log.Trace("FTP PASV server has sent data to connection[%s]", remoteAddress)
case chan []byte:
buf, err := io.ReadAll(conn)
if err != nil {
log.Warn("FTP PASV server received data from connection[%s] failed with error: %s", remoteAddress, err)
}
v <- buf
log.Trace("FTP PASV server has received data from connection[%s]", remoteAddress)
}
_ = conn.Close()
}
// run pasv server
func (s *Server) runPasvServer() (net.Listener, error) {
pasvAddress := fmt.Sprintf("%s:%d", strings.Split(s.Addr, ":")[0], s.PasvPort)
log.Info("Start to listen FTP PASV port at %v, PasvIP is %v", pasvAddress, s.pasvIP)
listener, err := net.Listen("tcp", pasvAddress)
if err != nil {
return nil, errors.Wrap(err, "FTP failed to listen on pasv port")
}
go func() {
for data := range s.dataChannel {
tcpConn, err := listener.Accept()
if err != nil {
if !strings.Contains(err.Error(), net.ErrClosed.Error()) {
log.Warn("FTP accept connection error: %v", err)
} else {
break
}
continue
}
s.handlePasvConnection(tcpConn, data)
}
}()
return listener, nil
}
func (s *Server) Stop() {
log.Info("FTP Server is stopping...")
s.Enable = false
s.livingLock.Unlock()
}
func (s *Server) Restart() {
s.Stop()
time.Sleep(time.Second * 2)
go s.Run()
}
func (s *Server) Run() {
s.Enable = true
s.livingLock.Lock()
defer func() {
if s.Enable {
log.Error("FTP Server exited unexpectedly")
}
s.Enable = false
s.livingLock.Unlock()
}()
if err := s.UpdateRules(); err != nil {
log.Error(err.Error())
return
}
pasvListener, err := s.runPasvServer()
if err != nil {
log.Error(err.Error())
}
defer func() {
if pasvListener != nil {
_ = pasvListener.Close()
}
}()
// run ftp server
log.Info("Starting FTP Server at %v", s.Addr)
listener, err := net.Listen("tcp", s.Addr)
if err != nil {
log.Error(errors.Wrap(err, "FTP failed to start").Error())
return
}
go func() {
s.livingLock.Lock()
if !s.Enable {
_ = listener.Close()
}
}()
for s.Enable {
tcpConn, err := listener.Accept()
if err != nil {
if !strings.Contains(err.Error(), net.ErrClosed.Error()) {
log.Warn("FTP accept connection error: %v", err)
} else {
break
}
continue
}
go s.handleConnection(tcpConn)
}
}