Skip to content

Commit c9dca93

Browse files
author
Javier Acuna
committed
Merge branch 'develop' into 'master'
Develop See merge request prey/js/prey-node-client!1238
2 parents df234b1 + 8c9693a commit c9dca93

File tree

8 files changed

+190
-103
lines changed

8 files changed

+190
-103
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Change Log
22

3-
## [v1.13.15](https://github.com/prey/prey-node-client/tree/v1.13.14) (2025-05-28)
3+
## [v1.13.16](https://github.com/prey/prey-node-client/tree/v1.13.16) (2025-05-30)
4+
[Full Changelog](https://github.com/prey/prey-node-client/compare/v1.13.15..v1.13.16)
5+
6+
- Fix: Fixes a bug killing Prey services when calling directly to updater.exe in order to mantain wpxsvc up to date. ([SoraKenji](https://github.com/SoraKenji))
7+
8+
- Fix: Fixes a case when trying to send a new location for location aware tracking and it didn't added "force" parameter. ([SoraKenji](https://github.com/SoraKenji))
9+
10+
## [v1.13.15](https://github.com/prey/prey-node-client/tree/v1.13.15) (2025-05-29)
411
[Full Changelog](https://github.com/prey/prey-node-client/compare/v1.13.14..v1.13.15)
512

613
- Fix: Fixes error on empty file for log retrieval action. ([Beregcamlost](https://github.com/beregcamlost)) ([SoraKenji](https://github.com/SoraKenji))

lib/agent/triggers/location/index.js

Lines changed: 38 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/* eslint-disable max-len */
33
const { join } = require('path');
44
const Emitter = require('events').EventEmitter;
5-
const https = require('https');
65

76
const basePath = join(__dirname, '..', '..');
87
// eslint-disable-next-line import/no-dynamic-require
@@ -22,37 +21,6 @@ let checking = false;
2221
const accuracyThreshold = 50;
2322
const timeToWaitForSecond = 60 * 1000;
2423

25-
const getCurrentTime = (cb) => {
26-
const options = {
27-
hostname: 'timeapi.io',
28-
path: '/api/time/current/zone?timeZone=GMT',
29-
method: 'GET',
30-
headers: {
31-
Accept: 'application/json',
32-
},
33-
};
34-
35-
const req = https.request(options, (res) => {
36-
let data = '';
37-
res.on('data', (chunk) => {
38-
data += chunk;
39-
});
40-
res.on('end', () => {
41-
try {
42-
cb(JSON.parse(data));
43-
} catch (e) {
44-
cb(null, e);
45-
}
46-
});
47-
});
48-
49-
req.on('error', (error) => {
50-
cb(null, error);
51-
});
52-
53-
req.end();
54-
};
55-
5624
const storeForceData = (type, query, cb) => {
5725
storage.do(type, query, (errSet) => {
5826
if (errSet) {
@@ -63,35 +31,35 @@ const storeForceData = (type, query, cb) => {
6331
});
6432
};
6533

66-
const dateDifference = (date, secondDate) => {
67-
const timeDiff = date.getTime() - secondDate.getTime();
68-
const diffHours = timeDiff / (1000 * 3600);
69-
return diffHours;
70-
};
71-
72-
const check24TimeLoop = (time, timeStoraged, cb) => {
73-
const dateTime = new Date(time);
74-
const dateTimeStoraged = new Date(timeStoraged);
75-
const diff = dateDifference(dateTime, dateTimeStoraged);
76-
logger.debug(`Diff: ${diff}`);
77-
if (diff > 24) {
78-
cb(true);
79-
} else cb(false);
34+
const checkOneDayDifference = (time, timeStoraged, cb) => {
35+
let timeDate;
36+
let timeStoragedDate;
37+
try {
38+
timeDate = new Date(time);
39+
timeStoragedDate = new Date(timeStoraged);
40+
} catch (ex) {
41+
return cb(false);
42+
}
43+
if (timeDate > timeStoragedDate) {
44+
if (timeDate.getDate() !== timeStoragedDate.getDate()) {
45+
return cb(true);
46+
}
47+
}
48+
cb(false);
8049
};
8150

82-
const writeStorage = (external, local, cb) => {
51+
const writeStorage = (local, cb) => {
8352
storage.do('query', { type: 'keys', column: 'id', data: 'last_force_datetime' }, (err, stored) => {
84-
if (err) logger.error('Error getting the last force location datetime');
53+
if (err) {
54+
logger.error('Error getting the last force location datetime');
55+
return cb(false, err);
56+
}
8557
if (stored && stored.length > 0) {
8658
const data = JSON.parse(stored[0].value);
8759
logger.debug(`Last force location datetime: ${JSON.stringify(data)}`);
88-
if ((external === null || data.externalDateTime === 'null')) {
89-
check24TimeLoop(local, data.localDateTime, cb);
90-
} else {
91-
check24TimeLoop(external, data.externalDateTime, cb);
92-
}
60+
checkOneDayDifference(local, data.localDateTime, cb);
9361
} else {
94-
const dataToStore = { value: JSON.stringify({ localDateTime: local, externalDateTime: external }) };
62+
const dataToStore = { value: JSON.stringify({ localDateTime: local }) };
9563
storeForceData('set', { type: 'keys', id: 'last_force_datetime', data: dataToStore }, (errStoreSet) => {
9664
if (errStoreSet) return cb(null, new Error('Unable to set db keys last force location values'));
9765
cb(true);
@@ -160,7 +128,7 @@ const fetchLocation = (typeFetch, callback) => {
160128
let callBackStored = null;
161129
if (typeFetch === 'force') callBackStored = { callback };
162130
if (callback && typeFetch !== 'force' && typeof callback === 'function') locCallbacks.push(callback);
163-
if (checking) return;
131+
if (checking && typeFetch !== 'force') return;
164132

165133
const fireCallbacks = (err, coords) => {
166134
if (callBackStored && (err || !coords)) callBackStored.callback(err, coords);
@@ -236,23 +204,19 @@ const fetchLocation = (typeFetch, callback) => {
236204
};
237205

238206
const forceLocation = () => {
239-
getCurrentTime((res, error) => {
240-
let externalTime = '';
241-
if (res && !error) externalTime = `${res.dateTime}Z`;
242-
const dataToUpdate = { externalDateTime: externalTime, localDateTime: new Date().toISOString() };
243-
writeStorage(dataToUpdate.externalDateTime, dataToUpdate.localDateTime, (respWrite) => {
244-
if (!respWrite) return;
245-
fetchLocation('force', (err) => {
246-
if (err) return;
247-
storeForceData('update', {
248-
type: 'keys', id: 'last_force_datetime', columns: ['value'], values: [JSON.stringify(dataToUpdate)],
249-
}, (errStoredUpdate) => {
250-
if (errStoredUpdate) {
251-
logger.error('Unable to update db keys last force location values');
252-
return;
253-
}
254-
logger.info('Updated db keys last force location values');
255-
});
207+
const dataToUpdate = { localDateTime: new Date().toISOString() };
208+
writeStorage(dataToUpdate.localDateTime, (respWrite) => {
209+
if (!respWrite) return;
210+
fetchLocation('force', (err) => {
211+
if (err) return;
212+
storeForceData('update', {
213+
type: 'keys', id: 'last_force_datetime', columns: ['value'], values: [JSON.stringify(dataToUpdate)],
214+
}, (errStoredUpdate) => {
215+
if (errStoredUpdate) {
216+
logger.error('Unable to update db keys last force location values');
217+
return;
218+
}
219+
logger.info('Updated db keys last force location values');
256220
});
257221
});
258222
});
@@ -291,3 +255,5 @@ exports.events = [];
291255
exports.current = current;
292256
exports.send_location = sendLocation;
293257
exports.post_it = postIt;
258+
exports.checkOneDayDifference = checkOneDayDifference;
259+
exports.writeStorage = writeStorage;

lib/agent/updater.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
const { eq } = require('semver');
2-
const { join } = require('path');
1+
const path = require('path');
2+
33
const exists = require('fs').existsSync;
44

55
const needle = require('needle');
66
const os = require('os');
77
const childProcess = require('child_process');
8+
const { greaterOrEqual } = require('./helpers');
89
const common = require('./common');
910

1011
const logger = common.logger.prefix('updater');
1112
const { system } = common;
13+
const { join } = path;
14+
15+
const nodeBin = join(system.paths.current, 'bin', 'node');
1216

1317
const { exec } = childProcess;
1418

@@ -89,9 +93,9 @@ const update_client = function (new_version, cb) {
8993
if (line.match('YOUARENOTMYFATHER')) {
9094
// Keep the process alive for a while in the case we get an error.
9195
setTimeout(() => {
92-
logger.warn('Upgrade successful! See you in another lifetime, young one.');
93-
exports.upgrading = false;
94-
let_the_child_go();
96+
logger.warn('Upgrade successful! See you in another lifetime, young one.');
97+
exports.upgrading = false;
98+
let_the_child_go();
9599
}, timeout);
96100
} else if (line.includes('Error')) {
97101
logger.warn(line);
@@ -134,11 +138,10 @@ exports.check_for_update_winsvc = (cb) => {
134138
/** Skip this block if OS is not windows. */
135139
if (os.platform() != 'win32') { return cb(new Error('Action only allowed on Windows')); }
136140

137-
const updater_path = join(system.paths.package, 'bin', 'updater.exe');
138-
const sys_win = require('../system/windows');
141+
const sysWin = require('../system/windows');
139142
/** Get the current version of winsvc running on the device. */
140143
// eslint-disable-next-line consistent-return
141-
sys_win.get_winsvc_version((err, current_service_version) => {
144+
sysWin.get_winsvc_version((err, current_service_version) => {
142145
if (!patternMajorMinorPatch.test(current_service_version)) return cb(new Error('WinSVC version doesnt have the correct format'));
143146
if (err) return cb(new Error('Error to get winsvc version'));
144147

@@ -152,18 +155,16 @@ exports.check_for_update_winsvc = (cb) => {
152155
if (!patternMajorMinorPatch.test(service_version_stable)) return cb(new Error('WinSVC stable version doesnt have the correct format'));
153156
if (err) return cb(new Error('Error to get stable version'));
154157

155-
logger.notice(`New version found winsvc: ${service_version_stable}`);
158+
logger.notice(`Version found winsvc: ${service_version_stable}`);
156159

157160
/** check if device is running the latest version. */
158-
if (service_version_stable && eq(current_service_version, service_version_stable)) {
159-
logger.notice(`Nothing to do. latest version already installed. ${service_version_stable}`);
161+
if (service_version_stable && greaterOrEqual(current_service_version, service_version_stable)) {
162+
logger.notice(`Nothing to do. latest version already installed. ${current_service_version}`);
160163
return cb(null, true);
161164
}
162165

163-
/** Perform the update. */
164-
exports.update_winsvc(`${updater_path} -v=${current_service_version}`, (err_update) => {
165-
if (err_update) { return cb(new Error(`error to update winsvc,${err_update.message}`)); }
166-
166+
system.updateAsAdminUser('winsvc', (errUpdate) => {
167+
if (errUpdate) { return cb(new Error('error to update winsvc')); }
167168
return cb(null, true);
168169
});
169170
});
@@ -179,25 +180,26 @@ const check_for_update = function (cb) {
179180

180181
exports.check_enabled = false;
181182

182-
const versions_path = system.paths.versions;
183+
const versionsPath = system.paths.versions;
183184
const branch = config.getData('download_edge') == true ? 'edge' : 'stable';
184185

185-
common.package.new_version_available(branch, common.version, (err, new_version, downloads_url) => {
186-
logger.debug(`Checking for updates on ${branch} branch... and on url: ${downloads_url}`);
187-
if (err || !new_version) {
188-
common.package.check_update_success(common.version, versions_path, (err) => cb && cb(err || new Error('Theres no new version available')));
186+
common.package.new_version_available(branch, common.version, (err, newVersion, downloadsUrl) => {
187+
logger.debug(`Checking for updates on ${branch} branch... and on url: ${downloadsUrl}`);
188+
if (err || !newVersion) {
189+
common.package.check_update_success(common.version, versionsPath, (errCheck) => cb && cb(errCheck || new Error('Theres no new version available')));
189190
} else {
190191
needle.put(updatingHost, null, { timeout: 4500 }, () => {
191-
logger.notice(`New Agent version found: ${new_version}`);
192-
update_client(new_version, cb);
192+
logger.notice(`New Agent version found: ${newVersion}`);
193+
update_client(newVersion, cb);
193194
});
194195
}
195196
});
196-
197-
exports.check_for_update_winsvc((err, is_updated) => {
198-
if (err) logger.info(err.message);
199-
if (is_updated) logger.info('winsvc updated ');
200-
});
197+
setTimeout(() => {
198+
exports.check_for_update_winsvc((err, isUpdated) => {
199+
if (err) logger.info(err.message);
200+
if (isUpdated) logger.info('winsvc called to verify correctly.');
201+
});
202+
}, 60 * 1000);
201203
});
202204
};
203205

lib/system/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ system.get_current_hostname = system.get_current_hostname;
8282
if (system.os_name == 'windows') {
8383
system.get_os_edition = system.get_os_edition;
8484
system.get_winsvc_version = system.get_winsvc_version;
85+
system.updateAsAdminUser = system.updateAsAdminUser;
8586
system.get_as_admin = system.get_as_admin;
8687
system.run_as_admin = system.run_as_admin;
8788
system.check_service = system.check_service;
@@ -139,6 +140,14 @@ system.spawn_as_logged_user = function (command, args, opts, cb) {
139140
as('logged_user', 'spawn', command, args, opts, cb);
140141
};
141142

143+
system.updateAsAdminUser = (what, cb) => {
144+
system.check_service(what, (err, data) => {
145+
if (err) return cb(err);
146+
147+
system.updateAsAdmin(data, cb);
148+
});
149+
};
150+
142151
system.get_as_admin_user = function (what, cb) {
143152
system.check_service(what, (err, data) => {
144153
if (err) return cb(err);

lib/system/windows/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const osName = os.platform().replace('win32', 'windows');
2020

2121
const LOCALHOST_ACTION = 'http://127.0.0.1:7739/action';
2222
const LOCALHOST_PROVIDER = 'http://127.0.0.1:7739/provider';
23+
const LOCALHOST_UPDATE = 'http://127.0.0.1:7739/update';
2324

2425
exports.monitoring_service_go = false;
2526
// add windows bin path to env
@@ -155,6 +156,22 @@ exports.check_service = (data, cb) => {
155156
return cb(null, data);
156157
});
157158
};
159+
160+
exports.updateAsAdmin = (data, cb) => {
161+
const opts = {
162+
timeout: 90000,
163+
json: true,
164+
};
165+
needle.post(`${LOCALHOST_UPDATE}?target=${data}`, null, opts, (err, resp) => {
166+
if (err) {
167+
if (typeof cb !== 'function') return;
168+
return cb(err);
169+
}
170+
if (resp?.statusCode !== 200) return cb(new Error('Unable to update provider'));
171+
cb();
172+
});
173+
};
174+
158175
exports.get_as_admin = function (provider, cb) {
159176
const body = {
160177
provider,

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prey",
3-
"version": "1.13.15",
3+
"version": "1.13.16",
44
"author": "Engineering <[email protected]>",
55
"keywords": [
66
"prey",

0 commit comments

Comments
 (0)