Compare commits
2 Commits
da042f2dba
...
3be8c28421
Author | SHA1 | Date | |
---|---|---|---|
3be8c28421 | |||
c562cee3a9 |
@ -60,6 +60,7 @@
|
|||||||
"jsonwebtoken": "^7.1.9",
|
"jsonwebtoken": "^7.1.9",
|
||||||
"mkdirp": "^0.5.1",
|
"mkdirp": "^0.5.1",
|
||||||
"proxy-packer": "^1.4.3",
|
"proxy-packer": "^1.4.3",
|
||||||
|
"ps-list": "^5.0.0",
|
||||||
"recase": "^1.0.4",
|
"recase": "^1.0.4",
|
||||||
"redirect-https": "^1.1.5",
|
"redirect-https": "^1.1.5",
|
||||||
"serve-index": "^1.9.1",
|
"serve-index": "^1.9.1",
|
||||||
|
@ -7,6 +7,58 @@ var exec = require('child_process').exec;
|
|||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
var Launcher = module.exports;
|
var Launcher = module.exports;
|
||||||
|
Launcher._getError = function getError(err, stderr) {
|
||||||
|
if (err) { return err; }
|
||||||
|
if (stderr) {
|
||||||
|
err = new Error(stderr);
|
||||||
|
err.code = 'ELAUNCHER';
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Launcher._detect = function (things, fn) {
|
||||||
|
if (things.launcher) {
|
||||||
|
if ('string' === typeof things.launcher) {
|
||||||
|
fn(null, things.launcher);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ('function' === typeof things.launcher) {
|
||||||
|
things.launcher(things);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// could have used "command-exists" but I'm trying to stay low-dependency
|
||||||
|
// os.platform(), os.type()
|
||||||
|
if (!/^win/i.test(os.platform())) {
|
||||||
|
if (/^darwin/i.test(os.platform())) {
|
||||||
|
exec('command -v launchctl', things._execOpts, function (err, stdout, stderr) {
|
||||||
|
err = Launcher._getError(err, stderr);
|
||||||
|
fn(err, 'launchctl');
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
exec('command -v systemctl', things._execOpts, function (err, stdout, stderr) {
|
||||||
|
err = Launcher._getError(err, stderr);
|
||||||
|
fn(err, 'systemctl');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// https://stackoverflow.com/questions/17908789/how-to-add-an-item-to-registry-to-run-at-startup-without-uac
|
||||||
|
// wininit? regedit? SCM?
|
||||||
|
// REG ADD "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /V "My App" /t REG_SZ /F /D "C:\MyAppPath\MyApp.exe"
|
||||||
|
// https://www.microsoft.com/developerblog/2015/11/09/reading-and-writing-to-the-windows-registry-in-process-from-node-js/
|
||||||
|
// https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/reg-add
|
||||||
|
// https://social.msdn.microsoft.com/Forums/en-US/5b318f44-281e-4098-8dee-3ba8435fa391/add-registry-key-for-autostart-of-app-in-ice?forum=quebectools
|
||||||
|
// utils.elevate
|
||||||
|
// https://github.com/CatalystCode/windows-registry-node
|
||||||
|
exec('where reg.exe', things._execOpts, function (err, stdout, stderr) {
|
||||||
|
//console.log((stdout||'').trim());
|
||||||
|
if (stderr) {
|
||||||
|
console.error(stderr);
|
||||||
|
}
|
||||||
|
fn(err, 'reg.exe');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Launcher.install = function (things, fn) {
|
Launcher.install = function (things, fn) {
|
||||||
if (!fn) { fn = function (err) { if (err) { console.error(err); } }; }
|
if (!fn) { fn = function (err) { if (err) { console.error(err); } }; }
|
||||||
things = things || {};
|
things = things || {};
|
||||||
@ -51,14 +103,6 @@ Launcher.install = function (things, fn) {
|
|||||||
vars.telebitRwDirs.push(vars.npmConfigPrefix);
|
vars.telebitRwDirs.push(vars.npmConfigPrefix);
|
||||||
}
|
}
|
||||||
vars.telebitRwDirs = vars.telebitRwDirs.join(' ');
|
vars.telebitRwDirs = vars.telebitRwDirs.join(' ');
|
||||||
function getError(err, stderr) {
|
|
||||||
if (err) { return err; }
|
|
||||||
if (stderr) {
|
|
||||||
err = new Error(stderr);
|
|
||||||
err.code = 'ELAUNCHER';
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var launchers = {
|
var launchers = {
|
||||||
'node': function () {
|
'node': function () {
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
@ -130,13 +174,13 @@ Launcher.install = function (things, fn) {
|
|||||||
var execstr = launcherstr + "unload -w " + launcher;
|
var execstr = launcherstr + "unload -w " + launcher;
|
||||||
exec(execstr, things._execOpts, function (/*err, stdout, stderr*/) {
|
exec(execstr, things._execOpts, function (/*err, stdout, stderr*/) {
|
||||||
// we probably only need to skip the stderr (saying that it can't stop something that isn't started)
|
// we probably only need to skip the stderr (saying that it can't stop something that isn't started)
|
||||||
//err = getError(err, stderr);
|
//err = Launcher._getError(err, stderr);
|
||||||
//if (err) { fn(err); return; }
|
//if (err) { fn(err); return; }
|
||||||
//console.log((stdout||'').trim());
|
//console.log((stdout||'').trim());
|
||||||
//console.log('unload worked?');
|
//console.log('unload worked?');
|
||||||
execstr = launcherstr + "load -w " + launcher;
|
execstr = launcherstr + "load -w " + launcher;
|
||||||
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
||||||
err = getError(err, stderr);
|
err = Launcher._getError(err, stderr);
|
||||||
if (err) { fn(err); return; }
|
if (err) { fn(err); return; }
|
||||||
//console.log((stdout||'').trim());
|
//console.log((stdout||'').trim());
|
||||||
//console.log('load worked?');
|
//console.log('load worked?');
|
||||||
@ -170,23 +214,23 @@ Launcher.install = function (things, fn) {
|
|||||||
var launcherstr = (vars.userspace ? "" : "sudo ") + "systemctl " + (vars.userspace ? "--user " : "");
|
var launcherstr = (vars.userspace ? "" : "sudo ") + "systemctl " + (vars.userspace ? "--user " : "");
|
||||||
var execstr = launcherstr + "daemon-reload";
|
var execstr = launcherstr + "daemon-reload";
|
||||||
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
||||||
err = getError(err, stderr);
|
err = Launcher._getError(err, stderr);
|
||||||
if (err) { fn(err); return; }
|
if (err) { fn(err); return; }
|
||||||
//console.log((stdout||'').trim());
|
//console.log((stdout||'').trim());
|
||||||
var execstr = launcherstr + "enable " + launchername;
|
var execstr = launcherstr + "enable " + launchername;
|
||||||
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
||||||
err = getError(err, !/Created symlink/.test(stderr||''));
|
err = Launcher._getError(err, !/Created symlink/i.test(stderr||''));
|
||||||
if (err) { fn(err); return; }
|
if (err) { fn(err); return; }
|
||||||
//console.log((stdout||'').trim());
|
//console.log((stdout||'').trim());
|
||||||
var execstr = launcherstr + "restart " + launchername;
|
var execstr = launcherstr + "restart " + launchername;
|
||||||
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
||||||
err = getError(err, stderr);
|
err = Launcher._getError(err, stderr);
|
||||||
if (err) { fn(err); return; }
|
if (err) { fn(err); return; }
|
||||||
//console.log((stdout||'').trim());
|
//console.log((stdout||'').trim());
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
var execstr = launcherstr + "status " + launchername;
|
var execstr = launcherstr + "status " + launchername;
|
||||||
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
||||||
err = getError(err, stderr);
|
err = Launcher._getError(err, stderr);
|
||||||
if (err) { fn(err); return; }
|
if (err) { fn(err); return; }
|
||||||
if (!/active.*running/i.test(stdout)) {
|
if (!/active.*running/i.test(stdout)) {
|
||||||
err = new Error("systemd failed to start '" + launchername + "'");
|
err = new Error("systemd failed to start '" + launchername + "'");
|
||||||
@ -222,7 +266,7 @@ Launcher.install = function (things, fn) {
|
|||||||
+ '" /F'
|
+ '" /F'
|
||||||
;
|
;
|
||||||
exec(cmd, things._execOpts, function (err, stdout, stderr) {
|
exec(cmd, things._execOpts, function (err, stdout, stderr) {
|
||||||
err = getError(err, stderr);
|
err = Launcher._getError(err, stderr);
|
||||||
if (err) { fn(err); return; }
|
if (err) { fn(err); return; }
|
||||||
// need to start it for the first time ourselves
|
// need to start it for the first time ourselves
|
||||||
run(null, 'node');
|
run(null, 'node');
|
||||||
@ -249,54 +293,149 @@ Launcher.install = function (things, fn) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (things.launcher) {
|
things._vars = vars;
|
||||||
if ('string' === typeof things.launcher) {
|
things._userspace = vars.userspace;
|
||||||
run(null, things.launcher);
|
Launcher._detect(things, run);
|
||||||
return;
|
};
|
||||||
|
Launcher.uninstall = function (things, fn) {
|
||||||
|
if (!fn) { fn = function (err) { if (err) { console.error(err); } }; }
|
||||||
|
things = things || {};
|
||||||
|
|
||||||
|
// Right now this is just for npm install -g and npx
|
||||||
|
if (things.env) {
|
||||||
|
things.env.PATH = things.env.PATH || process.env.PATH;
|
||||||
|
} else {
|
||||||
|
things.env = process.env;
|
||||||
|
}
|
||||||
|
things.argv = things.argv || process.argv;
|
||||||
|
things._execOpts = { windowsHide: true, env: things.env };
|
||||||
|
var vars = {
|
||||||
|
telebitUser: os.userInfo().username
|
||||||
|
};
|
||||||
|
vars.userspace = (!things.telebitUser || (things.telebitUser === os.userInfo().username)) ? true : false;
|
||||||
|
var launchers = {
|
||||||
|
'node': function () {
|
||||||
|
var psList = require('ps-list');
|
||||||
|
psList().then(function (procs) {
|
||||||
|
procs.forEach(function (proc) {
|
||||||
|
if ('node' === proc.name && /telebitd/i.test(proc.cmd)) {
|
||||||
|
console.log(proc);
|
||||||
|
process.kill(proc.pid);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// Two things:
|
||||||
|
// 1) wait to see if the process dies
|
||||||
|
// 2) wait to give time for the socket to connect
|
||||||
|
setTimeout(function () {
|
||||||
|
if (fn) { fn(null); return; }
|
||||||
|
}, 1.75 * 1000);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if ('function' === typeof things.launcher) {
|
, 'launchctl': function () {
|
||||||
things._vars = vars;
|
var launcher = path.join(os.homedir(), 'Library/LaunchAgents/cloud.telebit.remote.plist');
|
||||||
things._userspace = vars.userspace;
|
try {
|
||||||
things.launcher(things);
|
var launcherstr = (vars.userspace ? "" : "sudo ") + "launchctl ";
|
||||||
|
var execstr = launcherstr + "unload -w " + launcher;
|
||||||
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
||||||
|
// we probably only need to skip the stderr (saying that it can't stop something that isn't started)
|
||||||
|
//err = Launcher._getError(err, stderr);
|
||||||
|
//if (err) { fn(err); return; }
|
||||||
|
//console.log((stdout||'').trim());
|
||||||
|
//console.log('unload worked?');
|
||||||
|
err = Launcher._getError(err, stderr);
|
||||||
|
if (err) { fn(err); return; }
|
||||||
|
//console.log((stdout||'').trim());
|
||||||
|
//console.log('load worked?');
|
||||||
|
setTimeout(function () {
|
||||||
|
fn(null);
|
||||||
|
}, 1.25 * 1000);
|
||||||
|
});
|
||||||
|
} catch(e) {
|
||||||
|
console.error("'" + launcher + "' error (uninstall):");
|
||||||
|
console.error(e);
|
||||||
|
if (fn) { fn(e); return; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
, 'systemctl': function () {
|
||||||
|
var launcher = path.join(os.homedir(), '.config/systemd/user/telebit.service');
|
||||||
|
var launchername = 'telebit.service';
|
||||||
|
try {
|
||||||
|
mkdirp.sync(path.join(os.homedir(), '.config/systemd/user'));
|
||||||
|
// IMPORTANT
|
||||||
|
// It's a dangerous to go alone, take this:
|
||||||
|
// SYSTEMD_LOG_LEVEL=debug journalctl -xef --user-unit=telebit
|
||||||
|
// (makes debugging systemd issues not "easy" per se, but possible)
|
||||||
|
var launcherstr = (vars.userspace ? "" : "sudo ") + "systemctl " + (vars.userspace ? "--user " : "");
|
||||||
|
var execstr = launcherstr + "disable " + launchername;
|
||||||
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
||||||
|
err = Launcher._getError(err, !/Removed symlink /i.test(stderr||'') && stderr || '');
|
||||||
|
if (err) { fn(err); return; }
|
||||||
|
//console.log((stdout||'').trim());
|
||||||
|
var execstr = launcherstr + "stop " + launchername;
|
||||||
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
||||||
|
err = Launcher._getError(err, stderr);
|
||||||
|
if (err) { fn(err); return; }
|
||||||
|
//console.log((stdout||'').trim());
|
||||||
|
setTimeout(function () {
|
||||||
|
var execstr = launcherstr + "status " + launchername;
|
||||||
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
||||||
|
err = Launcher._getError(err, stderr);
|
||||||
|
if (err) { fn(err); return; }
|
||||||
|
if (!/inactive.*dead/i.test(stdout)) {
|
||||||
|
err = new Error("systemd failed to stop '" + launchername + "'");
|
||||||
|
}
|
||||||
|
if (err) { fn(err); return; }
|
||||||
|
//console.log((stdout||'').trim());
|
||||||
|
fn(null);
|
||||||
|
});
|
||||||
|
}, 1.25 * 1000);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} catch(e) {
|
||||||
|
console.error("'" + launcher + "' error:");
|
||||||
|
console.error(e);
|
||||||
|
if (fn) { fn(e); return; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
, 'reg.exe': function () {
|
||||||
|
if (!vars.userspace) {
|
||||||
|
console.warn("sysetm-level, privileged services are not yet supported on windows");
|
||||||
|
}
|
||||||
|
var cmd = 'reg.exe add "HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"'
|
||||||
|
+ ' /V "Telebit" /F'
|
||||||
|
;
|
||||||
|
exec(cmd, things._execOpts, function (err, stdout, stderr) {
|
||||||
|
err = Launcher._getError(err, stderr);
|
||||||
|
if (err) { fn(err); return; }
|
||||||
|
// need to start it for the first time ourselves
|
||||||
|
kill(null, 'node');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function kill(err, launcher) {
|
||||||
|
if (err) {
|
||||||
|
console.error("No luck with '" + launcher + "', trying a process.kill() instead...");
|
||||||
|
console.error(err);
|
||||||
|
launcher = 'node';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (launchers[launcher]) {
|
||||||
|
launchers[launcher]();
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
console.error("No launcher handler (uninstall) for '" + launcher + "'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// could have used "command-exists" but I'm trying to stay low-dependency
|
things._vars = vars;
|
||||||
// os.platform(), os.type()
|
things._userspace = vars.userspace;
|
||||||
if (!/^win/i.test(os.platform())) {
|
Launcher._detect(things, kill);
|
||||||
if (/^darwin/i.test(os.platform())) {
|
|
||||||
exec('command -v launchctl', things._execOpts, function (err, stdout, stderr) {
|
|
||||||
err = getError(err, stderr);
|
|
||||||
run(err, 'launchctl');
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
exec('command -v systemctl', things._execOpts, function (err, stdout, stderr) {
|
|
||||||
err = getError(err, stderr);
|
|
||||||
run(err, 'systemctl');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// https://stackoverflow.com/questions/17908789/how-to-add-an-item-to-registry-to-run-at-startup-without-uac
|
|
||||||
// wininit? regedit? SCM?
|
|
||||||
// REG ADD "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /V "My App" /t REG_SZ /F /D "C:\MyAppPath\MyApp.exe"
|
|
||||||
// https://www.microsoft.com/developerblog/2015/11/09/reading-and-writing-to-the-windows-registry-in-process-from-node-js/
|
|
||||||
// https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/reg-add
|
|
||||||
// https://social.msdn.microsoft.com/Forums/en-US/5b318f44-281e-4098-8dee-3ba8435fa391/add-registry-key-for-autostart-of-app-in-ice?forum=quebectools
|
|
||||||
// utils.elevate
|
|
||||||
// https://github.com/CatalystCode/windows-registry-node
|
|
||||||
exec('where reg.exe', things._execOpts, function (err, stdout, stderr) {
|
|
||||||
//console.log((stdout||'').trim());
|
|
||||||
if (stderr) {
|
|
||||||
console.error(stderr);
|
|
||||||
}
|
|
||||||
run(err, 'reg.exe');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (module === require.main) {
|
if (module === require.main) {
|
||||||
module.exports.install({
|
Launcher.install({
|
||||||
argv: process.argv
|
argv: process.argv
|
||||||
, env: process.env
|
, env: process.env
|
||||||
}, function (err) {
|
}, function (err) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user