Refactor meshuser: initial command handlers

Noah Zalev committed Jul 10, 2021 at 19:50 UTC ff4c67da7c1376523bea495da975b4ad7aaa8ee8
1 file changed +63 -45
meshuser.js
+63 -45
@@ -551,7 +551,21 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
551 try { command = JSON.parse(msg.toString('utf8')); } catch (e) { return; }
552 if (common.validateString(command.action, 3, 32) == false) return; // Action must be a string between 3 and 32 chars
553
554 + var commandHandler = serverCommands[command.action];
555 + if (commandHandler != null) {
556 + try { commandHandler(command); return;
557 + } catch (e) {
558 + console.log('Unhandled error while processing ' + command.action + ' for user ' + + user.name + ':\n' + e);
559 + }
560 + } else {
561 + // console.log('Unknown action from user ' + user.name + ': ' + command.action + '.');
562 + // pass through to switch statement
563 + }
564 +
565 switch (command.action) {
566 + // Avoid logging 'Unknown action...' for refactored commands
567 + case 'lastconnect':
568 + case 'serverconsole':
569 case 'pong': { break; } // NOP
570 case 'ping': { try { ws.send('{action:"pong"}'); } catch (ex) { } break; }
571 case 'intersession':
@@ -805,27 +819,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
819 });
820 break;
821 }
808 - case 'lastconnect':
809 - {
810 - if (common.validateString(command.nodeid, 1, 1024) == false) break; // Check the nodeid
811 - if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; }
812 - if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) return; // Invalid domain, operation only valid for current domain
813 -
814 - // Get the node and the rights for this node
815 - parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
816 - if (visible == false) { try { ws.send(JSON.stringify({ action: 'lastconnect', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'Invalid device id' })); } catch (ex) { } return; }
817 -
818 - // Query the database for the last time this node connected
819 - db.Get('lc' + command.nodeid, function (err, docs) {
820 - if ((docs != null) && (docs.length > 0)) {
821 - try { ws.send(JSON.stringify({ action: 'lastconnect', nodeid: command.nodeid, time: docs[0].time, addr: docs[0].addr })); } catch (ex) { }
822 - } else {
823 - try { ws.send(JSON.stringify({ action: 'lastconnect', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'No data' })); } catch (ex) { }
824 - }
825 - });
826 - });
827 - break;
828 - }
822 case 'files':
823 {
824 // Send the full list of server files to the browser app
@@ -895,30 +888,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
888 }
889 break;
890 }
898 - case 'serverconsole':
899 - {
900 - // Do not allow this command when logged in using a login token
901 - if (req.session.loginToken != null) break;
902 - // This is a server console message, only process this if full administrator
903 - if (user.siteadmin != SITERIGHT_ADMIN) break;
904 - // Only accept if the console is allowed for this domain
905 - if ((domain.myserver === false) || ((domain.myserver != null) && (domain.myserver !== true) && (domain.myserver.console !== true))) break;
906 -
907 - var cmdargs = splitArgs(command.value);
908 - if (cmdargs.length == 0) break;
909 - const cmd = cmdargs[0].toLowerCase();
910 - cmdargs = parseArgs(cmdargs);
911 - var cmdData = { result: '', command: command, cmdargs: cmdargs };
912 -
913 - // Find the command in the lookup table and run it.
914 - var cmdTableEntry = serverUserCommands[cmd];
915 - if (cmdTableEntry != null) { try { cmdTableEntry[0](cmdData); } catch (ex) { cmdData.result = '' + ex; }
916 - } else { cmdData.result = 'Unknown command \"' + cmd + '\", type \"help\" for list of available commands.'; }
917 -
918 - // Send back the command result
919 - if (cmdData.result != '') { try { ws.send(JSON.stringify({ action: 'serverconsole', value: cmdData.result, tag: command.tag })); } catch (ex) { } }
920 - break;
921 - }
891 case 'msg':
892 {
893 // Check the nodeid
@@ -5536,6 +5505,11 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5505 }
5506 }
5507
5508 + const serverCommands = {
5509 + 'lastconnect': serverCommandLastConnect,
5510 + 'serverconsole': serverCommandServerConsole
5511 + };
5512 +
5513 const serverUserCommands = {
5514 'acceleratorsstats': [serverUserCommandAcceleratorsStats, "Show data on work being offloaded to other CPU's"],
5515 'agentissues': [serverUserCommandAgentIssues, ""],
@@ -5586,6 +5560,50 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5560 'watchdog': [serverUserCommandWatchdog, ""],
5561 'webpush': [serverUserCommandWebPush, ""],
5562 'webstats': [serverUserCommandWebStats, ""]
5563 + };
5564 +
5565 +
5566 + function serverCommandLastConnect(command) {
5567 + if (common.validateString(command.nodeid, 1, 1024) == false) return; // Check the nodeid
5568 + if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; }
5569 + if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) return; // Invalid domain, operation only valid for current domain
5570 +
5571 + // Get the node and the rights for this node
5572 + parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
5573 + if (visible == false) { try { ws.send(JSON.stringify({ action: 'lastconnect', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'Invalid device id' })); } catch (ex) { } return; }
5574 +
5575 + // Query the database for the last time this node connected
5576 + db.Get('lc' + command.nodeid, function (err, docs) {
5577 + if ((docs != null) && (docs.length > 0)) {
5578 + try { ws.send(JSON.stringify({ action: 'lastconnect', nodeid: command.nodeid, time: docs[0].time, addr: docs[0].addr })); } catch (ex) { }
5579 + } else {
5580 + try { ws.send(JSON.stringify({ action: 'lastconnect', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'No data' })); } catch (ex) { }
5581 + }
5582 + });
5583 + });
5584 + }
5585 +
5586 + function serverCommandServerConsole(command) {
5587 + // Do not allow this command when logged in using a login token
5588 + if (req.session.loginToken != null) return;
5589 + // This is a server console message, only process this if full administrator
5590 + if (user.siteadmin != SITERIGHT_ADMIN) return;
5591 + // Only accept if the console is allowed for this domain
5592 + if ((domain.myserver === false) || ((domain.myserver != null) && (domain.myserver !== true) && (domain.myserver.console !== true))) return;
5593 +
5594 + var cmdargs = splitArgs(command.value);
5595 + if (cmdargs.length == 0) return;
5596 + const cmd = cmdargs[0].toLowerCase();
5597 + cmdargs = parseArgs(cmdargs);
5598 + var cmdData = { result: '', command: command, cmdargs: cmdargs };
5599 +
5600 + // Find the command in the lookup table and run it.
5601 + var cmdTableEntry = serverUserCommands[cmd];
5602 + if (cmdTableEntry != null) { try { cmdTableEntry[0](cmdData); } catch (ex) { cmdData.result = '' + ex; }
5603 + } else { cmdData.result = 'Unknown command \"' + cmd + '\", type \"help\" for list of available commands.'; }
5604 +
5605 + // Send back the command result
5606 + if (cmdData.result != '') { try { ws.send(JSON.stringify({ action: 'serverconsole', value: cmdData.result, tag: command.tag })); } catch (ex) { } }
5607 }
5608
5609