Improved Android push messaging token handling.

Ylian Saint-Hilaire committed Feb 22, 2021 at 16:54 UTC e6f24582bdb0e601be800d98a75d63ee881b2f1f
7 files changed +38 -11
db.js
+2 -10
@@ -1755,11 +1755,7 @@ module.exports.CreateDB = function (parent, func) {
1755 function dbNodeChange(nodeChange, added) {
1756 common.unEscapeLinksFieldName(nodeChange.fullDocument);
1757 const node = performTypedRecordDecrypt([nodeChange.fullDocument])[0];
1758 - if (node.intelamt != null) { // Remove the Intel AMT password and MPS password before eventing this.
1759 - if (node.intelamt.pass != null) { node.intelamt.pass = 1; }
1760 - if (node.intelamt.mpspass != null) { node.intelamt.mpspass = 1; }
1761 - }
1762 - parent.DispatchEvent(['*', node.meshid], obj, { etype: 'node', action: (added ? 'addnode' : 'changenode'), node: node, nodeid: node._id, domain: node.domain, nolog: 1 });
1758 + parent.DispatchEvent(['*', node.meshid], obj, { etype: 'node', action: (added ? 'addnode' : 'changenode'), node: parent.webserver.CloneSafeNode(node), nodeid: node._id, domain: node.domain, nolog: 1 });
1759 }
1760
1761 // Called when a device group has changed
@@ -1779,11 +1775,7 @@ module.exports.CreateDB = function (parent, func) {
1775 mesh.nolog = 1;
1776 delete mesh.type;
1777 delete mesh._id;
1782 - if ((mesh.amt != null) && (mesh.amt.password != null)) {
1783 - mesh.amt = Object.assign({}, mesh.amt); // Shallow clone
1784 - if (mesh.amt.password != null) { mesh.amt.password = 1; } // Remove the Intel AMT password if present
1785 - }
1786 - parent.DispatchEvent(['*', mesh.meshid], obj, mesh);
1778 + parent.DispatchEvent(['*', mesh.meshid], obj, parent.webserver.CloneSafeMesh(mesh));
1779 }
1780
1781 // Called when a user account has changed
meshagent.js
+6 -1
@@ -1584,7 +1584,12 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1584 }
1585
1586 // Push Messaging Token
1587 - if ((command.pmt != null) && (typeof command.pmt == 'string') && (device.pmt != command.pmt)) { device.pmt = command.pmt; change = 1; } // Don't save this as an event to the db.
1587 + if ((command.pmt != null) && (typeof command.pmt == 'string') && (device.pmt != command.pmt)) {
1588 + if (typeof device.pmt == 'string') { db.Remove('pmt_' + device.pmt); }
1589 + device.pmt = command.pmt;
1590 + change = 1; // Don't save this change as an event to the db, so no log=1.
1591 + parent.removePmtFromAllOtherNodes(device); // We need to make sure to remove this push messaging token from any other device on this server, all domains included.
1592 + }
1593
1594 if ((command.users != null) && (Array.isArray(command.users)) && (device.users != command.users)) { device.users = command.users; change = 1; } // Don't save this to the db.
1595 if ((mesh.mtype == 2) && (!args.wanonly)) {
meshcentral.js
+1
@@ -821,6 +821,7 @@ function CreateMeshCentralServer(config, args) {
821 if (db.RemoveSMBIOS) { db.RemoveSMBIOS(node._id); } // Remove SMBios data
822 db.RemoveAllNodeEvents(node._id); // Remove all events for this node
823 db.removeAllPowerEventsForNode(node._id); // Remove all power events for this node
824 + if (typeof node.pmt == 'string') { db.Remove('pmt_' + node.pmt); } // Remove Push Messaging Token
825 db.Get('ra' + node._id, function (err, nodes) {
826 if ((nodes != null) && (nodes.length == 1)) { db.Remove('da' + nodes[0].daid); } // Remove diagnostic agent to real agent link
827 db.Remove('ra' + node._id); // Remove real agent to diagnostic agent link
meshuser.js
+1
@@ -3683,6 +3683,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
3683 if (db.RemoveSMBIOS) { db.RemoveSMBIOS(node._id); } // Remove SMBios data
3684 db.RemoveAllNodeEvents(node._id); // Remove all events for this node
3685 db.removeAllPowerEventsForNode(node._id); // Remove all power events for this node
3686 + if (typeof node.pmt == 'string') { db.Remove('pmt_' + node.pmt); } // Remove Push Messaging Token
3687 db.Get('ra' + node._id, function (err, nodes) {
3688 if ((nodes != null) && (nodes.length == 1)) { db.Remove('da' + nodes[0].daid); } // Remove diagnostic agent to real agent link
3689 db.Remove('ra' + node._id); // Remove real agent to diagnostic agent link
views/default-mobile.handlebars
+1
@@ -1764,6 +1764,7 @@
1764 node.userloc = message.event.node.userloc;
1765 node.rdpport = message.event.node.rdpport;
1766 node.consent = message.event.node.consent;
1767 + node.pmt = message.event.node.pmt;
1768 if (message.event.node.agent != null) {
1769 if (node.agent == null) node.agent = {};
1770 if (message.event.node.agent.ver != null) { node.agent.ver = message.event.node.agent.ver; }
views/default.handlebars
+1
@@ -2884,6 +2884,7 @@
2884 node.rdpport = message.event.node.rdpport;
2885 node.rfbport = message.event.node.rfbport;
2886 node.consent = message.event.node.consent;
2887 + node.pmt = message.event.node.pmt;
2888 if (message.event.node.links != null) { node.links = message.event.node.links; } else { delete node.links; }
2889 if (message.event.node.agent != null) {
2890 if (node.agent == null) node.agent = {};
webserver.js
+26
@@ -6754,6 +6754,32 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
6754
6755 }
6756
6757 + // Insure exclusivity of a push messaging token for Android device
6758 + obj.removePmtFromAllOtherNodes = function (node) {
6759 + if (typeof node.pmt != 'string') return;
6760 + db.Get('pmt_' + node.pmt, function (err, docs) {
6761 + if ((err == null) && (docs.length == 1)) {
6762 + var oldNodeId = docs[0].nodeid;
6763 + db.Get(oldNodeId, function (nerr, ndocs) {
6764 + if ((nerr == null) && (ndocs.length == 1)) {
6765 + var oldNode = ndocs[0];
6766 + if (oldNode.pmt == node.pmt) {
6767 + // Remove the push messaging token and save the node.
6768 + delete oldNode.pmt;
6769 + db.Set(oldNode);
6770 +
6771 + // Event the node change
6772 + var event = { etype: 'node', action: 'changenode', nodeid: oldNode._id, domain: oldNode.domain, node: obj.CloneSafeNode(oldNode) }
6773 + 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.
6774 + parent.DispatchEvent(['*', oldNode.meshid, oldNode._id], obj, event);
6775 + }
6776 + }
6777 + });
6778 + }
6779 + db.Set({ _id: 'pmt_' + node.pmt, type: 'pmt', domain: node.domain, time: Date.now(), nodeid: node._id })
6780 + });
6781 + }
6782 +
6783 // Return true if a mobile browser is detected.
6784 // This code comes from "http://detectmobilebrowsers.com/" and was modified, This is free and unencumbered software released into the public domain. For more information, please refer to the http://unlicense.org/
6785 function isMobileBrowser(req) {