Improve the way dynamic dependencies work

Properly verify the version without using hacks. Allows mc to ensure exact versions are installed. Also added otplib as a normal dependency.
This commit is contained in:
TotallyNotElite 2020-01-25 16:58:44 +01:00
parent 785cbd8f3a
commit 10207d04f4
2 changed files with 16 additions and 9 deletions

View File

@ -2168,20 +2168,27 @@ function getConfig(createSampleConfig) {
function InstallModules(modules, func) { function InstallModules(modules, func) {
var missingModules = []; var missingModules = [];
if (modules.length > 0) { if (modules.length > 0) {
var dependencies = require("./package.json").dependencies;
for (var i in modules) { for (var i in modules) {
// Modules may contain a version tag (foobar@1.0.0), remove it so the module can be found using require // Modules may contain a version tag (foobar@1.0.0), remove it so the module can be found using require
var moduleName = modules[i].split("@", 1)[0]; var moduleInfo = modules[i].split("@", 2);
var moduleName = moduleInfo[0];
var moduleVersion = moduleInfo[1];
try { try {
if (moduleName == 'greenlock') { // Does the module need a specific version?
// Check if we have GreenLock v3 if (moduleVersion) {
delete require.cache[require.resolve('greenlock')]; // Clear the require cache if (require(`${moduleName}/package.json`).version != moduleVersion)
if (typeof require('greenlock').challengeType == 'string') { missingModules.push(modules[i]); } throw new Error();
} else { }
else {
// For all other modules, do the check here. // For all other modules, do the check here.
// Is the module in package.json? Install exact version.
if (typeof dependencies[moduleName] != undefined)
moduleVersion = dependencies[moduleName];
require(moduleName); require(moduleName);
} }
} catch (e) { } catch (e) {
if (previouslyInstalledModules[modules[i]] !== true) { missingModules.push(modules[i]); } if (previouslyInstalledModules[modules[i]] !== true) { missingModules.push(`${moduleName}${moduleVersion ? `@${moduleVersion}` : ""}`); }
} }
} }
if (missingModules.length > 0) { InstallModule(missingModules.shift(), InstallModules, modules, func); } else { func(); } if (missingModules.length > 0) { InstallModule(missingModules.shift(), InstallModules, modules, func); } else { func(); }
@ -2197,7 +2204,7 @@ function InstallModule(modulename, func, tag1, tag2) {
// Get the working directory // Get the working directory
if ((__dirname.endsWith('/node_modules/meshcentral')) || (__dirname.endsWith('\\node_modules\\meshcentral')) || (__dirname.endsWith('/node_modules/meshcentral/')) || (__dirname.endsWith('\\node_modules\\meshcentral\\'))) { parentpath = require('path').join(__dirname, '../..'); } if ((__dirname.endsWith('/node_modules/meshcentral')) || (__dirname.endsWith('\\node_modules\\meshcentral')) || (__dirname.endsWith('/node_modules/meshcentral/')) || (__dirname.endsWith('\\node_modules\\meshcentral\\'))) { parentpath = require('path').join(__dirname, '../..'); }
child_process.exec(`npm install --no-optional ${modulename}`, { maxBuffer: 512000, timeout: 120000, cwd: parentpath }, function (error, stdout, stderr) { child_process.exec(`npm install --no-save --no-optional ${modulename}`, { maxBuffer: 512000, timeout: 120000, cwd: parentpath }, function (error, stdout, stderr) {
if ((error != null) && (error != '')) { if ((error != null) && (error != '')) {
console.log('ERROR: Unable to install required module "' + modulename + '". MeshCentral may not have access to npm, or npm may not have suffisent rights to load the new module. Try "npm install ' + modulename + '" to manualy install this module.\r\n'); console.log('ERROR: Unable to install required module "' + modulename + '". MeshCentral may not have access to npm, or npm may not have suffisent rights to load the new module. Try "npm install ' + modulename + '" to manualy install this module.\r\n');
process.exit(); process.exit();

View File

@ -38,11 +38,11 @@
"express-handlebars": "^3.1.0", "express-handlebars": "^3.1.0",
"express-ws": "^4.0.0", "express-ws": "^4.0.0",
"ipcheck": "^0.1.0", "ipcheck": "^0.1.0",
"meshcentral": "*",
"minimist": "^1.2.0", "minimist": "^1.2.0",
"multiparty": "^4.2.1", "multiparty": "^4.2.1",
"nedb": "^1.8.0", "nedb": "^1.8.0",
"node-forge": "^0.8.4", "node-forge": "^0.8.4",
"otplib": "^12.0.1",
"ws": "^6.2.1", "ws": "^6.2.1",
"xmldom": "^0.1.27", "xmldom": "^0.1.27",
"yauzl": "^2.10.0" "yauzl": "^2.10.0"