From c4b11d3384e007875c1a845d59271f800e6e86f5 Mon Sep 17 00:00:00 2001 From: Noah Zalev Date: Tue, 13 Jul 2021 17:48:08 -0400 Subject: [PATCH 1/3] Create handler function for getnetworkinfo --- meshuser.js | 59 ++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/meshuser.js b/meshuser.js index 921ce0cb..bf5e8b5e 100644 --- a/meshuser.js +++ b/meshuser.js @@ -3606,35 +3606,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'toast', responseid: command.responseid, result: 'ok' })); } catch (ex) { } } break; } - case 'getnetworkinfo': - { - // Argument validation - if (common.validateString(command.nodeid, 1, 1024) == false) break; // Check nodeid - if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; } - if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) return; // Invalid domain, operation only valid for current domain - - // Get the node and the rights for this node - parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) { - if (visible == false) { try { ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'Invalid device id' })); } catch (ex) { } return; } - - // Get network information about this node - db.Get('if' + node._id, function (err, netinfos) { - if ((netinfos == null) || (netinfos.length != 1)) { try { ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: node._id, netif: null, netif2: null })); } catch (ex) { } return; } - var netinfo = netinfos[0]; - - // Unescape any field names that have special characters if needed - if (netinfo.netif2 != null) { - for (var i in netinfo.netif2) { - var esc = common.unEscapeFieldName(i); - if (esc !== i) { netinfo.netif2[esc] = netinfo.netif2[i]; delete netinfo.netif2[i]; } - } - } - - try { ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: node._id, updateTime: netinfo.updateTime, netif: netinfo.netif, netif2: netinfo.netif2 })); } catch (ex) { } - }); - }); - break; - } case 'changedevice': { var err = null; @@ -5503,6 +5474,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use } const serverCommands = { + 'getnetworkinfo': serverCommandGetNetworkInfo, 'lastconnect': serverCommandLastConnect, 'serverconsole': serverCommandServerConsole }; @@ -5559,7 +5531,34 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use 'webstats': [serverUserCommandWebStats, ""] }; - + function serverCommandGetNetworkInfo(command) { + // Argument validation + if (common.validateString(command.nodeid, 1, 1024) == false) return; // Check nodeid + if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; } + if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) return; // Invalid domain, operation only valid for current domain + + // Get the node and the rights for this node + parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) { + if (visible == false) { try { ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'Invalid device id' })); } catch (ex) { } return; } + + // Get network information about this node + db.Get('if' + node._id, function (err, netinfos) { + if ((netinfos == null) || (netinfos.length != 1)) { try { ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: node._id, netif: null, netif2: null })); } catch (ex) { } return; } + var netinfo = netinfos[0]; + + // Unescape any field names that have special characters if needed + if (netinfo.netif2 != null) { + for (var i in netinfo.netif2) { + var esc = common.unEscapeFieldName(i); + if (esc !== i) { netinfo.netif2[esc] = netinfo.netif2[i]; delete netinfo.netif2[i]; } + } + } + + try { ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: node._id, updateTime: netinfo.updateTime, netif: netinfo.netif, netif2: netinfo.netif2 })); } catch (ex) { } + }); + }); + } + function serverCommandLastConnect(command) { if (common.validateString(command.nodeid, 1, 1024) == false) return; // Check the nodeid if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; } From 34aab9c10a2396f6c2344cc54683356739bc0616 Mon Sep 17 00:00:00 2001 From: Noah Zalev Date: Tue, 13 Jul 2021 17:51:54 -0400 Subject: [PATCH 2/3] Factor out node id and domain validation --- meshuser.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/meshuser.js b/meshuser.js index bf5e8b5e..0b36ea9a 100644 --- a/meshuser.js +++ b/meshuser.js @@ -5532,10 +5532,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use }; function serverCommandGetNetworkInfo(command) { - // Argument validation - if (common.validateString(command.nodeid, 1, 1024) == false) return; // Check nodeid - if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; } - if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) return; // Invalid domain, operation only valid for current domain + if (!validNodeIdAndDomain(command)) return; // Get the node and the rights for this node parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) { @@ -5560,9 +5557,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use } function serverCommandLastConnect(command) { - if (common.validateString(command.nodeid, 1, 1024) == false) return; // Check the nodeid - if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; } - if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) return; // Invalid domain, operation only valid for current domain + if (!validNodeIdAndDomain(command)) return; // Get the node and the rights for this node parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) { @@ -6154,6 +6149,12 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use } + function validNodeIdAndDomain(command) { + if (common.validateString(command.nodeid, 1, 1024) == false) return false; // Check nodeid + if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; } + if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) return false; // Invalid domain, operation only valid for current domain + return true; + } function csvClean(s) { return '\"' + s.split('\"').join('').split(',').join('').split('\r').join('').split('\n').join('') + '\"'; } From f0d2581b2df68bc5daa44f26e33edbffdea8ed75 Mon Sep 17 00:00:00 2001 From: Noah Zalev Date: Tue, 13 Jul 2021 18:13:52 -0400 Subject: [PATCH 3/3] Added handler function for getsysinfo --- meshuser.js | 54 ++++++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/meshuser.js b/meshuser.js index 0b36ea9a..a0ed79f4 100644 --- a/meshuser.js +++ b/meshuser.js @@ -788,34 +788,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use }); break; } - case 'getsysinfo': - { - if (common.validateString(command.nodeid, 1, 1024) == false) break; // Check the nodeid - if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; } - if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) return; // Invalid domain, operation only valid for current domain - - // Get the node and the rights for this node - parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) { - if (visible == false) { try { ws.send(JSON.stringify({ action: 'getsysinfo', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'Invalid device id' })); } catch (ex) { } return; } - // Query the database system information - db.Get('si' + command.nodeid, function (err, docs) { - if ((docs != null) && (docs.length > 0)) { - var doc = docs[0]; - doc.action = 'getsysinfo'; - doc.nodeid = node._id; - doc.tag = command.tag; - delete doc.type; - delete doc.domain; - delete doc._id; - if (command.nodeinfo === true) { doc.node = node; doc.rights = rights; } - try { ws.send(JSON.stringify(doc)); } catch (ex) { } - } else { - try { ws.send(JSON.stringify({ action: 'getsysinfo', nodeid: node._id, tag: command.tag, noinfo: true, result: 'Invalid device id' })); } catch (ex) { } - } - }); - }); - break; - } case 'files': { // Send the full list of server files to the browser app @@ -5475,6 +5447,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use const serverCommands = { 'getnetworkinfo': serverCommandGetNetworkInfo, + 'getsysinfo': serverCommandGetSysInfo, 'lastconnect': serverCommandLastConnect, 'serverconsole': serverCommandServerConsole }; @@ -5556,6 +5529,31 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use }); } + function serverCommandGetSysInfo(command) { + if (!validNodeIdAndDomain(command)) return; + + // Get the node and the rights for this node + parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) { + if (visible == false) { try { ws.send(JSON.stringify({ action: 'getsysinfo', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'Invalid device id' })); } catch (ex) { } return; } + // Query the database system information + db.Get('si' + command.nodeid, function (err, docs) { + if ((docs != null) && (docs.length > 0)) { + var doc = docs[0]; + doc.action = 'getsysinfo'; + doc.nodeid = node._id; + doc.tag = command.tag; + delete doc.type; + delete doc.domain; + delete doc._id; + if (command.nodeinfo === true) { doc.node = node; doc.rights = rights; } + try { ws.send(JSON.stringify(doc)); } catch (ex) { } + } else { + try { ws.send(JSON.stringify({ action: 'getsysinfo', nodeid: node._id, tag: command.tag, noinfo: true, result: 'Invalid device id' })); } catch (ex) { } + } + }); + }); + } + function serverCommandLastConnect(command) { if (!validNodeIdAndDomain(command)) return;