Added server command handler functions
Noah Zalev committed
Aug 5, 2021 at 23:51 UTC
0addc28f11e1a31b5c27afbcfd5cd23ce07dede5
1 file changed
+63
-61
meshuser.js
+63
-61
@@ -591,8 +591,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
591
// pass through to switch statement until refactoring complete
592
593
switch (command.action) {
594
- case 'pong': { break; } // NOP
595
- case 'ping': { try { ws.send('{action:"pong"}'); } catch (ex) { } break; }
594
case 'intersession':
595
{
596
// Sends data between sessions of the same user
@@ -748,42 +746,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
746
});
747
break;
748
}
751
- case 'powertimeline':
752
- {
753
- // Get the node and the rights for this node
754
- parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
755
- if (visible == false) return;
756
- // Query the database for the power timeline for a given node
757
- // The result is a compacted array: [ startPowerState, startTimeUTC, powerState ] + many[ deltaTime, powerState ]
758
- db.getPowerTimeline(node._id, function (err, docs) {
759
- if ((err == null) && (docs != null) && (docs.length > 0)) {
760
- var timeline = [], time = null, previousPower;
761
- for (i in docs) {
762
- var doc = docs[i], j = parseInt(i);
763
- doc.time = Date.parse(doc.time);
764
- if (time == null) { // First element
765
- // Skip all starting power 0 events.
766
- if ((doc.power == 0) && ((doc.oldPower == null) || (doc.oldPower == 0))) continue;
767
- time = doc.time;
768
- if (doc.oldPower) { timeline.push(doc.oldPower, time / 1000, doc.power); } else { timeline.push(0, time / 1000, doc.power); }
769
- } else if (previousPower != doc.power) { // Delta element
770
- // If this event is of a short duration (2 minutes or less), skip it.
771
- if ((docs.length > (j + 1)) && ((Date.parse(docs[j + 1].time) - doc.time) < 120000)) continue;
772
- timeline.push((doc.time - time) / 1000, doc.power);
773
- time = doc.time;
774
- }
775
- previousPower = doc.power;
776
- }
777
- try { ws.send(JSON.stringify({ action: 'powertimeline', nodeid: node._id, timeline: timeline, tag: command.tag })); } catch (ex) { }
778
- } else {
779
- // No records found, send current state if we have it
780
- var state = parent.parent.GetConnectivityState(command.nodeid);
781
- 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) { } }
782
- }
783
- });
784
- });
785
- break;
786
- }
749
case 'fileoperation':
750
{
751
// Check permissions
@@ -2353,25 +2315,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2315
});
2316
break;
2317
}
2356
- case 'serverupdate':
2357
- {
2358
- // Do not allow this command when logged in using a login token
2359
- if (req.session.loginToken != null) break;
2360
-
2361
- // Perform server update
2362
- if ((user.siteadmin & 16) == 0) break;
2363
- if ((domain.myserver === false) || ((domain.myserver != null) && (domain.myserver !== true) && (domain.myserver.upgrade !== true))) break;
2364
- if ((command.version != null) && (typeof command.version != 'string')) break;
2365
- parent.parent.performServerUpdate(command.version);
2366
- break;
2367
- }
2368
- case 'serverclearerrorlog':
2369
- {
2370
- // Clear the server error log
2371
- if ((user.siteadmin & 16) == 0) break;
2372
- fs.unlink(parent.parent.getConfigFilePath('mesherrors.txt'), function (err) { });
2373
- break;
2374
- }
2318
case 'createmesh':
2319
{
2320
var err = null;
@@ -5106,10 +5049,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5049
for (var i in command.nodeids) { routeCommandToNode({ action: 'msg', type: 'console', nodeid: command.nodeids[i], value: 'agentupdate' }, MESHRIGHT_ADMIN, 0); }
5050
break;
5051
}
5109
- case 'print': {
5110
- console.log(command.value);
5111
- break;
5112
- }
5052
case 'previousLogins': {
5053
// TODO: Make a better database call to get filtered data.
5054
if (command.userid == null) {
@@ -5470,9 +5409,15 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5409
'lastconnect': serverCommandLastConnect,
5410
'lastconnects': serverCommandLastConnects,
5411
'meshes': serverCommandMeshes,
5412
+ 'ping': serverCommandPing,
5413
+ 'pong': serverCommandPong,
5414
+ 'powertimeline': serverCommandPowerTimeline,
5415
+ 'print': serverCommandPrint,
5416
+ 'serverclearerrorlog': serverCommandServerClearErrorLog,
5417
'serverconsole': serverCommandServerConsole,
5418
'servererrors': serverCommandServerErrors,
5419
'serverstats': serverCommandServerStats,
5420
+ 'serverupdate': serverCommandServerUpdate,
5421
'serverversion': serverCommandServerVersion,
5422
'users': serverCommandUsers
5423
};
@@ -5629,6 +5574,52 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5574
try { ws.send(JSON.stringify({ action: 'meshes', meshes: parent.GetAllMeshWithRights(user).map(parent.CloneSafeMesh), tag: command.tag })); } catch (ex) { }
5575
}
5576
5577
+ function serverCommandPing(command) { try { ws.send('{action:"pong"}'); } catch (ex) { } }
5578
+ function serverCommandPong(command) { } // NOP
5579
+
5580
+ function serverCommandPowerTimeline(command) {
5581
+ // Get the node and the rights for this node
5582
+ parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
5583
+ if (visible == false) return;
5584
+ // Query the database for the power timeline for a given node
5585
+ // The result is a compacted array: [ startPowerState, startTimeUTC, powerState ] + many[ deltaTime, powerState ]
5586
+ db.getPowerTimeline(node._id, function (err, docs) {
5587
+ if ((err == null) && (docs != null) && (docs.length > 0)) {
5588
+ var timeline = [], time = null, previousPower;
5589
+ for (i in docs) {
5590
+ var doc = docs[i], j = parseInt(i);
5591
+ doc.time = Date.parse(doc.time);
5592
+ if (time == null) { // First element
5593
+ // Skip all starting power 0 events.
5594
+ if ((doc.power == 0) && ((doc.oldPower == null) || (doc.oldPower == 0))) continue;
5595
+ time = doc.time;
5596
+ if (doc.oldPower) { timeline.push(doc.oldPower, time / 1000, doc.power); } else { timeline.push(0, time / 1000, doc.power); }
5597
+ } else if (previousPower != doc.power) { // Delta element
5598
+ // If this event is of a short duration (2 minutes or less), skip it.
5599
+ if ((docs.length > (j + 1)) && ((Date.parse(docs[j + 1].time) - doc.time) < 120000)) continue;
5600
+ timeline.push((doc.time - time) / 1000, doc.power);
5601
+ time = doc.time;
5602
+ }
5603
+ previousPower = doc.power;
5604
+ }
5605
+ try { ws.send(JSON.stringify({ action: 'powertimeline', nodeid: node._id, timeline: timeline, tag: command.tag })); } catch (ex) { }
5606
+ } else {
5607
+ // No records found, send current state if we have it
5608
+ var state = parent.parent.GetConnectivityState(command.nodeid);
5609
+ 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) { } }
5610
+ }
5611
+ });
5612
+ });
5613
+ }
5614
+
5615
+ function serverCommandPrint(command) { console.log(command.value); }
5616
+
5617
+ function serverCommandServerClearErrorLog(command) {
5618
+ // Clear the server error log
5619
+ if ((user.siteadmin & 16) == 0) return;
5620
+ fs.unlink(parent.parent.getConfigFilePath('mesherrors.txt'), function (err) { });
5621
+ }
5622
+
5623
function serverCommandServerConsole(command) {
5624
// Do not allow this command when logged in using a login token
5625
if (req.session.loginToken != null) return;
@@ -5674,6 +5665,17 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5665
}
5666
}
5667
5668
+ function serverCommandServerUpdate(command) {
5669
+ // Do not allow this command when logged in using a login token
5670
+ if (req.session.loginToken != null) return;
5671
+
5672
+ // Perform server update
5673
+ if ((user.siteadmin & 16) == 0) return;
5674
+ if ((domain.myserver === false) || ((domain.myserver != null) && (domain.myserver !== true) && (domain.myserver.upgrade !== true))) return;
5675
+ if ((command.version != null) && (typeof command.version != 'string')) return;
5676
+ parent.parent.performServerUpdate(command.version);
5677
+ }
5678
+
5679
function serverCommandServerVersion(command) {
5680
// Do not allow this command when logged in using a login token
5681
if (req.session.loginToken != null) return;