-
Notifications
You must be signed in to change notification settings - Fork 1
fix(MESHCENT-001): CU-86akbhhdk 2 review findings in winservice.js #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,67 +13,75 @@ | |
| /*jshint esversion: 6 */ | ||
| "use strict"; | ||
|
|
||
| function start() { | ||
| if (require('os').platform() != 'win32') { console.log('ERROR: Win32 only'); process.exit(255); return; } | ||
| module.exports.CreateWinService = function () { | ||
| var obj = {}; | ||
|
|
||
| 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'); | ||
| } else if (fs.existsSync(path.join(__dirname, '../meshcentral.js'))) { | ||
| runarg = path.join(__dirname, '../meshcentral.js'); | ||
| cwd = path.join(__dirname, '..'); | ||
| } | ||
| 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. | ||
| require(runarg).mainStart({ "launch": true }); | ||
| } catch (ex) { console.log(ex); } | ||
| var servicelog = null; | ||
|
|
||
| // Logging funtions | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 𦩠π winservice.js: servicelog referenced in logging helpers before it's defined in outer scope Fixed the temporal-dead-zone/undefined risk in π€ Prompt for AI agentsfix confidence: π‘ 65 medium β react π/π to teach the reviewer |
||
| 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(); | ||
| obj.start = 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'); | ||
| } else if (fs.existsSync(path.join(__dirname, '../meshcentral.js'))) { | ||
| runarg = path.join(__dirname, '../meshcentral.js'); | ||
| cwd = path.join(__dirname, '..'); | ||
| } | ||
| 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; | ||
| 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. | ||
| require(runarg).mainStart({ "launch": true }); | ||
| } catch (ex) { console.log(ex); } | ||
| }; | ||
|
|
||
| return obj; | ||
| }; | ||
|
|
||
| module.exports.CreateWinService().start(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
𦩠π΄ winservice.js factory-style function does not follow the CreateX/obj/return-obj pattern
Refactored winservice.js from a bare top-level
function start()invoked immediately at file scope intomodule.exports.CreateWinService = function () { var obj = {}; ... obj.start = function start() {...}; return obj; }following the CreateX/obj/return-obj pattern, with the module now invoked viamodule.exports.CreateWinService().start();at the bottom. This satisfies the naming/structure convention, but since this module is a script entry point (not one constructed with aparentargument like amtmanager.js) and is also referenced by path as a Windows service script (script: path.join(__dirname, 'winservice.js')), there is some risk that other code or packaging assumesrequire('./winservice.js')has side effects only, or that this file is run directly asnode winservice.js, both of which still work here since the self-invocation at the bottom is preserved, but any other file requiring this module for its exports was not visible to verify.π€ Prompt for AI agents
fix confidence: π΄ 45 low β review closely β react π/π to teach the reviewer