Skip to content

Commit

Permalink
Merge pull request #408 from prey/auth-command-restart-wipe-fixes
Browse files Browse the repository at this point in the history
Authorize client restart and wipe fixes
  • Loading branch information
javo authored Nov 21, 2018
2 parents b1160f3 + 6b6749d commit 0b3e121
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 28 deletions.
18 changes: 7 additions & 11 deletions lib/agent/actions/wipe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ exports.start = function(opts, cb) {
// run it as another process, using impersonation (to avoid permission errors)
var spawn = function() {
var wipe_opts = [
'-device-key', keys.get().device.toString(),
'-token', opts.token,
'-token', opts.token,
exports.cloud.toString(),
exports.directories.toString()
];
Expand All @@ -105,13 +104,11 @@ exports.start = function(opts, cb) {
if (typeof child == 'function') { // only for windows
os_wipe.paths.directories = exports.directories;

wipe.fetch_dirs(items, exports.directories, exports.cloud, (err, dirs_to_wipe) => {
wipe.fetch_dirs(items, exports.directories, exports.cloud, function(err, dirs_to_wipe) {
var opts = {
dirs: dirs_to_wipe,
token: token,
key: keys.get().device.toString(),
options: {
dirs: dirs_to_wipe
}
key: keys.get().device.toString()
};

child('wipe', opts, function(err) {
Expand All @@ -124,7 +121,7 @@ exports.start = function(opts, cb) {
child.stdout.on('data', function(str) {
var lines = str.toString().split(/\n/);
lines.forEach(function(line) {
if (line.toString().match('Removing file')) {
if (line.toString().match('Removing directory')) {
logger.warn(line.trim());
removed++;
} else if (line.toString().match('Error while removing dir')) {
Expand All @@ -148,14 +145,14 @@ exports.start = function(opts, cb) {
}

var finished = function(err, service) {
logger.warn('Process finished! ' + (service ? '' : removed + ' file(s) removed.'));
logger.warn('Process finished! ' + (service ? '' : removed + ' dir(s) removed.'));

if (!emitter) return;
if (service) return emitter.emit('end', err);

// if no files were removed, treat that as an error
if (!err && removed == 0) {
emitter.emit('end', new Error('No files were removed.'));
emitter.emit('end', new Error('No dirs were removed.'));
} else {
emitter.emit('end', err, { files_removed: removed });
}
Expand All @@ -164,7 +161,6 @@ exports.start = function(opts, cb) {
emitter = new Emitter;
cb(null, emitter);
fs.existsSync(node_bin) ? spawn() : finished(new Error('Node binary not present'));
if (!opts.token) return finished(new Error("Security Token necessary"))

}

Expand Down
Binary file modified lib/agent/actions/wipe/linux/wipe-linux
Binary file not shown.
Binary file modified lib/agent/actions/wipe/mac/wipe-osx
Binary file not shown.
Binary file modified lib/agent/actions/wipe/windows/wipe-win.exe
100755 → 100644
Binary file not shown.
12 changes: 9 additions & 3 deletions lib/agent/actions/wipe/wipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,15 @@ exports.fetch_dirs = function(items, to_erase, to_kill, cb) {
}

exports.wipeout = function(cb) {
var cmd = secure_wipe_cmd + '"' + dirs_to_wipe.join(',') + '" ' + credentials.join(' ');
exec(cmd, (err, stdout) => {
return cb(err, stdout)
var output = '';
dirs_to_wipe.forEach((dir, index) => {
var cmd = secure_wipe_cmd + '"' + dir + '" ' + credentials.join(' ');

exec(cmd, (err, stdout) => {
output += stdout;
if (index == dirs_to_wipe.length - 1)
return cb(err, output)
})
})
}

Expand Down
4 changes: 2 additions & 2 deletions lib/agent/triggers/hostname/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ var check_hostname = () => {
}

get_current_hostname()
.catch(error => { return done(new Error('Unable to get device current hostname')) })
.then(current_name => {
device_keys.exist('hostname', (err, stored) => {
if (err) return done(new Error('Error checking stored hostname'));
Expand All @@ -54,12 +53,12 @@ var check_hostname = () => {
} else device_keys.store('hostname', current_name, (err) => { done(err) });
});
})
.catch(error => { return done(new Error('Unable to get device current hostname')) })

}

exports.start = (opts, cb) => {
triggers.watch('hostname')
.catch(error => { return cb(error); })
.then((hostname) => {
hostname.on('state_changed', (info) => {
check_hostname();
Expand All @@ -69,6 +68,7 @@ exports.start = (opts, cb) => {

check_hostname();
})
.catch(error => { return cb(error); })
};

exports.stop = () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/agent/triggers/power/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ var check_battery_status = () => {
exports.start = (opts, cb) => {

triggers.watch('power')
.catch(error => { return cb(error); })
.then((power) => {
power.on('state_changed', (info) => {
setTimeout(check_battery_status, 1500);
Expand All @@ -43,6 +42,7 @@ exports.start = (opts, cb) => {
emitter = new Emitter();
cb(null, emitter);
})
.catch(error => { return cb(error); })
};

exports.stop = () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/agent/utils/custom-dirs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ exports.validateCustomDirs = (dirs, should_be_home) => {
let directories = [],
cloud = {};

dirs = dirs.split(',');
dirs = dirs.split(/[\n,]+/);
dirs.forEach((dir, index) => {
dirs[index] = dir.trim();

Expand Down
29 changes: 29 additions & 0 deletions lib/conf/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ common.program.logfile = system.tempfile_path(logfile);

var argv = process.argv.splice(3),
Operetta = require('./utils/operetta').Operetta,
exec = require('child_process').exec,
cli = new Operetta(argv, 'config');

var account = require('./account'),
Expand All @@ -46,6 +47,30 @@ var via_npm = function() {
return !!(process.env.npm_package_version || process.env.npm_lifecycle_script);
}

var restart_client = () => {
var client_pid = function(cb) {
var process_id;
if (os_name == 'windows') {
process_id = `for /f "tokens=2 delims=," %F in ('tasklist /nh /fi "imagename eq node.exe" /fo csv') do @echo %~F`;
} else {
var awk = os_name == 'mac' ? 2 : 1
process_id = "ps -u prey | grep prx | awk '{print $'" + awk + "'}'";
}

exec(process_id, function(err, pid) {
if (err) return cb("Error getting client process id:" + err)
pid = pid.toString().split("\r\n")[0];
cb(null, pid)
})
}

client_pid(function(err, pid) {
if (err || !pid) return;
var restart_cmd = os_name == 'windows' ? 'taskkill /F /PID ' : 'kill -9 ';
exec(restart_cmd + pid);
})
}

var start = function(cb) {

var run = function(scope, command) {
Expand Down Expand Up @@ -75,6 +100,10 @@ var start = function(cb) {
cmd.parameters(['-e', '--email'], 'Email')
cmd.parameters(['-p', '--password'], 'Password')
run_if_writable(cmd, account.authorize);

setTimeout(() => { // Wait for the key to be set
restart_client();
}, 2000)
});

sub.command('verify', 'Verifies API & Device keys, optionally saving them to config.', function(cmd) {
Expand Down
14 changes: 4 additions & 10 deletions lib/system/windows/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ var path = require('path'),
spawn = cp.spawn,
os_name = process.platform.replace('win32', 'windows');

var LOCALHOST_SERVICE = 'http://127.0.0.1:7739',
OPEN_TIMEOUT = 1000 * 60 * 60;

var LOCALHOST_ACTION = 'http://127.0.0.1:7739/action';
exports.monitoring_service_go = false;

// add windows bin path to env
Expand Down Expand Up @@ -77,7 +75,7 @@ exports.scan_networks = function(cb) {
exports.check_service = function(data, cb) {
if (exports.monitoring_service_go) return cb(null, data);

needle.get(LOCALHOST_SERVICE + '/action', function(err, resp) {
needle.get(LOCALHOST_ACTION, function(err, resp) {
if (err) return cb(err, data);
exports.monitoring_service_go = true;
return cb(null, data);
Expand All @@ -89,14 +87,10 @@ exports.run_as_admin = function(command, opts, cb) {
action: command,
key: opts.key,
token: opts.token,
opts: opts.options
},
options = {
json: true,
open_timeout: OPEN_TIMEOUT
opts: opts.dirs
};

needle.post(LOCALHOST_SERVICE + '/action', body, options, function(err, resp, body) {
needle.post(LOCALHOST_ACTION, body, { json : true }, function(err, resp, body) {
if (err) return cb(err);
return cb(null);
});
Expand Down

0 comments on commit 0b3e121

Please sign in to comment.