Socket
Socket
Sign inDemoInstall

node-red-contrib-n2n

Package Overview
Dependencies
5
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.2 to 1.0.0

163

n2n.js

@@ -1,24 +0,55 @@

/**
* Copyright 2015,2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
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-js');
//var util = require("util");
//var crypto = require('crypto');
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;

@@ -33,7 +64,7 @@ var links = {};

var addr = "225.0.18.80";
var ignore = true;
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 viz = false; // gather data for visualisations

@@ -65,2 +96,3 @@ var initSock = function(node) {

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...

@@ -86,3 +118,2 @@ sock.setBroadcast(true); // turn on broadcast

}
else { addr = "255.255.255.255"; }
});

@@ -117,12 +148,10 @@

sock.on('message', function(message, remote) {
//if (useKey) {
//decode
//buf = [];
//var decipher = crypto.createDecipher('aes256', useKey);
//buf.push(decipher.update(message));
//buf.push(decipher.final());
//message = new Buffer.concat(buf);
//console.log(message.length, message.toString('hex'));
//}
////var data = JSON.parse(message.toString()); // for use without msgpack
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 {

@@ -160,12 +189,9 @@ var data = msgpack.decode(message);

var message = msgpack.encode({h:host, w:wants, r:wantrates, l:agelimit});
//if (useKey) {
// encode (also add to msg send)
//console.log(message.length, message.toString('hex'));
//var buf = [];
//var cipher = crypto.createCipher('aes256', useKey);
//buf.push(cipher.update(message));
//buf.push(cipher.final());
//message = new Buffer.concat(buf);
//console.log(message.length, message.toString('hex'));
//}
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) {

@@ -185,3 +211,3 @@ if (err) { node.error("ANE " + err); }

}
hoop(2500); // kick off announcements fairly quick
hoop(2000); // kick off announcements fairly quick

@@ -236,3 +262,2 @@ } else {

wantrates = [];
addr = "225.0.18.80";
});

@@ -262,12 +287,9 @@ }

sock.on('message', function(message, remote) {
//if (useKey) {
//decode
//buf = [];
//var decipher = crypto.createDecipher('aes256', useKey);
//buf.push(decipher.update(message));
//buf.push(decipher.final());
//message = new Buffer.concat(buf);
//console.log(message.length, message.toString('hex'));
//}
////var data = JSON.parse(message.toString()); // for use without msgpack
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 {

@@ -277,8 +299,7 @@ var data = msgpack.decode(message);

if (data.hasOwnProperty("w")) { // it's a hello message as it has "wants" - so ignore it
} else { // Anything else is a real message...
}
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)) {

@@ -307,3 +328,2 @@ linksin[remote.address] = {};

}
}

@@ -367,5 +387,5 @@ //else {

// 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
}, 0);
//setTimeout(function() {
if (sock === null) { initSock(node); } // create socket if in node not already done so
//}, 500);

@@ -377,4 +397,8 @@ // Send Data messages to other links here

node.on("input", function(msg) {
if (msg.hasOwnProperty("want")) {
wants.push(msg.want);
node.log("ASK2 [ " + msg.want + " ]");
}
lno = Object.keys(links).length;
if (lno > 0 && sock) { // only try to send if we have a link to someone and socket exists
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

@@ -393,12 +417,9 @@ if (msg.topic.length === 0) { msg.topic = "public"; } // set a default topic if one doesn't exist

var message = msgpack.encode({ h:host, t:msg.topic, p:msg.payload });
//if (useKey) {
////encoder
//console.log(message.length, message.toString('hex'));
//var buf = [];
//var cipher = crypto.createCipher('aes256', useKey);
//buf.push(cipher.update(message));
//buf.push(cipher.final());
//message = new Buffer.concat(buf);
//console.log(message.length, message.toString('hex'));
//}
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;

@@ -405,0 +426,0 @@ for (var key in rates) {

{
"name" : "node-red-contrib-n2n",
"version" : "0.0.2",
"description" : "A Node-RED node that provides automatic links between different topics on Node-RED instances within multicast range.",
"dependencies" : {
"msgpack-js": "0.3.0"
"name": "node-red-contrib-n2n",
"version": "1.0.0",
"description": "A Node-RED node that provides automatic links between different topics on Node-RED instances within multicast range.",
"dependencies": {
"msgpack-lite": "^0.1.26"
},
"repository" : {
"type":"git",
"url":"https://github.com/node-red/node-red-nodes/tree/master/io/n2n"
"repository": {
"type": "git",
"url": "https://github.com/dceejay/node2node/tree/master"
},
"license": "Apache-2.0",
"keywords": [ "node-red", "discovery", "autodiscover", "link", "n2n", "node to node" ],
"node-red" : {
"nodes" : {
"keywords": [
"node-red",
"discovery",
"autodiscover",
"link",
"n2n",
"node to node"
],
"node-red": {
"version": ">=1.0.0",
"nodes": {
"autodiscover": "n2n.js"

@@ -21,5 +29,8 @@ }

"name": "Dave Conway-Jones",
"email": "ceejay@vnet.ibm.com",
"email": "dceejay@gmail.com",
"url": "http://nodered.org"
},
"engines": {
"node": ">=12"
}
}

@@ -7,2 +7,4 @@ node-red-contrib-n2n

**Only use this for short messages like data readings**.
Install

@@ -26,2 +28,4 @@ -------

**Only use this for short messages like data readings**.
Discover other Node-RED devices on the network and open channels from them.

@@ -28,0 +32,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc