Fixed event dispatching bug.

This commit is contained in:
Ylian Saint-Hilaire 2021-11-10 17:56:12 -08:00
parent 6dceb842d7
commit aab50dcbef
5 changed files with 52 additions and 23 deletions

View File

@ -1706,8 +1706,9 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
function addGuestSharing(flags, viewOnly, func) { function addGuestSharing(flags, viewOnly, func) {
// Create cookie // Create cookie
var publicid = 'AS:' + obj.dbNodeKey; const publicid = 'AS:' + obj.dbNodeKey;
var cookie = { a: 6, pid: publicid }; // New style sharing cookie const extrakey = getRandomAmtPassword();
const cookie = { a: 6, pid: publicid, k: extrakey }; // New style sharing cookie
const inviteCookie = parent.parent.encodeCookie(cookie, parent.parent.invitationLinkEncryptionKey); const inviteCookie = parent.parent.encodeCookie(cookie, parent.parent.invitationLinkEncryptionKey);
if (inviteCookie == null) return; if (inviteCookie == null) return;
@ -1720,7 +1721,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
if (serverName.split('.') == 1) { url = '/' + xdomain + page + '?c=' + inviteCookie; } if (serverName.split('.') == 1) { url = '/' + xdomain + page + '?c=' + inviteCookie; }
// Create a device sharing database entry // Create a device sharing database entry
var shareEntry = { _id: 'deviceshare-' + publicid, type: 'deviceshare', nodeid: obj.dbNodeKey, p: flags, domain: domain.id, publicid: publicid, guestName: 'Agent', consent: 0x7F, url: url }; var shareEntry = { _id: 'deviceshare-' + publicid, type: 'deviceshare', nodeid: obj.dbNodeKey, p: flags, domain: domain.id, publicid: publicid, guestName: 'Agent', consent: 0x7F, url: url, extrakey: extrakey };
if (viewOnly === true) { shareEntry.viewOnly = true; } if (viewOnly === true) { shareEntry.viewOnly = true; }
parent.db.Set(shareEntry); parent.db.Set(shareEntry);

View File

@ -1969,7 +1969,21 @@ function CreateMeshCentralServer(config, args) {
}; };
obj.RemoveEventDispatch = function (ids, target) { obj.RemoveEventDispatch = function (ids, target) {
obj.debug('dispatch', 'RemoveEventDispatch', ids); obj.debug('dispatch', 'RemoveEventDispatch', ids);
for (var i in ids) { var id = ids[i]; if (obj.eventsDispatch[id]) { var j = obj.eventsDispatch[id].indexOf(target); if (j >= 0) { if (obj.eventsDispatch[id].length == 1) { delete obj.eventsDispatch[id]; } else { obj.eventsDispatch[id].splice(j, 1); } } } } for (var i in ids) {
const id = ids[i];
if (obj.eventsDispatch[id]) {
var j = obj.eventsDispatch[id].indexOf(target);
if (j >= 0) {
if (obj.eventsDispatch[id].length == 1) {
delete obj.eventsDispatch[id];
} else {
const newList = []; // We create a new list so not to modify the original list. Allows this function to be called during an event dispatch.
for (var k in obj.eventsDispatch[i]) { if (obj.eventsDispatch[i][k] != target) { newList.push(obj.eventsDispatch[i][k]); } }
obj.eventsDispatch[i] = newList;
}
}
}
}
}; };
obj.RemoveEventDispatchId = function (id) { obj.RemoveEventDispatchId = function (id) {
obj.debug('dispatch', 'RemoveEventDispatchId', id); obj.debug('dispatch', 'RemoveEventDispatchId', id);
@ -1977,7 +1991,18 @@ function CreateMeshCentralServer(config, args) {
}; };
obj.RemoveAllEventDispatch = function (target) { obj.RemoveAllEventDispatch = function (target) {
obj.debug('dispatch', 'RemoveAllEventDispatch'); obj.debug('dispatch', 'RemoveAllEventDispatch');
for (var i in obj.eventsDispatch) { var j = obj.eventsDispatch[i].indexOf(target); if (j >= 0) { if (obj.eventsDispatch[i].length == 1) { delete obj.eventsDispatch[i]; } else { obj.eventsDispatch[i].splice(j, 1); } } } for (var i in obj.eventsDispatch) {
const j = obj.eventsDispatch[i].indexOf(target);
if (j >= 0) {
if (obj.eventsDispatch[i].length == 1) {
delete obj.eventsDispatch[i];
} else {
const newList = []; // We create a new list so not to modify the original list. Allows this function to be called during an event dispatch.
for (var k in obj.eventsDispatch[i]) { if (obj.eventsDispatch[i][k] != target) { newList.push(obj.eventsDispatch[i][k]); } }
obj.eventsDispatch[i] = newList;
}
}
}
}; };
obj.DispatchEvent = function (ids, source, event, fromPeerServer) { obj.DispatchEvent = function (ids, source, event, fromPeerServer) {
// If the database is not setup, exit now. // If the database is not setup, exit now.
@ -1992,7 +2017,7 @@ function CreateMeshCentralServer(config, args) {
if ((typeof event == 'object') && (!event.nolog)) { if ((typeof event == 'object') && (!event.nolog)) {
event.time = new Date(); event.time = new Date();
// The event we store is going to skip some of the fields so we don't store too much stuff in the database. // The event we store is going to skip some of the fields so we don't store too much stuff in the database.
var storeEvent = Object.assign({}, event); const storeEvent = Object.assign({}, event);
if (storeEvent.node) { delete storeEvent.node; } // Skip the "node" field. May skip more in the future. if (storeEvent.node) { delete storeEvent.node; } // Skip the "node" field. May skip more in the future.
if (storeEvent.links) { if (storeEvent.links) {
// Escape "links" names that may have "." and/or "$" // Escape "links" names that may have "." and/or "$"
@ -2002,16 +2027,15 @@ function CreateMeshCentralServer(config, args) {
storeEvent.ids = ids; storeEvent.ids = ids;
obj.db.StoreEvent(storeEvent); obj.db.StoreEvent(storeEvent);
} }
var targets = []; // List of targets we dispatched the event to, we don't want to dispatch to the same target twice. const targets = []; // List of targets we dispatched the event to, we don't want to dispatch to the same target twice.
for (var j in ids) { for (var j in ids) {
var id = ids[j]; const id = ids[j];
if (obj.eventsDispatch[id]) { const eventsDispatch = obj.eventsDispatch[id];
for (var i in obj.eventsDispatch[id]) { if (eventsDispatch) {
if (targets.indexOf(obj.eventsDispatch[id][i]) == -1) { // Check if we already displatched to this target for (var i in eventsDispatch) {
targets.push(obj.eventsDispatch[id][i]); if (targets.indexOf(eventsDispatch[i]) == -1) { // Check if we already displatched to this target
try { obj.eventsDispatch[id][i].HandleEvent(source, event, ids, id); } catch (ex) { targets.push(eventsDispatch[i]);
console.log(ex, obj.eventsDispatch[id][i]); try { eventsDispatch[i].HandleEvent(source, event, ids, id); } catch (ex) { console.log(ex, eventsDispatch[i]); }
}
} }
} }
} }

View File

@ -941,21 +941,21 @@ function CreateDesktopMultiplexor(parent, domain, nodeid, func) {
return obj; return obj;
} }
function checkDeviceSharePublicIdentifier(parent, domain, nodeid, pid, func) { function checkDeviceSharePublicIdentifier(parent, domain, nodeid, pid, extraKey, func) {
// Check the public id // Check the public id
parent.db.GetAllTypeNodeFiltered([nodeid], domain.id, 'deviceshare', null, function (err, docs) { parent.db.GetAllTypeNodeFiltered([nodeid], domain.id, 'deviceshare', null, function (err, docs) {
if ((err != null) || (docs.length == 0)) { func(false); return; } if ((err != null) || (docs.length == 0)) { func(false); return; }
// Search for the device share public identifier // Search for the device share public identifier
var found = false; var found = false;
for (var i = 0; i < docs.length; i++) { if (docs[i].publicid == pid) { found = true; } } for (var i = 0; i < docs.length; i++) { if ((docs[i].publicid == pid) && ((docs[i].extrakey == null) || (docs[i].extrakey === extraKey))) { found = true; } }
func(found); func(found);
}); });
} }
module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie) { module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie) {
if ((cookie != null) && (typeof cookie.nid == 'string') && (typeof cookie.pid == 'string')) { if ((cookie != null) && (typeof cookie.nid == 'string') && (typeof cookie.pid == 'string')) {
checkDeviceSharePublicIdentifier(parent, domain, cookie.nid, cookie.pid, function (result) { checkDeviceSharePublicIdentifier(parent, domain, cookie.nid, cookie.pid, cookie.k, function (result) {
// If the identifier if not found, close the connection // If the identifier if not found, close the connection
if (result == false) { try { ws.close(); } catch (e) { } return; } if (result == false) { try { ws.close(); } catch (e) { } return; }
// Public device sharing identifier found, continue as normal. // Public device sharing identifier found, continue as normal.

View File

@ -47,21 +47,23 @@ const MESHRIGHT_ADMIN = 0xFFFFFFFF;
// 101 = Intel AMT Redirection // 101 = Intel AMT Redirection
// 200 = Messenger // 200 = Messenger
function checkDeviceSharePublicIdentifier(parent, domain, nodeid, pid, func) { function checkDeviceSharePublicIdentifier(parent, domain, nodeid, pid, extraKey, func) {
// Check the public id // Check the public id
parent.db.GetAllTypeNodeFiltered([nodeid], domain.id, 'deviceshare', null, function (err, docs) { parent.db.GetAllTypeNodeFiltered([nodeid], domain.id, 'deviceshare', null, function (err, docs) {
if ((err != null) || (docs.length == 0)) { func(false); return; } if ((err != null) || (docs.length == 0)) { func(false); return; }
// Search for the device share public identifier // Search for the device share public identifier
var found = false; var found = false;
for (var i = 0; i < docs.length; i++) { if (docs[i].publicid == pid) { found = true; } } for (var i = 0; i < docs.length; i++) {
for (var i = 0; i < docs.length; i++) { if ((docs[i].publicid == pid) && ((docs[i].extrakey == null) || (docs[i].extrakey === extraKey))) { found = true; } }
}
func(found); func(found);
}); });
} }
module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie) { module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie) {
if ((cookie != null) && (typeof cookie.nid == 'string') && (typeof cookie.pid == 'string')) { if ((cookie != null) && (typeof cookie.nid == 'string') && (typeof cookie.pid == 'string')) {
checkDeviceSharePublicIdentifier(parent, domain, cookie.nid, cookie.pid, function (result) { checkDeviceSharePublicIdentifier(parent, domain, cookie.nid, cookie.pid, cookie.k, function (result) {
// If the identifier if not found, close the connection // If the identifier if not found, close the connection
if (result == false) { try { ws.close(); } catch (e) { } return; } if (result == false) { try { ws.close(); } catch (e) { } return; }
// Public device sharing identifier found, continue as normal. // Public device sharing identifier found, continue as normal.

View File

@ -3583,7 +3583,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
if ((err != null) || (docs == null) || (docs.length != 1)) { res.sendStatus(404); return; } if ((err != null) || (docs == null) || (docs.length != 1)) { res.sendStatus(404); return; }
const doc = docs[0]; const doc = docs[0];
// Generate an old style cookie from the information in the database // Generate an old style cookie from the information in the database
var cookie = { a: 5, p: doc.p, uid: doc.userid, gn: doc.guestName, nid: doc.nodeid, cf: doc.consent, pid: doc.publicid }; var cookie = { a: 5, p: doc.p, gn: doc.guestName, nid: doc.nodeid, cf: doc.consent, pid: doc.publicid, k: doc.extrakey };
if (doc.userid) { cookie.uid = doc.userid; }
if ((cookie.userid == null) && (cookie.pid.startsWith('AS:node/'))) { cookie.nouser = 1; } if ((cookie.userid == null) && (cookie.pid.startsWith('AS:node/'))) { cookie.nouser = 1; }
if ((doc.startTime != null) && (doc.expireTime != null)) { cookie.start = doc.startTime; cookie.expire = doc.expireTime; } if ((doc.startTime != null) && (doc.expireTime != null)) { cookie.start = doc.startTime; cookie.expire = doc.expireTime; }
if (doc.viewOnly === true) { cookie.vo = 1; } if (doc.viewOnly === true) { cookie.vo = 1; }
@ -3606,7 +3607,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
// Search for the device share public identifier, expire message. // Search for the device share public identifier, expire message.
var found = false; var found = false;
for (var i = 0; i < docs.length; i++) { if (docs[i].publicid == c.pid) { found = true; } } for (var i = 0; i < docs.length; i++) { if ((docs[i].publicid == c.pid) && ((docs[i].extrakey == null) || (docs[i].extrakey === c.k))) { found = true; } }
if (found == false) { render(req, res, getRenderPage((domain.sitestyle == 2) ? 'message2' : 'message', req, domain), getRenderArgs({ titleid: 2, msgid: 12, domainurl: encodeURIComponent(domain.url).replace(/'/g, '%27') }, req, domain)); return; } if (found == false) { render(req, res, getRenderPage((domain.sitestyle == 2) ? 'message2' : 'message', req, domain), getRenderArgs({ titleid: 2, msgid: 12, domainurl: encodeURIComponent(domain.url).replace(/'/g, '%27') }, req, domain)); return; }
// Get information about this node // Get information about this node
@ -3621,6 +3622,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
// Consent flags are 1 = Notify, 8 = Prompt, 64 = Privacy Bar. // Consent flags are 1 = Notify, 8 = Prompt, 64 = Privacy Bar.
const authCookieData = { userid: c.uid, domainid: domain.id, nid: c.nid, ip: req.clientIp, p: c.p, gn: c.gn, cf: c.cf, r: 8, expire: c.expire, pid: c.pid, vo: c.vo }; const authCookieData = { userid: c.uid, domainid: domain.id, nid: c.nid, ip: req.clientIp, p: c.p, gn: c.gn, cf: c.cf, r: 8, expire: c.expire, pid: c.pid, vo: c.vo };
if ((authCookieData.userid == null) && (authCookieData.pid.startsWith('AS:node/'))) { authCookieData.nouser = 1; } if ((authCookieData.userid == null) && (authCookieData.pid.startsWith('AS:node/'))) { authCookieData.nouser = 1; }
if (c.k != null) { authCookieData.k = c.k; }
const authCookie = obj.parent.encodeCookie(authCookieData, obj.parent.loginCookieEncryptionKey); const authCookie = obj.parent.encodeCookie(authCookieData, obj.parent.loginCookieEncryptionKey);
// Server features // Server features