SMBios improvements

Ylian Saint-Hilaire committed Dec 9, 2019 at 11:30 UTC 2494f1b1d4f04fd49264964c15205ea9f4578921
4 files changed +25 -78
db.js
+19 -71
@@ -650,10 +650,7 @@ module.exports.CreateDB = function (parent, func) {
650 function setupFunctions(func) {
651 if (obj.databaseType == 3) {
652 // Database actions on the main collection (MongoDB)
653 - obj.Set = function (data, func) {
654 - checkObjectNames(data, 'x1'); // DEBUG CHECKING
655 - obj.file.replaceOne({ _id: data._id }, performTypedRecordEncrypt(data), { upsert: true }, func);
656 - };
653 + obj.Set = function (data, func) { obj.file.replaceOne({ _id: data._id }, performTypedRecordEncrypt(data), { upsert: true }, func); };
654 obj.Get = function (id, func) {
655 if (arguments.length > 2) {
656 var parms = [func];
@@ -681,25 +678,11 @@ module.exports.CreateDB = function (parent, func) {
678 obj.Remove = function (id) { obj.file.deleteOne({ _id: id }); };
679 obj.RemoveAll = function (func) { obj.file.deleteMany({}, { multi: true }, func); };
680 obj.RemoveAllOfType = function (type, func) { obj.file.deleteMany({ type: type }, { multi: true }, func); };
684 - obj.InsertMany = function (data, func) {
685 - checkObjectNames(data, 'x2'); // DEBUG CHECKING
686 - obj.file.insertMany(data, func);
687 - };
681 + obj.InsertMany = function (data, func) { obj.file.insertMany(data, func); };
682 obj.RemoveMeshDocuments = function (id) { obj.file.deleteMany({ meshid: id }, { multi: true }); obj.file.deleteOne({ _id: 'nt' + id }); };
689 - obj.MakeSiteAdmin = function (username, domain) {
690 - obj.Get('user/' + domain + '/' + username, function (err, docs) {
691 - if (docs.length == 1) {
692 - checkObjectNames(docs[0], 'x3'); // DEBUG CHECKING
693 - docs[0].siteadmin = 0xFFFFFFFF; obj.Set(docs[0]);
694 - }
695 - });
696 - };
683 + obj.MakeSiteAdmin = function (username, domain) { obj.Get('user/' + domain + '/' + username, function (err, docs) { if (docs.length == 1) { docs[0].siteadmin = 0xFFFFFFFF; obj.Set(docs[0]); } }); };
684 obj.DeleteDomain = function (domain, func) { obj.file.deleteMany({ domain: domain }, { multi: true }, func); };
698 - obj.SetUser = function (user) {
699 - checkObjectNames(user, 'x4'); // DEBUG CHECKING
700 - var u = Clone(user);
701 - if (u.subscriptions) { delete u.subscriptions; } obj.Set(u);
702 - };
685 + obj.SetUser = function (user) { if (u.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
686 obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
687 obj.getLocalAmtNodes = function (func) { obj.file.find({ type: 'node', host: { $exists: true, $ne: null }, intelamt: { $exists: true } }).toArray(func); };
688 obj.getAmtUuidMeshNode = function (meshid, uuid, func) { obj.file.find({ type: 'node', meshid: meshid, 'intelamt.uuid': uuid }).toArray(func); };
@@ -740,28 +723,19 @@ module.exports.CreateDB = function (parent, func) {
723
724 // Database actions on the power collection
725 obj.getAllPower = function (func) { obj.powerfile.find({}).toArray(func); };
743 - obj.storePowerEvent = function (event, multiServer, func) {
744 - checkObjectNames(event, 'x6'); // DEBUG CHECKING
745 - if (multiServer != null) { event.server = multiServer.serverid; } obj.powerfile.insertOne(event, func);
746 - };
726 + obj.storePowerEvent = function (event, multiServer, func) { if (multiServer != null) { event.server = multiServer.serverid; } obj.powerfile.insertOne(event, func); };
727 obj.getPowerTimeline = function (nodeid, func) { obj.powerfile.find({ nodeid: { $in: ['*', nodeid] } }).project({ _id: 0, nodeid: 0, s: 0 }).sort({ time: 1 }).toArray(func); };
728 obj.removeAllPowerEvents = function () { obj.powerfile.deleteMany({}, { multi: true }); };
729 obj.removeAllPowerEventsForNode = function (nodeid) { obj.powerfile.deleteMany({ nodeid: nodeid }, { multi: true }); };
730
731 // Database actions on the SMBIOS collection
732 obj.GetAllSMBIOS = function (func) { obj.smbiosfile.find({}).toArray(func); };
753 - obj.SetSMBIOS = function (smbios, func) {
754 - checkObjectNames(smbios, 'x7'); // DEBUG CHECKING
755 - obj.smbiosfile.updateOne({ _id: smbios._id }, { $set: smbios }, { upsert: true }, func);
756 - };
733 + obj.SetSMBIOS = function (smbios, func) { obj.smbiosfile.updateOne({ _id: smbios._id }, { $set: smbios }, { upsert: true }, func); };
734 obj.RemoveSMBIOS = function (id) { obj.smbiosfile.deleteOne({ _id: id }); };
735 obj.GetSMBIOS = function (id, func) { obj.smbiosfile.find({ _id: id }).toArray(func); };
736
737 // Database actions on the Server Stats collection
761 - obj.SetServerStats = function (data, func) {
762 - checkObjectNames(data, 'x8'); // DEBUG CHECKING
763 - obj.serverstatsfile.insertOne(data, func);
764 - };
738 + obj.SetServerStats = function (data, func) { obj.serverstatsfile.insertOne(data, func); };
739 obj.GetServerStats = function (hours, func) { var t = new Date(); t.setTime(t.getTime() - (60 * 60 * 1000 * hours)); obj.serverstatsfile.find({ time: { $gt: t } }, { _id: 0, cpu: 0 }).toArray(func); };
740
741 // Read a configuration file from the database
@@ -791,11 +765,11 @@ module.exports.CreateDB = function (parent, func) {
765 obj.getDbStats = function (func) {
766 obj.stats = { c: 6 };
767 obj.getStats(function (r) { obj.stats.recordTypes = r; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } })
794 - obj.file.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, );
795 - obj.eventsfile.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, );
796 - obj.powerfile.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, );
797 - obj.smbiosfile.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, );
798 - obj.serverstatsfile.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, );
768 + obj.file.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } } );
769 + obj.eventsfile.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } } );
770 + obj.powerfile.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } } );
771 + obj.smbiosfile.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } } );
772 + obj.serverstatsfile.stats().then(function (stats) { obj.stats[stats.ns] = { size: stats.size, count: stats.count, avgObjSize: stats.avgObjSize, capped: stats.capped }; if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } }, function () { if (--obj.stats.c == 0) { delete obj.stats.c; func(obj.stats); } } );
773 }
774
775 // Plugin operations
@@ -810,11 +784,7 @@ module.exports.CreateDB = function (parent, func) {
784
785 } else {
786 // Database actions on the main collection (NeDB and MongoJS)
813 - obj.Set = function (data, func) {
814 - checkObjectNames(data, 'x9'); // DEBUG CHECKING
815 - var xdata = performTypedRecordEncrypt(data);
816 - obj.file.update({ _id: xdata._id }, xdata, { upsert: true }, func);
817 - };
787 + obj.Set = function (data, func) { var xdata = performTypedRecordEncrypt(data); obj.file.update({ _id: xdata._id }, xdata, { upsert: true }, func); };
788 obj.Get = function (id, func) {
789 if (arguments.length > 2) {
790 var parms = [func];
@@ -842,24 +812,11 @@ module.exports.CreateDB = function (parent, func) {
812 obj.Remove = function (id) { obj.file.remove({ _id: id }); };
813 obj.RemoveAll = function (func) { obj.file.remove({}, { multi: true }, func); };
814 obj.RemoveAllOfType = function (type, func) { obj.file.remove({ type: type }, { multi: true }, func); };
845 - obj.InsertMany = function (data, func) {
846 - checkObjectNames(data, 'x10'); // DEBUG CHECKING
847 - obj.file.insert(data, func);
848 - };
815 + obj.InsertMany = function (data, func) { obj.file.insert(data, func); };
816 obj.RemoveMeshDocuments = function (id) { obj.file.remove({ meshid: id }, { multi: true }); obj.file.remove({ _id: 'nt' + id }); };
850 - obj.MakeSiteAdmin = function (username, domain) {
851 - obj.Get('user/' + domain + '/' + username, function (err, docs) {
852 - if (docs.length == 1) {
853 - checkObjectNames(docs[0], 'x11'); // DEBUG CHECKING
854 - docs[0].siteadmin = 0xFFFFFFFF; obj.Set(docs[0]);
855 - }
856 - });
857 - };
817 + obj.MakeSiteAdmin = function (username, domain) { obj.Get('user/' + domain + '/' + username, function (err, docs) { if (docs.length == 1) { docs[0].siteadmin = 0xFFFFFFFF; obj.Set(docs[0]); } }); };
818 obj.DeleteDomain = function (domain, func) { obj.file.remove({ domain: domain }, { multi: true }, func); };
859 - obj.SetUser = function (user) {
860 - checkObjectNames(user, 'x12'); // DEBUG CHECKING
861 - var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u);
862 - };
819 + obj.SetUser = function (user) { if (u.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
820 obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
821 obj.getLocalAmtNodes = function (func) { obj.file.find({ type: 'node', host: { $exists: true, $ne: null }, intelamt: { $exists: true } }, func); };
822 obj.getAmtUuidMeshNode = function (meshid, uuid, func) { obj.file.find({ type: 'node', meshid: meshid, 'intelamt.uuid': uuid }, func); };
@@ -868,10 +825,7 @@ module.exports.CreateDB = function (parent, func) {
825
826 // Database actions on the events collection
827 obj.GetAllEvents = function (func) { obj.eventsfile.find({}, func); };
871 - obj.StoreEvent = function (event) {
872 - checkObjectNames(event, 'x13'); // DEBUG CHECKING
873 - obj.eventsfile.insert(event);
874 - };
828 + obj.StoreEvent = function (event) { obj.eventsfile.insert(event); };
829 obj.GetEvents = function (ids, domain, func) { if (obj.databaseType == 1) { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }, { _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).exec(func); } else { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }, func); } };
830 obj.GetEventsWithLimit = function (ids, domain, limit, func) { if (obj.databaseType == 1) { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }, { _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit).exec(func); } else { obj.eventsfile.find({ domain: domain, ids: { $in: ids } }, { type: 0, _id: 0, domain: 0, ids: 0, node: 0 }).sort({ time: -1 }).limit(limit, func); } };
831 obj.GetUserEvents = function (ids, domain, username, func) {
@@ -896,10 +850,7 @@ module.exports.CreateDB = function (parent, func) {
850
851 // Database actions on the power collection
852 obj.getAllPower = function (func) { obj.powerfile.find({}, func); };
899 - obj.storePowerEvent = function (event, multiServer, func) {
900 - checkObjectNames(event, 'x14'); // DEBUG CHECKING
901 - if (multiServer != null) { event.server = multiServer.serverid; } obj.powerfile.insert(event, func);
902 - };
853 + obj.storePowerEvent = function (event, multiServer, func) { if (multiServer != null) { event.server = multiServer.serverid; } obj.powerfile.insert(event, func); };
854 obj.getPowerTimeline = function (nodeid, func) { if (obj.databaseType == 1) { obj.powerfile.find({ nodeid: { $in: ['*', nodeid] } }, { _id: 0, nodeid: 0, s: 0 }).sort({ time: 1 }).exec(func); } else { obj.powerfile.find({ nodeid: { $in: ['*', nodeid] } }, { _id: 0, nodeid: 0, s: 0 }).sort({ time: 1 }, func); } };
855 obj.removeAllPowerEvents = function () { obj.powerfile.remove({}, { multi: true }); };
856 obj.removeAllPowerEventsForNode = function (nodeid) { obj.powerfile.remove({ nodeid: nodeid }, { multi: true }); };
@@ -911,10 +862,7 @@ module.exports.CreateDB = function (parent, func) {
862 obj.GetSMBIOS = function (id, func) { obj.smbiosfile.find({ _id: id }, func); };
863
864 // Database actions on the Server Stats collection
914 - obj.SetServerStats = function (data, func) {
915 - checkObjectNames(data, 'x15'); // DEBUG CHECKING
916 - obj.serverstatsfile.insert(data, func);
917 - };
865 + obj.SetServerStats = function (data, func) { obj.serverstatsfile.insert(data, func); };
866 obj.GetServerStats = function (hours, func) { var t = new Date(); t.setTime(t.getTime() - (60 * 60 * 1000 * hours)); obj.serverstatsfile.find({ time: { $gt: t } }, { _id: 0, cpu: 0 }, func); };
867
868 // Read a configuration file from the database
meshagent.js
+3 -4
@@ -1092,11 +1092,10 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1092 case 'smbios':
1093 {
1094 // Store the RAW SMBios table of this computer
1095 - // We store SMBIOS information as a string because we don't want the MongoDB to attempt to store all of the sub-documents seperatly.
1096 - // If an agent sends an insanely large SMBIOS table, don't store it.
1095 + // Perform sanity checks before storing
1096 try {
1098 - var smbiosstr = JSON.stringify(command.value);
1099 - if (smbiosstr.length < 65535) { db.SetSMBIOS({ _id: obj.dbNodeKey, domain: domain.id, time: new Date(), value: smbiosstr }); }
1097 + for (var i in command.value) { var k = parseInt(i); if ((k != i) || (i > 255) || (typeof command.value[i] != 'object') || (command.value[i].length == null) || (command.value[i].length > 1024) || (command.value[i].length < 0)) { delete command.value[i]; } }
1098 + db.SetSMBIOS({ _id: obj.dbNodeKey, domain: domain.id, time: new Date(), value: command.value });
1099 } catch (ex) { }
1100
1101 // Event the node interface information change (This is a lot of traffic, probably don't need this).
meshrelay.js
+1 -1
@@ -222,7 +222,7 @@ module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie
222 try { relayinfo.peer1.ws.send('c'); } catch (ex) { }
223 } else {
224 // Write the recording file header
225 - var metadata = { magic: 'MeshCentralRelaySession', ver: 1, userid: sessionUser._id, username: sessionUser.name, sessionid: obj.id, ipaddr1: cleanRemoteAddr(req.ip), ipaddr2: cleanRemoteAddr(obj.peer.req.ip), time: new Date().toLocaleString(), protocol: req.query.p, nodeid: req.query.nodeid };
225 + var metadata = { magic: 'MeshCentralRelaySession', ver: 1, userid: sessionUser._id, username: sessionUser.name, sessionid: obj.id, ipaddr1: cleanRemoteAddr(req.ip), ipaddr2: cleanRemoteAddr(obj.peer.req.ip), time: new Date().toLocaleString(), protocol: (((req == null) || (req.query == null)) ? null : req.query.p), nodeid: (((req == null) || (req.query == null)) ? null : req.query.nodeid ) };
226 if (xdevicename2 != null) { metadata.devicename = xdevicename2; }
227 var firstBlock = JSON.stringify(metadata);
228 recordingEntry(fd, 1, ((req.query.browser) ? 2 : 0), firstBlock, function () {
webserver.js
+2 -2
@@ -1357,7 +1357,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1357 // Indicates that any request to "/" should render "default" or "login" depending on login state
1358 function handleRootRequest(req, res, direct) {
1359 const domain = checkUserIpAddress(req, res);
1360 - if (domain == null) { parent.debug('web', 'handleRootRequest: invalid domain.'); res.sendStatus(404); return; }
1360 + if (domain == null) { parent.debug('web', 'handleRootRequest: invalid domain.'); try { res.sendStatus(404); } catch (ex) { } return; }
1361 if ((domain.loginkey != null) && (domain.loginkey.indexOf(req.query.key) == -1)) { res.sendStatus(404); return; } // Check 3FA URL key
1362 if (!obj.args) { parent.debug('web', 'handleRootRequest: no obj.args.'); res.sendStatus(500); return; }
1363
@@ -2910,7 +2910,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
2910 // Require modules
2911 const archive = require('archiver')('zip', { level: 9 }); // Sets the compression method to maximum.
2912
2913 - // Good practice to catch this error explicitly
2913 + // Good practice to catch this error explicitly
2914 archive.on('error', function (err) { throw err; });
2915
2916 // Set the archive name