From 041ab287a2196bd333fb3065dac782e9ddc6ee99 Mon Sep 17 00:00:00 2001 From: Bryan Roe Date: Fri, 12 Apr 2019 11:28:57 -0700 Subject: [PATCH] 1. Updated Diagnostic Registration to escape NodeID 2. Added helper method to db.js to escape base64 3. Updated db.Get to support optional paramter passing --- db.js | 24 +++++++++++++++++++++++- meshagent.js | 22 +++++++++++++--------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/db.js b/db.js index 61428627..d5564ec3 100644 --- a/db.js +++ b/db.js @@ -281,7 +281,28 @@ module.exports.CreateDB = function (parent) { // Database actions on the main collection obj.Set = function (data, func) { obj.file.update({ _id: data._id }, data, { upsert: true }, func); }; - obj.Get = function (id, func) { obj.file.find({ _id: id }, func); }; + obj.Get = function (id, func) + { + if (arguments.length > 2) + { + var parms = [func]; + for (var parmx = 2; parmx < arguments.length; ++parmx) { parms.push(arguments[parmx]); } + var func2 = function _func2(arg1, arg2) + { + console.log('callback'); + var userCallback = _func2.userArgs.shift(); + _func2.userArgs.unshift(arg2); + _func2.userArgs.unshift(arg1); + userCallback.apply(obj, _func2.userArgs); + }; + func2.userArgs = parms; + obj.file.find({ _id: id }, func2); + } + else + { + obj.file.find({ _id: id }, func); + } + }; obj.GetAll = function (func) { obj.file.find({}, func); }; obj.GetAllTypeNoTypeField = function (type, domain, func) { obj.file.find({ type: type, domain: domain }, { type: 0 }, func); }; obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, domain, type, id, func) { var x = { type: type, domain: domain, meshid: { $in: meshes } }; if (id) { x._id = id; } obj.file.find(x, { type: 0 }, func); }; @@ -422,6 +443,7 @@ module.exports.CreateDB = function (parent) { // This is used to rate limit a number of operation per day. Returns a startValue each new days, but you can substract it and save the value in the db. obj.getValueOfTheDay = function (id, startValue, func) { obj.Get(id, function (err, docs) { var date = new Date(), t = date.toLocaleDateString(); if (docs.length == 1) { var r = docs[0]; if (r.day == t) { func({ _id: id, value: r.value, day: t }); return; } } func({ _id: id, value: startValue, day: t }); }); }; + obj.escapeBase64 = function escapeBase64(val) { return (val.replace(/\+/g, '@').replace(/\//g, '$')); } function Clone(v) { return JSON.parse(JSON.stringify(v)); } diff --git a/meshagent.js b/meshagent.js index ae477e01..97edff29 100644 --- a/meshagent.js +++ b/meshagent.js @@ -773,14 +773,17 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { //console.log('recoveryAgentCoreIsStable()'); // Fetch the the real agent nodeid - db.Get('da' + obj.dbNodeKey, function (err, nodes) { - if (nodes.length == 1) { - obj.realNodeKey = nodes[0].raid; - obj.send(JSON.stringify({ action: 'diagnostic', value: { command: 'query', value: obj.realNodeKey } })); - } else { - obj.send(JSON.stringify({ action: 'diagnostic', value: { command: 'query', value: null } })); + db.Get('da' + obj.dbNodeKey, function (err, nodes, self) + { + if (nodes.length == 1) + { + self.realNodeKey = nodes[0].raid; + self.send(JSON.stringify({ action: 'diagnostic', value: { command: 'query', value: self.realNodeKey } })); + } else + { + self.send(JSON.stringify({ action: 'diagnostic', value: { command: 'query', value: null } })); } - }); + }, obj); } function agentCoreIsStable() { @@ -1105,9 +1108,10 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { switch (command.value.command) { case 'register': { // Only main agent can do this - if (((obj.agentInfo.capabilities & 0x40) == 0) && (typeof command.value.command.value == 'string') && (command.value.command.value.length == 64)) { + if (((obj.agentInfo.capabilities & 0x40) == 0) && (typeof command.value.value == 'string') && (command.value.value.length == 64)) + { // Store links to diagnostic agent id - var daNodeKey = 'node/' + domain.id + '/' + command.value.command.value; + var daNodeKey = 'node/' + domain.id + '/' + db.escapeBase64(command.value.value); db.Set({ _id: 'da' + daNodeKey, domain: domain.id, time: obj.connectTime, raid: obj.dbNodeKey }); // DiagnosticAgent --> Agent db.Set({ _id: 'ra' + obj.dbNodeKey, domain: domain.id, time: obj.connectTime, daid: daNodeKey }); // Agent --> DiagnosticAgent }