2018-06-27 09:21:44 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
//var fs = require('fs');
|
|
|
|
var os = require('os');
|
|
|
|
var mkdirp = require('mkdirp');
|
|
|
|
var exec = require('child_process').exec;
|
|
|
|
var path = require('path');
|
|
|
|
|
2018-06-28 03:35:14 +00:00
|
|
|
var Launcher = module.exports;
|
|
|
|
Launcher.install = function (things, fn) {
|
|
|
|
if (!fn) { fn = function (err) { if (err) { console.error(err); } }; }
|
2018-06-27 09:21:44 +00:00
|
|
|
things = things || {};
|
|
|
|
// in some future version we can take this file out
|
|
|
|
// and accept process.env from things
|
2018-06-27 10:24:56 +00:00
|
|
|
var installLauncher = require('./template-launcher');
|
2018-06-27 09:21:44 +00:00
|
|
|
|
|
|
|
// Right now this is just for npm install -g and npx
|
|
|
|
if (things.env) {
|
|
|
|
things.env.PATH = things.env.PATH || process.env.PATH;
|
2018-06-28 03:35:14 +00:00
|
|
|
} else {
|
|
|
|
things.env = process.env;
|
2018-06-27 09:21:44 +00:00
|
|
|
}
|
2018-06-28 03:35:14 +00:00
|
|
|
things.argv = things.argv || process.argv;
|
|
|
|
things._execOpts = { windowsHide: true, env: things.env };
|
2018-06-27 09:21:44 +00:00
|
|
|
var telebitRoot = path.join(__dirname, '../..');
|
|
|
|
var vars = {
|
|
|
|
telebitPath: telebitRoot
|
|
|
|
, telebitUser: os.userInfo().username
|
|
|
|
, telebitGroup: (/^darwin/i.test(os.platform()) ? 'staff' : os.userInfo().username)
|
|
|
|
, telebitRwDirs: [
|
2018-06-28 07:53:03 +00:00
|
|
|
telebitRoot
|
2018-06-27 09:21:44 +00:00
|
|
|
, path.join(os.homedir(), '.config/telebit')
|
|
|
|
, path.join(os.homedir(), '.local/share/telebit')
|
2018-06-27 10:24:56 +00:00
|
|
|
]
|
2018-06-28 03:35:14 +00:00
|
|
|
, telebitNode: (things.argv[0]||'').replace(/\.exe/i, '') // path.join(telebitRoot, 'bin/node')
|
2018-06-27 09:21:44 +00:00
|
|
|
, telebitBin: path.join(telebitRoot, 'bin/telebit')
|
|
|
|
, telebitdBin: path.join(telebitRoot, 'bin/telebitd')
|
2018-06-27 10:24:56 +00:00
|
|
|
, telebitJs: path.join(telebitRoot, 'bin/telebit.js')
|
|
|
|
, telebitdJs: path.join(telebitRoot, 'bin/telebitd.js')
|
2018-06-27 09:21:44 +00:00
|
|
|
, telebitConfig: path.join(os.homedir(), '.config/telebit/telebit.yml')
|
|
|
|
, telebitdConfig: path.join(os.homedir(), '.config/telebit/telebitd.yml')
|
2018-06-28 04:58:43 +00:00
|
|
|
, TELEBIT_LOG_DIR: path.join(os.homedir(), '.local/share/telebit/var/log')
|
2018-06-28 18:37:57 +00:00
|
|
|
, TELEBIT_SOCK_DIR: path.join(os.homedir(), '.local/share/telebit/var/run')
|
2018-06-27 09:21:44 +00:00
|
|
|
};
|
2018-06-28 03:35:14 +00:00
|
|
|
vars.telebitBinTpl = path.join(telebitRoot, 'usr/share/dist/bin/telebit.tpl');
|
2018-06-27 10:24:56 +00:00
|
|
|
vars.telebitNpm = path.resolve(vars.telebitNode, '../npm');
|
|
|
|
vars.nodePath = path.resolve(vars.telebitNode, '../lib/node_modules');
|
|
|
|
vars.npmConfigPrefix = path.resolve(vars.telebitNode, '..');
|
2018-06-28 03:35:14 +00:00
|
|
|
vars.userspace = (!things.telebitUser || (things.telebitUser === os.userInfo().username)) ? true : false;
|
2018-06-27 10:24:56 +00:00
|
|
|
if (-1 === vars.telebitRwDirs.indexOf(vars.npmConfigPrefix)) {
|
|
|
|
vars.telebitRwDirs.push(vars.npmConfigPrefix);
|
|
|
|
}
|
|
|
|
vars.telebitRwDirs = vars.telebitRwDirs.join(' ');
|
2018-06-28 03:35:14 +00:00
|
|
|
function getError(err, stderr) {
|
|
|
|
if (err) { return err; }
|
|
|
|
if (stderr) {
|
|
|
|
err = new Error(stderr);
|
|
|
|
err.code = 'ELAUNCHER';
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2018-06-27 09:21:44 +00:00
|
|
|
var launchers = {
|
2018-06-28 03:35:14 +00:00
|
|
|
'node': function () {
|
|
|
|
var fs = require('fs');
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var logpath = path.join(os.homedir(), '.local/share/telebit/var/log');
|
|
|
|
try {
|
|
|
|
mkdirp.sync(logpath);
|
|
|
|
} catch(e) {
|
|
|
|
if (fn) { fn(e); return; }
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var stdout = fs.openSync(path.join(logpath, 'info.log'), 'a');
|
|
|
|
var stderr = fs.openSync(path.join(logpath, 'error.log'), 'a');
|
|
|
|
|
2018-06-28 07:53:03 +00:00
|
|
|
var killed = 0;
|
2018-06-28 03:35:14 +00:00
|
|
|
var err;
|
2018-07-03 10:12:53 +00:00
|
|
|
var args = [
|
|
|
|
path.join(telebitRoot, 'bin/telebitd.js')
|
|
|
|
, 'daemon'
|
|
|
|
, '--config'
|
|
|
|
, vars.telebitdConfig
|
|
|
|
];
|
2018-06-28 03:35:14 +00:00
|
|
|
var subprocess = spawn(
|
|
|
|
vars.telebitNode
|
2018-07-03 10:12:53 +00:00
|
|
|
, args
|
2018-06-28 03:35:14 +00:00
|
|
|
, { detached: true
|
|
|
|
, stdio: [ 'ignore', stdout, stderr ]
|
|
|
|
}
|
|
|
|
);
|
2018-07-03 10:12:53 +00:00
|
|
|
//console.log('[debug]', vars.telebitNode, args.join(' '));
|
2018-06-28 03:35:14 +00:00
|
|
|
subprocess.unref();
|
|
|
|
subprocess.on('error', function (_err) {
|
|
|
|
err = _err;
|
2018-06-28 07:53:03 +00:00
|
|
|
killed += 1;
|
2018-06-28 03:35:14 +00:00
|
|
|
});
|
|
|
|
subprocess.on('exit', function (code, signal) {
|
|
|
|
if (!err) { err = new Error('' + code + ' ' + signal + ' failure to launch'); }
|
2018-06-28 07:53:03 +00:00
|
|
|
killed += 1;
|
2018-06-28 03:35:14 +00:00
|
|
|
});
|
|
|
|
|
2018-06-28 09:12:13 +00:00
|
|
|
// Two things:
|
|
|
|
// 1) wait to see if the process dies
|
2018-07-03 10:12:53 +00:00
|
|
|
// 2) wait to give time for the socket to connect
|
2018-06-28 03:35:14 +00:00
|
|
|
setTimeout(function () {
|
2018-06-28 09:12:13 +00:00
|
|
|
if (fn) { fn(err); return; }
|
2018-07-03 10:12:53 +00:00
|
|
|
}, 1.75 * 1000);
|
2018-06-28 03:35:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
, 'launchctl': function () {
|
2018-06-27 09:21:44 +00:00
|
|
|
var launcher = path.join(os.homedir(), 'Library/LaunchAgents/cloud.telebit.remote.plist');
|
|
|
|
try {
|
|
|
|
mkdirp.sync(path.join(os.homedir(), 'Library/LaunchAgents'));
|
2018-06-27 10:24:56 +00:00
|
|
|
installLauncher.sync({
|
2018-06-28 03:35:14 +00:00
|
|
|
file: {
|
|
|
|
tpl: vars.telebitBinTpl
|
|
|
|
, launcher: path.join(vars.telebitPath, 'bin/telebit')
|
2018-06-28 19:01:19 +00:00
|
|
|
, executable: true
|
2018-06-27 09:21:44 +00:00
|
|
|
}
|
|
|
|
, vars: vars
|
2018-06-27 10:24:56 +00:00
|
|
|
});
|
|
|
|
installLauncher({
|
|
|
|
file: {
|
2018-06-28 03:35:14 +00:00
|
|
|
tpl: path.join(vars.telebitPath, 'usr/share/dist/etc/skel/Library/LaunchAgents/cloud.telebit.remote.plist.tpl')
|
|
|
|
, launcher: launcher
|
2018-06-27 10:24:56 +00:00
|
|
|
}
|
2018-06-28 03:35:14 +00:00
|
|
|
, vars: vars
|
|
|
|
});
|
|
|
|
var launcherstr = (vars.userspace ? "" : "sudo ") + "launchctl ";
|
2018-06-28 04:58:43 +00:00
|
|
|
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 = getError(err, stderr);
|
|
|
|
//if (err) { fn(err); return; }
|
2018-06-28 03:35:14 +00:00
|
|
|
//console.log((stdout||'').trim());
|
|
|
|
//console.log('unload worked?');
|
2018-06-28 04:58:43 +00:00
|
|
|
execstr = launcherstr + "load -w " + launcher;
|
|
|
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
2018-06-28 03:35:14 +00:00
|
|
|
err = getError(err, stderr);
|
|
|
|
if (err) { fn(err); return; }
|
|
|
|
//console.log((stdout||'').trim());
|
|
|
|
//console.log('load worked?');
|
2018-06-28 09:12:13 +00:00
|
|
|
setTimeout(function () {
|
|
|
|
fn(null);
|
|
|
|
}, 1.25 * 1000);
|
2018-06-27 09:21:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch(e) {
|
2018-06-27 10:24:56 +00:00
|
|
|
console.error("'" + launcher + "' error:");
|
|
|
|
console.error(e);
|
2018-06-28 03:35:14 +00:00
|
|
|
if (fn) { fn(e); return; }
|
2018-06-27 09:21:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-28 03:35:14 +00:00
|
|
|
, 'systemctl': function () {
|
2018-06-27 09:21:44 +00:00
|
|
|
var launcher = path.join(os.homedir(), '.config/systemd/user/telebit.service');
|
2018-06-28 07:53:03 +00:00
|
|
|
var launchername = 'telebit.service';
|
2018-06-27 09:21:44 +00:00
|
|
|
try {
|
|
|
|
mkdirp.sync(path.join(os.homedir(), '.config/systemd/user'));
|
|
|
|
installLauncher({
|
|
|
|
file: {
|
2018-06-28 03:35:14 +00:00
|
|
|
tpl: path.join(vars.telebitPath, 'usr/share/dist/etc/skel/.config/systemd/user/telebit.service.tpl')
|
2018-06-27 09:21:44 +00:00
|
|
|
, launcher: launcher
|
|
|
|
}
|
|
|
|
, vars: vars
|
|
|
|
}, function () {
|
2018-06-28 07:53:03 +00:00
|
|
|
// 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)
|
2018-06-28 03:35:14 +00:00
|
|
|
var launcherstr = (vars.userspace ? "" : "sudo ") + "systemctl " + (vars.userspace ? "--user " : "");
|
2018-06-28 08:01:43 +00:00
|
|
|
var execstr = launcherstr + "daemon-reload";
|
|
|
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
2018-06-28 03:35:14 +00:00
|
|
|
err = getError(err, stderr);
|
|
|
|
if (err) { fn(err); return; }
|
|
|
|
//console.log((stdout||'').trim());
|
2018-06-28 08:01:43 +00:00
|
|
|
var execstr = launcherstr + "enable " + launchername;
|
|
|
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
2018-07-21 10:42:44 +00:00
|
|
|
err = getError(err, !/Created symlink/.test(stderr||''));
|
2018-06-28 03:35:14 +00:00
|
|
|
if (err) { fn(err); return; }
|
|
|
|
//console.log((stdout||'').trim());
|
2018-06-28 08:01:43 +00:00
|
|
|
var execstr = launcherstr + "restart " + launchername;
|
|
|
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
2018-06-28 03:35:14 +00:00
|
|
|
err = getError(err, stderr);
|
|
|
|
if (err) { fn(err); return; }
|
|
|
|
//console.log((stdout||'').trim());
|
2018-06-28 08:01:43 +00:00
|
|
|
setTimeout(function () {
|
|
|
|
var execstr = launcherstr + "status " + launchername;
|
|
|
|
exec(execstr, things._execOpts, function (err, stdout, stderr) {
|
|
|
|
err = getError(err, stderr);
|
|
|
|
if (err) { fn(err); return; }
|
|
|
|
if (!/active.*running/i.test(stdout)) {
|
|
|
|
err = new Error("systemd failed to start '" + launchername + "'");
|
|
|
|
}
|
|
|
|
if (err) { fn(err); return; }
|
|
|
|
//console.log((stdout||'').trim());
|
|
|
|
fn(null);
|
|
|
|
});
|
|
|
|
}, 1.25 * 1000);
|
2018-06-27 09:21:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch(e) {
|
2018-06-27 10:24:56 +00:00
|
|
|
console.error("'" + launcher + "' error:");
|
|
|
|
console.error(e);
|
2018-06-28 03:35:14 +00:00
|
|
|
if (fn) { fn(e); return; }
|
2018-06-27 09:21:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
, 'reg.exe': function () {
|
2018-06-28 03:35:14 +00:00
|
|
|
if (!vars.userspace) {
|
|
|
|
console.warn("sysetm-level, privileged services are not yet supported on windows");
|
|
|
|
}
|
2018-06-27 09:21:44 +00:00
|
|
|
vars.telebitNode += '.exe';
|
|
|
|
var cmd = 'reg.exe add "HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"'
|
|
|
|
+ ' /V "Telebit" /t REG_SZ /D '
|
2018-06-28 03:35:14 +00:00
|
|
|
+ '"' + things.argv[0] + ' /c ' // something like C:\\Program Files (x64)\nodejs\node.exe
|
2018-06-27 09:21:44 +00:00
|
|
|
+ [ path.join(__dirname, 'bin/telebitd.js')
|
|
|
|
, 'daemon'
|
|
|
|
, '--config'
|
|
|
|
, path.join(os.homedir(), '.config/telebit/telebitd.yml')
|
|
|
|
].join(' ')
|
|
|
|
+ '" /F'
|
|
|
|
;
|
2018-06-28 03:35:14 +00:00
|
|
|
exec(cmd, things._execOpts, function (err, stdout, stderr) {
|
|
|
|
err = getError(err, stderr);
|
|
|
|
if (err) { fn(err); return; }
|
2018-06-28 09:12:13 +00:00
|
|
|
// need to start it for the first time ourselves
|
2018-06-28 18:37:57 +00:00
|
|
|
run(null, 'node');
|
2018-06-28 03:35:14 +00:00
|
|
|
});
|
2018-06-27 09:21:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function run(err, launcher) {
|
|
|
|
if (err) {
|
2018-06-28 03:35:14 +00:00
|
|
|
console.error("No luck with '" + launcher + "', trying a child process instead...");
|
2018-06-27 10:24:56 +00:00
|
|
|
console.error(err);
|
2018-06-28 03:35:14 +00:00
|
|
|
launcher = 'node';
|
2018-06-27 09:21:44 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 10:24:56 +00:00
|
|
|
if (launchers[launcher]) {
|
2018-06-28 03:35:14 +00:00
|
|
|
// console.log('Launching with launcher ' + launcher);
|
2018-06-28 18:37:57 +00:00
|
|
|
mkdirp.sync(path.join(vars.telebitPath, 'bin'));
|
|
|
|
mkdirp.sync(vars.TELEBIT_LOG_DIR);
|
|
|
|
mkdirp.sync(vars.TELEBIT_SOCK_DIR);
|
2018-06-27 09:21:44 +00:00
|
|
|
launchers[launcher]();
|
|
|
|
return;
|
2018-06-27 10:24:56 +00:00
|
|
|
} else {
|
|
|
|
console.error("No launcher handler for '" + launcher+ "'");
|
2018-06-27 09:21:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 03:35:14 +00:00
|
|
|
if (things.launcher) {
|
|
|
|
if ('string' === typeof things.launcher) {
|
|
|
|
run(null, things.launcher);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ('function' === typeof things.launcher) {
|
|
|
|
things._vars = vars;
|
|
|
|
things._userspace = vars.userspace;
|
|
|
|
things.launcher(things);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 09:21:44 +00:00
|
|
|
// could have used "command-exists" but I'm trying to stay low-dependency
|
|
|
|
// os.platform(), os.type()
|
|
|
|
if (!/^win/i.test(os.platform())) {
|
2018-06-27 10:24:56 +00:00
|
|
|
if (/^darwin/i.test(os.platform())) {
|
2018-06-28 07:53:03 +00:00
|
|
|
exec('command -v launchctl', things._execOpts, function (err, stdout, stderr) {
|
2018-06-28 03:35:14 +00:00
|
|
|
err = getError(err, stderr);
|
2018-06-27 09:21:44 +00:00
|
|
|
run(err, 'launchctl');
|
2018-06-28 03:35:14 +00:00
|
|
|
});
|
2018-06-27 09:21:44 +00:00
|
|
|
} else {
|
2018-06-28 07:53:03 +00:00
|
|
|
exec('command -v systemctl', things._execOpts, function (err, stdout, stderr) {
|
2018-06-28 03:35:14 +00:00
|
|
|
err = getError(err, stderr);
|
2018-06-27 10:24:56 +00:00
|
|
|
run(err, 'systemctl');
|
2018-06-28 03:35:14 +00:00
|
|
|
});
|
2018-06-27 09:21:44 +00:00
|
|
|
}
|
|
|
|
} 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
|
2018-06-28 03:35:14 +00:00
|
|
|
exec('where reg.exe', things._execOpts, function (err, stdout, stderr) {
|
2018-07-03 10:12:53 +00:00
|
|
|
//console.log((stdout||'').trim());
|
2018-06-27 09:21:44 +00:00
|
|
|
if (stderr) {
|
|
|
|
console.error(stderr);
|
|
|
|
}
|
|
|
|
run(err, 'reg.exe');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2018-06-27 10:24:56 +00:00
|
|
|
|
|
|
|
if (module === require.main) {
|
2018-06-28 03:35:14 +00:00
|
|
|
module.exports.install({
|
|
|
|
argv: process.argv
|
|
|
|
, env: process.env
|
|
|
|
}, function (err) {
|
|
|
|
if (err) { console.error(err); return; }
|
|
|
|
console.log("Telebit launched, or so it seems.");
|
|
|
|
});
|
2018-06-27 10:24:56 +00:00
|
|
|
}
|