Create handler function for getnetworkinfo

Noah Zalev committed Jul 13, 2021 at 17:48 UTC c4b11d3384e007875c1a845d59271f800e6e86f5
1 file changed +29 -30
meshuser.js
+29 -30
@@ -3606,35 +3606,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
3606 if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'toast', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
3607 break;
3608 }
3609 - case 'getnetworkinfo':
3610 - {
3611 - // Argument validation
3612 - if (common.validateString(command.nodeid, 1, 1024) == false) break; // Check nodeid
3613 - if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; }
3614 - if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) return; // Invalid domain, operation only valid for current domain
3615 -
3616 - // Get the node and the rights for this node
3617 - parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
3618 - 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; }
3619 -
3620 - // Get network information about this node
3621 - db.Get('if' + node._id, function (err, netinfos) {
3622 - if ((netinfos == null) || (netinfos.length != 1)) { try { ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: node._id, netif: null, netif2: null })); } catch (ex) { } return; }
3623 - var netinfo = netinfos[0];
3624 -
3625 - // Unescape any field names that have special characters if needed
3626 - if (netinfo.netif2 != null) {
3627 - for (var i in netinfo.netif2) {
3628 - var esc = common.unEscapeFieldName(i);
3629 - if (esc !== i) { netinfo.netif2[esc] = netinfo.netif2[i]; delete netinfo.netif2[i]; }
3630 - }
3631 - }
3632 -
3633 - try { ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: node._id, updateTime: netinfo.updateTime, netif: netinfo.netif, netif2: netinfo.netif2 })); } catch (ex) { }
3634 - });
3635 - });
3636 - break;
3637 - }
3609 case 'changedevice':
3610 {
3611 var err = null;
@@ -5503,6 +5474,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5474 }
5475
5476 const serverCommands = {
5477 + 'getnetworkinfo': serverCommandGetNetworkInfo,
5478 'lastconnect': serverCommandLastConnect,
5479 'serverconsole': serverCommandServerConsole
5480 };
@@ -5559,7 +5531,34 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5531 'webstats': [serverUserCommandWebStats, ""]
5532 };
5533
5562 -
5534 + function serverCommandGetNetworkInfo(command) {
5535 + // Argument validation
5536 + if (common.validateString(command.nodeid, 1, 1024) == false) return; // Check nodeid
5537 + if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; }
5538 + if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) return; // Invalid domain, operation only valid for current domain
5539 +
5540 + // Get the node and the rights for this node
5541 + parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
5542 + 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; }
5543 +
5544 + // Get network information about this node
5545 + db.Get('if' + node._id, function (err, netinfos) {
5546 + if ((netinfos == null) || (netinfos.length != 1)) { try { ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: node._id, netif: null, netif2: null })); } catch (ex) { } return; }
5547 + var netinfo = netinfos[0];
5548 +
5549 + // Unescape any field names that have special characters if needed
5550 + if (netinfo.netif2 != null) {
5551 + for (var i in netinfo.netif2) {
5552 + var esc = common.unEscapeFieldName(i);
5553 + if (esc !== i) { netinfo.netif2[esc] = netinfo.netif2[i]; delete netinfo.netif2[i]; }
5554 + }
5555 + }
5556 +
5557 + try { ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: node._id, updateTime: netinfo.updateTime, netif: netinfo.netif, netif2: netinfo.netif2 })); } catch (ex) { }
5558 + });
5559 + });
5560 + }
5561 +
5562 function serverCommandLastConnect(command) {
5563 if (common.validateString(command.nodeid, 1, 1024) == false) return; // Check the nodeid
5564 if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; }