-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
303 lines (259 loc) · 7.49 KB
/
client.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
package main
import (
"errors"
"fmt"
"github.com/mgutz/ansi"
"log"
"net"
"os"
"socks"
"time"
"tun"
)
var packet_seq uint64 = 0
var send_buf []byte = make([]byte, BUF_SIZE)
var ifs []*Iface
func client(remote_addr *net.UDPAddr, local_ifs []string) {
debug(2, "Initializing tun device")
tundev, err := tun.Tun_alloc(tun.IFF_TUN | tun.IFF_NO_PI)
if err != nil {
log.Print("Could not allocate tun device")
log.Fatal(err)
}
err = tun.SetMTU(tundev.Name(), 1500-TUNNEL_OVERHEAD)
if err != nil {
log.Print("Could not set MTU: ", err)
}
debug(0, "Opened up tun device "+tundev.Name())
debug(0, "Initializing interfaces...")
// create list of local Ifs and store in global
ifs = setupIfs(local_ifs)
debug(0, "Sending handshake to server...")
err = registerBegin(ifs[0], remote_addr)
if err != nil {
log.Fatal(err)
}
for _, iface := range ifs {
debugf(1, "Registering %s with server...", iface.IP.IP)
err = registerClient(iface, remote_addr)
if err != nil {
log.Fatal(err)
}
}
debug(1, "Configuring device with ifconfig")
err = tun.Ifconfig(tundev.Name(), TTT_CLIENT_IP, TTT_SERVER_IP)
if err != nil {
log.Fatal(err)
}
log.Print("Ready ...")
// set up listening channels for udp and tun
tunchan := make(chan []byte)
udpchan := make(chan UDPRecv)
fwdchan := make(chan []byte)
go listenTun(tundev, tunchan)
for _, iface := range ifs {
go listenUDP(iface, udpchan)
}
// put packet forwarding in a separate goroutine to be able to do
// round-robin load-balancing and more
go forwardPacketHandler(remote_addr, fwdchan)
for {
select {
case tun_pkt, ok := <-tunchan:
if !ok {
log.Fatal("Error reading from tun")
}
debugf(3, "Got a packet of %d bytes for %s", len(tun_pkt),
get_ip_dest(tun_pkt))
debugf(3, "Sending to "+remote_addr.String())
// pass along packet
// TODO: use another buffer to prevent clobbering things
fwdchan <- tun_pkt
case udpr, ok := <-udpchan:
if !ok {
log.Fatal("Error reading from udp")
}
envelope := udpr.Data
remote_addr := udpr.RemoteAddr
debugf(3, "Got packet of len %d from %s", len(envelope), remote_addr)
switch envelope[0] {
case TTT_DATA: // packet to be forwarded
pkt := envelope[ENVELOPE_LENGTH:]
// pass along packet
tundev.Write(pkt)
default:
debug(1, "Received unhandled packet of type ", envelope[0])
}
}
}
}
// gets a list of network interfaces (eth0, eth1, wlan0, ...)
// and does the following:
// 1. gets the (first) IP associated with that interface
// 2. creates a unix socket bound to that interface
// 3. creates a golang udp listener to listen on that address
// 4. creates a tuntuntun Iface struct for that interface
func setupIfs(ifs []string) []*Iface {
var iflist = make([]*Iface, 0)
for i, v := range ifs {
// Figure out interface's IP
log.Printf("Getting ip of %s", v)
ip, err := getIfaceAddr(v)
if err != nil {
log.Fatal(err)
}
debugf(1, "IP of %s is %s", v, ip)
debug(1, "Disabling reverse path filtering on ", v)
disableRPFilter(v)
// create socket bound to this if
// put every socket on a different port to avoid conflicts
fd, err := socks.CreateDeviceBoundUDPSocket(ip, uint16(TUNTUNTUN_CLIENT_PORT+i), v)
if err != nil {
log.Fatal(err)
}
// try listening on this IP
/* deprecated, listening on unix sockets now
udpaddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", ip, TUNTUNTUN_CLIENT_PORT))
if err != nil {
log.Fatal(err)
}
debug(1, "Listening on ", udpaddr)
conn, err := net.ListenUDP("udp", udpaddr)
if err != nil {
log.Print("Could not listen to ", udpaddr)
log.Fatal(err)
}
*/
// create if struct and add to list
iflist = append(iflist, &Iface{
Name: v,
FD: fd,
IP: &net.UDPAddr{
IP: *ip,
Port: TUNTUNTUN_CLIENT_PORT,
},
Status: IFACE_STATUS_UP,
//Conn: conn,
})
debug(1, fmt.Sprintf("Created link %s", v))
}
return iflist
}
func registerBegin(iface *Iface, remote_addr *net.UDPAddr) error {
registration := []byte{TTT_REGISTER_BEGIN}
debug(1, "Beginning registration...")
_, err := iface.WriteToUDP(registration, remote_addr)
if err != nil {
log.Fatal("Could not send registration: ", err)
}
ack_chan := make(chan bool) // alert us if we get an ack from the server
// keep reading until we get a REGISTER_BEGIN ack from server
go func() {
read_buf := make([]byte, 100) // buffer to receive ack
for {
count, _, err := iface.ReadFromUDP(read_buf)
if err != nil {
log.Fatal(err)
}
if count > 0 && read_buf[0] == TTT_REGISTER_BEGIN {
ack_chan <- true
break
}
}
}()
// retry registration every 2 seconds
WAIT:
for {
select {
case did_ack := <-ack_chan:
if did_ack {
break WAIT
}
case <-time.After(2 * time.Second):
debug(0, "Resending registration...")
_, err = iface.WriteToUDP(registration, remote_addr)
if err != nil {
log.Fatal("Could not send registration: ", err)
}
}
}
// TODO: wait for registration acknownledgment
return err
}
func registerClient(iface *Iface, remote_addr *net.UDPAddr) error {
registration := []byte{TTT_REGISTER}
log.Printf("Registering via fd %d", iface.FD)
b, err := iface.WriteToUDP(registration, remote_addr)
log.Printf("Sent out %d bytes for registration", b)
time.Sleep(400 * time.Millisecond)
// TODO: wait for registration acknownledgment
return err
}
func forward_packet(writer UDPReadWrite, remote_addr *net.UDPAddr, pkt []byte) error {
total_len := len(pkt) + ENVELOPE_LENGTH
if uint(total_len) > BUF_SIZE {
return errors.New(fmt.Sprintf("%d packet too long for %d", len(pkt), BUF_SIZE))
}
send_buf[0] = ENV_DATA
send_buf[1] = byte(packet_seq >> 24)
send_buf[2] = byte(packet_seq >> 16)
send_buf[3] = byte(packet_seq >> 8)
send_buf[4] = byte(packet_seq >> 0)
packet_seq++
copy(send_buf[5:], pkt)
_, err := writer.WriteToUDP(send_buf[:total_len], remote_addr)
if err != nil {
return err
}
return nil
}
func forwardPacketHandler(remote_addr *net.UDPAddr, fwdchan chan []byte) {
colors := []string{"magenta", "yellow", "cyan", "white:blue", "black:white"}
ansi_colors := make([]string, len(colors))
for i, color := range colors {
ansi_colors[i] = ansi.ColorCode(color)
}
ansi_reset := ansi.ColorCode("reset")
skip_ifaces := 0 // offset to packet_seq, used for skipping down ifaces
for {
// round-robin by using packet sequence
iface := ifs[packet_seq%uint64(len(ifs)+skip_ifaces)]
// skip this iface if it's status is DOWN or CONGESTED only if
// there are other working ifaces to use
for iface.Status != IFACE_STATUS_UP && upIfaces(ifs) > 0 {
debug(2, "Skipping iface ", iface.Name)
skip_ifaces++
iface = ifs[packet_seq%uint64(len(ifs)+skip_ifaces)]
}
pkt := <-fwdchan
//fmt.Printf("sending out conn %p", iface.IP)
err := forward_packet(iface, remote_addr, pkt)
if err != nil {
log.Print(err)
}
// log statistics in if
iface.packets_sent++
// does not count envelope in bytes sent
iface.bytes_sent = iface.bytes_sent + uint64(len(pkt))
// make colorful display of packets
// each iface gets a different color, and we print 'S' for sent and 'R' for received
if DEBUG_LEVEL >= 1 {
fmt.Print(ansi_colors[int(packet_seq%uint64(len(ifs)))%len(colors)], "S", ansi_reset)
}
if err != nil {
log.Print(err)
}
}
}
// disables reverse-path filtering for the given device (or "all")
func disableRPFilter(device string) error {
f, err := os.Open("/proc/sys/net/ipv4/conf/" + device + "/rp_filter")
if err != nil {
return err
}
_, err = f.Write([]byte{'0'})
if err != nil {
return err
}
return f.Close()
}