Improved Android push messaging token handling.

This commit is contained in:
Ylian Saint-Hilaire 2021-02-22 16:54:30 -08:00
parent 5298d1c5c9
commit e6f24582bd
7 changed files with 38 additions and 11 deletions

12
db.js
View File

@ -1755,11 +1755,7 @@ module.exports.CreateDB = function (parent, func) {
function dbNodeChange(nodeChange, added) { function dbNodeChange(nodeChange, added) {
common.unEscapeLinksFieldName(nodeChange.fullDocument); common.unEscapeLinksFieldName(nodeChange.fullDocument);
const node = performTypedRecordDecrypt([nodeChange.fullDocument])[0]; const node = performTypedRecordDecrypt([nodeChange.fullDocument])[0];
if (node.intelamt != null) { // Remove the Intel AMT password and MPS password before eventing this. parent.DispatchEvent(['*', node.meshid], obj, { etype: 'node', action: (added ? 'addnode' : 'changenode'), node: parent.webserver.CloneSafeNode(node), nodeid: node._id, domain: node.domain, nolog: 1 });
if (node.intelamt.pass != null) { node.intelamt.pass = 1; }
if (node.intelamt.mpspass != null) { node.intelamt.mpspass = 1; }
}
parent.DispatchEvent(['*', node.meshid], obj, { etype: 'node', action: (added ? 'addnode' : 'changenode'), node: node, nodeid: node._id, domain: node.domain, nolog: 1 });
} }
// Called when a device group has changed // Called when a device group has changed
@ -1779,11 +1775,7 @@ module.exports.CreateDB = function (parent, func) {
mesh.nolog = 1; mesh.nolog = 1;
delete mesh.type; delete mesh.type;
delete mesh._id; delete mesh._id;
if ((mesh.amt != null) && (mesh.amt.password != null)) { parent.DispatchEvent(['*', mesh.meshid], obj, parent.webserver.CloneSafeMesh(mesh));
mesh.amt = Object.assign({}, mesh.amt); // Shallow clone
if (mesh.amt.password != null) { mesh.amt.password = 1; } // Remove the Intel AMT password if present
}
parent.DispatchEvent(['*', mesh.meshid], obj, mesh);
} }
// Called when a user account has changed // Called when a user account has changed

View File

