2018-03-15 01:16:55 +03:00
/ * *
2019-08-08 22:43:00 +03:00
* @ description Windows Service Launcher
2018-03-15 01:16:55 +03:00
* @ author Ylian Saint - Hilaire
2022-01-24 10:21:24 +03:00
* @ copyright Intel Corporation 2018 - 2022
2018-03-15 01:16:55 +03:00
* @ license Apache - 2.0
* @ version v0 . 0.1
* /
2018-08-30 22:05:23 +03:00
/*jslint node: true */
/*jshint node: true */
/*jshint strict:false */
/*jshint -W097 */
/*jshint esversion: 6 */
"use strict" ;
2018-08-27 22:24:15 +03:00
2019-08-08 22:43:00 +03:00
function start ( ) {
if ( require ( 'os' ) . platform ( ) != 'win32' ) { console . log ( 'ERROR: Win32 only' ) ; process . exit ( 255 ) ; return ; }
try {
const fs = require ( 'fs' ) ;
const path = require ( 'path' ) ;
// Search for meshcentral.js
var cwd = null ;
var runarg = null ;
if ( fs . existsSync ( path . join ( _ _dirname , 'meshcentral.js' ) ) ) {
runarg = path . join ( _ _dirname , 'meshcentral.js' ) ;
cwd = _ _dirname ;
} else if ( fs . existsSync ( path . join ( _ _dirname , '../node_modules/meshcentral/meshcentral.js' ) ) ) {
runarg = path . join ( _ _dirname , '../node_modules/meshcentral/meshcentral.js' ) ;
cwd = path . join ( _ _dirname , '..' ) ;
} else if ( fs . existsSync ( path . join ( _ _dirname , '../meshcentral/meshcentral.js' ) ) ) {
runarg = path . join ( _ _dirname , '../meshcentral/meshcentral.js' ) ;
cwd = path . join ( _ _dirname , '../meshcentral' ) ;
2020-11-28 23:58:40 +03:00
} else if ( fs . existsSync ( path . join ( _ _dirname , '../meshcentral.js' ) ) ) {
runarg = path . join ( _ _dirname , '../meshcentral.js' ) ;
cwd = path . join ( _ _dirname , '..' ) ;
2019-08-08 22:43:00 +03:00
}
if ( runarg == null ) { console . log ( 'ERROR: Unable to find MeshCentral.js' ) ; process . exit ( 255 ) ; return ; }
// Setup libraries
const args = require ( path . join ( cwd , 'node_modules/minimist' ) ) ( process . argv . slice ( 2 ) ) ;
const nodewindows = require ( path . join ( cwd , 'node_modules/node-windows' ) ) ;
const service = nodewindows . Service ;
const eventlogger = nodewindows . EventLogger ;
const servicelog = new eventlogger ( 'MeshCentral' ) ;
// Check if we need to install, start, stop, remove ourself as a background service
if ( ( ( args . install == true ) || ( args . uninstall == true ) || ( args . start == true ) || ( args . stop == true ) || ( args . restart == true ) ) ) {
var env = [ ] , xenv = [ 'user' , 'port' , 'aliasport' , 'mpsport' , 'mpsaliasport' , 'redirport' , 'exactport' , 'debug' ] ;
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.
var svc = new service ( { name : 'MeshCentral' , description : 'MeshCentral Remote Management Server' , script : path . join ( _ _dirname , 'winservice.js' ) , env : env , wait : 2 , grow : 0.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 ( ) ; } ) ;
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 ) ; } } ) ;
svc . on ( 'alreadyinstalled' , function ( ) { console . log ( 'MeshCentral service already installed.' ) ; process . exit ( ) ; } ) ;
svc . on ( 'invalidinstallation' , function ( ) { console . log ( 'Invalid MeshCentral service installation.' ) ; process . exit ( ) ; } ) ;
if ( args . install == true ) { try { svc . install ( ) ; } catch ( e ) { logException ( e ) ; } }
if ( args . stop == true || args . restart == true ) { try { svc . stop ( ) ; } catch ( e ) { logException ( e ) ; } }
if ( args . start == true || args . restart == true ) { try { svc . start ( ) ; } catch ( e ) { logException ( e ) ; } }
if ( args . uninstall == true ) { try { svc . uninstall ( ) ; } catch ( e ) { logException ( e ) ; } }
return ;
}
// 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.
2019-08-08 23:02:49 +03:00
require ( runarg ) . mainStart ( { "launch" : true } ) ;
2019-08-08 22:43:00 +03:00
} catch ( ex ) { console . log ( ex ) ; }
// Logging funtions
function logException ( e ) { e += '' ; logErrorEvent ( e ) ; }
function logInfoEvent ( msg ) { if ( servicelog != null ) { servicelog . info ( msg ) ; } console . log ( msg ) ; }
function logWarnEvent ( msg ) { if ( servicelog != null ) { servicelog . warn ( msg ) ; } console . log ( msg ) ; }
function logErrorEvent ( msg ) { if ( servicelog != null ) { servicelog . error ( msg ) ; } console . error ( msg ) ; }
}
start ( ) ;