From cc7670cc312d521bd4006064826b01c23cdbd1a5 Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Mon, 24 Oct 2022 16:58:05 -0700 Subject: [PATCH] Improved state-less server code (#4645) --- db.js | 99 +++++++------------------------------------------- meshcentral.js | 4 +- 2 files changed, 17 insertions(+), 86 deletions(-) diff --git a/db.js b/db.js index b8d109f5..ea8ecc6e 100644 --- a/db.js +++ b/db.js @@ -1542,20 +1542,6 @@ module.exports.CreateDB = function (parent, func) { // List all configuration files obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); } - // Get all configuration files (TODO: This is not SQL) - obj.getAllConfigFiles = function (password, func) { - obj.file.find({ type: 'cfile' }).toArray(function (err, docs) { - if (err != null) { func(null); return; } - var r = null; - for (var i = 0; i < docs.length; i++) { - var name = docs[i]._id.split('/')[1]; - var data = obj.decryptData(password, docs[i].data); - if (data != null) { if (r == null) { r = {}; } r[name] = data; } - } - func(r); - }); - } - // Get database information (TODO: Complete this) obj.getDbStats = function (func) { obj.stats = { c: 4 }; @@ -1796,21 +1782,6 @@ module.exports.CreateDB = function (parent, func) { }); } - // Get all configuration files - obj.getAllConfigFiles = function (password, func) { - obj.file.query('meshcentral').filter('type', '==', 'cfile').sort('_id').get(function (snapshots) { - const docs = []; - for (var i in snapshots) { docs.push(snapshots[i].val()); } - var r = null; - for (var i = 0; i < docs.length; i++) { - var name = docs[i]._id.split('/')[1]; - var data = obj.decryptData(password, docs[i].data); - if (data != null) { if (r == null) { r = {}; } r[name] = data; } - } - func(r); - }); - } - // Get database information obj.getDbStats = function (func) { obj.stats = { c: 5 }; @@ -1983,20 +1954,6 @@ module.exports.CreateDB = function (parent, func) { // List all configuration files obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); } - // Get all configuration files - obj.getAllConfigFiles = function (password, func) { - obj.file.find({ type: 'cfile' }).toArray(function (err, docs) { - if (err != null) { func(null); return; } - var r = null; - for (var i = 0; i < docs.length; i++) { - var name = docs[i]._id.split('/')[1]; - var data = obj.decryptData(password, docs[i].data); - if (data != null) { if (r == null) { r = {}; } r[name] = data; } - } - func(r); - }); - } - // Get database information (TODO: Complete this) obj.getDbStats = function (func) { obj.stats = { c: 4 }; @@ -2166,20 +2123,6 @@ module.exports.CreateDB = function (parent, func) { // List all configuration files obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); } - - // Get all configuration files - obj.getAllConfigFiles = function (password, func) { - obj.file.find({ type: 'cfile' }).toArray(function (err, docs) { - if (err != null) { func(null); return; } - var r = null; - for (var i = 0; i < docs.length; i++) { - var name = docs[i]._id.split('/')[1]; - var data = obj.decryptData(password, docs[i].data); - if (data != null) { if (r == null) { r = {}; } r[name] = data; } - } - func(r); - }); - } // Get database information (TODO: Complete this) obj.getDbStats = function (func) { @@ -2451,20 +2394,6 @@ module.exports.CreateDB = function (parent, func) { // List all configuration files obj.listConfigFiles = function (func) { obj.file.find({ type: 'cfile' }).sort({ _id: 1 }).toArray(func); } - // Get all configuration files - obj.getAllConfigFiles = function (password, func) { - obj.file.find({ type: 'cfile' }).toArray(function (err, docs) { - if (err != null) { func(null); return; } - var r = null; - for (var i = 0; i < docs.length; i++) { - var name = docs[i]._id.split('/')[1]; - var data = obj.decryptData(password, docs[i].data); - if (data != null) { if (r == null) { r = {}; } r[name] = data; } - } - func(r); - }); - } - // Get database information obj.getDbStats = function (func) { obj.stats = { c: 6 }; @@ -2635,20 +2564,6 @@ module.exports.CreateDB = function (parent, func) { // List all configuration files obj.listConfigFiles = function (func) { obj.file.find({ type: 'cfile' }).sort({ _id: 1 }).exec(func); } - // Get all configuration files - obj.getAllConfigFiles = function (password, func) { - obj.file.find({ type: 'cfile' }, function (err, docs) { - if (err != null) { func(null); return; } - var r = null; - for (var i = 0; i < docs.length; i++) { - var name = docs[i]._id.split('/')[1]; - var data = obj.decryptData(password, docs[i].data); - if (data != null) { if (r == null) { r = {}; } r[name] = data; } - } - func(r); - }); - } - // Get database information obj.getDbStats = function (func) { obj.stats = { c: 5 }; @@ -2682,6 +2597,20 @@ module.exports.CreateDB = function (parent, func) { } + // Get all configuration files + obj.getAllConfigFiles = function (password, func) { + obj.GetAllType('cfile', function (err, docs) { + if (err != null) { func(null); return; } + var r = null; + for (var i = 0; i < docs.length; i++) { + var name = docs[i]._id.split('/')[1]; + var data = obj.decryptData(password, docs[i].data); + if (data != null) { if (r == null) { r = {}; } r[name] = data; } + } + func(r); + }); + } + func(obj); // Completed function setup } diff --git a/meshcentral.js b/meshcentral.js index 126eefc8..8ed85fc8 100644 --- a/meshcentral.js +++ b/meshcentral.js @@ -1189,11 +1189,13 @@ function CreateMeshCentralServer(config, args) { obj.db.getAllConfigFiles(key, function (configFiles) { if (configFiles == null) { console.log("Error, no configuration files found or invalid configkey."); process.exit(); return; } if (!configFiles['config.json']) { console.log("Error, could not file config.json from database."); process.exit(); return; } + if (typeof configFiles['config.json'] == 'object') { configFiles['config.json'] = configFiles['config.json'].toString(); } + if (configFiles['config.json'].charCodeAt(0) == 65279) { configFiles['config.json'] = configFiles['config.json'].substring(1); } obj.configurationFiles = configFiles; // Parse the new configuration file var config2 = null; - try { config2 = JSON.parse(configFiles['config.json']); } catch (ex) { console.log('Error, unable to parse config.json from database.'); process.exit(); return; } + try { config2 = JSON.parse(configFiles['config.json']); } catch (ex) { console.log('Error, unable to parse config.json from database.', ex); process.exit(); return; } // Set the command line arguments to the config file if they are not present if (!config2.settings) { config2.settings = {}; }