@ -1584,7 +1584,12 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
} }
// Push Messaging Token // Push Messaging Token
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. if ((command.pmt != null) && (typeof command.pmt == 'string') && (device.pmt != command.pmt)) {
if (typeof device.pmt == 'string') { db.Remove('pmt_' + device.pmt); }
device.pmt = command.pmt;
change = 1; // Don't save this change as an event to the db, so no log=1.
parent.removePmtFromAllOtherNodes(device); // We need to make sure to remove this push messaging token from any other device on this server, all domains included.
}
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. 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.
if ((mesh.mtype == 2) && (!args.wanonly)) { if ((mesh.mtype == 2) && (!args.wanonly)) {

View File

@ -821,6 +821,7 @@ function CreateMeshCentralServer(config, args) {
if (db.RemoveSMBIOS) { db.RemoveSMBIOS(node._id); } // Remove SMBios data if (db.RemoveSMBIOS) { db.RemoveSMBIOS(node._id); } // Remove SMBios data
db.RemoveAllNodeEvents(node._id); // Remove all events for this node db.RemoveAllNodeEvents(node._id); // Remove all events for this node
db.removeAllPowerEventsForNode(node._id); // Remove all power events for this node db.removeAllPowerEventsForNode(node._id); // Remove all power events for this node
if (typeof node.pmt == 'string') { db.Remove('pmt_' + node.pmt); } // Remove Push Messaging Token
db.Get('ra' + node._id, function (err, nodes) { db.Get('ra' + node._id, function (err, nodes) {
if ((nodes != null) && (nodes.length == 1)) { db.Remove('da' + nodes[0].daid); } // Remove diagnostic agent to real agent link if ((nodes != null) && (nodes.length == 1)) { db.Remove('da' + nodes[0].daid); } // Remove diagnostic agent to real agent link
db.Remove('ra' + node._id); // Remove real agent to diagnostic agent link db.Remove('ra' + node._id); // Remove real agent to diagnostic agent link

View File

@ -3683,6 +3683,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
if (db.RemoveSMBIOS) { db.RemoveSMBIOS(node._id); } // Remove SMBios data if (db.RemoveSMBIOS) { db.RemoveSMBIOS(node._id); } // Remove SMBios data
db.RemoveAllNodeEvents(node._id); // Remove all events for this node db.RemoveAllNodeEvents(node._id); // Remove all events for this node
db.removeAllPowerEventsForNode(node._id); // Remove all power events for this node db.removeAllPowerEventsForNode(node._id); // Remove all power events for this node
if (typeof node.pmt == 'string') { db.Remove('pmt_' + node.pmt); } // Remove Push Messaging Token
db.Get('ra' + node._id, function (err, nodes) { db.Get('ra' + node._id, function (err, nodes) {
if ((nodes != null) && (nodes.length == 1)) { db.Remove('da' + nodes[0].daid); } // Remove diagnostic agent to real agent link if ((nodes != null) && (nodes.length == 1)) { db.Remove('da' + nodes[0].daid); } // Remove diagnostic agent to real agent link
db.Remove('ra' + node._id); // Remove real agent to diagnostic agent link db.Remove('ra' + node._id); // Remove real agent to diagnostic agent link

View File

@ -1764,6 +1764,7 @@
node.userloc = message.event.node.userloc; node.userloc = message.event.node.userloc;
node.rdpport = message.event.node.rdpport; node.rdpport = message.event.node.rdpport;
node.consent = message.event.node.consent; node.consent = message.event.node.consent;
node.pmt = message.event.node.pmt;
if (message.event.node.agent != null) { if (message.event.node.agent != null) {
if (node.agent == null) node.agent = {}; if (node.agent == null) node.agent = {};
if (message.event.node.agent.ver != null) { node.agent.ver = message.event.node.agent.ver; } if (message.event.node.agent.ver != null) { node.agent.ver = message.event.node.agent.ver; }

View File

@ -2884,6 +2884,7 @@
node.rdpport = message.event.node.rdpport; node.rdpport = message.event.node.rdpport;
node.rfbport = message.event.node.rfbport; node.rfbport = message.event.node.rfbport;
node.consent = message.event.node.consent; node.consent = message.event.node.consent;
node.pmt = message.event.node.pmt;
if (message.event.node.links != null) { node.links = message.event.node.links; } else { delete node.links; } if (message.event.node.links != null) { node.links = message.event.node.links; } else { delete node.links; }
if (message.event.node.agent != null) { if (message.event.node.agent != null) {
if (node.agent == null) node.agent = {}; if (node.agent == null) node.agent = {};

View File

@ -6754,6 +6754,32 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
} }
// Insure exclusivity of a push messaging token for Android device
obj.removePmtFromAllOtherNodes = function (node) {
if (typeof node.pmt != 'string') return;
db.Get('pmt_' + node.pmt, function (err, docs) {
if ((err == null) && (docs.length == 1)) {
var oldNodeId = docs[0].nodeid;
db.Get(oldNodeId, function (nerr, ndocs) {
if ((nerr == null) && (ndocs.length == 1)) {
var oldNode = ndocs[0];
if (oldNode.pmt == node.pmt) {
// Remove the push messaging token and save the node.
delete oldNode.pmt;
db.Set(oldNode);
// Event the node change
var event = { etype: 'node', action: 'changenode', nodeid: oldNode._id, domain: oldNode.domain, node: obj.CloneSafeNode(oldNode) }
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.DispatchEvent(['*', oldNode.meshid, oldNode._id], obj, event);
}
}
});
}
db.Set({ _id: 'pmt_' + node.pmt, type: 'pmt', domain: node.domain, time: Date.now(), nodeid: node._id })
});
}
// Return true if a mobile browser is detected. // Return true if a mobile browser is detected.
// 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/ // 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/
function isMobileBrowser(req) { function isMobileBrowser(req) {