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

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

Bryan Roe committed Apr 12, 2019 at 11:28 UTC 041ab287a2196bd333fb3065dac782e9ddc6ee99
2 files changed +36 -10
db.js
+23 -1
@@ -281,7 +281,28 @@ module.exports.CreateDB = function (parent) {
281
282 // Database actions on the main collection
283 obj.Set = function (data, func) { obj.file.update({ _id: data._id }, data, { upsert: true }, func); };
284 - obj.Get = function (id, func) { obj.file.find({ _id: id }, func); };
284 + obj.Get = function (id, func)
285 + {
286 + if (arguments.length > 2)
287 + {
288 + var parms = [func];
289 + for (var parmx = 2; parmx < arguments.length; ++parmx) { parms.push(arguments[parmx]); }
290 + var func2 = function _func2(arg1, arg2)
291 + {
292 + console.log('callback');
293 + var userCallback = _func2.userArgs.shift();
294 + _func2.userArgs.unshift(arg2);
295 + _func2.userArgs.unshift(arg1);
296 + userCallback.apply(obj, _func2.userArgs);
297 + };
298 + func2.userArgs = parms;
299 + obj.file.find({ _id: id }, func2);
300 + }
301 + else
302 + {
303 + obj.file.find({ _id: id }, func);
304 + }
305 + };
306 obj.GetAll = function (func) { obj.file.find({}, func); };
307 obj.GetAllTypeNoTypeField = function (type, domain, func) { obj.file.find({ type: type, domain: domain }, { type: 0 }, func); };
308 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) {
443
444 // 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.
445 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 }); }); };
446 + obj.escapeBase64 = function escapeBase64(val) { return (val.replace(/\+/g, '@').replace(/\//g, '$')); }
447
448 function Clone(v) { return JSON.parse(JSON.stringify(v)); }
449
meshagent.js
+13 -9
@@ -773,14 +773,17 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
773 //console.log('recoveryAgentCoreIsStable()');
774
775 // Fetch the the real agent nodeid
776 - db.Get('da' + obj.dbNodeKey, function (err, nodes) {
777 - if (nodes.length == 1) {
778 - obj.realNodeKey = nodes[0].raid;
779 - obj.send(JSON.stringify({ action: 'diagnostic', value: { command: 'query', value: obj.realNodeKey } }));
780 - } else {
781 - obj.send(JSON.stringify({ action: 'diagnostic', value: { command: 'query', value: null } }));
776 + db.Get('da' + obj.dbNodeKey, function (err, nodes, self)
777 + {
778 + if (nodes.length == 1)
779 + {
780 + self.realNodeKey = nodes[0].raid;
781 + self.send(JSON.stringify({ action: 'diagnostic', value: { command: 'query', value: self.realNodeKey } }));
782 + } else
783 + {
784 + self.send(JSON.stringify({ action: 'diagnostic', value: { command: 'query', value: null } }));
785 }
783 - });
786 + }, obj);
787 }
788
789 function agentCoreIsStable() {
@@ -1105,9 +1108,10 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1108 switch (command.value.command) {
1109 case 'register': {
1110 // Only main agent can do this
1108 - if (((obj.agentInfo.capabilities & 0x40) == 0) && (typeof command.value.command.value == 'string') && (command.value.command.value.length == 64)) {
1111 + if (((obj.agentInfo.capabilities & 0x40) == 0) && (typeof command.value.value == 'string') && (command.value.value.length == 64))
1112 + {
1113 // Store links to diagnostic agent id
1110 - var daNodeKey = 'node/' + domain.id + '/' + command.value.command.value;
1114 + var daNodeKey = 'node/' + domain.id + '/' + db.escapeBase64(command.value.value);
1115 db.Set({ _id: 'da' + daNodeKey, domain: domain.id, time: obj.connectTime, raid: obj.dbNodeKey }); // DiagnosticAgent --> Agent
1116 db.Set({ _id: 'ra' + obj.dbNodeKey, domain: domain.id, time: obj.connectTime, daid: daNodeKey }); // Agent --> DiagnosticAgent
1117 }