EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
pluginHandler.js
Go to the documentation of this file.
1/**
2* @description MeshCentral plugin module
3* @author Ryan Blenis
4* @copyright
5* @license Apache-2.0
6* @version v0.0.1
7*/
8
9/*xjslint node: true */
10/*xjslint plusplus: true */
11/*xjslint maxlen: 256 */
12/*jshint node: true */
13/*jshint strict: false */
14/*jshint esversion: 6 */
15"use strict";
16
17/*
18Existing plugins:
19https://raw.githubusercontent.com/ryanblenis/MeshCentral-Sample/master/config.json
20https://raw.githubusercontent.com/ryanblenis/MeshCentral-DevTools/master/config.json
21*/
22
23
24module.exports.pluginHandler = function (parent) {
25 var obj = {};
26
27 obj.fs = require('fs');
28 obj.path = require('path');
29 obj.common = require('./common.js');
30 obj.parent = parent;
31 obj.pluginPath = obj.parent.path.join(obj.parent.datapath, 'plugins');
32 obj.plugins = {};
33 obj.exports = {};
34 obj.loadList = obj.parent.config.settings.plugins.list; // For local development / manual install, not from DB
35
36 if (typeof obj.loadList != 'object') {
37 obj.loadList = {};
38 parent.db.getPlugins(function (err, plugins) {
39 plugins.forEach(function (plugin) {
40 if (plugin.status != 1) return;
41 if (obj.fs.existsSync(obj.pluginPath + '/' + plugin.shortName)) {
42 try {
43 obj.plugins[plugin.shortName] = require(obj.pluginPath + '/' + plugin.shortName + '/' + plugin.shortName + '.js')[plugin.shortName](obj);
44 obj.exports[plugin.shortName] = obj.plugins[plugin.shortName].exports;
45 } catch (e) {
46 console.log("Error loading plugin: " + plugin.shortName + " (" + e + "). It has been disabled.", e.stack);
47 }
48 try { // try loading local info about plugin to database (if it changed locally)
49 var plugin_config = obj.fs.readFileSync(obj.pluginPath + '/' + plugin.shortName + '/config.json');
50 plugin_config = JSON.parse(plugin_config);
51 parent.db.updatePlugin(plugin._id, plugin_config);
52 } catch (e) { console.log("Plugin config file for " + plugin.name + " could not be parsed."); }
53 }
54 });
55 obj.parent.updateMeshCore(); // db calls are async, lets inject here once we're ready
56 });
57 } else {
58 obj.loadList.forEach(function (plugin, index) {
59 if (obj.fs.existsSync(obj.pluginPath + '/' + plugin)) {
60 try {
61 obj.plugins[plugin] = require(obj.pluginPath + '/' + plugin + '/' + plugin + '.js')[plugin](obj);
62 obj.exports[plugin] = obj.plugins[plugin].exports;
63 } catch (e) {
64 console.log("Error loading plugin: " + plugin + " (" + e + "). It has been disabled.", e.stack);
65 }
66 }
67 });
68 }
69
70 obj.prepExports = function () {
71 var str = 'function() {\r\n';
72 str += ' var obj = {};\r\n';
73
74 for (var p of Object.keys(obj.plugins)) {
75 str += ' obj.' + p + ' = {};\r\n';
76 if (Array.isArray(obj.exports[p])) {
77 for (var l of Object.values(obj.exports[p])) {
78 str += ' obj.' + p + '.' + l + ' = ' + obj.plugins[p][l].toString() + '\r\n';
79 }
80 }
81 }
82
83 str += `
84 obj.callHook = function(hookName, ...args) {
85 for (const p of Object.keys(obj)) {
86 if (typeof obj[p][hookName] == 'function') {
87 obj[p][hookName].apply(this, args);
88 }
89 }
90 };
91 // accepts a function returning an object or an object with { tabId: "yourTabIdValue", tabTitle: "Your Tab Title" }
92 obj.registerPluginTab = function(pluginRegInfo) {
93 var d = null;
94 if (typeof pluginRegInfo == 'function') d = pluginRegInfo();
95 else d = pluginRegInfo;
96 if (d.tabId == null || d.tabTitle == null) { return false; }
97 if (!Q(d.tabId)) {
98 var defaultOn = 'class="on"';
99 if (Q('p19headers').querySelectorAll("span.on").length) defaultOn = '';
100 QA('p19headers', '<span ' + defaultOn + ' id="p19ph-' + d.tabId + '" onclick="return pluginHandler.callPluginPage(\\''+d.tabId+'\\', this);">'+d.tabTitle+'</span>');
101 QA('p19pages', '<div id="' + d.tabId + '"></div>');
102 }
103 QV('MainDevPlugins', true);
104 };
105 obj.callPluginPage = function(id, el) {
106 var pages = Q('p19pages').querySelectorAll("#p19pages>div");
107 for (const i of pages) { i.style.display = 'none'; }
108 QV(id, true);
109 var tabs = Q('p19headers').querySelectorAll("span");
110 for (const i of tabs) { i.classList.remove('on'); }
111 el.classList.add('on');
112 putstore('_curPluginPage', id);
113 };
114 obj.addPluginEx = function() {
115 meshserver.send({ action: 'addplugin', url: Q('pluginurlinput').value});
116 };
117 obj.addPluginDlg = function() {
118 if (typeof showModal === 'function') {
119 setDialogMode(2, "Plugin Download URL", 3, obj.addPluginEx, '<p><b>WARNING:</b> Downloading plugins may compromise server security. Only download from trusted sources.</p><input type=text id=pluginurlinput style=width:100% placeholder="https://" />');
120 showModal('xxAddAgentModal', 'idx_dlgOkButton', obj.addPluginEx);
121 focusTextBox('pluginurlinput');
122 } else {
123 // Fallback to setDialogMode for default.handlebars
124 setDialogMode(2, "Plugin Download URL", 3, obj.addPluginEx, '<p><b>WARNING:</b> Downloading plugins may compromise server security. Only download from trusted sources.</p><input type=text id=pluginurlinput style=width:100% placeholder="https://" />');
125 focusTextBox('pluginurlinput');
126 }
127 };
128 obj.refreshPluginHandler = function() {
129 let st = document.createElement('script');
130 st.src = '/pluginHandler.js';
131 document.body.appendChild(st);
132 };
133 return obj; }`;
134 return str;
135 }
136
137 obj.refreshJS = function (req, res) {
138 // to minimize server reboots when installing new plugins, we call the new data and overwrite the old pluginHandler on the front end
139 res.set('Content-Type', 'text/javascript');
140 res.send('pluginHandlerBuilder = ' + obj.prepExports() + '\r\n' + ' pluginHandler = new pluginHandlerBuilder(); pluginHandler.callHook("onWebUIStartupEnd");');
141 }
142
143 obj.callHook = function (hookName, ...args) {
144 for (var p in obj.plugins) {
145 if (typeof obj.plugins[p][hookName] == 'function') {
146 try {
147 obj.plugins[p][hookName](...args);
148 } catch (e) {
149 console.log("Error occurred while running plugin hook " + p + ':' + hookName, e);
150 }
151 }
152 }
153 };
154
155 obj.addMeshCoreModules = function (modulesAdd) {
156 for (var plugin in obj.plugins) {
157 var moduleDirPath = null;
158 var modulesDir = null;
159 //if (obj.args.minifycore !== false) { try { moduleDirPath = obj.path.join(obj.pluginPath, 'modules_meshcore_min'); modulesDir = obj.fs.readdirSync(moduleDirPath); } catch (e) { } } // Favor minified modules if present.
160 if (modulesDir == null) { try { moduleDirPath = obj.path.join(obj.pluginPath, plugin + '/modules_meshcore'); modulesDir = obj.fs.readdirSync(moduleDirPath); } catch (e) { } } // Use non-minified mofules.
161 if (modulesDir != null) {
162 for (var i in modulesDir) {
163 if (modulesDir[i].toLowerCase().endsWith('.js')) {
164 var moduleName = modulesDir[i].substring(0, modulesDir[i].length - 3);
165 if (moduleName.endsWith('.min')) { moduleName = moduleName.substring(0, moduleName.length - 4); } // Remove the ".min" for ".min.js" files.
166 var moduleData = ['try { addModule("', moduleName, '", "', obj.parent.escapeCodeString(obj.fs.readFileSync(obj.path.join(moduleDirPath, modulesDir[i])).toString('binary')), '"); addedModules.push("', moduleName, '"); } catch (e) { }\r\n'];
167
168 // Merge this module
169 // NOTE: "smbios" module makes some non-AI Linux segfault, only include for IA platforms.
170 if (moduleName.startsWith('amt-') || (moduleName == 'smbios')) {
171 // Add to IA / Intel AMT cores only
172 modulesAdd['windows-amt'].push(...moduleData);
173 modulesAdd['linux-amt'].push(...moduleData);
174 } else if (moduleName.startsWith('win-')) {
175 // Add to Windows cores only
176 modulesAdd['windows-amt'].push(...moduleData);
177 } else if (moduleName.startsWith('linux-')) {
178 // Add to Linux cores only
179 modulesAdd['linux-amt'].push(...moduleData);
180 modulesAdd['linux-noamt'].push(...moduleData);
181 } else {
182 // Add to all cores
183 modulesAdd['windows-amt'].push(...moduleData);
184 modulesAdd['linux-amt'].push(...moduleData);
185 modulesAdd['linux-noamt'].push(...moduleData);
186 }
187
188 // Merge this module to recovery modules if needed
189 if (modulesAdd['windows-recovery'] != null) {
190 if ((moduleName == 'win-console') || (moduleName == 'win-message-pump') || (moduleName == 'win-terminal')) {
191 modulesAdd['windows-recovery'].push(...moduleData);
192 }
193 }
194
195 // Merge this module to agent recovery modules if needed
196 if (modulesAdd['windows-agentrecovery'] != null) {
197 if ((moduleName == 'win-console') || (moduleName == 'win-message-pump') || (moduleName == 'win-terminal')) {
198 modulesAdd['windows-agentrecovery'].push(...moduleData);
199 }
200 }
201 }
202 }
203 }
204 }
205 };
206
207 obj.deviceViewPanel = function () {
208 var panel = {};
209 for (var p in obj.plugins) {
210 if (typeof obj.plugins[p].on_device_header === "function" && typeof obj.plugins[p].on_device_page === "function") {
211 try {
212 panel[p] = {
213 header: obj.plugins[p].on_device_header(),
214 content: obj.plugins[p].on_device_page()
215 };
216 } catch (e) {
217 console.log("Error occurred while getting plugin views " + p + ':' + ' (' + e + ')');
218 }
219 }
220 }
221 return panel;
222 };
223
224 obj.isValidConfig = function (conf, url) { // check for the required attributes
225 var isValid = true;
226 if (!(
227 typeof conf.name == 'string'
228 && typeof conf.shortName == 'string'
229 && typeof conf.version == 'string'
230 // && typeof conf.author == 'string'
231 && typeof conf.description == 'string'
232 && typeof conf.hasAdminPanel == 'boolean'
233 && typeof conf.homepage == 'string'
234 && typeof conf.changelogUrl == 'string'
235 && typeof conf.configUrl == 'string'
236 && typeof conf.repository == 'object'
237 && typeof conf.repository.type == 'string'
238 && typeof conf.repository.url == 'string'
239 && typeof conf.meshCentralCompat == 'string'
240 // && conf.configUrl == url // make sure we're loading a plugin from its desired config
241 )) isValid = false;
242 // more checks here?
243 if (conf.repository.type == 'git') {
244 if (typeof conf.downloadUrl != 'string') isValid = false;
245 }
246 return isValid;
247 };
248
249 // https://raw.githubusercontent.com/ryanblenis/MeshCentral-Sample/master/config.json
250 obj.getPluginConfig = function (configUrl) {
251 return new Promise(function (resolve, reject) {
252 var http = (configUrl.indexOf('https://') >= 0) ? require('https') : require('http');
253 if (configUrl.indexOf('://') === -1) reject("Unable to fetch the config: Bad URL (" + configUrl + ")");
254 var options = require('url').parse(configUrl);
255 if (typeof parent.config.settings.plugins.proxy == 'string') { // Proxy support
256 const HttpsProxyAgent = require('https-proxy-agent');
257 options.agent = new HttpsProxyAgent(require('url').parse(parent.config.settings.plugins.proxy));
258 }
259 http.get(options, function (res) {
260 var configStr = '';
261 res.on('data', function (chunk) {
262 configStr += chunk;
263 });
264 res.on('end', function () {
265 if (configStr[0] == '{') { // Let's be sure we're JSON
266 try {
267 var pluginConfig = JSON.parse(configStr);
268 if (Array.isArray(pluginConfig) && pluginConfig.length == 1) pluginConfig = pluginConfig[0];
269 if (obj.isValidConfig(pluginConfig, configUrl)) {
270 resolve(pluginConfig);
271 } else {
272 reject("This does not appear to be a valid plugin configuration.");
273 }
274 } catch (e) { reject("Error getting plugin config. Check that you have valid JSON."); }
275 } else {
276 reject("Error getting plugin config. Check that you have valid JSON.");
277 }
278 });
279
280 }).on('error', function (e) {
281 reject("Error getting plugin config: " + e.message);
282 });
283 })
284 };
285
286 // MeshCentral now adheres to semver, drop the -<alpha> off the version number for later versions for comparing plugins prior to this change
287 obj.versionToNumber = function(ver) { var x = ver.split('-'); if (x.length != 2) return ver; return x[0]; }
288
289 // Check if the current version of MeshCentral is at least the minimal required.
290 obj.versionCompare = function(current, minimal) {
291 if (minimal.startsWith('>=')) { minimal = minimal.substring(2); }
292 var c = obj.versionToNumber(current).split('.'), m = obj.versionToNumber(minimal).split('.');
293 if (c.length != m.length) return false;
294 for (var i = 0; i < c.length; i++) { var cx = parseInt(c[i]), cm = parseInt(m[i]); if (cx > cm) { return true; } if (cx < cm) { return false; } }
295 return true;
296 }
297
298 obj.versionGreater = function(a, b) {
299 a = obj.versionToNumber(String(a).replace(/^v/, ''));
300 b = obj.versionToNumber(String(b).replace(/^v/, ''));
301 const partsA = a.split('.').map(Number);
302 const partsB = b.split('.').map(Number);
303
304 for (let i = 0; i < Math.max(partsA.length, partsB.length); i++) {
305 const numA = partsA[i] || 0;
306 const numB = partsB[i] || 0;
307 if (numA > numB) return true;
308 if (numA < numB) return false;
309 }
310 return false;
311 };
312
313 obj.versionLower = function(a, b) {
314 a = obj.versionToNumber(String(a).replace(/^v/, ''));
315 b = obj.versionToNumber(String(b).replace(/^v/, ''));
316 const partsA = a.split('.').map(Number);
317 const partsB = b.split('.').map(Number);
318
319 for (let i = 0; i < Math.max(partsA.length, partsB.length); i++) {
320 const numA = partsA[i] || 0;
321 const numB = partsB[i] || 0;
322 if (numA < numB) return true;
323 if (numA > numB) return false;
324 }
325 return false;
326 };
327
328 obj.getPluginLatest = function () {
329 return new Promise(function (resolve, reject) {
330 parent.db.getPlugins(function (err, plugins) {
331 var proms = [];
332 plugins.forEach(function (curconf) {
333 proms.push(obj.getPluginConfig(curconf.configUrl).catch(e => { return null; }));
334 });
335 var latestRet = [];
336 Promise.all(proms).then(function (newconfs) {
337 var nconfs = [];
338 // Filter out config download issues
339 newconfs.forEach(function (nc) { if (nc !== null) nconfs.push(nc); });
340 if (nconfs.length == 0) { resolve([]); } else {
341 nconfs.forEach(function (newconf) {
342 var curconf = null;
343 plugins.forEach(function (conf) {
344 if (conf.configUrl == newconf.configUrl) curconf = conf;
345 });
346 if (curconf == null) reject("Some plugin configs could not be parsed");
347 latestRet.push({
348 'id': curconf._id,
349 'installedVersion': curconf.version,
350 'version': newconf.version,
351 'hasUpdate': obj.versionGreater(newconf.version, curconf.version),
352 'meshCentralCompat': obj.versionCompare(parent.currentVer, newconf.meshCentralCompat),
353 'changelogUrl': curconf.changelogUrl,
354 'status': curconf.status
355 });
356 resolve(latestRet);
357 });
358 }
359 }).catch((e) => { console.log("Error reaching plugins, update call aborted.", e) });
360 });
361 });
362 };
363
364 obj.addPlugin = function (pluginConfig) {
365 return new Promise(function (resolve, reject) {
366 parent.db.addPlugin({
367 'name': pluginConfig.name,
368 'shortName': pluginConfig.shortName,
369 'version': pluginConfig.version,
370 'description': pluginConfig.description,
371 'hasAdminPanel': pluginConfig.hasAdminPanel,
372 'homepage': pluginConfig.homepage,
373 'changelogUrl': pluginConfig.changelogUrl,
374 'configUrl': pluginConfig.configUrl,
375 'downloadUrl': pluginConfig.downloadUrl,
376 'repository': {
377 'type': pluginConfig.repository.type,
378 'url': pluginConfig.repository.url
379 },
380 'meshCentralCompat': pluginConfig.meshCentralCompat,
381 'versionHistoryUrl': pluginConfig.versionHistoryUrl,
382 'status': 0 // 0: disabled, 1: enabled
383 }, function () {
384 parent.db.getPlugins(function (err, docs) {
385 if (err) reject(err);
386 else resolve(docs);
387 });
388 });
389 });
390 };
391
392 obj.installPlugin = function (id, version_only, force_url, func) {
393 parent.db.getPlugin(id, function (err, docs) {
394 // the "id" would probably suffice, but is probably an sanitary issue, generate a random instead
395 var randId = Math.random().toString(32).replace('0.', '');
396 var tmpDir = require('os').tmpdir();
397 var fileName = obj.parent.path.join(tmpDir, 'Plugin_' + randId + '.zip');
398 try {
399 obj.fs.accessSync(tmpDir, obj.fs.constants.W_OK);
400 } catch (e) {
401 var pluginTmpPath = obj.parent.path.join(obj.pluginPath, '_tmp');
402 if (!obj.fs.existsSync(pluginTmpPath)) {
403 obj.fs.mkdirSync(pluginTmpPath, { recursive: true });
404 }
405 fileName = obj.parent.path.join(pluginTmpPath, 'Plugin_' + randId + '.zip');
406 }
407 var plugin = docs[0];
408 if (plugin.repository.type == 'git') {
409 var file;
410 try {
411 file = obj.fs.createWriteStream(fileName);
412 } catch (e) {
413 if (fileName.indexOf(tmpDir) >= 0) {
414 var pluginTmpPath = obj.parent.path.join(obj.pluginPath, '_tmp');
415 if (!obj.fs.existsSync(pluginTmpPath)) {
416 obj.fs.mkdirSync(pluginTmpPath, { recursive: true });
417 }
418 fileName = obj.parent.path.join(pluginTmpPath, 'Plugin_' + randId + '.zip');
419 file = obj.fs.createWriteStream(fileName);
420 } else {
421 throw e;
422 }
423 }
424 var dl_url = plugin.downloadUrl;
425 if (version_only != null && version_only != false) dl_url = version_only.url;
426 if (force_url != null) dl_url = force_url;
427 var url = require('url');
428 var q = url.parse(dl_url, true);
429 var http = (q.protocol == "http:") ? require('http') : require('https');
430 var opts = {
431 path: q.pathname,
432 host: q.hostname,
433 port: q.port,
434 headers: {
435 'User-Agent': 'MeshCentral'
436 },
437 followRedirects: true,
438 method: 'GET'
439 };
440 if (typeof parent.config.settings.plugins.proxy == 'string') { // Proxy support
441 const HttpsProxyAgent = require('https-proxy-agent');
442 opts.agent = new HttpsProxyAgent(require('url').parse(parent.config.settings.plugins.proxy));
443 }
444 var request = http.get(opts, function (response) {
445 // handle redirections with grace
446 if (response.headers.location) {
447 file.close(() => obj.fs.unlink(fileName, () => {}));
448 return obj.installPlugin(id, version_only, response.headers.location, func);
449 }
450 response.pipe(file);
451 file.on('finish', function () {
452 file.close(function () {
453 var yauzl = require('yauzl');
454 if (!obj.fs.existsSync(obj.pluginPath)) {
455 obj.fs.mkdirSync(obj.pluginPath);
456 }
457 if (!obj.fs.existsSync(obj.parent.path.join(obj.pluginPath, plugin.shortName))) {
458 obj.fs.mkdirSync(obj.parent.path.join(obj.pluginPath, plugin.shortName));
459 }
460 yauzl.open(fileName, { lazyEntries: true }, function (err, zipfile) {
461 if (err) throw err;
462 zipfile.readEntry();
463 zipfile.on('entry', function (entry) {
464 let pluginPath = obj.parent.path.join(obj.pluginPath, plugin.shortName);
465 let pathReg = new RegExp(/(.*?\/)/);
466 //if (process.platform == 'win32') { pathReg = new RegExp(/(.*?\\/); }
467 let filePath = obj.parent.path.join(pluginPath, entry.fileName.replace(pathReg, '')); // remove top level dir
468
469 if (/\/$/.test(entry.fileName)) { // dir
470 if (!obj.fs.existsSync(filePath))
471 obj.fs.mkdirSync(filePath);
472 zipfile.readEntry();
473 } else { // file
474 zipfile.openReadStream(entry, function (err, readStream) {
475 if (err) throw err;
476 readStream.on('end', function () { zipfile.readEntry(); });
477 if (process.platform == 'win32') {
478 readStream.pipe(obj.fs.createWriteStream(filePath));
479 } else {
480 var fileMode = (entry.externalFileAttributes >> 16) & 0x0fff;
481 if( fileMode <= 0 ) fileMode = 0o644;
482 readStream.pipe(obj.fs.createWriteStream(filePath, { mode: fileMode }));
483 }
484 });
485 }
486 });
487 zipfile.on('end', function () {
488 setTimeout(function () {
489 obj.fs.unlinkSync(fileName);
490 if (version_only == null || version_only === false) {
491 parent.db.setPluginStatus(id, 1, func);
492 } else {
493 parent.db.updatePlugin(id, { status: 1, version: version_only.name }, func);
494 }
495 try {
496 obj.plugins[plugin.shortName] = require(obj.pluginPath + '/' + plugin.shortName + '/' + plugin.shortName + '.js')[plugin.shortName](obj);
497 obj.exports[plugin.shortName] = obj.plugins[plugin.shortName].exports;
498 if (typeof obj.plugins[plugin.shortName].server_startup == 'function') obj.plugins[plugin.shortName].server_startup();
499 } catch (e) { console.log('Error instantiating new plugin: ', e); }
500 try {
501 var plugin_config = obj.fs.readFileSync(obj.pluginPath + '/' + plugin.shortName + '/config.json');
502 plugin_config = JSON.parse(plugin_config);
503 parent.db.updatePlugin(plugin._id, plugin_config);
504 } catch (e) { console.log('Error reading plugin config upon install'); }
505 parent.updateMeshCore();
506 });
507 });
508 });
509 });
510 });
511 });
512 } else if (plugin.repository.type == 'npm') {
513 // @TODO npm support? (need a test plugin)
514 }
515 });
516 };
517
518 obj.getPluginVersions = function (id) {
519 return new Promise(function (resolve, reject) {
520 parent.db.getPlugin(id, function (err, docs) {
521 var plugin = docs[0];
522 if (plugin.versionHistoryUrl == null) reject("No version history available for this plugin.");
523 var url = require('url');
524 var q = url.parse(plugin.versionHistoryUrl, true);
525 var http = (q.protocol == 'http:') ? require('http') : require('https');
526 var opts = {
527 path: q.pathname,
528 host: q.hostname,
529 port: q.port,
530 headers: {
531 'User-Agent': 'MeshCentral',
532 'Accept': 'application/vnd.github.v3+json'
533 }
534 };
535 if (typeof parent.config.settings.plugins.proxy == 'string') { // Proxy support
536 const HttpsProxyAgent = require('https-proxy-agent');
537 options.agent = new HttpsProxyAgent(require('url').parse(parent.config.settings.plugins.proxy));
538 }
539 http.get(opts, function (res) {
540 var versStr = '';
541 res.on('data', function (chunk) {
542 versStr += chunk;
543 });
544 res.on('end', function () {
545 if ((versStr[0] == '{') || (versStr[0] == '[')) { // let's be sure we're JSON
546 try {
547 var vers = JSON.parse(versStr);
548 var vList = [];
549 vers.forEach((v) => {
550 if (obj.versionLower(v.name, plugin.version)) vList.push(v);
551 });
552 if (vers.length == 0) reject("No previous versions available.");
553 resolve({ 'id': plugin._id, 'name': plugin.name, versionList: vList });
554 } catch (e) { reject("Version history problem."); }
555 } else {
556 reject("Version history appears to be malformed." + versStr);
557 }
558 });
559 }).on('error', function (e) {
560 reject("Error getting plugin versions: " + e.message);
561 });
562 });
563 });
564 };
565
566 obj.disablePlugin = function (id, func) {
567 parent.db.getPlugin(id, function (err, docs) {
568 var plugin = docs[0];
569 parent.db.setPluginStatus(id, 0, func);
570 delete obj.plugins[plugin.shortName];
571 delete obj.exports[plugin.shortName];
572 parent.updateMeshCore();
573 });
574 };
575
576 obj.removePlugin = function (id, func) {
577 parent.db.getPlugin(id, function (err, docs) {
578 var plugin = docs[0];
579 let pluginPath = obj.parent.path.join(obj.pluginPath, plugin.shortName);
580 if (obj.fs.existsSync(pluginPath)) {
581 try {
582 obj.fs.rmSync(pluginPath, { recursive: true, force: true });
583 } catch (e) {
584 console.log("Error removing plugin directory:", e);
585 }
586 }
587 parent.db.deletePlugin(id, func);
588 delete obj.plugins[plugin.shortName];
589 });
590 };
591
592 obj.handleAdminReq = function (req, res, user, serv) {
593 if ((req.query.pin == null) || (obj.common.isAlphaNumeric(req.query.pin) !== true)) { res.sendStatus(401); return; }
594 var path = obj.path.join(obj.pluginPath, req.query.pin, 'views');
595 serv.app.set('views', path);
596 if ((obj.plugins[req.query.pin] != null) && (typeof obj.plugins[req.query.pin].handleAdminReq == 'function')) {
597 obj.plugins[req.query.pin].handleAdminReq(req, res, user);
598 } else {
599 res.sendStatus(401);
600 }
601 }
602
603 obj.handleAdminPostReq = function (req, res, user, serv) {
604 if ((req.query.pin == null) || (obj.common.isAlphaNumeric(req.query.pin) !== true)) { res.sendStatus(401); return; }
605 var path = obj.path.join(obj.pluginPath, req.query.pin, 'views');
606 serv.app.set('views', path);
607 if ((obj.plugins[req.query.pin] != null) && (typeof obj.plugins[req.query.pin].handleAdminPostReq == 'function')) {
608 obj.plugins[req.query.pin].handleAdminPostReq(req, res, user);
609 } else {
610 res.sendStatus(401);
611 }
612 }
613 return obj;
614};