Updated MeshCommander and fixes to message dispatch.

This commit is contained in:
Ylian Saint-Hilaire 2019-07-10 14:27:38 -07:00
parent a803af854b
commit d443645423
6 changed files with 911 additions and 909 deletions

4
db.js
View File

@ -31,6 +31,7 @@ module.exports.CreateDB = function (parent, func) {
var expireEventsSeconds = (60 * 60 * 24 * 20); // By default, expire events after 20 days. (Seconds * Minutes * Hours * Days)
var expirePowerEventsSeconds = (60 * 60 * 24 * 10); // By default, expire power events after 10 days. (Seconds * Minutes * Hours * Days)
var expireServerStatsSeconds = (60 * 60 * 24 * 30); // By default, expire power events after 30 days. (Seconds * Minutes * Hours * Days)
const common = require('./common.js');
obj.identifier = null;
obj.dbKey = null;
obj.changeStream = false;
@ -851,6 +852,7 @@ module.exports.CreateDB = function (parent, func) {
// Called when a device group has changed
function dbMeshChange(meshChange, added) {
common.unEscapeLinksFieldName(meshChange.fullDocument);
const mesh = meshChange.fullDocument;
// Update the mesh object in memory
@ -865,7 +867,7 @@ module.exports.CreateDB = function (parent, func) {
delete mesh.type;
delete mesh._id;
if (mesh.amt) { delete mesh.amt.password; } // Remove the Intel AMT password if present
parent.DispatchEvent(['*', mesh._id], obj, mesh);
parent.DispatchEvent(['*', mesh.meshid], obj, mesh);
}
// Called when a user account has changed

View File

@ -566,7 +566,14 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
case 'help': {
r = 'Available commands: help, info, versions, args, resetserver, showconfig, usersessions, tasklimiter, setmaxtasks, cores,\r\n'
r += 'migrationagents, agentstats, webstats, mpsstats, swarmstats, acceleratorsstats, updatecheck, serverupdate, nodeconfig,\r\n';
r += 'heapdump, relays, autobackup, backupconfig, dupagents.';
r += 'heapdump, relays, autobackup, backupconfig, dupagents, dispatchtable.';
break;
}
case 'dispatchtable': {
r = '';
for (var i in parent.parent.eventsDispatch) {
r += (i + ', ' + parent.parent.eventsDispatch[i].length + '\r\n');
}
break;
}
case 'dupagents': {
@ -972,7 +979,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
mesh = parent.meshes[meshid];
if (mesh) {
// Remove user from the mesh
if (mesh.links[deluser._id] != null) { delete mesh.links[deluser._id]; parent.db.Set(mesh); }
if (mesh.links[deluser._id] != null) { delete mesh.links[deluser._id]; parent.db.Set(common.escapeLinksFieldName(mesh)); }
// Notify mesh change
change = 'Removed user ' + deluser.name + ' from group ' + mesh.name;
var event = { etype: 'mesh', username: user.name, userid: user._id, meshid: mesh._id, name: mesh.name, mtype: mesh.mtype, desc: mesh.desc, action: 'meshchange', links: mesh.links, msg: change, domain: domain.id };
@ -1522,65 +1529,50 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
} catch (ex) { err = 'Validation exception: ' + ex; }
// Handle any errors
if (err != null) {
if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'deletemesh', responseid: command.responseid, result: err })); } catch (ex) { } }
break;
if (err != null) { if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'deletemesh', responseid: command.responseid, result: err })); } catch (ex) { } } break; }
// Get the device group reference we are going to delete
var mesh = parent.meshes[command.meshid];
if (mesh == null) { if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'deletemesh', responseid: command.responseid, result: 'Unknown device group' })); } catch (ex) { } } return; }
// Check if this user has rights to do this
var err = null;
if (mesh.links[user._id] == null || mesh.links[user._id].rights != 0xFFFFFFFF) { err = 'Access denied'; }
if ((command.meshid.split('/').length != 3) || (command.meshid.split('/')[1] != domain.id)) { err = 'Invalid group'; } // Invalid domain, operation only valid for current domain
// Handle any errors
if (err != null) { if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'deletemesh', responseid: command.responseid, result: err })); } catch (ex) { } } return; }
// Fire the removal event first, because after this, the event will not route
var event = { etype: 'mesh', username: user.name, meshid: command.meshid, name: command.meshname, action: 'deletemesh', msg: 'Mesh deleted: ' + command.meshname, domain: domain.id };
parent.parent.DispatchEvent(['*', command.meshid], obj, event); // Even if DB change stream is active, this event need to be acted on.
// Remove all user links to this mesh
for (var j in mesh.links) {
var xuser = parent.users[j];
if (xuser && xuser.links) {
delete xuser.links[mesh._id];
db.SetUser(xuser);
parent.parent.DispatchEvent([xuser._id], obj, 'resubscribe');
}
}
db.Get(command.meshid, function (err, meshes) {
if (meshes.length != 1) {
if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'deletemesh', responseid: command.responseid, result: 'Unknown device group' })); } catch (ex) { } }
return;
}
var mesh = common.unEscapeLinksFieldName(meshes[0]);
// Delete all files on the server for this mesh
try {
var meshpath = parent.getServerRootFilePath(mesh);
if (meshpath != null) { parent.deleteFolderRec(meshpath); }
} catch (e) { }
// Check if this user has rights to do this
var err = null;
if (mesh.links[user._id] == null || mesh.links[user._id].rights != 0xFFFFFFFF) { err = 'Access denied'; }
if ((command.meshid.split('/').length != 3) || (command.meshid.split('/')[1] != domain.id)) { err = 'Invalid group'; } // Invalid domain, operation only valid for current domain
parent.parent.RemoveEventDispatchId(command.meshid); // Remove all subscriptions to this mesh
// Handle any errors
if (err != null) {
if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'deletemesh', responseid: command.responseid, result: err })); } catch (ex) { } }
return;
}
// Mark the mesh as deleted
mesh.deleted = new Date(); // Mark the time this mesh was deleted, we can expire it at some point.
db.Set(common.escapeLinksFieldName(mesh)); // We don't really delete meshes because if a device connects to is again, we will un-delete it.
// Fire the removal event first, because after this, the event will not route
var event = { etype: 'mesh', username: user.name, meshid: command.meshid, name: command.meshname, action: 'deletemesh', msg: 'Mesh deleted: ' + command.meshname, domain: domain.id };
parent.parent.DispatchEvent(['*', command.meshid], obj, event); // Even if DB change stream is active, this event need to be acted on.
// Delete all devices attached to this mesh in the database
db.RemoveMeshDocuments(command.meshid);
// Remove all user links to this mesh
for (i in meshes) {
var links = meshes[i].links;
for (var j in links) {
var xuser = parent.users[j];
if (xuser && xuser.links) {
delete xuser.links[meshes[i]._id];
db.SetUser(xuser);
parent.parent.DispatchEvent([xuser._id], obj, 'resubscribe');
}
}
}
// Delete all files on the server for this mesh
try {
var meshpath = parent.getServerRootFilePath(mesh);
if (meshpath != null) { parent.deleteFolderRec(meshpath); }
} catch (e) { }
parent.parent.RemoveEventDispatchId(command.meshid); // Remove all subscriptions to this mesh
// Mark the mesh as deleted
var dbmesh = meshes[0];
dbmesh.deleted = new Date(); // Mark the time this mesh was deleted, we can expire it at some point.
db.Set(common.escapeLinksFieldName(mesh)); // We don't really delete meshes because if a device connects to is again, we will up-delete it.
parent.meshes[command.meshid] = mesh; // Update the mesh in memory;
// Delete all devices attached to this mesh in the database
db.RemoveMeshDocuments(command.meshid);
if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'deletemesh', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
});
if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'deletemesh', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
break;
}
case 'editmesh':
@ -1712,7 +1704,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
} else {
event = { etype: 'mesh', username: user.name, userid: (deluserid.split('/')[2]), meshid: mesh._id, name: mesh.name, mtype: mesh.mtype, desc: mesh.desc, action: 'meshchange', links: mesh.links, msg: 'Removed user ' + (deluserid.split('/')[2]) + ' from group ' + mesh.name, domain: domain.id };
}
if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the mesh. Another event will come.
parent.parent.DispatchEvent(['*', mesh._id, user._id, command.userid], obj, event);
if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'removemeshuser', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
} else {

File diff suppressed because one or more lines are too long

View File

@ -49,9 +49,10 @@ var CreateAmtRemoteDesktop = function (divid, scrolldiv) {
obj.lastKeepAlive = Date.now();
// ###END###{DesktopInband}
// ###BEGIN###{DesktopFocus}
obj.mNagleTimer = null; // Mouse motion slowdown timer
obj.mx = 0; // Last mouse x position
obj.my = 0; // Last mouse y position
// ###BEGIN###{DesktopFocus}
obj.ox = -1; // Old mouse x position
obj.oy = -1; // Old mouse y position
obj.focusmode = 0;
@ -836,10 +837,10 @@ var CreateAmtRemoteDesktop = function (divid, scrolldiv) {
// RFB "PointerEvent" and mouse handlers
obj.mousedblclick = function (e) { }
obj.mousedown = function (e) { obj.buttonmask |= (1 << e.button); return obj.mousemove(e); }
obj.mouseup = function (e) { obj.buttonmask &= (0xFFFF - (1 << e.button)); return obj.mousemove(e); }
obj.mousemove = function (e) {
if (obj.state != 4) return true;
obj.mousedown = function (e) { obj.buttonmask |= (1 << e.button); return obj.mousemove(e, 1); }
obj.mouseup = function (e) { obj.buttonmask &= (0xFFFF - (1 << e.button)); return obj.mousemove(e, 1); }
obj.mousemove = function (e, force) {
if (obj.state < 4) return true;
var ScaleFactorHeight = (obj.canvas.canvas.height / Q(obj.canvasid).offsetHeight);
var ScaleFactorWidth = (obj.canvas.canvas.width / Q(obj.canvasid).offsetWidth);
var Offsets = obj.getPositionOfControl(Q(obj.canvasid));
@ -856,7 +857,18 @@ var CreateAmtRemoteDesktop = function (divid, scrolldiv) {
}
// ###END###{DesktopRotation}
obj.send(String.fromCharCode(5, obj.buttonmask) + ShortToStr(obj.mx) + ShortToStr(obj.my));
// This is the mouse motion nagle timer. Slow down the mouse motion event rate.
if (force == 1) {
obj.send(String.fromCharCode(5, obj.buttonmask) + ShortToStr(obj.mx) + ShortToStr(obj.my));
if (obj.mNagleTimer != null) { clearTimeout(obj.mNagleTimer); obj.mNagleTimer = null; }
} else {
if (obj.mNagleTimer == null) {
obj.mNagleTimer = setTimeout(function () {
obj.send(String.fromCharCode(5, obj.buttonmask) + ShortToStr(obj.mx) + ShortToStr(obj.my));
obj.mNagleTimer = null;
}, 50);
}
}
// ###BEGIN###{DesktopFocus}
// Update focus area if we are in focus mode

View File

@ -261,7 +261,7 @@ var CreateAmtRedirect = function (module, authCookie) {
if (obj.debugmode == 1) { console.log('Send', x); }
var b = new Uint8Array(x.length);
for (var i = 0; i < x.length; ++i) { b[i] = x.charCodeAt(i); }
obj.socket.send(b.buffer);
try { obj.socket.send(b.buffer); } catch (ex) { }
}
}

View File

@ -1125,8 +1125,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
var mesh = obj.meshes[meshid];
if (mesh) {
// Remove user from the mesh
var escUserId = obj.common.escapeFieldName(userid);
if (mesh.links[escUserId] != null) { delete mesh.links[escUserId]; obj.db.Set(mesh); }
if (mesh.links[userid] != null) { delete mesh.links[userid]; obj.db.Set(obj.common.escapeLinksFieldName(mesh)); }
// Notify mesh change
var change = 'Removed user ' + user.name + ' from group ' + mesh.name;
obj.parent.DispatchEvent(['*', mesh._id, user._id, userid], obj, { etype: 'mesh', username: user.name, userid: userid, meshid: mesh._id, name: mesh.name, mtype: mesh.mtype, desc: mesh.desc, action: 'meshchange', links: mesh.links, msg: change, domain: domain.id });
@ -2529,7 +2528,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
if (domain == null) { res.sendStatus(404); return; }
// If required, check if this user has rights to do this
if ((obj.parent.config.settings != null) && (obj.parent.config.settings.lockagentdownload == true) && (req.session.userid == null)) { res.sendStatus(401); return; }
if ((obj.parent.config.settings != null) && ((obj.parent.config.settings.lockagentdownload == true) || (domain.lockagentdownload == true)) && (req.session.userid == null)) { res.sendStatus(401); return; }
if (req.query.id != null) {
// Send a specific mesh agent back
@ -2545,10 +2544,9 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
if (mesh == null) { res.sendStatus(401); return; }
// If required, check if this user has rights to do this
if ((obj.parent.config.settings != null) && (obj.parent.config.settings.lockagentdownload == true)) {
if ((obj.parent.config.settings != null) && ((obj.parent.config.settings.lockagentdownload == true) || (domain.lockagentdownload == true))) {
var user = obj.users[req.session.userid];
var escUserId = obj.common.escapeFieldName(user._id);
if ((user == null) || (mesh.links[escUserId] == null) || ((mesh.links[escUserId].rights & 1) == 0)) { res.sendStatus(401); return; }
if ((user == null) || (mesh.links[user._id] == null) || ((mesh.links[user._id].rights & 1) == 0)) { res.sendStatus(401); return; }
if (domain.id != mesh.domain) { res.sendStatus(401); return; }
}
@ -2690,7 +2688,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
if ((domain == null) || (req.query.id == null)) { res.sendStatus(404); return; }
// If required, check if this user has rights to do this
if ((obj.parent.config.settings != null) && (obj.parent.config.settings.lockagentdownload == true) && (req.session.userid == null)) { res.sendStatus(401); return; }
if ((obj.parent.config.settings != null) && ((obj.parent.config.settings.lockagentdownload == true) || (domain.lockagentdownload == true)) && (req.session.userid == null)) { res.sendStatus(401); return; }
// Send a specific mesh agent back
var argentInfo = obj.parent.meshAgentBinaries[req.query.id];
@ -2702,10 +2700,9 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
if (mesh == null) { res.sendStatus(401); return; }
// If required, check if this user has rights to do this
if ((obj.parent.config.settings != null) && (obj.parent.config.settings.lockagentdownload == true)) {
if ((obj.parent.config.settings != null) && ((obj.parent.config.settings.lockagentdownload == true) || (domain.lockagentdownload == true))) {
var user = obj.users[req.session.userid];
var escUserId = obj.common.escapeFieldName(user._id);
if ((user == null) || (mesh.links[escUserId] == null) || ((mesh.links[escUserId].rights & 1) == 0)) { res.sendStatus(401); return; }
if ((user == null) || (mesh.links[user._id] == null) || ((mesh.links[user._id].rights & 1) == 0)) { res.sendStatus(401); return; }
if (domain.id != mesh.domain) { res.sendStatus(401); return; }
}
@ -2785,17 +2782,16 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
//if ((domain.id !== '') || (!req.session) || (req.session == null) || (!req.session.userid)) { res.sendStatus(401); return; }
// If required, check if this user has rights to do this
if ((obj.parent.config.settings != null) && (obj.parent.config.settings.lockagentdownload == true) && (req.session.userid == null)) { res.sendStatus(401); return; }
if ((obj.parent.config.settings != null) && ((obj.parent.config.settings.lockagentdownload == true) || (domain.lockagentdownload == true)) && (req.session.userid == null)) { res.sendStatus(401); return; }
// Fetch the mesh object
var mesh = obj.meshes['mesh/' + domain.id + '/' + req.query.id];
if (mesh == null) { res.sendStatus(401); return; }
// If needed, check if this user has rights to do this
if ((obj.parent.config.settings != null) && (obj.parent.config.settings.lockagentdownload == true)) {
if ((obj.parent.config.settings != null) && ((obj.parent.config.settings.lockagentdownload == true) || (domain.lockagentdownload == true))) {
var user = obj.users[req.session.userid];
var escUserId = obj.common.escapeFieldName(user._id);
if ((user == null) || (mesh.links[escUserId] == null) || ((mesh.links[escUserId].rights & 1) == 0)) { res.sendStatus(401); return; }
if ((user == null) || (mesh.links[user._id] == null) || ((mesh.links[user._id].rights & 1) == 0)) { res.sendStatus(401); return; }
if (domain.id != mesh.domain) { res.sendStatus(401); return; }
}