From 8bbed2c831e4b663454e0c7735f3f1183649fa21 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 18 May 2021 11:32:17 +0100 Subject: [PATCH] Change node id generation to give fixed length values without '.' --- .../node_modules/@node-red/editor-client/src/js/nodes.js | 7 +++++-- .../node_modules/@node-red/nodes/core/network/10-mqtt.js | 2 +- .../@node-red/nodes/core/network/22-websocket.js | 6 +++--- .../node_modules/@node-red/nodes/core/network/31-tcpin.js | 4 ++-- packages/node_modules/@node-red/util/lib/util.js | 6 +++++- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/node_modules/@node-red/editor-client/src/js/nodes.js b/packages/node_modules/@node-red/editor-client/src/js/nodes.js index e134487a1..67a924775 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/nodes.js +++ b/packages/node_modules/@node-red/editor-client/src/js/nodes.js @@ -210,8 +210,11 @@ RED.nodes = (function() { })(); function getID() { - // return Math.floor(Math.random()*15728640 + 1048576).toString(16) - return (1+Math.random()*4294967295).toString(16); + var bytes = []; + for (var i=0;i<8;i++) { + bytes.push(Math.round(0xff*Math.random()).toString(16).padStart(2,'0')); + } + return bytes.join(""); } function parseNodePropertyTypeString(typeString) { diff --git a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js index fe4ce1b3b..5c1c75881 100644 --- a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js +++ b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js @@ -314,7 +314,7 @@ module.exports = function(RED) { } // Build options for passing to the MQTT.js API - this.options.clientId = this.clientid || 'mqtt_' + (1+Math.random()*4294967295).toString(16); + this.options.clientId = this.clientid || 'mqtt_' + RED.util.generateId(); this.options.username = this.username; this.options.password = this.password; this.options.keepalive = this.keepalive; diff --git a/packages/node_modules/@node-red/nodes/core/network/22-websocket.js b/packages/node_modules/@node-red/nodes/core/network/22-websocket.js index 315f3caaa..e47439f5f 100644 --- a/packages/node_modules/@node-red/nodes/core/network/22-websocket.js +++ b/packages/node_modules/@node-red/nodes/core/network/22-websocket.js @@ -62,14 +62,14 @@ module.exports = function(RED) { if (process.env.HTTP_PROXY) { prox = process.env.HTTP_PROXY; } if (process.env.no_proxy) { noprox = process.env.no_proxy.split(","); } if (process.env.NO_PROXY) { noprox = process.env.NO_PROXY.split(","); } - + var noproxy = false; if (noprox) { for (var i in noprox) { if (node.path.indexOf(noprox[i].trim()) !== -1) { noproxy=true; } } } - + var agent = undefined; if (prox && !noproxy) { agent = new HttpsProxyAgent(prox); @@ -92,7 +92,7 @@ module.exports = function(RED) { } function handleConnection(/*socket*/socket) { - var id = (1+Math.random()*4294967295).toString(16); + var id = RED.util.generateId(); if (node.isServer) { node._clients[id] = socket; node.emit('opened',{count:Object.keys(node._clients).length,id:id}); diff --git a/packages/node_modules/@node-red/nodes/core/network/31-tcpin.js b/packages/node_modules/@node-red/nodes/core/network/31-tcpin.js index e463f09f0..11d533619 100644 --- a/packages/node_modules/@node-red/nodes/core/network/31-tcpin.js +++ b/packages/node_modules/@node-red/nodes/core/network/31-tcpin.js @@ -69,7 +69,7 @@ module.exports = function(RED) { var setupTcpClient = function() { node.log(RED._("tcpin.status.connecting",{host:node.host,port:node.port})); node.status({fill:"grey",shape:"dot",text:"common.status.connecting"}); - var id = (1+Math.random()*4294967295).toString(16); + var id = RED.util.generateId(); client = net.connect(node.port, node.host, function() { buffer = (node.datatype == 'buffer') ? Buffer.alloc(0) : ""; node.connected = true; @@ -153,7 +153,7 @@ module.exports = function(RED) { var server = net.createServer(function (socket) { socket.setKeepAlive(true,120000); if (socketTimeout !== null) { socket.setTimeout(socketTimeout); } - var id = (1+Math.random()*4294967295).toString(16); + var id = RED.util.generateId(); var fromi; var fromp; connectionPool[id] = socket; diff --git a/packages/node_modules/@node-red/util/lib/util.js b/packages/node_modules/@node-red/util/lib/util.js index fc9e69acb..5d164af76 100644 --- a/packages/node_modules/@node-red/util/lib/util.js +++ b/packages/node_modules/@node-red/util/lib/util.js @@ -31,7 +31,11 @@ const util = require("util"); * @memberof @node-red/util_util */ function generateId() { - return (1+Math.random()*4294967295).toString(16); + var bytes = []; + for (var i=0;i<8;i++) { + bytes.push(Math.round(0xff*Math.random()).toString(16).padStart(2,'0')); + } + return bytes.join(""); } /**