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

Feat/alternative to semver #945

Merged
merged 8 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
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
Binary file modified bin/prey-user
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/agent/actions/triggers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function set_up_hooks() {
}

if (action.after) {
var last_connection = lp.last_connection();
var last_connection = websocket.lastConnection();
if (!last_connection) {
logger.debug('No last connection to server registered');
return;
Expand Down
17 changes: 15 additions & 2 deletions lib/agent/control-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ const lpConf = require('../../conf/long-polling');
const common = require('../common');
const hooks = require('../hooks');
const commands = require('../commands');
const permissions = require('../permissions');
const listeners = require('../socket/listeners');

const { logger } = common;
const config = require('../../utils/configfile');

exports.timeout_send_info_encrypt = 8 * 60 * 60 * 1000; // Every 8 hours
const timeoutPermissionInformation = 5 * 60 * 60 * 1000;

const init_api = (opts, cb) => {
if (!opts) return cb && cb(new Error('Invalid config.'));
const data = {
Expand Down Expand Up @@ -44,6 +49,8 @@ const handle_response = (what, err, resp) => {
};

const load_hooks = () => {
hooks.on('check-location-perms', listeners.reactToCheckLocationPerms);
hooks.on('info', listeners.reactToWdutil);
hooks.on('action', websocket.notify_action);
hooks.on('event', sender.notify_event);
hooks.on('data', sender.send_data);
Expand All @@ -59,6 +66,8 @@ const load_hooks = () => {
};

const unload_hooks = () => {
hooks.remove('check-location-perms', listeners.reactToCheckLocationPerms);
hooks.remove('info', listeners.reactToWdutil);
hooks.remove('action', sender.notify_action);
hooks.remove('event', sender.notify_event);
hooks.remove('data', sender.send_data);
Expand All @@ -71,12 +80,18 @@ const boot = (cb) => {
lpConf.unload();
load_hooks();
sync();
logger.info('GETTING PERMISSION LOCATION');
permissions.getLocationPermission();
websocket.load.call(common, (err, emitter) => {
if (!emitter) return;
setInterval(() => {
module.exports.send_info_encrypt(() => {
});
}, exports.timeout_send_info_encrypt);

setInterval(() => {
permissions.getLocationPermission();
}, timeoutPermissionInformation);
emitter.on('command', commands.perform);
});
cb && cb();
Expand All @@ -101,8 +116,6 @@ const wait_for_config = (cb) => {
}, 10000); // 10 seconds
};

exports.timeout_send_info_encrypt = 8 * 60 * 60 * 1000; // Every 8 hours

module.exports.send_info_encrypt = function (cb) {
const data = {};
const os_name = os.platform().replace('win32', 'windows').replace('darwin', 'mac');
Expand Down
1 change: 1 addition & 0 deletions lib/agent/control-panel/websockets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ exports.heartbeatTimed = () => {
const updateTimestamp = () => {
lastTime = Date.now();
};
exports.lastConnection = () => lastConnection;

const loadHooks = () => {
storage.do('query', { type: 'keys', column: 'id', data: 'last_connection' }, (err, stored) => {
Expand Down
17 changes: 17 additions & 0 deletions lib/agent/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ helpers.run_via_service = function(){
return (process.platform == 'win32' && !process.env.HOMEPATH);
}

helpers.greaterOrEqual = (first, second) => {
if (typeof first !== 'string' || typeof second !== 'string') return -1;
const versionAOutDot = first.replaceAll('.', '');
const versionBOutDot = second.replaceAll('.', '');
let versionADotInt = parseInt(versionAOutDot, 10);
let versionBDotInt = parseInt(versionBOutDot, 10);

if (versionAOutDot.length === versionBOutDot.length) return (versionADotInt >= versionBDotInt);
if (versionAOutDot.length > versionBOutDot.length) {
versionBDotInt *= 10 ** (versionAOutDot.length - versionBOutDot.length);
}
if (versionAOutDot.length < versionBOutDot.length) {
versionADotInt *= 10 ** (versionBOutDot.length - versionAOutDot.length);
}

return (versionADotInt >= versionBDotInt);
};

// is_greater_than("1.3.10", "1.3.9") returns true
helpers.is_greater_than = function(first, second) {
Expand Down
14 changes: 14 additions & 0 deletions lib/agent/permissions/darwin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const socket = require('../socket');

const permissionFunction = 'check-location-perms';
/**
* Executes the `getLocationPermission` function.
*
* @param {type} paramName - description of parameter
* @return {type} description of return value
*/
const getLocationPermission = () => {
socket.writeMessage(permissionFunction);
};

exports.getLocationPermission = getLocationPermission;
7 changes: 7 additions & 0 deletions lib/agent/permissions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const osName = process.platform.replace('win32', 'windows');
// eslint-disable-next-line import/no-dynamic-require
const osFunctions = require(`./${osName}`);

const { getLocationPermission } = osFunctions;

exports.getLocationPermission = getLocationPermission;
42 changes: 21 additions & 21 deletions lib/agent/providers/geo/darwin/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
var join = require('path').join,
app = join(__dirname, '..', '..', '..', 'utils', 'Prey.app'),
common = require('../../../common'),
system = common.system,
run_as_user = system.run_as_logged_user,
gte = common.helpers.is_greater_or_equal;
const { join } = require('path');
const common = require('../../../common');

exports.get_location = function(cb) {
system.get_os_version((err, version) => {
if (version && gte(version, "10.6.0")) {
var bin = join(app, 'Contents', 'MacOS', 'Prey'),
args = ['-location'];
const app = join(__dirname, '..', '..', '..', 'utils', 'Prey.app');
const { system } = common;
const runAsUser = system.run_as_logged_user;
const { greaterOrEqual } = common.helpers;

run_as_user(bin, args, {timeout: 5000}, (err, data) => {
if (err || (data && data.includes('error'))) return cb(new Error('Unable to get native location'));
exports.get_location = (cb) => {
// eslint-disable-next-line consistent-return
system.get_os_version((_err, version) => {
if (version && greaterOrEqual(version, '10.6.0')) {
const bin = join(app, 'Contents', 'MacOS', 'Prey');
const args = ['-location'];

runAsUser(bin, args, { timeout: 120000 }, (errRun, data) => {
if (errRun || (data && data.includes('error'))) return cb(new Error('Unable to get native location'));
let response;
try {
response = JSON.parse(data);
if(response.hasOwnProperty('accuracy'))
response.accuracy = parseFloat(response.accuracy).toFixed(6);
return cb(null, response)
} catch (err) {
return cb(new Error(err.message))
if (Object.prototype.hasOwnProperty.call(response, 'accuracy')) response.accuracy = parseFloat(response.accuracy).toFixed(6);
return cb(null, response);
} catch (errExcept) {
return cb(new Error(errExcept.message));
}
})

});
} else {
return cb(new Error('Not yet supported'));
}
});
}
};
38 changes: 27 additions & 11 deletions lib/agent/providers/geo/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const strategies = require('./strategies');
const hooks = require('../../hooks');
const logger = require('../../common').logger.prefix('geo');
const permissionFile = require('../../../utils/permissionfile');
const socket = require('../../socket');

const osName = process.platform.replace('win32', 'windows').replace('darwin', 'mac');
const strategiesList = ['native', 'wifi', 'geoip'];

let defaultStrategy = 'wifi';
Expand All @@ -19,17 +22,30 @@ exports.get_location = (cb) => {
hooks.trigger('get_location', cb);
};

const strategyCallback = (err, res, cb) => {
if (err) {
logError(err, current);
const next = strategiesList.indexOf(current) + 1;
if (next === strategiesList.length) return cb(err);
current = strategiesList[next];
return strategies[strategiesList[next]]((errInside, resInside) => {
strategyCallback(errInside, resInside, cb);
});
}
return cb(null, res);
};

// eslint-disable-next-line consistent-return
exports.fetch_location = (cb) => {
current = defaultStrategy;
const strategyCallback = (err, res) => {
if (err) {
logError(err, current);
const next = strategiesList.indexOf(current) + 1;
if (next === strategiesList.length) return cb(err);
current = strategiesList[next];
return strategies[strategiesList[next]](strategyCallback);
if (osName === 'mac') {
// eslint-disable-next-line consistent-return
const permissionNative = permissionFile.getData('nativeLocation');
if (!permissionNative || permissionNative.localeCompare('true') !== 0) {
socket.writeMessage('check-location-perms');
return cb(new Error('Not allowed.'));
}
return cb(null, res);
};
strategies[defaultStrategy](strategyCallback);
defaultStrategy = 'native';
}
current = defaultStrategy;
strategies[defaultStrategy]((err, res) => strategyCallback(err, res, cb));
};
2 changes: 2 additions & 0 deletions lib/agent/providers/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ exp.get_wireless_interfaces_list = osFunctions.get_wireless_interfaces_list;

exp.get_active_access_point_mac = osFunctions.get_active_access_point_mac;

exp.isWifiPermissionActive = osFunctions.isWifiPermissionActive;

/**
*
* */
Expand Down
4 changes: 4 additions & 0 deletions lib/agent/providers/network/linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,7 @@ exports.parse_access_points_list = function(output) {
}).filter(function(el) { return !!el })

};

exports.isWifiPermissionActive = (callback) => {
callback();
};
Loading