Skip to content

Commit

Permalink
Merge pull request #244 from prey/geofencing-storage
Browse files Browse the repository at this point in the history
Geofencing on node client using sqlite storage
  • Loading branch information
javo authored Nov 28, 2016
2 parents 55d3831 + 6e60886 commit 357f92c
Show file tree
Hide file tree
Showing 13 changed files with 380 additions and 101 deletions.
11 changes: 7 additions & 4 deletions lib/agent/actions/fileretrieval/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ storage.init('files', storage_path, function(err) {
});

var exist = function(id, cb) {
var key = ['file', id].join('-');
storage.all('files', function(err, files) {
if (err)
return err.message;
if (files[id])
if (files[key])
return cb(true);
return cb(false);
});
Expand All @@ -34,14 +35,16 @@ exports.store = function(id, path, size, user, name) {
user: user,
name: name
}
storage.set(id, opts);
var key = ['file', id].join('-');
storage.set(key, opts);
}
});
}

exports.del = function(id) {
var key = ['file', id].join('-');
logger.debug('Removing file_id from DB: ' + id);
storage.del(id);
storage.del(key);
}

exports.run_stored = function(cb) {
Expand All @@ -60,7 +63,7 @@ exports.run_stored = function(cb) {
user: files[key].user,
name: files[key].name,
size: files[key].size,
file_id: key,
file_id: key.substring(5, key.length),
resumable: true
}
fileretrieval.start(opts, cb);
Expand Down
33 changes: 27 additions & 6 deletions lib/agent/actions/geofencing/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"use strict";

var EventEmitter = require('events').EventEmitter,
api = require('./../../plugins/control-panel/api'),
watcher = require('./../../triggers/geofencing');
logger = require('./../../common').logger.prefix('geofencing'),
api = require('./../../plugins/control-panel/api'),
watcher = require('./../../triggers/geofencing'),
storage = require('./storage');

var emitter;

Expand All @@ -16,9 +18,29 @@ function sync(geofences) {
watching = [];

if (len === 0) {
watcher.stop(done);
storage.clear_geo(function(err) {
if (err) logger.err("Error deleting geofences from local database")
watcher.stop(done);
})
} else {
start_watcher(geofences, done);
storage.get_geofences(function(err, stored_geofences) {
if (err) stored_geofences = {};

geofences.forEach(function (geofence) {
var geo = "geofence-" + geofence.id;

//if the geofence has a previous state it's reassignated
if (stored_geofences[geo]) {
geofence.state = stored_geofences[geo].state;
}

storage.store(geofence, function(err) {
if (err) logger.error("Error storing geofences: " + err);
});
});

start_watcher(geofences, done);
});
}

function start_watcher(geofences, cb) {
Expand All @@ -33,7 +55,6 @@ function sync(geofences) {
});

function notify_if_done() {

if (!(count === len)) return;

var data = {
Expand Down Expand Up @@ -94,4 +115,4 @@ function refresh_geofences(opts, cb) {
cb && cb(null, emitter);
}

exports.start = exports.stop = refresh_geofences;
exports.start = exports.stop = refresh_geofences;
77 changes: 77 additions & 0 deletions lib/agent/actions/geofencing/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var join = require('path').join,
common = require('./../../common'),
storage = require('./../../utils/storage'),
logger = common.logger;

var storage_path = join(common.system.paths.config, 'commands.db');
storage.init('geofences', storage_path, function(err) {
if (err) logger.error('Unable to initialize db for geofences: ' + err);
});

exports.store = function(geofence, cb) {
var opts = {
id: geofence.id,
name: geofence.name,
state: geofence.state
}
var key = ['geofence', opts.id].join('-'),
obj = {};

obj[key] = opts;
var to_add = new Buffer(JSON.stringify(obj, null, 0)).toString('base64');
storage.set_geofence(key, to_add, cb);
}

exports.update = function(id, name, del, add, cb) {
var key = ["geofence", id].join("-");

if (del != add) {
var fence_del,
fence_add,
obj_del = {},
obj_add = {},
to_delete,
to_add;

fence_del = {
"id": id,
"name": name,
"state": del
}
fence_add = {
"id": id,
"name": name,
"state": add
}

obj_del[key] = fence_del;
obj_add[key] = fence_add;

to_delete = new Buffer(JSON.stringify(obj_del, null, 0)).toString('base64');
to_add = new Buffer(JSON.stringify(obj_add, null, 0)).toString('base64');

storage.update(key, to_delete, to_add, cb);

} else {
return cb(null);
}
}

exports.get_geofences = function(cb) {
storage.all('geofences', function(err, fences) {
if (err) cb(new Error("Error retrieving geofences from local database"));

clear_geo(function(err) {
if (err) return cb(new Error("Error deleting geofences from local database"), {});
cb(null, fences);
});

})
}

var clear_geo = function(cb) {
storage.clear('geofences', cb);
}

exports.clear_geo = clear_geo;

2 changes: 1 addition & 1 deletion lib/agent/plugins/control-panel/api/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var send = function(attempt, method, path, data, options, cb) {
opts.user_agent = opts.user_agent || defaults.user_agent;

if (!opts.username) {
opts.username = keys.get().api;
opts.username = keys.get().api.toLowerCase();
opts.password = 'x';
}

Expand Down
20 changes: 20 additions & 0 deletions lib/agent/providers/file-type/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

//////////////////////////////////////////
// (C) 2016 Prey, Inc.
// By Javir Acuña - http://preyproject.com
// GPLv3 Licensed
// Prey File Type discriminate if a file has ASCII or SQLite format, used for
// the commands.db file
////////////////////////////////////////////

var os_name = process.platform.replace('darwin', 'mac').replace('win32', 'windows'),
os_functions = require('./' + os_name);

module.exports.get_file_type = function(path, cb) {
os_functions.get_file_type(path, function(err, file_type) {
if (err) return cb(err);

return cb(null, file_type);
});
}
20 changes: 20 additions & 0 deletions lib/agent/providers/file-type/linux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

////////////////////////////////////////////
// (C) 2016 Prey, Inc.
// By Javier Acuña - http://preyproject.com
// GPLv3 Licensed
////////////////////////////////////////////

module.exports.get_file_type = function(path, cb) {
var exec = require('child_process').exec,
cmd = 'file ' + path;

exec(cmd, function(err, stdout) {
var format = "ASCII";
if (err) return cb(new Error("Error getting file type"))

if (stdout.includes('SQLite')) format = "SQLite";
cb(null, format);
});
};
20 changes: 20 additions & 0 deletions lib/agent/providers/file-type/mac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

////////////////////////////////////////////
// (C) 2016 Prey, Inc.
// By Javier Acuña - http://preyproject.com
// GPLv3 Licensed
////////////////////////////////////////////

module.exports.get_file_type = function(path, cb) {
var exec = require('child_process').exec,
cmd = 'file ' + path;

exec(cmd, function(err, stdout) {
var format = "ASCII";
if (err) return cb(new Error("Error getting file type"))

if (stdout.includes('SQLite')) format = "SQLite";
cb(null, format);
});
};
20 changes: 20 additions & 0 deletions lib/agent/providers/file-type/windows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

////////////////////////////////////////////
// (C) 2016 Prey, Inc.
// By Javier Acuña - http://preyproject.com
// GPLv3 Licensed
////////////////////////////////////////////

module.exports.get_file_type = function(path, cb) {
var exec = require('child_process').exec,
cmd = 'type ' + path;

exec(cmd, function(err, stdout) {
var format = "ASCII";
if (err) return cb(new Error("Error getting file type"))

if (stdout.includes('SQLite')) format = "SQLite";
cb(null, format);
});
};
17 changes: 16 additions & 1 deletion lib/agent/triggers/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var join = require('path').join,
hooks = require(join(base_path, 'hooks')),
network = require(join(base_path, 'providers', 'network')),
file_retrieval = require(join(base_path, 'actions', 'fileretrieval')),
actions = require('./../../actions'),
commands = require('./../../commands'),
Emitter = require('events').EventEmitter;

var emitter,
Expand All @@ -17,8 +19,21 @@ var check_status = function() {
network.get_connection_status(function(new_status) {

if (status != new_status) {
if (new_status == 'connected')
if (new_status == 'connected') {
var running = actions.running(),
running_geofencing = false;

for (var k in running) {
if (running[k].name == 'geofencing') {
running_geofencing = true;
break;
}
}
if (!running_geofencing) {
commands.perform({command: 'start', target: 'geofencing'});
}
file_retrieval.check_pending_files();
}
// trigger directly the event instead of emitting it to the actions manager
hooks.trigger(new_status);
}
Expand Down
Loading

0 comments on commit 357f92c

Please sign in to comment.