Fixed MeshCentral running as a Windows service.

Ylian Saint-Hilaire committed Mar 14, 2018 at 15:16 UTC d28454c5ec81e51888962a8ec96054f2f3df3899
3 files changed +42 -19
meshcentral.js
+30 -18
@@ -9,7 +9,7 @@
9 // If app metrics is available
10 if (process.argv[2] == '--launch') { try { require('appmetrics-dash').monitor({ url: '/', title: 'MeshCentral', port: 88, host: '127.0.0.1' }); } catch (e) { } }
11
12 -function CreateMeshCentralServer(config) {
12 +function CreateMeshCentralServer(config, args) {
13 var obj = {};
14 obj.db;
15 obj.webserver;
@@ -27,7 +27,7 @@ function CreateMeshCentralServer(config) {
27 obj.crypto = require('crypto');
28 obj.exeHandler = require('./exeHandler.js');
29 obj.platform = require('os').platform();
30 - obj.args = require('minimist')(process.argv.slice(2));
30 + obj.args = args;
31 obj.common = require('./common.js');
32 obj.certificates = null;
33 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) {
108 if ((obj.service != null) && ((obj.args.install == true) || (obj.args.uninstall == true) || (obj.args.start == true) || (obj.args.stop == true) || (obj.args.restart == true))) {
109 var env = [], xenv = ['user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'exactport', 'debug'];
110 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.
111 - 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 });
111 + 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 });
112 svc.on('install', function () { console.log('MeshCentral service installed.'); svc.start(); });
113 svc.on('uninstall', function () { console.log('MeshCentral service uninstalled.'); process.exit(); });
114 svc.on('start', function () { console.log('MeshCentral service started.'); process.exit(); });
@@ -196,6 +196,9 @@ function CreateMeshCentralServer(config) {
196 obj.performServerCertUpdate = function () { console.log('Updating server certificates...'); process.exit(200); }
197
198 obj.StartEx = function () {
199 + //var wincmd = require('node-windows');
200 + //wincmd.list(function (svc) { console.log(svc); }, true);
201 +
202 // Write the server state
203 obj.updateServerState('state', 'starting');
204
@@ -428,6 +431,7 @@ function CreateMeshCentralServer(config) {
431 }
432
433 obj.debug(1, 'Server started');
434 + if (obj.args.nousers == true) { obj.updateServerState('nousers', '1'); }
435 obj.updateServerState('state', 'running');
436 });
437 });
@@ -1110,18 +1114,26 @@ process.on('SIGINT', function () { if (meshserver != null) { meshserver.Stop();
1114
1115 // Load the really basic modules
1116 var meshserver = null;
1113 -InstallModules(['minimist'], function () {
1114 - // Get the server configuration
1115 - var config = getConfig();
1116 - if (config == null) { process.exit(); }
1117 -
1118 - // Build the list of required modules
1119 - var modules = ['ws', 'nedb', 'https', 'unzip', 'xmldom', 'express', 'archiver', 'multiparty', 'node-forge', 'express-ws', 'compression', 'body-parser', 'connect-redis', 'express-session', 'express-handlebars'];
1120 - if (require('os').platform() == 'win32') { modules.push('node-sspi'); modules.push('node-windows'); } // Add Windows modules
1121 - 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
1122 - if (config.settings.mongodb != null) { modules.push('mongojs'); } // Add MongoDB
1123 - if (config.smtp != null) { modules.push('nodemailer'); } // Add SMTP support
1124 -
1125 - // Install any missing modules and launch the server
1126 - InstallModules(modules, function () { meshserver = CreateMeshCentralServer(config); meshserver.Start(); });
1127 -});
1117 +function mainStart(args) {
1118 + InstallModules(['minimist'], function () {
1119 + // Get the server configuration
1120 + var config = getConfig();
1121 + if (config == null) { process.exit(); }
1122 +
1123 + // Build the list of required modules
1124 + var modules = ['ws', 'nedb', 'https', 'unzip', 'xmldom', 'express', 'archiver', 'multiparty', 'node-forge', 'express-ws', 'compression', 'body-parser', 'connect-redis', 'express-session', 'express-handlebars'];
1125 + if (require('os').platform() == 'win32') { modules.push('node-sspi'); modules.push('node-windows'); } // Add Windows modules
1126 + 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
1127 + if (config.settings.mongodb != null) { modules.push('mongojs'); } // Add MongoDB
1128 + if (config.smtp != null) { modules.push('nodemailer'); } // Add SMTP support
1129 +
1130 + // Install any missing modules and launch the server
1131 + InstallModules(modules, function () { meshserver = CreateMeshCentralServer(config, args); meshserver.Start(); });
1132 + });
1133 +}
1134 +
1135 +if (require.main === module) {
1136 + mainStart(require('minimist')(process.argv.slice(2))); // Called directly, launch normally.
1137 +} else {
1138 + module.exports.mainStart = mainStart; // Required as a module, useful for winservice.js
1139 +}
\ No newline at end of file
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.1.5-a",
3 + "version": "0.1.5-c",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",
winservice.js new
+11
@@ -0,0 +1,11 @@
1 +/**
2 +* @description MeshCentral main module
3 +* @author Ylian Saint-Hilaire
4 +* @copyright Intel Corporation 2018
5 +* @license Apache-2.0
6 +* @version v0.0.1
7 +*/
8 +
9 +// This module is only called when MeshCentral is running as a Windows service.
10 +// In this case, we don't want to start a child process, so we launch directly without arguments.
11 +require('./meshcentral.js').mainStart({ "launch": true });