-
Notifications
You must be signed in to change notification settings - Fork 1
/
bridge_tcp.go
531 lines (471 loc) · 11 KB
/
bridge_tcp.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
529
530
531
package l2
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
"math/rand"
"net"
"sort"
"strconv"
"sync"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
type TCPListener struct {
Listener net.Listener
StartedAt time.Time
}
type TCPConn struct {
sync.RWMutex
*net.TCPConn
closeOnce sync.Once
IPs []net.IP
Addrs []net.HardwareAddr
T0 time.Time
}
type StartTCP func(
bridgeIndex int,
inboundChan chan *Inbound,
outboundChan chan *Outbound,
ready chan struct{},
inboundSenderGroup *sync.WaitGroup,
)
func (n *Network) StartTCP(
closing Closing,
spawn Spawn,
getTime GetTime,
trigger Trigger,
sysAddrs SystemInterfaceAddrs,
readInbound ReadInbound,
localNode *Node,
newSendQueue NewSendQueue,
key CryptoKey,
allNodes AllNodes,
getReachableIP GetReachableIP,
) StartTCP {
return func(
bridgeIndex int,
inboundChan chan *Inbound,
outboundChan chan *Outbound,
ready chan struct{},
inboundSenderGroup *sync.WaitGroup,
) {
scope := n.RootScope
// port
portShiftInterval := time.Millisecond * 5113
listenerDuration := portShiftInterval * 2
connDuration := portShiftInterval * 20
getPort := shiftingPort(
fmt.Sprintf("%x-tcp-", key),
portShiftInterval,
)
// listener
listeners := make(map[string]*TCPListener)
// conn
// string -> *TCPConn
var conns sync.Map
// conn funcs
deleteConn := func(conn *TCPConn) {
// delete conn from conns
var deleted []*TCPConn
conns.Range(func(k, v any) bool {
if v == nil {
return true
}
c := v.(*TCPConn)
if c == conn {
conns.Delete(k)
deleted = append(deleted, c)
}
return true
})
for _, c := range deleted {
trigger(scope.Fork(
&c,
), EvTCP, EvTCPConnDeleted)
}
}
readConn := func(conn *TCPConn) {
inboundSenderGroup.Add(1)
defer inboundSenderGroup.Done()
var err error
defer func() {
if err != nil {
trigger(scope.Fork(
&conn, &err,
), EvTCP, EvTCPReadInboundError)
}
conn.closeOnce.Do(func() {
conn.Close()
})
select {
case <-closing:
default:
deleteConn(conn)
}
}()
parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet)
parser.SetDecodingLayerContainer(gopacket.DecodingLayerSparse(nil))
var eth layers.Ethernet
var arp layers.ARP
var ipv4 layers.IPv4
parser.AddDecodingLayer(ð)
parser.AddDecodingLayer(&ipv4)
parser.AddDecodingLayer(&arp)
decoded := make([]gopacket.LayerType, 0, 10)
var inbound *Inbound
for {
var length uint16
if err = binary.Read(conn, binary.LittleEndian, &length); err != nil {
break
}
inbound, err = readInbound(
&io.LimitedReader{
R: conn,
N: int64(length),
},
)
if err != nil {
break
}
conn.RLock()
if (len(conn.Addrs) == 0 || len(conn.IPs) == 0) && len(inbound.Eth) > 0 {
conn.RUnlock()
parser.DecodeLayers(inbound.Eth, &decoded)
for _, t := range decoded {
s:
switch t {
case layers.LayerTypeEthernet:
if !bytes.Equal(eth.DstMAC, EthernetBroadcast) {
destAddr := make(net.HardwareAddr, len(eth.DstMAC))
copy(destAddr, eth.DstMAC)
inbound.DestAddr = &destAddr
}
if !bytes.Equal(eth.SrcMAC, EthernetBroadcast) {
addr := make(net.HardwareAddr, len(eth.SrcMAC))
copy(addr, eth.SrcMAC)
for _, a := range conn.Addrs {
if bytes.Equal(a, addr) {
break s
}
}
conn.Lock()
conn.Addrs = append(conn.Addrs, addr)
conn.Unlock()
trigger(scope.Fork(
&conn, &addr,
), EvTCP, EvTCPConnGotAddr)
}
case layers.LayerTypeARP:
if !IPv4zero.Equal(arp.SourceProtAddress) {
ip := make(net.IP, len(arp.SourceProtAddress))
copy(ip, arp.SourceProtAddress)
for _, i := range conn.IPs {
if i.Equal(ip) {
break s
}
}
conn.Lock()
conn.IPs = append(conn.IPs, ip)
conn.Unlock()
trigger(scope.Fork(
&conn, &ip,
), EvTCP, EvTCPConnGotIP)
}
case layers.LayerTypeIPv4:
if !IPv4zero.Equal(ipv4.SrcIP) {
ip := make(net.IP, len(ipv4.SrcIP))
copy(ip, ipv4.SrcIP)
for _, i := range conn.IPs {
if i.Equal(ip) {
break s
}
}
conn.Lock()
conn.IPs = append(conn.IPs, ip)
conn.Unlock()
trigger(scope.Fork(
&conn, &ip,
), EvTCP, EvTCPConnGotIP)
}
}
}
} else {
conn.RUnlock()
}
inbound.BridgeIndex = uint8(bridgeIndex)
select {
case inboundChan <- inbound:
trigger(scope.Fork(
&conn, &inbound,
), EvTCP, EvTCPInboundSent)
case <-closing:
}
}
}
dialer := newDialer()
dialer.Timeout = listenerDuration
refreshConns := func() {
for _, node := range allNodes {
if !node.HasBridge(BridgeTCP) {
continue
}
node := node
if node == localNode {
port := getPort(node, getTime())
hostPort := net.JoinHostPort("0.0.0.0", strconv.Itoa(port))
// local node, listen
if _, ok := listeners[hostPort]; ok {
continue
}
// listen
trigger(scope.Fork(
&hostPort,
), EvTCP, EvTCPListening)
ln, err := listenConfig.Listen(context.Background(), "tcp", hostPort)
if err != nil {
trigger(scope.Fork(
&hostPort, &err,
), EvTCP, EvTCPListenFailed)
continue
}
listener := &TCPListener{
Listener: ln,
StartedAt: getTime(),
}
trigger(scope.Fork(
&listener,
), EvTCP, EvTCPListened)
spawn(func() {
for {
netConn, err := ln.Accept()
if err != nil {
return
}
trigger(scope.Fork(
&listener, &netConn,
), EvTCP, EvTCPAccepted)
spawn(func() {
if err := netConn.SetDeadline(getTime().Add(connDuration)); err != nil {
return
}
conn := &TCPConn{
TCPConn: netConn.(*net.TCPConn),
T0: time.Now(),
}
conns.Store(netConn.RemoteAddr().String(), conn)
trigger(scope.Fork(
&conn,
), EvTCP, EvTCPConnAdded)
readConn(conn)
})
}
})
listeners[hostPort] = listener
} else {
// non-local node
ip := getReachableIP(node)
if len(ip) > 0 {
port := getPort(node, getTime())
hostPort := net.JoinHostPort(ip.String(), strconv.Itoa(port))
if _, ok := conns.Load(hostPort); ok {
continue
}
conns.Store(hostPort, nil)
// connect
spawn(func() {
trigger(scope.Fork(
&hostPort, &node,
), EvTCP, EvTCPDialing)
netConn, err := dialer.Dial("tcp", hostPort)
if err != nil {
conns.Delete(hostPort)
trigger(scope.Fork(
&hostPort, &node,
), EvTCP, EvTCPDialFailed)
return
}
trigger(scope.Fork(
&hostPort, &node, &netConn,
), EvTCP, EvTCPDialed)
if err := netConn.SetDeadline(getTime().Add(connDuration)); err != nil {
conns.Delete(hostPort)
return
}
conn := &TCPConn{
TCPConn: netConn.(*net.TCPConn),
IPs: []net.IP{
node.LanIP,
},
T0: time.Now(),
}
trigger(scope.Fork(
&conn, &node.LanIP,
), EvTCP, EvTCPConnGotIP)
conns.Store(hostPort, conn)
trigger(scope.Fork(
&conn,
), EvTCP, EvTCPConnAdded)
readConn(conn)
})
}
}
}
trigger(scope, EvTCPRefreshed)
}
refreshConns()
refreshConnsTicker := time.NewTicker(time.Second * 1)
listenerCheckTicker := time.NewTicker(time.Second * 5)
close(ready)
trigger(scope, EvTCP, EvTCPReady)
queue := newSendQueue(
func(ip *net.IP, addr *net.HardwareAddr, data []byte) {
sent := false
var candidates []*TCPConn
conns.Range(func(_, v any) bool {
if v == nil {
return true
}
conn := v.(*TCPConn)
skip := false
conn.RLock()
// no identity
if len(conn.Addrs) == 0 && len(conn.IPs) == 0 {
skip = true
}
// ip not match
if ip != nil && len(conn.IPs) > 0 {
ok := false
for _, connIP := range conn.IPs {
if connIP.Equal(*ip) {
ok = true
break
}
}
if !ok {
skip = true
}
}
// addr not match
if addr != nil && len(conn.Addrs) > 0 {
ok := false
for _, connAddr := range conn.Addrs {
if bytes.Equal(connAddr, *addr) {
ok = true
break
}
}
if !ok {
skip = true
}
}
conn.RUnlock()
if skip {
return true
}
candidates = append(candidates, conn)
return true
})
sort.Slice(candidates, func(i, j int) bool {
a := candidates[i]
b := candidates[j]
aIsNew := time.Since(a.T0) < portShiftInterval
bIsNew := time.Since(b.T0) < portShiftInterval
if aIsNew != bIsNew {
if aIsNew && !bIsNew {
return true
} else if !aIsNew && bIsNew {
return false
}
}
aGotAddr := len(a.Addrs) > 0
bGotAddr := len(b.Addrs) > 0
if aGotAddr != bGotAddr {
if aGotAddr && !bGotAddr {
return true
} else if !aGotAddr && bGotAddr {
return false
}
}
return rand.Intn(2) == 0
})
// send
for _, conn := range candidates {
conn.SetWriteDeadline(time.Now().Add(time.Second * 2))
if _, err := conn.Write(data); err != nil {
conn.closeOnce.Do(func() {
conn.Close()
})
trigger(scope.Fork(
&conn, &err,
), EvTCP, EvTCPWriteError)
deleteConn(conn)
continue
}
sent = true
break
}
if !sent {
trigger(scope.Fork(
&conns, &ip, &addr,
), EvTCP, EvTCPNotSent)
}
},
)
for {
t0 := time.Now()
var op string
select {
case outbound := <-outboundChan:
op = "enqueue outbound"
if outbound == nil {
break
}
queue.enqueue(outbound)
case <-refreshConnsTicker.C:
op = "refresh"
refreshConns()
case <-listenerCheckTicker.C:
op = "check listeners"
now := getTime()
for addr, listener := range listeners {
if now.Sub(listener.StartedAt) > listenerDuration {
listener.Listener.Close()
delete(listeners, addr)
trigger(scope.Fork(
&listener,
), EvTCP, EvTCPListenerClosed)
}
}
case <-queue.timer.C:
op = "queue tick"
queue.tick()
case <-closing:
for _, ln := range listeners {
ln.Listener.Close()
trigger(scope.Fork(
&ln,
), EvTCP, EvTCPListenerClosed)
}
conns.Range(func(_, v any) bool {
if v == nil {
return true
}
conn := v.(*TCPConn)
_ = conn.SetDeadline(getTime().Add(-time.Hour))
return true
})
trigger(scope, EvTCP, EvTCPClosed)
return
}
if d := time.Since(t0); d > time.Second*5 {
trigger(scope.Fork(
&d, &op,
), EvTCP, EvTCPSlow)
}
}
}
}