winservice.js 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /*jslint node: true */
  9. /*jshint node: true */
  10. /*jshint strict:false */
  11. /*jshint -W097 */
  12. /*jshint esversion: 6 */
  13. "use strict";
  14. function start() {
  15. if (require('os').platform() != 'win32') { console.log('ERROR: Win32 only'); process.exit(255); return; }
  16. try {
  17. const fs = require('fs');
  18. const path = require('path');
  19. // Search for meshcentral.js
  20. var cwd = null;
  21. var runarg = null;
  22. if (fs.existsSync(path.join(__dirname, 'meshcentral.js'))) {
  23. runarg = path.join(__dirname, 'meshcentral.js');
  24. cwd = __dirname;
  25. } else if (fs.existsSync(path.join(__dirname, '../node_modules/meshcentral/meshcentral.js'))) {
  26. runarg = path.join(__dirname, '../node_modules/meshcentral/meshcentral.js');
  27. cwd = path.join(__dirname, '..');
  28. } else if (fs.existsSync(path.join(__dirname, '../meshcentral/meshcentral.js'))) {
  29. runarg = path.join(__dirname, '../meshcentral/meshcentral.js');
  30. cwd = path.join(__dirname, '../meshcentral');
  31. } else if (fs.existsSync(path.join(__dirname, '../meshcentral.js'))) {
  32. runarg = path.join(__dirname, '../meshcentral.js');
  33. cwd = path.join(__dirname, '..');
  34. }
  35. if (runarg == null) { console.log('ERROR: Unable to find MeshCentral.js'); process.exit(255); return; }
  36. // Setup libraries
  37. const args = require(path.join(cwd, 'node_modules/minimist'))(process.argv.slice(2));
  38. const nodewindows = require(path.join(cwd, 'node_modules/node-windows'));
  39. const service = nodewindows.Service;
  40. const eventlogger = nodewindows.EventLogger;
  41. const servicelog = new eventlogger('MeshCentral');
  42. // Check if we need to install, start, stop, remove ourself as a background service
  43. if (((args.install == true) || (args.uninstall == true) || (args.start == true) || (args.stop == true) || (args.restart == true))) {
  44. var env = [], xenv = ['user', 'port', 'aliasport', 'mpsport', 'mpsaliasport', 'redirport', 'exactport', 'debug'];
  45. 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.
  46. var svc = new service({ name: 'MeshCentral', description: 'MeshCentral Remote Management Server', script: path.join(__dirname, 'winservice.js'), env: env, wait: 2, grow: 0.5 });
  47. svc.on('install', function () { console.log('MeshCentral service installed.'); svc.start(); });
  48. svc.on('uninstall', function () { console.log('MeshCentral service uninstalled.'); process.exit(); });
  49. svc.on('start', function () { console.log('MeshCentral service started.'); process.exit(); });
  50. 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); } });
  51. svc.on('alreadyinstalled', function () { console.log('MeshCentral service already installed.'); process.exit(); });
  52. svc.on('invalidinstallation', function () { console.log('Invalid MeshCentral service installation.'); process.exit(); });
  53. if (args.install == true) { try { svc.install(); } catch (e) { logException(e); } }
  54. if (args.stop == true || args.restart == true) { try { svc.stop(); } catch (e) { logException(e); } }
  55. if (args.start == true || args.restart == true) { try { svc.start(); } catch (e) { logException(e); } }
  56. if (args.uninstall == true) { try { svc.uninstall(); } catch (e) { logException(e); } }
  57. return;
  58. }
  59. // This module is only called when MeshCentral is running as a Windows service.
  60. // In this case, we don't want to start a child process, so we launch directly without arguments.
  61. require(runarg).mainStart({ "launch": true });
  62. } catch (ex) { console.log(ex); }
  63. // Logging funtions
  64. function logException(e) { e += ''; logErrorEvent(e); }
  65. function logInfoEvent(msg) { if (servicelog != null) { servicelog.info(msg); } console.log(msg); }
  66. function logWarnEvent(msg) { if (servicelog != null) { servicelog.warn(msg); } console.log(msg); }
  67. function logErrorEvent(msg) { if (servicelog != null) { servicelog.error(msg); } console.error(msg); }
  68. }
  69. start();