Skip to content
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

Fixs alert problem when message is not a string #578

Merged
merged 1 commit into from
Apr 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 54 additions & 56 deletions lib/agent/actions/alert/index.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,89 @@
/* eslint-disable linebreak-style */
// Prey Alert
// Written by Tomas Pollak

var join = require('path').join,
common = require('./../../common'),
system = common.system,
flash = join(__dirname, process.platform, 'flash'),
prey_app = join(__dirname, '..', '..', 'utils', 'Prey.app'),
actions_app = join(__dirname, '..', '..', 'utils', 'prey-actions.app'),
Emitter = require('events').EventEmitter,
is_win = process.platform == 'win32',
is_mac = process.platform == 'darwin';

var child,
emitter,
app,
binary,
user_input_str = 'User input: ';

exports.start = function(id, opts, cb) {
var opts = opts || {},
message = opts.message || opts.alert_message,
title = opts.title,
level = opts.level || 'info',
reply = opts.reply || opts.entry || opts.response;

if (!message || message.toString().trim() == '')
return cb(new Error('Message required'));
const { join } = require('path');
const common = require('../../common');

const { system } = common;
const flash = join(__dirname, process.platform, 'flash');
const preyApp = join(__dirname, '..', '..', 'utils', 'Prey.app');
const actionsApp = join(__dirname, '..', '..', 'utils', 'prey-actions.app');
// eslint-disable-next-line import/order
const Emitter = require('events').EventEmitter;

const isWin = process.platform === 'win32';
const isMac = process.platform === 'darwin';

let child;
let emitter;
let app;
let binary;
const userInputStr = 'User input: ';

// eslint-disable-next-line consistent-return
exports.start = (id, opts, cb) => {
let message = opts.message || opts.alert_message;
const { title } = opts;
const level = opts.level || 'info';
let reply = opts.reply || opts.entry || opts.response;

if (!message || message.toString().trim() === '') return cb(new Error('Message required'));

// remove newlines so the message can be completely displayed
message = message.replace(/(\r\n|\n|\r)/gm," ");
message = message.toString().replace(/(\r\n|\n|\r)/gm, ' ');

var reply,
returned = 0,
bin = flash,
args = ['-l', level];
let returned = 0;
let bin = flash;
let args = ['-l', level];

if (reply)
args = args.concat(['-e', 'Type here:']);
if (reply) args = args.concat(['-e', 'Type here:']);

if (title && title.toString().trim() != '')
args = args.concat(['-t', title]);
if (title && title.toString().trim() !== '') args = args.concat(['-t', title]);

if (is_win) {
if (isWin) {
args.push('-m', message); // in windows, the bin expects a -m message argument
bin += '.exe';
} else if (is_mac) {
} else if (isMac) {
args.push(message);
bin += '.py';
} else {
args.push(message);
bin += ((system.python_version && system.python_version >= "3.0.0") ? '3.py' : '.py');
bin += ((system.python_version && system.python_version >= '3.0.0') ? '3.py' : '.py');
}

if (is_mac && common.os_release >= '10.14') {
app = prey_app;
if (isMac && common.os_release >= '10.14') {
app = preyApp;
binary = 'Prey';
if (common.os_release >= '11.0') {
app = actions_app;
app = actionsApp;
binary = 'prey-actions';
}
bin = join(app, 'Contents', 'MacOS', binary);

bin = join(app, 'Contents', 'MacOS', binary);
args = ['-alert', message];
}

function done(err) {
const done = (err) => {
// eslint-disable-next-line no-plusplus
if (returned++) return;

if (emitter)
emitter.emit('end', id, err, reply);
if (emitter) emitter.emit('end', id, err, reply);

emitter = null;
}
};

system.spawn_as_logged_user(bin, args, function(err, alert) {
// eslint-disable-next-line consistent-return
system.spawn_as_logged_user(bin, args, (err, alert) => {
if (err) return done(err);

alert.stdout.on('data', function(chunk) {
if (chunk.toString().match(user_input_str)) {
reply = chunk.toString().replace(user_input_str, '').trim();
}
alert.stdout.on('data', (chunk) => {
if (chunk.toString().match(userInputStr)) reply = chunk.toString().replace(userInputStr, '').trim();
});

alert.on('error', done);

alert.once('exit', function() {
alert.once('exit', () => {
child = null;
done();
});
Expand All @@ -93,14 +92,13 @@ exports.start = function(id, opts, cb) {
emitter = new Emitter();
cb(null, emitter);
});
};

}

exports.stop = function() {
exports.stop = () => {
// if child is killed, the 'exit' event is triggered
// and it will fire the emitter's end' event, marking
// the action as stopped.
if (child && !child.exitCode) {
child.kill();
}
}
};