EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
winservice.js
Go to the documentation of this file.
1/**
2* @description Windows Service Launcher
3* @author Ylian Saint-Hilaire
4* @copyright Intel Corporation 2018-2022
5* @license Apache-2.0
6* @version v0.0.1
7*/
8
9/*jslint node: true */
10/*jshint node: true */
11/*jshint strict:false */
12/*jshint -W097 */
13/*jshint esversion: 6 */
14"use strict";
15
16function start() {
17 if (require('os').platform() != 'win32') { console.log('ERROR: Win32 only'); process.exit(255); return; }
18
19 try {
20 const fs = require('fs');
21 const path = require('path');
22
23 // Search for meshcentral.js
24 var cwd = null;
25 var runarg = null;
26 if (fs.existsSync(path.join(__dirname, 'meshcentral.js'))) {
27 runarg = path.join(__dirname, 'meshcentral.js');
28 cwd = __dirname;
29 } else if (fs.existsSync(path.join(__dirname, '../node_modules/meshcentral/meshcentral.js'))) {
30 runarg = path.join(__dirname, '../node_modules/meshcentral/meshcentral.js');
31 cwd = path.join(__dirname, '..');
32 } else if (fs.existsSync(path.join(__dirname, '../meshcentral/meshcentral.js'))) {
33 runarg = path.join(__dirname, '../meshcentral/meshcentral.js');
34 cwd = path.join(__dirname, '../meshcentral');
35 } else if (fs.existsSync(path.join(__dirname, '../meshcentral.js'))) {
36 runarg = path.join(__dirname, '../meshcentral.js');
37 cwd = path.join(__dirname, '..');
38 }
39 if (runarg == null) { console.log('ERROR: Unable to find MeshCentral.js'); process.exit(255); return; }
40
41 // Setup libraries
42 const args = require(path.join(cwd, 'node_modules/minimist'))(process.argv.slice(2));
43 const nodewindows = require(path.join(cwd, 'node_modules/node-windows'));
44 const service = nodewindows.Service;
45 const eventlogger = nodewindows.EventLogger;
46 const servicelog = new eventlogger('MeshCentral');
47
48 // Check if we need to install, start, stop, remove ourself as a background service
49 if (((args.install == true) || (args.uninstall == true) || (args.start == true) || (args.stop == true) || (args.restart == true))) {
50 var env = [], xenv = ['user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'exactport', 'debug'];
51 for (var i in xenv) { if (args[xenv[i]] != null) { env.push({ name: 'mesh' + xenv[i], value: args[xenv[i]] }); } } // Set some args as service environement variables.
52 var svc = new service({ name: 'MeshCentral', description: 'MeshCentral Remote Management Server', script: path.join(__dirname, 'winservice.js'), env: env, wait: 2, grow: 0.5 });
53 svc.on('install', function () { console.log('MeshCentral service installed.'); svc.start(); });
54 svc.on('uninstall', function () { console.log('MeshCentral service uninstalled.'); process.exit(); });
55 svc.on('start', function () { console.log('MeshCentral service started.'); process.exit(); });
56 svc.on('stop', function () { console.log('MeshCentral service stopped.'); if (args.stop) { process.exit(); } if (args.restart) { console.log('Holding 5 seconds...'); setTimeout(function () { svc.start(); }, 5000); } });
57 svc.on('alreadyinstalled', function () { console.log('MeshCentral service already installed.'); process.exit(); });
58 svc.on('invalidinstallation', function () { console.log('Invalid MeshCentral service installation.'); process.exit(); });
59
60 if (args.install == true) { try { svc.install(); } catch (e) { logException(e); } }
61 if (args.stop == true || args.restart == true) { try { svc.stop(); } catch (e) { logException(e); } }
62 if (args.start == true || args.restart == true) { try { svc.start(); } catch (e) { logException(e); } }
63 if (args.uninstall == true) { try { svc.uninstall(); } catch (e) { logException(e); } }
64 return;
65 }
66
67 // This module is only called when MeshCentral is running as a Windows service.
68 // In this case, we don't want to start a child process, so we launch directly without arguments.
69 require(runarg).mainStart({ "launch": true });
70 } catch (ex) { console.log(ex); }
71
72 // Logging funtions
73 function logException(e) { e += ''; logErrorEvent(e); }
74 function logInfoEvent(msg) { if (servicelog != null) { servicelog.info(msg); } console.log(msg); }
75 function logWarnEvent(msg) { if (servicelog != null) { servicelog.warn(msg); } console.log(msg); }
76 function logErrorEvent(msg) { if (servicelog != null) { servicelog.error(msg); } console.error(msg); }
77}
78
79start();