mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2025-01-27 06:33:15 -05:00
Merge pull request #2992 from nzalev/refactor-meshuser
Added server command handler functions
This commit is contained in:
commit
69d2dc7e12
124
meshuser.js
124
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
|
// pass through to switch statement until refactoring complete
|
||||||
|
|
||||||
switch (command.action) {
|
switch (command.action) {
|
||||||
case 'pong': { break; } // NOP
|
|
||||||
case 'ping': { try { ws.send('{action:"pong"}'); } catch (ex) { } break; }
|
|
||||||
case 'intersession':
|
case 'intersession':
|
||||||
{
|
{
|
||||||
// Sends data between sessions of the same user
|
// Sends data between sessions of the same user
|
||||||
@ -748,42 +746,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
|
|||||||
});
|
});
|
||||||
break;
|
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':
|
case 'fileoperation':
|
||||||
{
|
{
|
||||||
// Check permissions
|
// Check permissions
|
||||||
@ -2353,25 +2315,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
|
|||||||
});
|
});
|
||||||
break;
|
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':
|
case 'createmesh':
|
||||||
{
|
{
|
||||||
var err = null;
|
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); }
|
for (var i in command.nodeids) { routeCommandToNode({ action: 'msg', type: 'console', nodeid: command.nodeids[i], value: 'agentupdate' }, MESHRIGHT_ADMIN, 0); }
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'print': {
|
|
||||||
console.log(command.value);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'previousLogins': {
|
case 'previousLogins': {
|
||||||
// TODO: Make a better database call to get filtered data.
|
// TODO: Make a better database call to get filtered data.
|
||||||
if (command.userid == null) {
|
if (command.userid == null) {
|
||||||
@ -5470,9 +5409,15 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
|
|||||||
'lastconnect': serverCommandLastConnect,
|
'lastconnect': serverCommandLastConnect,
|
||||||
'lastconnects': serverCommandLastConnects,
|
'lastconnects': serverCommandLastConnects,
|
||||||
'meshes': serverCommandMeshes,
|
'meshes': serverCommandMeshes,
|
||||||
|
'ping': serverCommandPing,
|
||||||
|
'pong': serverCommandPong,
|
||||||
|
'powertimeline': serverCommandPowerTimeline,
|
||||||
|
'print': serverCommandPrint,
|
||||||
|
'serverclearerrorlog': serverCommandServerClearErrorLog,
|
||||||
'serverconsole': serverCommandServerConsole,
|
'serverconsole': serverCommandServerConsole,
|
||||||
'servererrors': serverCommandServerErrors,
|
'servererrors': serverCommandServerErrors,
|
||||||
'serverstats': serverCommandServerStats,
|
'serverstats': serverCommandServerStats,
|
||||||
|
'serverupdate': serverCommandServerUpdate,
|
||||||
'serverversion': serverCommandServerVersion,
|
'serverversion': serverCommandServerVersion,
|
||||||
'users': serverCommandUsers
|
'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) { }
|
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) {
|
function serverCommandServerConsole(command) {
|
||||||
// Do not allow this command when logged in using a login token
|
// Do not allow this command when logged in using a login token
|
||||||
if (req.session.loginToken != null) return;
|
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) {
|
function serverCommandServerVersion(command) {
|
||||||
// Do not allow this command when logged in using a login token
|
// Do not allow this command when logged in using a login token
|
||||||
if (req.session.loginToken != null) return;
|
if (req.session.loginToken != null) return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user