From d28454c5ec81e51888962a8ec96054f2f3df3899 Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Wed, 14 Mar 2018 15:16:55 -0700 Subject: [PATCH] Fixed MeshCentral running as a Windows service. --- meshcentral.js | 44 ++++++++++++++++++++++++++++---------------- package.json | 2 +- winservice.js | 11 +++++++++++ 3 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 winservice.js diff --git a/meshcentral.js b/meshcentral.js index 85ad25e9..12292997 100644 --- a/meshcentral.js +++ b/meshcentral.js @@ -9,7 +9,7 @@ // If app metrics is available if (process.argv[2] == '--launch') { try { require('appmetrics-dash').monitor({ url: '/', title: 'MeshCentral', port: 88, host: '127.0.0.1' }); } catch (e) { } } -function CreateMeshCentralServer(config) { +function CreateMeshCentralServer(config, args) { var obj = {}; obj.db; obj.webserver; @@ -27,7 +27,7 @@ function CreateMeshCentralServer(config) { obj.crypto = require('crypto'); obj.exeHandler = require('./exeHandler.js'); obj.platform = require('os').platform(); - obj.args = require('minimist')(process.argv.slice(2)); + obj.args = args; obj.common = require('./common.js'); obj.certificates = null; obj.connectivityByNode = {}; // This object keeps a list of all connected CIRA and agents, by nodeid->value (value: 1 = Agent, 2 = CIRA, 4 = AmtDirect) @@ -108,7 +108,7 @@ function CreateMeshCentralServer(config) { if ((obj.service != null) && ((obj.args.install == true) || (obj.args.uninstall == true) || (obj.args.start == true) || (obj.args.stop == true) || (obj.args.restart == true))) { var env = [], xenv = ['user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'exactport', 'debug']; for (var i in xenv) { if (obj.args[xenv[i]] != null) { env.push({ name: 'mesh' + xenv[i], value: obj.args[xenv[i]] }); } } // Set some args as service environement variables. - var svc = new obj.service({ name: 'MeshCentral', description: 'MeshCentral Remote Management Server', script: obj.path.join(__dirname, 'meshcentral.js'), env: env, wait: 2, grow: .5 }); + var svc = new obj.service({ name: 'MeshCentral', description: 'MeshCentral Remote Management Server', script: obj.path.join(__dirname, 'winservice.js'), env: env, wait: 2, grow: .5 }); svc.on('install', function () { console.log('MeshCentral service installed.'); svc.start(); }); svc.on('uninstall', function () { console.log('MeshCentral service uninstalled.'); process.exit(); }); svc.on('start', function () { console.log('MeshCentral service started.'); process.exit(); }); @@ -196,6 +196,9 @@ function CreateMeshCentralServer(config) { obj.performServerCertUpdate = function () { console.log('Updating server certificates...'); process.exit(200); } obj.StartEx = function () { + //var wincmd = require('node-windows'); + //wincmd.list(function (svc) { console.log(svc); }, true); + // Write the server state obj.updateServerState('state', 'starting'); @@ -428,6 +431,7 @@ function CreateMeshCentralServer(config) { } obj.debug(1, 'Server started'); + if (obj.args.nousers == true) { obj.updateServerState('nousers', '1'); } obj.updateServerState('state', 'running'); }); }); @@ -1110,18 +1114,26 @@ process.on('SIGINT', function () { if (meshserver != null) { meshserver.Stop(); // Load the really basic modules var meshserver = null; -InstallModules(['minimist'], function () { - // Get the server configuration - var config = getConfig(); - if (config == null) { process.exit(); } +function mainStart(args) { + InstallModules(['minimist'], function () { + // Get the server configuration + var config = getConfig(); + if (config == null) { process.exit(); } - // Build the list of required modules - var modules = ['ws', 'nedb', 'https', 'unzip', 'xmldom', 'express', 'archiver', 'multiparty', 'node-forge', 'express-ws', 'compression', 'body-parser', 'connect-redis', 'express-session', 'express-handlebars']; - if (require('os').platform() == 'win32') { modules.push('node-sspi'); modules.push('node-windows'); } // Add Windows modules - if (config.letsencrypt != null) { modules.push('greenlock'); modules.push('le-store-certbot'); modules.push('le-challenge-fs'); modules.push('le-acme-core'); } // Add Greenlock Modules - if (config.settings.mongodb != null) { modules.push('mongojs'); } // Add MongoDB - if (config.smtp != null) { modules.push('nodemailer'); } // Add SMTP support + // Build the list of required modules + var modules = ['ws', 'nedb', 'https', 'unzip', 'xmldom', 'express', 'archiver', 'multiparty', 'node-forge', 'express-ws', 'compression', 'body-parser', 'connect-redis', 'express-session', 'express-handlebars']; + if (require('os').platform() == 'win32') { modules.push('node-sspi'); modules.push('node-windows'); } // Add Windows modules + if (config.letsencrypt != null) { modules.push('greenlock'); modules.push('le-store-certbot'); modules.push('le-challenge-fs'); modules.push('le-acme-core'); } // Add Greenlock Modules + if (config.settings.mongodb != null) { modules.push('mongojs'); } // Add MongoDB + if (config.smtp != null) { modules.push('nodemailer'); } // Add SMTP support - // Install any missing modules and launch the server - InstallModules(modules, function () { meshserver = CreateMeshCentralServer(config); meshserver.Start(); }); -}); + // Install any missing modules and launch the server + InstallModules(modules, function () { meshserver = CreateMeshCentralServer(config, args); meshserver.Start(); }); + }); +} + +if (require.main === module) { + mainStart(require('minimist')(process.argv.slice(2))); // Called directly, launch normally. +} else { + module.exports.mainStart = mainStart; // Required as a module, useful for winservice.js +} \ No newline at end of file diff --git a/package.json b/package.json index d14a5e45..c1c66b2e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "meshcentral", - "version": "0.1.5-a", + "version": "0.1.5-c", "keywords": [ "Remote Management", "Intel AMT", diff --git a/winservice.js b/winservice.js new file mode 100644 index 00000000..3c48d785 --- /dev/null +++ b/winservice.js @@ -0,0 +1,11 @@ +/** +* @description MeshCentral main module +* @author Ylian Saint-Hilaire +* @copyright Intel Corporation 2018 +* @license Apache-2.0 +* @version v0.0.1 +*/ + +// This module is only called when MeshCentral is running as a Windows service. +// In this case, we don't want to start a child process, so we launch directly without arguments. +require('./meshcentral.js').mainStart({ "launch": true });