-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpublicserver.js
123 lines (91 loc) · 3.43 KB
/
publicserver.js
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
#!/usr/bin/env node
// based on http://www.bford.info/pub/net/p2pnat/index.html
var socketA = null;
var socketB = null;
var detailsA = {
name: 'A',
localAddress: null,
localPort: null,
remoteAddress: null,
remotePort: null
};
var detailsB = {
name: 'B',
localAddress: null,
localPort: null,
remoteAddress: null,
remotePort: null
};
// assuming A will connect first:
var server = require('net').createServer(function (socket) {
if(!socketA) {
aConnects(socket);
}else{
bConnects(socket);
}
});
server.listen(9999, function (err) {
if(err) return console.log(err);
console.log('server listening on', server.address().address + ':' + server.address().port);
});
function aConnects (socket) {
socketA = socket;
console.log('> (A) assuming A is connecting');
console.log('> (A) remote address and port are:', socket.remoteAddress, socket.remotePort);
console.log('> (A) storing this for when B connects');
detailsA.remoteAddress = socket.remoteAddress;
detailsA.remotePort = socket.remotePort;
socket.on('data', function (data) {
console.log('> (A) incomming data from A:', data.toString());
var localDataA = JSON.parse(data.toString());
if(!localDataA.name || localDataA.name != 'A') return console.log('> (A) this is not the local data of A');
console.log('> (A) storing this for when B connects');
console.log('');
detailsA.localAddress = localDataA.localAddress;
detailsA.localPort = localDataA.localPort;
console.log('> (A) sending remote details back to A');
socket.write(JSON.stringify(detailsA));
console.log('> (A)', detailsA.localAddress + ':' + detailsA.localPort, '===> (NAT of A)', detailsA.remoteAddress + ':' + detailsA.remotePort, '===> (S)', socket.localAddress + ':' + socket.localPort);
});
socket.on('end', function () {
console.log('> (A) connection closed.');
socketA = null;
});
socket.on('error', function (err) {
console.log('> (A) connection closed with err (',err,').');
socketA = null;
});
}
function bConnects(socket) {
socketB = socket;
console.log('> (B) assuming B is connecting');
console.log('> (B) remote address and port are:', socket.remoteAddress, socket.remotePort);
console.log('> (B) storing this');
detailsB.remoteAddress = socket.remoteAddress;
detailsB.remotePort = socket.remotePort;
socket.on('data', function (data) {
console.log('> (B) incomming data from B:', data.toString());
var localDataB = JSON.parse(data.toString());
if(!localDataB.name || localDataB.name != 'B') return console.log('> (B) this is not the local data of B');
console.log('> (B) storing this');
console.log('');
detailsB.localAddress = localDataB.localAddress;
detailsB.localPort = localDataB.localPort;
console.log('> (B) sending remote details back to B');
socket.write(JSON.stringify(detailsB));
console.log('> (B)', detailsB.localAddress + ':' + detailsB.localPort, '===> (NAT of B)', detailsB.remoteAddress + ':' + detailsB.remotePort, '===> (S)', socket.localAddress + ':' + socket.localPort);
console.log('> (S->A) sending B\'s details:', detailsB);
socketA.write(JSON.stringify(detailsB));
console.log('> (S->B) sending A\'s details:', detailsA);
socketB.write(JSON.stringify(detailsA));
console.log('');
});
socket.on('end', function () {
console.log('> (B) connection closed.');
socketB = null;
});
socket.on('error', function (err) {
console.log('> (B) connection closed with err (',err,').');
socketB = null;
});
}