-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
198 lines (176 loc) · 5.96 KB
/
index.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
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
const WebSocket = require('ws');
const wss = new WebSocket.Server({
port: 8080,
perMessageDeflate: {
zlibDeflateOptions: {
// See zlib defaults.
chunkSize: 1024,
memLevel: 7,
level: 3
},
zlibInflateOptions: {
chunkSize: 10 * 1024
},
// Other options settable:
clientNoContextTakeover: true, // Defaults to negotiated value.
serverNoContextTakeover: true, // Defaults to negotiated value.
serverMaxWindowBits: 10, // Defaults to negotiated value.
// Below options specified as default values.
concurrencyLimit: 10, // Limits zlib concurrency for perf.
threshold: 1024 // Size (in bytes) below which messages
// should not be compressed.
}
});
const meshHosts = {};
const connections = {};
const hostExpireMilliseconds = 15 * 60 * 1000;
const registerHost = function (hostInfo) {
if (meshHosts.hasOwnProperty(hostInfo.id)) {
meshHosts[hostInfo.id].connectionId = hostInfo.connectionId;
meshHosts[hostInfo.id].remoteAddress = hostInfo.remoteAddress;
meshHosts[hostInfo.id].updatedAt = new Date();
} else {
meshHosts[hostInfo.id] = Object.assign(hostInfo, {
"createdAt": new Date(),
"updatedAt": new Date()
});
}
expireHosts();
console.log(`Registered Host:\n${JSON.stringify(meshHosts, null, 4)}`);
};
const expireHosts = function () {
Object.keys(meshHosts).forEach(id => {
if (new Date() - meshHosts[id].updatedAt >= hostExpireMilliseconds) {
console.log(`Expired Host:\n${JSON.stringify(meshHosts[id], null, 4)}`);
delete meshHosts[id];
}
});
};
const isSameNetwork = function (addressA, addressB) {
if (addressA === addressB) {
return true;
}
return true;
};
wss.on('connection', (socket, request) => {
const remoteAddress = request.connection.remoteAddress;
console.log(`connected: remote addrees=<${remoteAddress}>`);
socket.on('message', message => {
console.log(`received message: ${message}`);
const { action, data } = JSON.parse(message);
let hostInfo;
let clientInfo;
let hostIds;
switch (action) {
case 'register':
hostInfo = {
connectionId: data.id,
id: data.id,
remoteAddress: remoteAddress
};
registerHost(hostInfo);
connections[data.id] = socket;
socket.send(JSON.stringify({
service: 'mesh',
action: 'register',
result: true,
data: {}
}));
break;
case 'list':
expireHosts();
hostIds = [];
Object.keys(meshHosts).forEach(id => {
const hostInfo = meshHosts[id];
if (isSameNetwork(remoteAddress, hostInfo.remoteAddress)) {
hostIds.push({
id: hostInfo.id,
updatedAt: hostInfo.updatedAt
});
}
});
hostIds = hostIds.sort((a, b) => {
return b.updatedAt - a.updatedAt;
});
socket.send(JSON.stringify({
service: 'mesh',
action: 'list',
result: true,
data: {
hostIds: hostIds
}
}));
break;
case 'offer':
connections[data.id] = socket;
hostInfo = meshHosts[data.hostId];
if (isSameNetwork(remoteAddress, hostInfo.remoteAddress)) {
if (connections[hostInfo.id]) {
connections[hostInfo.id].send(JSON.stringify({
service: 'mesh',
action: 'offer',
data: {
id: data.id,
hostId: hostInfo.id,
clientDescription: data.clientDescription
}
}));
} else {
socket.send(JSON.stringify({
service: 'mesh',
action: 'offer',
result: false,
data: {
error: `Host is not connected: ${hostInfo.id}`
}
}));
}
} else {
socket.send(JSON.stringify({
service: 'mesh',
action: 'offer',
result: false,
data: {
error: `Host is not same network`
}
}));
}
break;
case 'answer':
hostInfo = meshHosts[data.id];
registerHost(hostInfo);
connections[data.clientId].send(JSON.stringify({
service: 'mesh',
action: 'answer',
data: {
id: data.id,
clientId: data.clientId,
hostDescription: data.hostDescription
}
}));
break;
default:
socket.send(JSON.stringify({
service: 'mesh',
action: data.action,
result: false,
data: {
error: `invalid action: ${action}`
}
}));
break;
}
});
socket.on('close', () => {
console.log(`socket closed`);
Object.keys(connections).forEach(id => {
if (connections[id] === socket) {
delete connections[id];
}
});
});
});
wss.on('error', error => {
console.log(`server on error: ${error}`);
});
console.info(`WebRTC signaling server for Smalruby3's Mesh extension is running at ${wss.address().address}:${wss.address().port}`);