-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrouter.go
1091 lines (1016 loc) · 28.1 KB
/
router.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Author: Armaan Sood
// A simple overlay network based on Tor.
// For educational purposes.
//
// If I had more time, I would make the code
// more modular by creating a router struct that
// can be passed to other classes, rather than using
// this 1000+ line class.
// I would also change the fnv32 hashing function of
// the maps (from github.com/orcaman) to use Go's built-in
// fnv32 function.
package main
import (
"bufio"
"encoding/binary"
"flag"
"fmt"
"io"
"log"
"math/rand"
"net"
_ "net/http/pprof"
"os"
"strconv"
"strings"
"sync"
"time"
mapuint16 "torgo/concurrent_uint16map"
mapuint32 "torgo/concurrent_uint32map"
p "torgo/proxy"
r "torgo/regagent"
)
// Cell types
const (
create uint8 = 1
created uint8 = 2
relayCell uint8 = 3
destroy uint8 = 4
open uint8 = 5
opened uint8 = 6
openFailed uint8 = 7
createFailed uint8 = 8
)
// Relay types
const (
begin uint8 = 1
data uint8 = 2
end uint8 = 3
connected uint8 = 4
extend uint8 = 6
extended uint8 = 7
beginFailed uint8 = 11
extendFailed uint8 = 12
)
// Size of channels.
const bufferSize int = 10000
// Size of channel that TCP writes from.
const sendBufferSize int = 1000000
const circuitLength int = 3
const maxDataSize int = 498
type circuit struct {
circuitID uint16
routerID uint32
}
type relay struct {
circuitID uint16
streamID uint16
digest uint32
bodyLength uint16
relayCommand uint8
body []byte
}
// TODO: Create structs for each map and
// unify the design to only use RWMutexes.
// Maps from router ID to a sending channel.
var currentConnections = mapuint32.New()
func currentConnectionsRead(routerID uint32) chan []byte {
result, _ := currentConnections.Get(routerID)
if result == nil {
return nil
}
return result.(chan []byte)
}
// Maps from a circuit to a reading channel.
var circuitToInput = make(map[circuit](chan []byte))
var circuitToInputLock = sync.RWMutex{}
func circuitToInputRead(c circuit) (chan []byte, bool) {
circuitToInputLock.RLock()
defer circuitToInputLock.RUnlock()
result, ok := circuitToInput[c]
return result, ok
}
func circuitToInputWrite(c circuit, channel chan []byte) {
circuitToInputLock.Lock()
defer circuitToInputLock.Unlock()
circuitToInput[c] = channel
}
// Maps from a circuit to a replies channel (extended, created).
var circuitToReply = make(map[circuit](chan []byte))
var circuitToReplyLock = sync.RWMutex{}
// Holds a circuit if it's the last hop.
var circuitToIsEnd = make(map[circuit](bool))
var circuitToIsEndLock = sync.RWMutex{}
func circuitToIsEndRead(c circuit) (bool, bool) {
circuitToIsEndLock.RLock()
defer circuitToIsEndLock.RUnlock()
result, ok := circuitToIsEnd[c]
return result, ok
}
func circuitToIsEndWrite(c circuit, value bool) {
circuitToIsEndLock.Lock()
defer circuitToIsEndLock.Unlock()
circuitToIsEnd[c] = value
}
// Maps from a stream to a receiving channel.
var streamToReceiverLock = sync.RWMutex{}
var streamToReceiver = mapuint16.New()
func streamToReceiverRead(streamID uint16) chan []byte {
result, ok := streamToReceiver.Get(streamID)
if !ok {
return nil
}
return result.(chan []byte)
}
// Stores a value if we initiated the TCP connection to the agent.
var initiatedConnection = make(map[uint32]bool)
var initiatedConnectionLock = sync.RWMutex{}
var firstCircuit circuit
var agent *r.Agent
var proxyPort uint16
var port uint16
var ip string
var routersToFetch string
var ipToRegister = getOutboundIP()
// Bijective mapping from (circuitID, routerID) to (circuitID, routerID).
// Mapping from (circuitID, routerID) to (0, 0) means it is the
var routingTableForward = make(map[circuit]circuit)
var routingTableBackward = make(map[circuit]circuit)
var routingTableLock = sync.RWMutex{}
var routerID uint32
var wg sync.WaitGroup
func cleanup() {
circuits := getAllCircuitsOnThisRouter()
for _, c := range circuits {
sendCellToAgent(c.routerID, createCell(c.circuitID, destroy))
}
}
func getAllCircuitsOnThisRouter() []circuit {
circuitToInputLock.RLock()
defer circuitToInputLock.RUnlock()
result := make([]circuit, len(circuitToInput))
i := 0
for k := range circuitToInput {
result[i] = k
i++
}
return result
}
func deleteRouter(targetRouterID uint32) {
for _, c := range getAllCircuitsOnThisRouter() {
if c.routerID == targetRouterID {
destroyCircuit(c)
}
}
initiatedConnectionLock.Lock()
delete(initiatedConnection, targetRouterID)
initiatedConnectionLock.Unlock()
currentConnections.Remove(targetRouterID)
}
func startRouter(routerName string, group int, address string) {
go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
}
go cleanup()
fmt.Printf("\nUnregistering...\n")
agent.Unregister(ipToRegister, port)
fmt.Println("Shutting down router in 2 seconds...")
time.Sleep(2 * time.Second)
os.Exit(1)
}()
l, err := net.Listen("tcp", "0.0.0.0:0")
if err != nil {
fmt.Println(err)
os.Exit(2)
}
port = uint16(l.Addr().(*net.TCPAddr).Port)
fmt.Printf("Registering on %s with port %d\n", ipToRegister, port)
addressSplit := strings.Split(address, ":")
if len(addressSplit) != 2 {
log.Fatal("Incorrect registration server address!")
}
regServerIP := addressSplit[0]
regServerPort := addressSplit[1]
agent = new(r.Agent)
agent.StartAgent(regServerIP, regServerPort, false)
fmt.Printf("Attempting to register...\n")
for !(agent.Register(ipToRegister, port, routerID,
uint8(len(routerName)), routerName)) {
fmt.Println("Retrying...")
time.Sleep(3 * time.Second)
}
fmt.Printf("Registered router %d!\n\n", routerID)
rand.Seed(time.Now().Unix())
routersToFetch = "Tor61Router-" + string(fmt.Sprintf("%04d", group))
go routerServer(l)
createInitialCircuit()
proxyServer(proxyPort)
}
func createInitialCircuit() {
var responses []r.FetchResponse
fmt.Printf("Fetching routers that begin with %s...\n", routersToFetch)
responses = agent.Fetch(routersToFetch)
fetch:
for len(responses) < 2 {
fmt.Println("No other routers online. Waiting 3 seconds and retrying...")
time.Sleep(3 * time.Second)
responses = agent.Fetch(routersToFetch)
}
var circuitNodes []r.FetchResponse
potentialFirstNode := responses[rand.Intn(len(responses))]
for potentialFirstNode.Data == routerID {
potentialFirstNode = responses[rand.Intn(len(responses))]
}
potentialLastNode := responses[rand.Intn(len(responses))]
for potentialLastNode.Data == routerID {
potentialLastNode = responses[rand.Intn(len(responses))]
}
circuitNodes = append(circuitNodes, potentialFirstNode)
for i := 1; i < circuitLength-1; i++ {
circuitNodes = append(circuitNodes, responses[rand.Intn(len(responses))])
}
circuitNodes = append(circuitNodes, potentialLastNode)
firstCircuit = circuit{0, 0}
for (firstCircuit == circuit{0, 0}) {
fmt.Printf("Attempting to create first hop circuit with router %d...\n", circuitNodes[0].Data)
firstCircuit = createCircuit(circuitNodes[0].Data,
circuitNodes[0].IP+":"+strconv.Itoa(int(circuitNodes[0].Port)))
// If this node failed, try another.
if (firstCircuit == circuit{0, 0}) {
fmt.Printf("Failed to connect to %d\n", circuitNodes[0].Data)
for i, v := range responses {
if v == circuitNodes[0] {
responses[i] = responses[len(responses)-1]
}
}
responses = responses[:len(responses)-1]
if len(responses) < 2 {
goto fetch
}
circuitNodes[0] = responses[rand.Intn(len(responses))]
for circuitNodes[0].Data == routerID {
circuitNodes[0] = responses[rand.Intn(len(responses))]
}
fmt.Printf("Retrying with router %d\n", circuitNodes[0].Data)
}
}
fmt.Printf("Created first hop circuit: %+v\n", firstCircuit)
firstHopConnection := currentConnectionsRead(firstCircuit.routerID)
circuitToReplyLock.Lock()
circuitToReply[firstCircuit] = make(chan []byte, bufferSize)
circuitToReplyLock.Unlock()
go watchChannel(firstCircuit)
for i := 1; i < circuitLength; i++ {
fmt.Println("Attempting to extend circuit...")
var body []byte
address := circuitNodes[i].IP + ":" + strconv.Itoa(int(circuitNodes[i].Port))
body = append(body, address...)
body = append(body, 0)
body = append(body, make([]byte, 4)...)
binary.BigEndian.PutUint32(body[len(address)+1:], circuitNodes[i].Data)
relayReply := relay{}
relay := createRelay(firstCircuit.circuitID, 0, 0, uint16(len(body)), extend, body)
firstHopConnection <- relay
circuitToReplyLock.RLock()
waitChan := circuitToReply[firstCircuit]
circuitToReplyLock.RUnlock()
timeout := false
select {
case reply := <-waitChan:
relayReply = parseRelay(reply)
case <-time.After(5 * time.Second):
timeout = true
}
if timeout == true || relayReply.relayCommand != extended {
fmt.Printf("Failed to extend to router %d\n", circuitNodes[i].Data)
circuitNodes[i] = responses[rand.Intn(len(responses))]
if i == circuitLength-1 {
for circuitNodes[i].Data == routerID {
circuitNodes[i] = responses[rand.Intn(len(responses))]
}
}
fmt.Printf("Retrying with router %d\n", circuitNodes[i].Data)
i--
} else {
fmt.Printf("Successfully extended to %d!\n", circuitNodes[i].Data)
}
}
fmt.Printf("Created full circuit: %+v\n\n", circuitNodes)
}
// Creates a circuit between this router and the target router.
// Returns {0, 0} on failure.
func createCircuit(targetRouterID uint32, address string) circuit {
for !openConnectionIfNotExists(address, targetRouterID) {
return circuit{0, 0}
}
targetConnection := currentConnectionsRead(targetRouterID)
initiatedConnectionLock.RLock()
_, odd := initiatedConnection[targetRouterID]
initiatedConnectionLock.RUnlock()
newCircuitID := uint16(2 * rand.Intn(100000))
if odd {
newCircuitID = newCircuitID + 1
}
// In the rare case that there is already a circuit between this router
// and the target with the random number that was just generated, retry.
if _, ok := circuitToInputRead(circuit{newCircuitID, targetRouterID}); ok {
newCircuitID = uint16(2 * rand.Intn(100000))
if odd {
newCircuitID = newCircuitID + 1
}
}
cell := createCell(newCircuitID, create)
result := circuit{newCircuitID, targetRouterID}
if _, ok := circuitToInputRead(result); !ok {
circuitToInputWrite(result, make(chan []byte, bufferSize))
}
targetConnection <- cell
inputs, _ := circuitToInputRead(result)
reply := <-inputs
_, replyType := cellCIDAndType(reply)
if replyType != created {
circuitToInputLock.Lock()
delete(circuitToInput, result)
circuitToInputLock.Unlock()
return circuit{0, 0}
}
return result
}
func openConnectionIfNotExists(address string, targetRouterID uint32) bool {
_, ok := currentConnections.Get(targetRouterID)
return ok || openConnection(address, targetRouterID)
}
// Opens a connection to target router with address.
func openConnection(address string, targetRouterID uint32) bool {
conn, err := net.Dial("tcp", address)
if err != nil {
fmt.Println(err)
return false
}
openCell := createOpenCell(routerID, targetRouterID)
conn.Write(openCell)
reply := make([]byte, 512)
_, err = conn.Read(reply)
if err != nil {
return false
}
_, replyType := cellCIDAndType(reply)
if replyType != opened {
return false
}
currentConnections.SetIfAbsent(targetRouterID, make(chan []byte, sendBufferSize))
initiatedConnectionLock.Lock()
initiatedConnection[targetRouterID] = true
initiatedConnectionLock.Unlock()
go handleConnection(conn, targetRouterID)
return true
}
func acceptConnection(conn net.Conn) {
// New connection. First, wait for an "open".
cell := make([]byte, 512)
_, err := conn.Read(cell)
if err != nil {
conn.Close()
fmt.Println(err)
return
}
targetRouterID := uint32(0)
_, cellType := cellCIDAndType(cell)
if cellType == open {
agentOpened := uint32(0)
targetRouterID, agentOpened = readOpenCell(cell)
if agentOpened != routerID {
// Open cell sent with wrong router ID.
cell[2] = openFailed
conn.Write(cell)
conn.Close()
return
} else {
// Opened connection.
cell[2] = opened
conn.Write(cell)
}
} else {
cell[2] = openFailed
conn.Write(cell)
conn.Close()
return
}
currentConnections.SetIfAbsent(targetRouterID, make(chan []byte, sendBufferSize))
handleConnection(conn, targetRouterID)
}
func handleConnection(conn net.Conn, targetRouterID uint32) {
toSend := currentConnectionsRead(targetRouterID)
// Thread that blocks on data to send and sends it.
go func() {
for {
cell, stillOpen := <-toSend
if !stillOpen {
return
}
_, err := conn.Write(cell)
if err != nil {
return
}
}
}()
c := bufio.NewReader(conn)
for {
// Wait for data to come on the channel then process it.
cell := make([]byte, 512)
_, err := io.ReadFull(c, cell)
if err != nil {
fmt.Printf("Connection failure from %d! Deleting router information...\n", targetRouterID)
deleteRouter(targetRouterID)
conn.Close()
return
}
circuitID, cellType := cellCIDAndType(cell)
switch cellType {
case create:
// Other agent wants to create a new circuit.
// Need to ensure that this circuit ID is unique.
// We are now the endpoint of some circuit.
routingTableLock.RLock()
_, ok := routingTableForward[circuit{circuitID, targetRouterID}]
routingTableLock.RUnlock()
if ok {
// Circuit already existed.
cell[2] = createFailed
} else {
// Otherwise, create a channel to put that circuits data on
// and add it to the routing table.
_, ok := circuitToInputRead(circuit{circuitID, targetRouterID})
circuitToIsEndWrite(circuit{circuitID, targetRouterID}, true)
cell[2] = created
if !ok {
// Make sure it doesn't already exist.
circuitToInputWrite(circuit{circuitID, targetRouterID},
make(chan []byte, bufferSize))
// If not, someone is already watching the channel (self)
go watchChannel(circuit{circuitID, targetRouterID})
}
}
fmt.Printf("Created circuit %d to %d\n", circuitID, targetRouterID)
toSend <- cell
continue
default:
// Send the data directly to the circuit handler.
if cellType > 8 || cellType < 0 {
continue
}
inputs, _ := circuitToInputRead(circuit{circuitID, targetRouterID})
if inputs == nil {
continue
}
inputs <- cell
}
}
}
// Handles all messages sent by agent A on circuit C.
func watchChannel(c circuit) {
circuitToReplyLock.RLock()
_, ok := circuitToReply[c]
circuitToReplyLock.RUnlock()
if !ok {
circuitToReplyLock.Lock()
circuitToReply[c] = make(chan []byte, bufferSize)
circuitToReplyLock.Unlock()
}
for {
inputs, _ := circuitToInputRead(c)
cell := <-inputs
_, cellType := cellCIDAndType(cell)
_, endOfRelay := circuitToIsEndRead(c)
// The circuitID should already be known.
switch cellType {
case relayCell:
r := parseRelay(cell)
if r.body == nil {
continue
}
switch r.relayCommand {
case extend:
extendRelay(r, c, endOfRelay, cell)
case begin:
beginRelay(r, c, endOfRelay, cell)
case end:
endStream(r, c, endOfRelay, cell)
case data:
handleData(r, c, cell)
case connected:
handleStreamMessages(r, c, cell)
case beginFailed:
handleStreamMessages(r, c, cell)
case extended:
handleCircuitMessages(r, c, cell)
case extendFailed:
handleCircuitMessages(r, c, cell)
}
case destroy:
if !routeCellBackwards(c, cell) {
routeCellForwards(c, cell)
}
destroyCircuit(c)
if (firstCircuit == circuit{0, 0}) {
go createInitialCircuit()
}
}
}
}
func destroyCircuit(c circuit) {
circuitToReplyLock.Lock()
circuitToIsEndLock.Lock()
circuitToInputLock.Lock()
routingTableLock.Lock()
if firstCircuit == c {
firstCircuit = circuit{0, 0}
}
delete(circuitToReply, c)
delete(circuitToIsEnd, c)
delete(circuitToInput, c)
otherEnd, ok := routingTableForward[c]
if ok {
delete(routingTableForward, c)
delete(routingTableBackward, otherEnd)
} else {
otherEnd, ok = routingTableBackward[c]
if ok {
delete(routingTableForward, otherEnd)
delete(routingTableBackward, c)
}
}
routingTableLock.Unlock()
circuitToInputLock.Unlock()
circuitToIsEndLock.Unlock()
circuitToReplyLock.Unlock()
return
}
func handleCircuitMessages(r relay, c circuit, cell []byte) {
// Extended, extend failed.
if !routeCellBackwards(c, cell) {
circuitToReplyLock.RLock()
channel := circuitToReply[c]
if channel == nil {
return
}
circuitToReplyLock.RUnlock()
channel <- cell
}
}
func handleStreamMessages(r relay, c circuit, cell []byte) {
// Connected, begin failed.
if !routeCellBackwards(c, cell) {
channel := streamToReceiverRead(r.streamID)
if channel == nil {
return
}
channel <- cell
}
}
func routeCellBackwards(c circuit, cell []byte) bool {
routingTableLock.RLock()
previousCircuit, back := routingTableBackward[c]
routingTableLock.RUnlock()
if back {
binary.BigEndian.PutUint16(cell[:2], previousCircuit.circuitID)
sendCellToAgent(previousCircuit.routerID, cell)
return true
}
return false
}
func routeCellForwards(c circuit, cell []byte) bool {
routingTableLock.RLock()
nextCircuit, front := routingTableForward[c]
routingTableLock.RUnlock()
if front {
binary.BigEndian.PutUint16(cell[:2], nextCircuit.circuitID)
sendCellToAgent(nextCircuit.routerID, cell)
return true
}
return false
}
// r is the relay sent on circuit c.
// endOfRelay indicates whether this router is the last stop in the relay.
func extendRelay(r relay, c circuit, endOfRelay bool, cell []byte) {
if endOfRelay {
address := string(r.body[:clen(r.body)])
targetRouterID := binary.BigEndian.Uint32(r.body[(clen(r.body))+1:])
if targetRouterID == routerID {
// Don't extend a relay to ourselves.
sendCellToAgent(c.routerID, createRelay(r.circuitID, r.streamID,
r.digest, 0, extended, nil))
return
}
result := createCircuit(targetRouterID, address)
if (result != circuit{0, 0}) {
routingTableLock.Lock()
routingTableForward[c] = result
routingTableBackward[result] = c
routingTableLock.Unlock()
circuitToIsEndLock.Lock()
delete(circuitToIsEnd, c)
circuitToIsEndLock.Unlock()
sendCellToAgent(c.routerID, createRelay(r.circuitID, r.streamID,
r.digest, 0, extended, nil))
go watchChannel(result)
} else {
sendCellToAgent(c.routerID, createRelay(r.circuitID, r.streamID,
r.digest, 0, extendFailed, nil))
}
} else {
// If we're not the end of the relay, just foward the message.
routeCellForwards(c, cell)
}
}
func handleData(r relay, c circuit, cell []byte) {
if routeCellBackwards(c, cell) {
return
}
if routeCellForwards(c, cell) {
return
}
// This must be the endpoint, since there's nowhere to route it.
channel := streamToReceiverRead(r.streamID)
if channel == nil {
return
}
channel <- cell
}
func sendCellToAgent(targetRouterID uint32, cell []byte) {
if channel, ok := currentConnections.Get(targetRouterID); ok {
channel.(chan []byte) <- cell
}
}
func endStream(r relay, c circuit, endOfRelay bool, cell []byte) {
if routeCellBackwards(c, cell) {
return
}
if routeCellForwards(c, cell) {
return
}
// This must be the endpoint, since there's nowhere to route it.
channel := streamToReceiverRead(r.streamID)
if channel == nil {
return
}
close(channel)
streamToReceiver.Remove(r.streamID)
return
}
func beginRelay(r relay, c circuit, endOfRelay bool, cell []byte) {
if !endOfRelay {
routeCellForwards(c, cell)
return
}
address := string(r.body[:clen(r.body)])
conn, err := net.Dial("tcp", address)
if err != nil {
sendCellToAgent(c.routerID, createRelay(r.circuitID, r.streamID,
r.digest, 0, beginFailed, nil))
return
}
if _, ok := streamToReceiver.Get(r.streamID); ok {
// Stream ID is not unique.
sendCellToAgent(c.routerID, createRelay(r.circuitID, r.streamID,
r.digest, 0, beginFailed, nil))
return
}
streamToReceiver.Set(r.streamID, make(chan []byte, bufferSize))
fmt.Printf("Created stream %d to %s\n", r.streamID, address)
go handleStreamEnd(conn, r.streamID, c)
sendCellToAgent(c.routerID, createRelay(r.circuitID, r.streamID,
r.digest, 0, connected, nil))
circuitToIsEndWrite(c, true)
}
// The server end of the relay.
func handleStreamEnd(conn net.Conn, streamID uint16, c circuit) {
channel := streamToReceiverRead(streamID)
if channel == nil {
return
}
cell, alive := <-channel
if !alive {
return
}
relayReply := parseRelay(cell)
if relayReply.relayCommand == end {
conn.Close()
return
} else if relayReply.relayCommand == data {
conn.Write(relayReply.body)
go func() {
channel := streamToReceiverRead(streamID)
if channel == nil {
// Channel must have been closed.
conn.Close()
return
}
for {
// In HTTP, in case there is more to a request.
cell, alive := <-channel
if !alive {
conn.Close()
return
}
relayReply = parseRelay(cell)
if relayReply.relayCommand == data {
_, err := conn.Write(relayReply.body)
if err != nil {
// Connection must have been closed here.
// We now need to wait for the other end to
// close their side.
break
}
}
}
for {
_, alive := <-channel
if !alive {
return
}
}
}()
for {
buffer := make([]byte, maxDataSize)
n, err := conn.Read(buffer)
if err != nil {
toSend := createRelay(c.circuitID, streamID, 0, 0, end, nil)
sendCellToAgent(c.routerID, toSend)
// We don't explicitly close the channel until we receive an
// end message. Otherwise, we risk the chance of closing while
// someone sends data.
streamToReceiver.Remove(streamID)
conn.Close()
return
}
sendData(c, streamID, 0, buffer[:n])
}
}
}
func routerServer(l net.Listener) {
defer l.Close()
for {
c, err := l.Accept()
if err != nil {
fmt.Println(err)
continue
}
go acceptConnection(c)
}
}
func sendData(c circuit, streamID uint16, digest uint32, body []byte) {
relay := createRelay(c.circuitID, streamID, digest, uint16(len(body)), data, body)
sendCellToAgent(c.routerID, relay)
}
// DATA MANIPULATION FUNCTIONS
func createRelay(circuitID uint16, streamID uint16, digest uint32,
bodyLength uint16, relayCommand uint8, body []byte) []byte {
cell := createCell(circuitID, relayCell)
binary.BigEndian.PutUint16(cell[3:5], streamID)
binary.BigEndian.PutUint32(cell[7:11], digest)
binary.BigEndian.PutUint16(cell[11:13], bodyLength)
cell[13] = relayCommand
for i := uint16(0); i < bodyLength; i++ {
cell[i+14] = body[i]
}
return cell
}
func cellCIDAndType(cell []byte) (uint16, uint8) {
return binary.BigEndian.Uint16(cell[:2]), cell[2]
}
func parseRelay(cell []byte) relay {
circuitID, _ := cellCIDAndType(cell)
streamID := binary.BigEndian.Uint16(cell[3:5])
digest := binary.BigEndian.Uint32(cell[7:11])
bodyLength := binary.BigEndian.Uint16(cell[11:13])
relayCommand := cell[13]
if 14+bodyLength > 512 {
return relay{0, 0, 1, 0, 0, nil}
}
body := cell[14:(14 + bodyLength)]
return relay{circuitID, streamID, digest, bodyLength, relayCommand, body}
}
func createCell(circuitID uint16, cellType uint8) []byte {
header := make([]byte, 512)
binary.BigEndian.PutUint16(header[:2], circuitID)
header[2] = cellType
return header
}
func createOpenCell(agentOpener uint32, agentOpened uint32) []byte {
header := createCell(0, open)
binary.BigEndian.PutUint32(header[3:7], agentOpener)
binary.BigEndian.PutUint32(header[7:11], agentOpened)
return header
}
func readOpenCell(cell []byte) (uint32, uint32) {
agentOpener := binary.BigEndian.Uint32(cell[3:7])
agentOpened := binary.BigEndian.Uint32(cell[7:11])
return agentOpener, agentOpened
}
func createStream(streamID uint16, address string) bool {
var body []byte
body = append(body, address...)
body = append(body, 0)
streamToReceiver.Set(streamID, make(chan []byte, bufferSize))
relay := createRelay(firstCircuit.circuitID, streamID, 0, uint16(len(body)),
begin, body)
sendCellToAgent(firstCircuit.routerID, relay)
channel := streamToReceiverRead(streamID)
reply, alive := <-channel
if !alive {
streamToReceiver.Remove(streamID)
return false
}
relayReply := parseRelay(reply)
if relayReply.relayCommand != connected {
streamToReceiver.Remove(streamID)
return false
}
return true
}
func proxyServer(port uint16) {
fmt.Printf("Proxy listening on port %d\n", port)
l, err := net.Listen("tcp", "0.0.0.0:"+strconv.Itoa(int(port)))
if err != nil {
fmt.Println(err)
os.Exit(2)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
fmt.Println(err)
continue
}
go handleProxyConnection(conn)
}
}
func handleProxyConnection(conn net.Conn) {
if (firstCircuit == circuit{0, 0}) {
conn.Close()
return
}
header := p.ParseHTTPRequest(conn)
if header.IP == "" {
conn.Close()
return
}
ok := true
streamID := uint16(0)
for ok {
streamID = uint16(rand.Intn(1000000))
_, ok = streamToReceiver.Get(streamID)
}
if !createStream(streamID, header.IP+":"+header.Port) {
fmt.Printf("Could not connect to %s.\n", header.IP+":"+header.Port)
conn.Write([]byte("HTTP/1.1 502 Bad Gateway\r\n\r\n"))
conn.Close()
return
}
if !header.HTTPS {
// Now that we have a stream, we send over the initial request via a data
// message.
splitData := splitUpResult(header.Data, maxDataSize)
for _, request := range splitData {
// First, we send all the data...
sendData(firstCircuit, streamID, 0, request)
}
// Shouldn't reread, actually, and the endstream shouldn't delete.
channel := streamToReceiverRead(streamID)
if channel == nil {
// Channel must have been closed before we could even start.
conn.Close()
return
}
for {
// Then, we read all the data (until the other end gets an EOF).
cell, alive := <-channel
if !alive {
conn.Close()
return
}
replyRelay := parseRelay(cell)
if replyRelay.relayCommand == data {
_, err := conn.Write(replyRelay.body)
if err != nil {
toSend := createRelay(firstCircuit.circuitID, streamID, 0, 0, end, nil)
sendCellToAgent(firstCircuit.routerID, toSend)
// This means that we may never hear from the endStream.
streamToReceiver.Remove(streamID)
break
}
}
}
conn.Close()
for {
_, alive := <-channel
if !alive {
return
}
}
} else {
conn.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
go func() {
channel := streamToReceiverRead(streamID)
if channel == nil {
// Channel must have been closed.
conn.Close()
return
}
for {
// Reading data from the Tor network to the browser.
cell, alive := <-channel
if !alive {
conn.Close()
return
}
relayReply := parseRelay(cell)
if relayReply.relayCommand == data {
_, err := conn.Write(relayReply.body)
if err != nil {
break