2018-12-19 11:47:15 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
getRemoteSupport: getRemoteSupport,
|
2019-10-22 11:08:19 -07:00
|
|
|
enableRemoteSupport: enableRemoteSupport
|
2018-12-19 11:47:15 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let assert = require('assert'),
|
2019-10-22 11:08:19 -07:00
|
|
|
BoxError = require('./boxerror.js'),
|
2019-07-26 10:10:14 -07:00
|
|
|
constants = require('./constants.js'),
|
2018-12-19 11:47:15 -08:00
|
|
|
shell = require('./shell.js'),
|
|
|
|
|
once = require('once'),
|
|
|
|
|
path = require('path'),
|
2019-07-26 10:04:54 -07:00
|
|
|
paths = require('./paths.js'),
|
2019-10-22 11:08:19 -07:00
|
|
|
sysinfo = require('./sysinfo.js');
|
2018-12-19 11:47:15 -08:00
|
|
|
|
2019-05-21 09:44:58 -07:00
|
|
|
// the logic here is also used in the cloudron-support tool
|
2019-10-22 11:08:19 -07:00
|
|
|
const AUTHORIZED_KEYS_FILEPATH = constants.TEST ? path.join(paths.baseDir(), 'authorized_keys') : ((sysinfo.provider() === 'ec2' || sysinfo.provider() === 'lightsail' || sysinfo.provider() === 'ami') ? '/home/ubuntu/.ssh/authorized_keys' : '/root/.ssh/authorized_keys'),
|
2019-07-26 10:10:14 -07:00
|
|
|
AUTHORIZED_KEYS_USER = constants.TEST ? process.getuid() : ((sysinfo.provider() === 'ec2' || sysinfo.provider() === 'lightsail' || sysinfo.provider() === 'ami') ? 'ubuntu' : 'root'),
|
2018-12-19 11:47:15 -08:00
|
|
|
AUTHORIZED_KEYS_CMD = path.join(__dirname, 'scripts/remotesupport.sh');
|
|
|
|
|
|
|
|
|
|
function getRemoteSupport(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
callback = once(callback); // exit may or may not be called after an 'error'
|
|
|
|
|
|
|
|
|
|
let result = '';
|
|
|
|
|
let cp = shell.sudo('support', [ AUTHORIZED_KEYS_CMD, 'is-enabled', AUTHORIZED_KEYS_FILEPATH ], {}, function (error) {
|
2019-10-24 18:32:33 -07:00
|
|
|
if (error) callback(new BoxError(BoxError.FS_ERROR, error));
|
2018-12-19 11:47:15 -08:00
|
|
|
|
|
|
|
|
callback(null, { enabled: result.trim() === 'true' });
|
|
|
|
|
});
|
|
|
|
|
cp.stdout.on('data', (data) => result = result + data.toString('utf8'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enableRemoteSupport(enable, callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
shell.sudo('support', [ AUTHORIZED_KEYS_CMD, enable ? 'enable' : 'disable', AUTHORIZED_KEYS_FILEPATH, AUTHORIZED_KEYS_USER ], {}, function (error) {
|
2019-10-24 18:32:33 -07:00
|
|
|
if (error) callback(new BoxError(BoxError.FS_ERROR, error));
|
2018-12-19 11:47:15 -08:00
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
}
|