-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer_cmd.go
70 lines (64 loc) · 1.47 KB
/
transfer_cmd.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
package main
import (
"fmt"
"math/rand"
"net"
"time"
)
// handleType provides support for the type command.
func handleType(c *client) {
if len(c.input) < 2 {
sendMessage(c, 500)
return
}
arg := rune(c.input[1][0])
// Only supporting one mode currently.
if arg == 'A' {
c.ftype = arg
sendMessage(c, 200)
} else {
sendMessage(c, 504)
}
}
// getPassiveConn sets up passive connection listener on the given address.
func getPassiveConn(c *client, lstraddr *net.TCPAddr) {
listener, err := net.ListenTCP("tcp", lstraddr)
if err != nil {
sendMessage(c, 500)
}
listener.SetDeadline(time.Now().Add(2 * time.Minute))
defer listener.Close()
for {
pasvConn, err := listener.Accept()
if err == err.(*net.OpError) {
sendMessage(c, 500)
break
} else if err != nil {
continue
}
c.pasv = pasvConn
break
}
}
// handlePasv sets up address to being called by the client.
func handlePasv(c *client) {
port := rand.Int()%49151 + 1024
p1 := port & 0xff
p2 := (port >> 8) & 0xff
outAddr := fmt.Sprintf("(0,0,0,0,%d,%d)", p1, p2)
fmt.Println(port)
address := fmt.Sprintf("0.0.0.0:%d", port)
fmt.Println(address)
lstraddr, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
sendMessage(c, 500)
return
}
msg := fmt.Sprintf("Connect to %s", outAddr)
sendPasv(c, msg)
go getPassiveConn(c, lstraddr)
}
// handleMode sets transfer mode. Currently always reports 200, as nothing special goes on here!
func handleMode(c *client) {
sendMessage(c, 200)
}