Skip to content

Commit

Permalink
Merge pull request #25 from prey/api-refactor
Browse files Browse the repository at this point in the history
Client API refactor
  • Loading branch information
tomas committed Oct 6, 2013
2 parents df588d8 + 27ffdd6 commit bf52b44
Show file tree
Hide file tree
Showing 39 changed files with 1,114 additions and 518 deletions.
1 change: 0 additions & 1 deletion index.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/agent/actions/filebrowser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var FileBrowser = function(options){
var self = this;
this.options = options || {};

this.tunnel_host = options.host || 'localhost';
this.tunnel_port = options.port || 9996;
this.tunnel_host = this.options.host || 'localhost';
this.tunnel_port = this.options.port || 9996;

// open: first we open the tunnel, then we run the command
// close: first we close the tunnel, then we kill the command
Expand Down
3 changes: 1 addition & 2 deletions lib/agent/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ pid.store(pid_file, function(err, running){
if (err) throw(err);
if (!running) return agent.run();

var run_time = (new Date() - running.stat.ctime)/(60 * 1000);

if (process.stdout._type == 'tty') {
var run_time = (new Date() - running.stat.ctime)/(60 * 1000);
var run_time_str = run_time.toString().substring(0,5);
logger.write('\nLive instance for ' + run_time_str + ' minutes with PID: ' + running.pid + '.');
}
Expand Down
50 changes: 18 additions & 32 deletions lib/agent/drivers/interval/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
var needle = require('needle'),
common = require('./../../common'),
keys = require('./../../keys'),
config = common.config,
hooks = require('./../../hooks'),
Emitter = require('events').EventEmitter;

var config = common.config,
var loaded,
pull = common.api.pull,
logger = common.logger.prefix('interval');

var timer,
emitter,
device_key,
current_delay,
default_delay = 20 * 1000 * 60,
short_delay = 2 * 1000 * 60;

var get_url = function() {
var host = config.get('protocol') + '://' + config.get('host');
return host + '/api/v2/devices/' + device_key + '.json';
};
default_delay = 20 * 1000 * 60, // 20 min
short_delay = 2 * 1000 * 60; // 2 min

var request = function() {
var url = get_url(),
opts = { username: config.get('api_key'), password: 'x' };

logger.info('Sending request to ' + url.replace(/.*\/\/([^\/]+).*/, '$1'));

needle.get(url, function(err, resp, body){
pull.commands(function(err, resp, body){
if (err)
return hooks.trigger('error', err, 'interval');
else if (resp.statusCode != 200)
Expand All @@ -44,16 +34,19 @@ var load_hooks = function() {
// whenever reachable state changes, hasten or slowen
hooks.on('reachable', set_interval);
hooks.on('unreachable', set_faster_interval);

loaded = true;
}

// set timer to check on intervals
var set_interval = function(delay) {
if (!loaded) return;
if (!delay) delay = default_delay;

if (delay == current_delay) return;
current_delay = delay;

logger.debug('Queueing check-ins every ' + delay/60000 + ' minutes.');
logger.info('Queueing check-ins every ' + delay/60000 + ' minutes.');
if (timer) clearInterval(timer);
timer = setInterval(request, delay);
}
Expand All @@ -71,27 +64,20 @@ var unload = function(err) {
if (timer) clearInterval(timer);

emitter.emit('unload', err);
loaded = false;
}

exports.load = function(opts, cb) {
var opts = opts || {},
delay = opts.delay; // if null, defaults to default_delay

var opts = opts || {},
delay = opts.delay; // if null, defaults to default_delay

keys.verify(function(err, linked){
if (err) return cb(err);
emitter = new Emitter();

device_key = config.get('device_key');
emitter = new Emitter();

load_hooks();
set_interval(delay);

if (linked) request(); // if just linked, make a request right away

cb(null, emitter); // listeners get attached
})
load_hooks();
set_interval(delay);
// request();

cb(null, emitter); // listeners get attached
}

exports.unload = function(){
Expand Down
21 changes: 5 additions & 16 deletions lib/agent/drivers/push/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ var common = require('./../../common'),
hooks = require('./../../hooks'),
config = common.config,
logger = common.logger.prefix('push'),
push = common.api.push,
server = require('./server'),
entry = require('entry'),
needle = require('needle'),
scrambler = require('./../../../utils/scrambler'),
createHash = require('crypto').createHash,
Emitter = require('events').EventEmitter;

var secret,
emitter,
device_key,
mapped_port,
last_nid,
upnp_desc = 'Prey Anti-Theft',
Expand Down Expand Up @@ -44,14 +43,10 @@ var map_port = function(port, attempt, cb){

var send_notification_id = function(str, cb) {

var host = config.get('protocol') + '://' + config.get('host'),
url = host + '/api/v2/devices/' + device_key + '/data.json',
data = { notification_id: str },
opts = { username: config.get('api_key'), password: 'x' };

var data = { notification_id: str };
logger.info('Updating notification ID...');

needle.post(url, data, opts, function(err, resp, body){
push.data(data, {}, function(err, resp, body){
var success = resp && resp.statusCode < 300;
if (success) last_nid = str;
cb && cb(success ? null : new Error('Unable to send notification ID.'));
Expand Down Expand Up @@ -175,7 +170,8 @@ var check_mapping = function(cb){
logger.info('Found existing mapping! Using port ' + mapping.NewExternalPort);
mapped(null, mapping.NewExternalPort, cb);
} else { // not mapped or mapped to someone else
var port = port_start + chars.split('').indexOf(device_key[0]);
var key = config.get('device_key');
var port = port_start + chars.split('').indexOf(key[0]);

map_port(port, 1, function(err, port){
if (err) return cb && cb(err);
Expand All @@ -188,13 +184,6 @@ var check_mapping = function(cb){
}

exports.load = function(opts, cb){

var port;
device_key = config.get('device_key');

if (!device_key || device_key == '')
return cb(new Error('Device key not present.'));

hooks.on('connected', check_mapping);
hooks.on('disconnected', stop);

Expand Down
58 changes: 14 additions & 44 deletions lib/agent/endpoints/control-panel/index.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,54 @@
var needle = require('needle'),
common = require('./../../common'),
providers = require('./../../providers'),
keys = require('./../../keys'),
var common = require('./../../common'),
api = common.api,
config = common.config,
providers = require('./../../providers'),
logger = common.logger.prefix('control-panel');

var request_format = '.json';

var has_files = function(data) {
for (var key in data) {
if (data[key] && data[key].file && data[key].content_type)
return true;
}
};

var get_url = function(what) {
var host = config.get('protocol') + '://' + config.get('host'),
path = '/api/v2/devices/' + config.get('device_key');

return host + path + '/' + what + request_format;
};

var get_status_info = function(cb) {
var headers = {};

providers.get('status', function(err, data){
if (err) return cb(err);

headers['X-Prey-Status'] = JSON.stringify(data);
cb(null, headers);
})
providers.get('status', cb)
}

var make_request = function(what, data, opts, callback) {

var url = get_url(what),
opts = opts || {};
var opts = opts || {},
what = what.replace(/s$/, '');

var request_opts = {
username : config.get('api_key'),
password : 'x',
multipart : has_files(data),
timeout : 20000,
proxy : opts.proxy
};

if (opts.headers)
request_opts.headers = opts.headers;

var host = url.replace(/.*\/\/([^\/]+).*/, '$1'),
msg = 'Posting data to ' + host;
opts.multipart = has_files(data);

var msg = 'Posting ' + what;
if (opts.proxy) msg += ' using proxy: ' + opts.proxy;
logger.info(msg);

needle.post(url, data, request_opts, function(err, resp, body){
api.push[what](data, opts, function(err, resp, body){

// if there was an error, lets try connecting via a proxy if possible
if (err && !opts.proxy && config.get('try_proxy') && config.get('try_proxy').match('.')) {
return make_request(what, data, { proxy: config.get('try_proxy') }, callback);
opts.proxy = config.get('try_proxy');
return make_request(what, data, opts, callback);
}

if (resp) resp.body = body;
callback(err, resp);
});

};

exports.init = function(cb) {
keys.verify(cb);
cb();
}

exports.send = function(what, data, opts, callback) {
if (!config.get('send_status_info') || what == 'response')
return make_request(what, data, opts, callback);

get_status_info(function(err, headers){
if (!err && headers)
opts.headers = headers;

get_status_info(function(err, status) {
opts.status = status;
make_request(what, data, opts, callback);
})
};
1 change: 0 additions & 1 deletion lib/agent/endpoints/email/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ var deliver = function(host, email_data, cb){
};

exports.init = function(cb) {

var err,
recipient = config.get('email') && config.get('email').recipient;

Expand Down
52 changes: 33 additions & 19 deletions lib/agent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ var config = common.config,
system = common.system,
logger = common.logger.prefix('agent'),
program = common.program,
helpers = common.helpers,
watch_list = ['network', 'power'],
config_wait = 120000, // 2 minutes
running = false,
unloading = false,
started_at = null,
Expand Down Expand Up @@ -84,18 +84,22 @@ var engage = function(trigger) {
}

var boot = function() {
load_hooks();
load_drivers(get_option('driver'), function(err){
if (err) return; // handle_error(err);
common.setup(common, function(err, linked) {
if (err) return handle_setup_error(err);

triggers.watch(watch_list);
endpoints.init(get_option('endpoint'), function(err){
if (err) handle_error(err);
load_hooks();
load_drivers(get_option('driver'), function(err){
if (err) throw err;

connection.watch();
logger.info('Initialized.');
triggers.watch(watch_list);
endpoints.init(get_option('endpoint'), function(err){
if (err) handle_error(err);

connection.watch();
logger.info('Initialized.');
});
});
});
})
};

var load_drivers = function(list, cb){
Expand Down Expand Up @@ -200,28 +204,38 @@ var handle_response = function(what, endpoint, resp) {
// error handling
////////////////////////////////////////////////////////////////////

var handle_setup_error = function(err) {
if (!helpers.running_on_background())
throw err;
else
wait_for_config();
}

var handle_error = function(err, source) {
logger.error(err, source);

if (err.code == 'EADDRINFO' || err.code == 'ENOTFOUND') // no connection
// connection.down();
logger.info('Connection seems to be down.')
else if (err.code == 'NOAPIKEY' && common.helpers.running_on_background())
wait_for_config();
else if (config.get('send_crash_reports'))
exceptions.send(err);
}

var wait_for_config = function() {
logger.info('Not configured. Waiting a few seconds to retry.')
logger.info('Not configured. Waiting for user input...')
var attempts = 0;

hooks.unload(); // revert from boot()

setTimeout(function(){
var timer = setInterval(function(){
logger.info('Reloading config...');
config.reload();
if (config.get('api_key')) boot();
else process.exit(1);
}, config_wait);

if (config.get('api_key')) {
clearInterval(timer);
boot()
} else if (++attempts > 12) {
throw new Error('Not configured. Stopping.');
}
}, 10000);
}

////////////////////////////////////////////////////////////////////
Expand Down
Loading

0 comments on commit bf52b44

Please sign in to comment.