/
n2n.js
463 lines (429 loc) · 21.5 KB
/
n2n.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
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
module.exports = function(RED) {
"use strict";
var passPhrase = "";
//passPhrase = "bananaisnotaverystrongpassword";
var algorithm = "aes-256-cbc-hmac-sha256";
var dgram = require('dgram');
var msgpack = require('msgpack-lite');
var crypto = require('crypto');
var os = require("os");
//console.log(crypto.getCiphers())
function sizes(cipher) {
for (let nkey = 1, niv = 0;;) {
try {
crypto.createCipheriv(cipher, '.'.repeat(nkey), '.'.repeat(niv));
return [nkey, niv];
} catch (e) {
if (/invalid iv length/i.test(e.message)) { niv += 1; }
else if (/invalid key length/i.test(e.message)) { nkey += 1; }
else { throw e; }
}
}
}
function computeKey(cipher, passphrase) {
let [nkey, niv] = sizes(cipher);
for (let key = '', iv = '', p = '';;) {
const h = crypto.createHash('md5');
h.update(p, 'hex');
h.update(passphrase);
p = h.digest('hex');
let n, i = 0;
n = Math.min(p.length-i, 2*nkey);
nkey -= n/2;
key += p.slice(i, i+n);
i += n;
n = Math.min(p.length-i, 2*niv);
niv -= n/2;
iv += p.slice(i, i+n);
i += n;
if (nkey+niv === 0) { return [key, iv]; }
}
}
var key;
if (passPhrase !== "") {
let [key, iv] = computeKey(algorithm, passPhrase);
key = key.slice(0, 32);
iv = iv.slice(0, 16);
}
var sock = null;
var links = {};
var requests = {};
var rates = {};
var wants = [];
var wantrates = [];
var lno = 0;
var port = 61880;
var addr = "225.0.18.80";
var ignore = false; // ignore our own messages
var host = os.hostname();
var tick = 20 * 1000; // say hello every... milliseconds
var agelimit = tick * 3 / 1000; // check for not seen after 3 x check interval
var viz = false; // gather data for visualisations
var initSock = function(node) {
node.log("You may need to unblock your firewall for this node to work...");
node.log("e.g. iptables -I INPUT 1 -p udp --dport 61880 -j ACCEPT");
//console.log(os.networkInterfaces());
if (node.iface) {
//console.log(os.networkInterfaces());
if (os.networkInterfaces().hasOwnProperty(node.iface)) {
var osif = os.networkInterfaces()[node.iface];
node.ifip = undefined;
for (var i in osif) {
if (osif[i].family === "IPv4") {
node.ifip = os.networkInterfaces()[node.iface][i].address;
}
}
if (node.ifip !== undefined) { node.log("Using " + node.iface + " : " + node.ifip); }
else { node.warn("Interface " + node.iface + " is not using IPV4"); }
} else {
node.warn("Interface " + node.iface + " not online/available.");
}
}
if (node.ifip === undefined) { node.ifip = null; }
if (sock === null) {
node.status({fill:"grey",shape:"dot",text:"Master"});
sock = dgram.createSocket('udp4'); // only use ipv4 for now
sock.setMaxListeners(0); // Allow loads of listeners just in case
if (node.bcast) { node.ifip = "255.255.255.255";}
sock.bind(port, node.ifip, function() { // have to bind before you can enable broadcast...
sock.setBroadcast(true); // turn on broadcast
if (!node.bcast) {
try {
sock.addMembership(addr, node.ifip); // Add to the multicast group
sock.setMulticastTTL(4); // set TTL to 4
//sock.setMulticastLoopback(true); // turn on loopback
} catch (e) {
if (e.errno == "EINVAL") {
node.error("Bad Multicast Address");
} else if (e.errno == "ENODEV") {
node.warn("No network device available");
} else {
node.error("Error :" + e.errno);
}
sock.close();
sock = null;
node.tsock = setTimeout(function() { initSock(node); }, tick);
}
}
});
sock.on("listening", function() {
node.log('RDY ' + addr + ":" + port);
if (sock) {
if (ignore === true) {
node.log("Loopback off"); // turn off loopback
sock.setMulticastLoopback(false);
} else {
node.log("Loopback on"); // turn on loopback
sock.setMulticastLoopback(true);
}
} else { node.log("socket not valid"); }
});
sock.on("error", function(err) {
if ((err.code == "EACCES") && (port < 1024)) {
node.error("UDP access error, you may need root access for ports below 1024");
} else if (err.code == "EADDRINUSE") {
node.error("UDP access error, Port " + port + " already in use");
} else {
node.error("UDP error : " + err.code);
}
sock.close();
sock = null;
node.tsock = setTimeout(function() { initSock(node); }, tick);
});
sock.on('message', function(message, remote) {
if (key) {
//decode
console.log("KK",key.length,iv.length,typeof message,message.length,message.toString('hex'));
var decipher = crypto.createDecipheriv(algorithm, key, iv);
message = Buffer.concat([decipher.update(message) , decipher.final()]);
console.log(message.length, message.toString('hex'));
}
//var data = JSON.parse(message.toString()); // for use without msgpack
try {
var data = msgpack.decode(message);
if (data.hasOwnProperty("w")) { // it's a hello message as it has "wants"
links[remote.address] = data;
links[remote.address].lastseen = Date.now();
if (!data.hasOwnProperty("l")) { links[remote.address].l = 60; }
if (!data.hasOwnProperty("r")) { links[remote.address].r = 0; }
requests[data.h] = data.w;
rates[data.h] = data.r;
lno = Object.keys(links).length;
if (RED.settings.verbose) { console.log("R-HLO " + JSON.stringify(data)); }
//if (RED.settings.verbose) { console.log("R-HLO " + data.h + " " + data.w + " " + data.r + " " + data.l); }
if (viz) { RED.settings.functionGlobalContext.discoOut = links; }
}
else {
if (RED.settings.verbose) { console.log("R-DAT",data); }
}
}
catch (e) {
if (RED.settings.verbose) { console.log("R-BAD", e); }
}
});
// Send Hello Message
// h = host = hostname of requesting device
// w = wants = topics wanted by the requesting device
// l = agelimit = how many seconds before you can call me dead...
// r = rate = no of seconds between messages you send me
var sendHello = function() {
if (wants.length > 0) { // Only ask for stuff is we need it. (can still send to others)
//var message = new Buffer(JSON.stringify({ h:host, w:wants, l:agelimit})); // for use without msgpack
var message = msgpack.encode({h:host, w:wants, r:wantrates, l:agelimit});
if (key) {
//encode (also add to msg send)
//console.log(message.length, message.toString('hex'));
var cipher = crypto.createCipheriv(algorithm, key, iv);
message = Buffer.concat([cipher.update(message),cipher.final()]);
//console.log(message.length, message.toString('hex'));
}
sock.send(message, 0, message.length, port, addr, function(err, bytes) {
if (err) { node.error("ANE " + err); }
else { if (RED.settings.verbose) { console.log("T-ANN " + JSON.stringify({h:host, w:wants, r:wantrates, l:agelimit})); } }
message = null;
});
}
}
var hoop = function(t) {
node.hout = setTimeout(function() {
if (sock) { sendHello(); }
hoop(tick); // so we can adjust this on the fly
}, t);
}
hoop(2000); // kick off announcements fairly quick
} else {
node.warn("Socket not open");
}
var cleanUp = function() {
// Remove links not seen within agelimit - limit is set by requesting end
for (var n in links) {
//console.log("S-TTL "+links[n].h+" "+parseInt(((links[n].lastseen + (links[n].l * 1000)) - Date.now())/1000));
if (Date.now() > (links[n].lastseen + (links[n].l * 1000))) {
if (RED.settings.verbose) { console.log("S-BYE " + links[n].h + " " + links[n].w); }
//node.status({text:"l:"+links[n].h});
delete requests[links[n].h];
delete rates[links[n].h]
delete links[n];
lno = Object.keys(links).length;
if (viz) { RED.settings.functionGlobalContext.discoOut = links; }
//node.status({fill:(lno === 0 ? "blue" : "yellow"), shape:"dot", text:lno+" link"+(lno === 1 ? "" : "s")});
}
}
}
var loop = function(t) {
node.tout = setTimeout(function() {
cleanUp();
loop(tick); // we can adjust this on the fly
}, t);
}
loop(tick); // kick off after "tick" secs
node.on("close", function(done) {
node.status({});
if (node.tout) { clearTimeout(node.tout); }
if (node.hout) { clearTimeout(node.hout); }
if (node.tsock) { clearTimeout(node.tsock); }
if (sock) {
try {
sock.close();
sock = null;
node.log("discovery stopped");
done();
} catch (err) {
sock = null;
node.error(err);
done();
}
}
wants = [];
wantrates = [];
});
}
function N2nInNode(n) {
RED.nodes.createNode(this, n);
this.iface = n.iface || null;
this.want = n.topic;
this.rate = Number(n.rate || 0);
this.bcast = n.bcast || false;
wants.push(this.want);
wantrates.push(this.rate);
if (n.ignore === false) { ignore = false; }
var node = this;
var got = {};
var linksin = {};
//var useKey = "bananaflimflam";
node.log("ASK [ " + node.want + " ]");
setTimeout(function() {
if (sock === null) { initSock(node); } // create socket if out node not already done so
sock.on('message', function(message, remote) {
if (key) {
//decode
var decipher = crypto.createDecipheriv(algorithm, key, iv);
message = Buffer.concat([decipher.update(message) , decipher.final()]);
//console.log(message.length, message.toString('hex'));
}
//var data = JSON.parse(message.toString()); // for use without msgpack
try {
var data = msgpack.decode(message);
//if (RED.settings.verbose) { console.log("R-DAT", data); }
if (data.hasOwnProperty("w")) { // it's a hello message as it has "wants" - so ignore it
}
else { // Anything else is a real message...
//console.log(data.t,w,node.wants[w]);
var re = new RegExp(node.want);
if (re.test(data.t)) { // And it's for me
if (!linksin.hasOwnProperty(remote.address)) {
linksin[remote.address] = {};
linksin[remote.address].h = data.h;
node.links = Object.keys(linksin).length;
got[data.h] = got[data.h] || {};
if (viz) {
RED.settings.functionGlobalContext.discoIn = linksin;
node.status({text:"+:"+data.h});
}
}
linksin[remote.address].lastseen = Date.now();
//if (RED.settings.verbose) { console.log("R-GOT " + data.h + " " + data.t + " " + JSON.stringify(data.p)); }
if (Date.now() >= (got[data.h][data.t] || 0) + (node.rate * 1000)) {
try { data.p = JSON.parse(data.p); }
catch(e) {}
node.send({payload:data.p, topic:data.t, host:data.h, hostip:remote.address}); // maybe ? hostip:remote.address
got[data.h][data.t] = Date.now();
node.status({fill:(node.links === 0 ? "blue" : "yellow"), shape:"dot", text:node.links + " source" + (node.links === 1 ? "" : "s")});
} else {
node.status({fill:(node.links === 0 ? "blue" : "yellow"), shape:"ring", text:node.links + " source" + (node.links === 1 ? "" : "s")});
if (RED.settings.verbose) { console.log("R-TOO SOON for RX", data.h, data.t, parseInt((got[data.h][data.t] + (node.rate * 1000) - Date.now()) / 1000) + "s"); }
}
}
//else {
// if (RED.settings.verbose) { console.log("R-SAW " + util.inspect(data, {depth:10})); }
//}
}
}
catch (e) {
if (RED.settings.verbose) { console.log("R-BAD", e); }
}
});
}, 500);
var cleanup = function(evt) {
// Remove incoming links not seen for a while (set by agelimit = 3*20s)
for (var m in linksin) {
if (linksin.hasOwnProperty(m)) {
//console.log("R-TTL "+linksin[m].h+" "+parseInt(linksin[m].lastseen + agelimit));
if (Date.now() > (linksin[m].lastseen + (agelimit * 1000))) {
if (RED.settings.verbose) { console.log("R-BYE " + m + " : " + linksin[m].h); }
delete got[linksin[m].h];
delete linksin[m];
if (viz) {
RED.settings.functionGlobalContext.discoIn = linksin;
node.status({text:"-:"+linksin[m].h});
}
node.links = Object.keys(linksin).length;
node.status({fill:(node.links === 0 ? "blue" : "yellow"), shape:"dot", text:node.links+" source"+(node.links === 1 ? "" : "s")});
}
}
}
};
var loopc = function(t) {
node.toutc = setTimeout(function() {
cleanup();
loopc(tick); // we can adjust this on the fly
}, t);
}
loopc(tick); // kick off after "tick" secs
node.on("close", function() {
if (node.toutc) { clearTimeout(node.toutc); }
node.status({});
});
}
RED.nodes.registerType("n2n in", N2nInNode);
function N2nOutNode(n) {
RED.nodes.createNode(this, n);
this.iface = n.iface || null;
this.bcast = n.bcast || false;
var node = this;
var sent = {};
//var useKey = "bananaflimflam";
// Delay start to allow any in nodes to start first (seems to work better that way)
//setTimeout(function() {
if (sock === null) { initSock(node); } // create socket if in node not already done so
//}, 500);
// Send Data messages to other links here
// h = host = hostname of requesting device
// t = topic = topics of message
// p = payload = content on message
node.on("input", function(msg) {
if (msg.hasOwnProperty("want")) {
wants.push(msg.want);
node.log("ASK2 [ " + msg.want + " ]");
}
lno = Object.keys(links).length;
if (msg.hasOwnProperty("payload") && (lno > 0) && sock) { // only try to send if we have a link to someone and socket exists
if (!msg.hasOwnProperty("topic")) { msg.topic = "public"; } // set a default topic if one doesn't exist
if (msg.topic.length === 0) { msg.topic = "public"; } // set a default topic if one doesn't exist
//if (RED.settings.verbose) { console.log("T-REQS",requests); }
//console.log("T-RATS",rates);
loopt:
for (var r in requests) {
if (requests.hasOwnProperty(r)) {
for (var x in requests[r]) {
if (requests[r].hasOwnProperty(x)) {
var re = new RegExp(requests[r][x]);
if (re.test(msg.topic)) {
//var message = new Buffer(JSON.stringify({ h:host, t:msg.topic, p:msg.payload })); // for use without msgpack
var message = msgpack.encode({ h:host, t:msg.topic, p:msg.payload });
if (key) {
//encoder
//console.log(message.length, message.toString('hex'));
var cipher = crypto.createCipheriv(algorithm, key, iv);
message = Buffer.concat([cipher.update(message),cipher.final()]);
console.log("T",message.length, message.toString('hex'));
}
var rate = 9999999;
for (var key in rates) {
if (rates.hasOwnProperty(key) && rates[key].hasOwnProperty(x)) {
if (rates[key][x] < rate) { rate = rates[key][x]; }
}
}
if (rate === 9999999) { rate = 0; }
//if (RED.settings.verbose) { console.log("RATE ",rate); }
if (Date.now() >= (sent[msg.topic] || 0) + rate*1000) {
sock.send(message, 0, message.length, port, addr, function(err, bytes) {
if (err) { node.error("MER " + err); }
//else { if (RED.settings.verbose) { console.log("T-MSG " + msg.topic + " : " + msg.payload.toString()); } }
message = null;
});
sent[msg.topic] = Date.now();
node.status({fill:"yellow", shape:"dot", text:lno + " : " + message.length + " bytes"});
} else {
node.status({fill:"yellow", shape:"ring", text:lno + " : " + message.length + " bytes"});
if (RED.settings.verbose) { console.log("T-TOO SOON for TX", msg.topic, parseInt((sent[msg.topic] + rate - Date.now()) / 1000) + "s"); }
}
break loopt;
}
else {
//node.status({});
}
}
}
}
}
}
});
node.on("close", function() {
node.status({});
});
}
RED.nodes.registerType("n2n out", N2nOutNode);
RED.httpAdmin.get("/mcastinterfaces", RED.auth.needsPermission('n2n.read'), function(req,res) {
var osif = os.networkInterfaces();
var ports = [];
for (var key in osif) {
if (osif.hasOwnProperty(key)) {
if (key.indexOf("lo") === -1) {
ports.push(key);
}
}
}
res.json(ports);
});
}