pluginHandler.js 28 KB

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