-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftp.go
528 lines (451 loc) · 11.2 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
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
// Froxy - HTTP over SSH proxy
//
// Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
// See LICENSE for license terms and conditions
//
// FTP proxy
package main
import (
"fmt"
"html/template"
"io"
"net"
"net/http"
"net/textproto"
"net/url"
"sort"
"strings"
"sync"
"time"
"github.com/jlaffaye/ftp"
)
type FTPProxy struct {
froxy *Froxy // Back link to froxy
idleLock sync.Mutex // Protects idle connections machinery
idleConnections map[string]ftpIddleConnBucket // Per-site buckets of idle connections
idleChan chan struct{} // Signaling channel for idle expiration goroutine
}
//
// Type ftpIddleConnBucket represents a bucket of
// idle connections to the same site
//
type ftpIddleConnBucket map[*ftpConn]time.Time
//
// FTP connection
//
type ftpConn struct {
*ftp.ServerConn // Underlying ftp.ServerConn
ftpp *FTPProxy // Back link to owning FTPProxy
netconn net.Conn // Underlying net.Conn
site string // Site URL
reused bool // This is connection was idle and reused
closed bool // This is closed connection
}
//
// Create new FTP proxy
//
func NewFTPProxy(froxy *Froxy) *FTPProxy {
ftpp := &FTPProxy{
froxy: froxy,
idleConnections: make(map[string]ftpIddleConnBucket),
idleChan: make(chan struct{}),
}
go ftpp.expireIdleConnections()
return ftpp
}
//
// Handle a single "HTTP" GET request
//
func (ftpp *FTPProxy) Handle(w http.ResponseWriter, r *http.Request, transport Transport) {
// Check protocol and method
if r.URL.Scheme != "ftp" {
ftpp.froxy.httpError(w, http.StatusServiceUnavailable,
fmt.Errorf("unsupported protocol scheme %q", r.URL.Scheme))
return
}
if r.Method != "GET" {
ftpp.froxy.httpError(w, http.StatusMethodNotAllowed,
fmt.Errorf("unsupported method %q", r.Method))
return
}
// Create a copy of URL that contains only parts related to site
site_url := url.URL{
Scheme: r.URL.Scheme,
User: r.URL.User,
Host: r.URL.Host,
}
site := site_url.String()
// Normalize path
path := r.URL.Path
isDir := strings.HasSuffix(path, "/")
if isDir && len(path) > 1 {
path = path[:len(path)-1]
}
// Obtain a connection
RETRY:
conn := ftpp.getConn(site)
var err error
if conn == nil {
conn, err = ftpp.dialConn(transport, site_url)
if err != nil {
ftpp.sendError(w, http.StatusServiceUnavailable, err)
return
}
}
defer conn.putConn()
// Try to interpret path as file
httpStatus := 0
if !isDir {
var body *ftp.Response
body, httpStatus, err = ftpp.readFile(conn, path)
if err == nil {
ftpp.sendFile(w, body)
return
}
}
// Try to interpret path as directory
if list, httpStatus2, err2 := ftpp.readDir(conn, path); err2 == nil {
ftpp.sendDirectory(w, r, path, list)
return
} else {
if err == nil {
err = err2
httpStatus = httpStatus2
}
}
// Drop the connection in a case of error - in many cases
// it's hard to tell if this connection useful for further
// operations
//
// If we've got an error on reused connection,
// retry the operation with a fresh connection
conn.Close()
if conn.reused {
goto RETRY
}
// Error handling
if httpStatus == 0 {
httpStatus = http.StatusServiceUnavailable
}
ftpp.sendError(w, httpStatus, err)
}
//
// Read the file
//
func (ftpp *FTPProxy) readFile(conn *ftpConn, path string) (*ftp.Response, int, error) {
// Try to fetch the file
ftpp.froxy.Debug("FTP: RETR %q", path)
body, err := conn.Retr(path)
if err == nil {
return body, 0, nil
} else {
ftpp.froxy.Debug("FTP: RETR: %s", err)
}
// Try to guess appropriate HTTP status
httpStatus := 0
if ftperr, ok := err.(*textproto.Error); ok && ftperr.Code == ftp.StatusFileUnavailable {
ftpp.froxy.Debug("FTP: SIZE %q", path)
_, err2 := conn.FileSize(path)
if err2 == nil {
httpStatus = http.StatusForbidden
} else {
ftpp.froxy.Debug("FTP: SIZE: %s", err2)
httpStatus = http.StatusNotFound
}
}
return nil, httpStatus, err
}
//
// Read the directory
//
func (ftpp *FTPProxy) readDir(conn *ftpConn, path string) ([]*ftp.Entry, int, error) {
ftpp.froxy.Debug("FTP: CWD %q", path)
err := conn.ChangeDir(path)
if err != nil {
ftpp.froxy.Debug("FTP: CWD %s", err)
return nil, 0, err
}
ftpp.froxy.Debug("FTP: LIST .")
files, err := conn.List(".")
if err != nil {
ftpp.froxy.Debug("FTP: LIST %s", err)
}
ftpp.froxy.Debug("FTP: CWD /")
err2 := conn.ChangeDir("/")
if err2 != nil {
ftpp.froxy.Debug("FTP: CWD: %s", err2)
conn.Close()
}
return files, 0, err
}
//
// Send a error response
//
func (ftpp *FTPProxy) sendError(w http.ResponseWriter, httpStatus int, err error) {
if ftperr, ok := err.(*textproto.Error); ok {
err = fmt.Errorf("FTP: %s", err)
if httpStatus == 0 {
switch ftperr.Code {
case ftp.StatusNotLoggedIn:
httpStatus = http.StatusUnauthorized
}
}
}
ftpp.froxy.httpError(w, httpStatus, err)
}
//
// Send a response with directory listing
//
func (ftpp *FTPProxy) sendDirectory(w http.ResponseWriter, r *http.Request,
path string, files []*ftp.Entry) {
// Prepare list of files
sort.Slice(files, func(i, j int) bool {
f1 := files[i]
f2 := files[j]
// Directories first
switch {
case f1.Type == ftp.EntryTypeFolder && f2.Type != ftp.EntryTypeFolder:
return true
case f1.Type != ftp.EntryTypeFolder && f2.Type == ftp.EntryTypeFolder:
return false
}
// Special folders first
switch {
case f1.Name == "." && f2.Name != ".":
return true
case f1.Name != "." && f2.Name == ".":
return false
case f1.Name == ".." && f2.Name != "..":
return true
case f1.Name != ".." && f2.Name == "..":
return false
}
// Then sort by name
return f1.Name < f2.Name
})
// Make sure we have parent directory
switch {
case len(files) > 0 && files[0].Name == "..":
case len(files) > 1 && files[1].Name == "..":
default:
files = append([]*ftp.Entry{{Name: "..", Type: ftp.EntryTypeFolder}}, files...)
}
// Format HTML head
favicon := ftpp.froxy.BaseURL() + "icons/froxy.png"
w.Write([]byte("<html>"))
w.Write([]byte(`<head><meta charset="utf-8">` + "\n"))
fmt.Fprintf(w, `<link rel="icon" type="image/png" href="%s">`+"\n", favicon)
w.Write([]byte("<style>\n"))
w.Write([]byte("th, td {\n"))
w.Write([]byte(" padding-right: 15px;\n"))
w.Write([]byte("}\n"))
w.Write([]byte("</style>\n"))
w.Write([]byte("</head>\n"))
w.Write([]byte("<title>"))
template.HTMLEscape(w, []byte(r.URL.String()))
w.Write([]byte("</title>\n"))
w.Write([]byte("<body>\n"))
// Format table of files
w.Write([]byte(`<fieldset style="border-radius:10px">`))
fmt.Fprintf(w, "<legend>Listing of %s</legend>\n", template.HTMLEscapeString(path))
w.Write([]byte("<table><tbody>\n"))
for _, f := range files {
var href, name, symbol string
switch f.Name {
case ".":
continue
case "..":
href = path
i := 0
switch i = strings.LastIndexByte(href, '/'); {
case i > 0:
href = href[:i] + "/"
case i == 0:
href = "/"
}
name = "Parent directory"
symbol = "🢠"
default:
href = path
if len(href) > 1 {
href += "/"
}
href += f.Name
name = template.HTMLEscapeString(f.Name)
switch f.Type {
case ftp.EntryTypeFolder:
symbol = "📂"
href += "/"
default:
symbol = "📄"
}
}
// Format file time and size
time := ""
size := ""
if f.Type != ftp.EntryTypeFolder {
switch {
case f.Size < 1024:
size = fmt.Sprintf("%d", f.Size)
case f.Size < 1024*1024:
size = fmt.Sprintf("%.1fK", float64(f.Size)/1024)
case f.Size < 1024*1024*1024:
size = fmt.Sprintf("%.1fM", float64(f.Size)/(1024*1024))
case f.Size < 1024*1024*1024*1024:
size = fmt.Sprintf("%.1fG", float64(f.Size)/(1024*1024*1024))
}
time = fmt.Sprintf("%.2d-%.2d-%.4d %.2d:%.2d",
f.Time.Day(),
f.Time.Month(),
f.Time.Year(),
f.Time.Hour(),
f.Time.Minute(),
)
}
// Create table row
w.Write([]byte("<tr>"))
fmt.Fprintf(w, `<td>%s <a href=%q>%s</a></td>`, symbol, href, name)
fmt.Fprintf(w, `<td>%s</td>`, size)
fmt.Fprintf(w, `<td>%s</td>`, time)
w.Write([]byte("</tr>\n"))
}
w.Write([]byte("</tbody></table>\n"))
w.Write([]byte("</fieldset></body></html>\n"))
}
//
// Send a response with directory listing
//
func (ftpp *FTPProxy) sendFile(w http.ResponseWriter, body *ftp.Response) {
io.Copy(w, body)
body.Close()
}
//
// Dial a connection
//
func (ftpp *FTPProxy) dialConn(transport Transport, site_url url.URL) (*ftpConn, error) {
// Connect
addr := NetDefaultPort(site_url.Host, "21")
netconn, err := transport.Dial("tcp", addr)
if err != nil {
return nil, err
}
// create ftpConn
conn := &ftpConn{ftpp: ftpp, netconn: netconn, site: site_url.String()}
ftpp.froxy.Debug("FTP: trying %s", addr)
conn.ServerConn, err = ftp.Dial(addr,
ftp.DialWithNetConn(netconn),
ftp.DialWithDialFunc(transport.Dial))
if err != nil {
ftpp.froxy.Debug("FTP: %s: %s", addr, err)
netconn.Close()
return nil, err
}
// Login
user := site_url.User.Username()
pass, _ := site_url.User.Password()
if user == "" {
user, pass = "anonymous", "anonymous"
}
ftpp.froxy.Debug("FTP: login %s %s", user, pass)
err = conn.Login(user, pass)
if err != nil {
ftpp.froxy.Debug("FTP: login %s %s: %s", user, pass, err)
netconn.Close()
return nil, err
} else {
ftpp.froxy.Debug("FTP: login %s %s: OK", user, pass)
}
ftpp.froxy.IncCounter(&ftpp.froxy.Counters.FTPConnections)
return conn, nil
}
//
// Get a connection
//
func (ftpp *FTPProxy) getConn(site string) *ftpConn {
ftpp.idleLock.Lock()
defer ftpp.idleLock.Unlock()
var conn *ftpConn
if bucket := ftpp.idleConnections[site]; bucket != nil {
var expires time.Time
for c, t := range bucket {
if conn == nil || expires.After(t) {
conn, expires = c, t
}
}
if conn != nil {
delete(bucket, conn)
conn.reused = true
}
if len(bucket) == 0 {
delete(ftpp.idleConnections, site)
}
}
return conn
}
//
// Put a connection
//
func (conn *ftpConn) putConn() {
if conn.closed {
return
}
conn.ftpp.idleLock.Lock()
defer conn.ftpp.idleLock.Unlock()
bucket := conn.ftpp.idleConnections[conn.site]
if bucket == nil {
bucket = make(ftpIddleConnBucket)
conn.ftpp.idleConnections[conn.site] = bucket
}
if _, found := bucket[conn]; found {
panic("internal error")
}
bucket[conn] = time.Now().Add(5 * time.Minute)
select {
case conn.ftpp.idleChan <- struct{}{}:
}
}
//
// Close a connection
//
func (conn *ftpConn) Close() {
if !conn.closed {
conn.closed = true
conn.netconn.Close()
conn.ftpp.froxy.DecCounter(&conn.ftpp.froxy.Counters.FTPConnections)
}
}
//
// This function expires idle connections. It runs as a goroutine
//
func (ftpp *FTPProxy) expireIdleConnections() {
timer := time.NewTimer(time.Hour)
timer.Stop()
for {
select {
case <-timer.C:
case _, ok := <-ftpp.idleChan:
if !ok {
return
}
}
now := time.Now()
next := now.Add(1000 * time.Hour)
for site, bucket := range ftpp.idleConnections {
for conn, exp := range bucket {
switch {
case !exp.After(now):
conn.Close()
delete(bucket, conn)
case exp.Before(next):
next = exp
}
}
if len(bucket) == 0 {
delete(ftpp.idleConnections, site)
}
}
if len(ftpp.idleConnections) != 0 {
timer.Reset(next.Sub(now))
}
}
}