From 0addc28f11e1a31b5c27afbcfd5cd23ce07dede5 Mon Sep 17 00:00:00 2001 From: Noah Zalev Date: Thu, 5 Aug 2021 23:51:19 -0400 Subject: [PATCH] Added server command handler functions --- meshuser.js | 124 ++++++++++++++++++++++++++-------------------------- 1 file changed, 63 insertions(+), 61 deletions(-) diff --git a/meshuser.js b/meshuser.js index f38e8b16..4fb2550a 100644 --- a/meshuser.js +++ b/meshuser.js @@ -591,8 +591,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use // pass through to switch statement until refactoring complete switch (command.action) { - case 'pong': { break; } // NOP - case 'ping': { try { ws.send('{action:"pong"}'); } catch (ex) { } break; } case 'intersession': { // Sends data between sessions of the same user @@ -748,42 +746,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use }); break; } - case 'powertimeline': - { - // Get the node and the rights for this node - parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) { - if (visible == false) return; - // Query the database for the power timeline for a given node - // The result is a compacted array: [ startPowerState, startTimeUTC, powerState ] + many[ deltaTime, powerState ] - db.getPowerTimeline(node._id, function (err, docs) { - if ((err == null) && (docs != null) && (docs.length > 0)) { - var timeline = [], time = null, previousPower; - for (i in docs) { - var doc = docs[i], j = parseInt(i); - doc.time = Date.parse(doc.time); - if (time == null) { // First element - // Skip all starting power 0 events. - if ((doc.power == 0) && ((doc.oldPower == null) || (doc.oldPower == 0))) continue; - time = doc.time; - if (doc.oldPower) { timeline.push(doc.oldPower, time / 1000, doc.power); } else { timeline.push(0, time / 1000, doc.power); } - } else if (previousPower != doc.power) { // Delta element - // If this event is of a short duration (2 minutes or less), skip it. - if ((docs.length > (j + 1)) && ((Date.parse(docs[j + 1].time) - doc.time) < 120000)) continue; - timeline.push((doc.time - time) / 1000, doc.power); - time = doc.time; - } - previousPower = doc.power; - } - try { ws.send(JSON.stringify({ action: 'powertimeline', nodeid: node._id, timeline: timeline, tag: command.tag })); } catch (ex) { } - } else { - // No records found, send current state if we have it - var state = parent.parent.GetConnectivityState(command.nodeid); - if (state != null) { try { ws.send(JSON.stringify({ action: 'powertimeline', nodeid: node._id, timeline: [state.powerState, Date.now(), state.powerState], tag: command.tag })); } catch (ex) { } } - } - }); - }); - break; - } case 'fileoperation': { // Check permissions @@ -2353,25 +2315,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use }); break; } - case 'serverupdate': - { - // Do not allow this command when logged in using a login token - if (req.session.loginToken != null) break; - - // Perform server update - if ((user.siteadmin & 16) == 0) break; - if ((domain.myserver === false) || ((domain.myserver != null) && (domain.myserver !== true) && (domain.myserver.upgrade !== true))) break; - if ((command.version != null) && (typeof command.version != 'string')) break; - parent.parent.performServerUpdate(command.version); - break; - } - case 'serverclearerrorlog': - { - // Clear the server error log - if ((user.siteadmin & 16) == 0) break; - fs.unlink(parent.parent.getConfigFilePath('mesherrors.txt'), function (err) { }); - break; - } case 'createmesh': { var err = null; @@ -5106,10 +5049,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use for (var i in command.nodeids) { routeCommandToNode({ action: 'msg', type: 'console', nodeid: command.nodeids[i], value: 'agentupdate' }, MESHRIGHT_ADMIN, 0); } break; } - case 'print': { - console.log(command.value); - break; - } case 'previousLogins': { // TODO: Make a better database call to get filtered data. if (command.userid == null) { @@ -5470,9 +5409,15 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use 'lastconnect': serverCommandLastConnect, 'lastconnects': serverCommandLastConnects, 'meshes': serverCommandMeshes, + 'ping': serverCommandPing, + 'pong': serverCommandPong, + 'powertimeline': serverCommandPowerTimeline, + 'print': serverCommandPrint, + 'serverclearerrorlog': serverCommandServerClearErrorLog, 'serverconsole': serverCommandServerConsole, 'servererrors': serverCommandServerErrors, 'serverstats': serverCommandServerStats, + 'serverupdate': serverCommandServerUpdate, 'serverversion': serverCommandServerVersion, 'users': serverCommandUsers }; @@ -5629,6 +5574,52 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use try { ws.send(JSON.stringify({ action: 'meshes', meshes: parent.GetAllMeshWithRights(user).map(parent.CloneSafeMesh), tag: command.tag })); } catch (ex) { } } + function serverCommandPing(command) { try { ws.send('{action:"pong"}'); } catch (ex) { } } + function serverCommandPong(command) { } // NOP + + function serverCommandPowerTimeline(command) { + // Get the node and the rights for this node + parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) { + if (visible == false) return; + // Query the database for the power timeline for a given node + // The result is a compacted array: [ startPowerState, startTimeUTC, powerState ] + many[ deltaTime, powerState ] + db.getPowerTimeline(node._id, function (err, docs) { + if ((err == null) && (docs != null) && (docs.length > 0)) { + var timeline = [], time = null, previousPower; + for (i in docs) { + var doc = docs[i], j = parseInt(i); + doc.time = Date.parse(doc.time); + if (time == null) { // First element + // Skip all starting power 0 events. + if ((doc.power == 0) && ((doc.oldPower == null) || (doc.oldPower == 0))) continue; + time = doc.time; + if (doc.oldPower) { timeline.push(doc.oldPower, time / 1000, doc.power); } else { timeline.push(0, time / 1000, doc.power); } + } else if (previousPower != doc.power) { // Delta element + // If this event is of a short duration (2 minutes or less), skip it. + if ((docs.length > (j + 1)) && ((Date.parse(docs[j + 1].time) - doc.time) < 120000)) continue; + timeline.push((doc.time - time) / 1000, doc.power); + time = doc.time; + } + previousPower = doc.power; + } + try { ws.send(JSON.stringify({ action: 'powertimeline', nodeid: node._id, timeline: timeline, tag: command.tag })); } catch (ex) { } + } else { + // No records found, send current state if we have it + var state = parent.parent.GetConnectivityState(command.nodeid); + if (state != null) { try { ws.send(JSON.stringify({ action: 'powertimeline', nodeid: node._id, timeline: [state.powerState, Date.now(), state.powerState], tag: command.tag })); } catch (ex) { } } + } + }); + }); + } + + function serverCommandPrint(command) { console.log(command.value); } + + function serverCommandServerClearErrorLog(command) { + // Clear the server error log + if ((user.siteadmin & 16) == 0) return; + fs.unlink(parent.parent.getConfigFilePath('mesherrors.txt'), function (err) { }); + } + function serverCommandServerConsole(command) { // Do not allow this command when logged in using a login token if (req.session.loginToken != null) return; @@ -5674,6 +5665,17 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use } } + function serverCommandServerUpdate(command) { + // Do not allow this command when logged in using a login token + if (req.session.loginToken != null) return; + + // Perform server update + if ((user.siteadmin & 16) == 0) return; + if ((domain.myserver === false) || ((domain.myserver != null) && (domain.myserver !== true) && (domain.myserver.upgrade !== true))) return; + if ((command.version != null) && (typeof command.version != 'string')) return; + parent.parent.performServerUpdate(command.version); + } + function serverCommandServerVersion(command) { // Do not allow this command when logged in using a login token if (req.session.loginToken != null) return;