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

1.3.7-rc2 #83

Merged
merged 28 commits into from
Mar 11, 2015
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0593498
Bump node version to v0.10.36
mauricioschneider Jan 29, 2015
46a8e3e
Point login shell to /bin/false for linux and /sbin/nologin in osx. F…
mauricioschneider Jan 16, 2015
5dac472
Refactor permission tests to use tmp folder instead of repo dir so it…
mauricioschneider Jan 28, 2015
d9b8320
Finish refactor of the tasks spec
mauricioschneider Jan 29, 2015
703ccbb
Move login shell definition to already existing if/else. Remove comme…
mauricioschneider Feb 9, 2015
b197a3a
CP: Refactor interval to use setTimeout instead of setInterval
mauricioschneider Feb 10, 2015
42ecc29
CP: Refactor set_interval and related functions
mauricioschneider Feb 12, 2015
e1ef81a
CP: Interval, fix typo in function name
mauricioschneider Feb 12, 2015
6bf1e9c
Fix #78. Raise volume and unmute in windows.
mauricioschneider Feb 27, 2015
df855e5
Revert "Fix #78. Raise volume and unmute in windows."
mauricioschneider Mar 3, 2015
d7cb499
CP: Move proxy logic from sender to api/request. Add fallback from pr…
mauricioschneider Mar 4, 2015
3aa040d
Retry without proxy if proxy is set and get temp error.
mauricioschneider Mar 4, 2015
c20dd42
Check for opts.proxy instead of options.proxy for consistency
mauricioschneider Mar 4, 2015
5bd79f9
Add missing return to retry_without_proxy calls.
mauricioschneider Mar 4, 2015
bc92c92
Append try_proxy property to opts when initialising API from conf mod…
mauricioschneider Mar 4, 2015
1deb6e5
Remove unused var from panel conf module.
mauricioschneider Mar 4, 2015
c49c60a
Bump needle version to 0.8.1 and update shrinkwrap
mauricioschneider Mar 5, 2015
70f6d7d
Add graceful-fs package required in wipe module.
mauricioschneider Mar 5, 2015
24bf444
Add specs for request. Add retry_timeout as request default option to…
mauricioschneider Mar 5, 2015
5f60030
Change request test to use defaults for setting proxy and check for e…
mauricioschneider Mar 5, 2015
c9996e2
Don't override opts.proxy with default if provided.
mauricioschneider Mar 6, 2015
560152c
Refactor request specs.
mauricioschneider Mar 6, 2015
f7c7e31
Merge pull request #96 from prey/1.3.7-rc2-prettyproxyfix
mauricioschneider Mar 6, 2015
165c29b
CP: Make sure to remove bus listeners on interval module unload
mauricioschneider Mar 10, 2015
ec03fe1
CP: Add specs for interval module
mauricioschneider Mar 10, 2015
88cd9cd
Remove woken hook remover.
mauricioschneider Mar 11, 2015
f0bdbdd
Require api.devices.get instead of requiring up to commands for test …
mauricioschneider Mar 11, 2015
5f06463
Add spec for interval.check.
mauricioschneider Mar 11, 2015
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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.10.32
0.10.36
81 changes: 54 additions & 27 deletions lib/agent/plugins/control-panel/api/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@ var needle = require('needle'),
version = '2';

var defaults = {
client : needle,
protocol : 'https',
host : 'solid.preyproject.com',
user_agent : 'Prey Client API v' + version,
timeout : 90 * 1000
}
client : needle,
protocol : 'https',
host : 'solid.preyproject.com',
user_agent : 'Prey Client API v' + version,
timeout : 90 * 1000,
retry_timeout : 3 * 1000,
try_proxy : ''
};

https.globalAgent.options.secureProtocol = 'TLSv1_method';

var api_root = '/api/v2';
var max_attempts = 3;
var api_root = '/api/v2',
max_attempts = 3;

var is_network_down = function(err) {
var codes = ['ENETDOWN', 'ENETUNREACH', 'EADDRINFO', 'ENOTFOUND'];
var codes = ['ENETDOWN', 'ENETUNREACH', 'EADDRINFO', 'ENOTFOUND', 'EHOSTUNREACH'];
return codes.indexOf(err.code) !== -1;
}
};

var is_server_down = function(err) {
var codes = ['ETIMEDOUT', 'ECONNRESET', 'ECONNREFUSED'];
return codes.indexOf(err.code) !== -1;
}
};

var is_temporary_error = function(err, resp) {
var retry = false;
Expand All @@ -36,12 +38,15 @@ var is_temporary_error = function(err, resp) {
retry = (resp.statusCode == 502 || resp.statusCode == 503);

return retry;
}
};

var send = function(attempt, method, path, data, options, cb) {
if (!defaults.client)
return cb(new Error('No HTTP client set!'))
if (!defaults.client) {
return cb(new Error('No HTTP client set!'));
}

// opts are used for the current request, while options are
// used in the recursive call in case of retry
var opts = options || {};
opts.timeout = opts.timeout || defaults.timeout;
opts.user_agent = opts.user_agent || defaults.user_agent;
Expand All @@ -53,18 +58,30 @@ var send = function(attempt, method, path, data, options, cb) {

var base = defaults.protocol + '://' + defaults.host,
url = base + api_root + path,
start = new Date();
start = new Date(),
logger_msg;

logger.debug('Sending ' + method + ' request #' + attempt + ' to ' + base);
// console.log(opts);
logger_msg = 'Sending ' + method + ' request #' + attempt + ' to ' + base;
if (opts.proxy) {
logger_msg += " using proxy: " + opts.proxy;
}
logger.debug(logger_msg);

defaults.client.request(method, url, data, opts, function(err, resp, body) {
var seconds = (new Date() - start) / 1000;
var seconds = (new Date() - start) / 1000,
retry_without_proxy = function() {
delete options.proxy;
logger.debug('Retrying request without proxy.');
send(1, method, path, data, options, cb);
};
logger.debug('Attempt #' + attempt + ' took ' + seconds + ' seconds.');

if (err && is_network_down(err)) {

err.message = 'Network seems to be down. Check your connection and try again.';
if(opts.proxy) {
return retry_without_proxy();
}
return cb(err);

} else if (is_temporary_error(err, resp)) {
Expand All @@ -73,7 +90,9 @@ var send = function(attempt, method, path, data, options, cb) {
logger.debug('Temporary network error. Retrying...');
return setTimeout(function() {
send(attempt + 1, method, path, data, options, cb);
}, 3000);
}, defaults.retry_timeout);
} else if (opts.proxy) {
return retry_without_proxy();
} else if (err) { // maxed out all attempts. tell user to retry in a sec.
err.message = err.message + ' - Please try again in a minute.';
}
Expand All @@ -82,19 +101,27 @@ var send = function(attempt, method, path, data, options, cb) {

cb(err, resp, body);
});
}
};

var set_proxy_and_send = function (method, path, data, opts, cb) {
if (!opts.hasOwnProperty("proxy") && defaults.try_proxy) {
opts.proxy = defaults.try_proxy;
logger.debug("Setting proxy to " + opts.proxy);
}
send(1, method, path, data, opts, cb);
};

exports.get = function(path, opts, cb) {
send(1, 'GET', path, null, opts, cb);
}
set_proxy_and_send('GET', path, null, opts, cb);
};

exports.post = function(path, data, opts, cb) {
send(1, 'POST', path, data, opts, cb);
}
set_proxy_and_send('POST', path, data, opts, cb);
};

exports.delete = function(path, opts, cb) {
send(1, 'DELETE', path, null, opts, cb);
}
set_proxy_and_send('DELETE', path, null, opts, cb);
};

exports.use = function(obj) {
for (var key in obj) {
Expand All @@ -116,4 +143,4 @@ exports.use = function(obj) {
}
}
return defaults;
}
};
84 changes: 6 additions & 78 deletions lib/agent/plugins/control-panel/api/test/request_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,85 +8,13 @@ describe('API Request Wrapper', function() {
describe('.use', function() {

it('overrides defaults', function() {
var defaults = api_req.use({ host: 'http://foobar.com' });
var defaults = api_req.use({
host: 'http://foobar.com',
retry_timeout: 100
});
defaults.host.should.equal('http://foobar.com');
})
});

})

describe('get', function() {

var stub;

describe('on a 200 response', function() {

before(function() {
stub = sinon.stub(needle, 'request', function(method, url, data, opts, cb) {
cb(null, { statusCode: 200 });
})
})

after(function() {
stub.restore();
})

it('does not retry the request', function(done) {

api_req.get('/devices/something', {}, function(err, resp, body) {
stub.callCount.should.equal(1);
done();
})

})

})

describe('on a 503 response', function() {

before(function() {
stub = sinon.stub(needle, 'request', function(method, url, data, opts, cb) {
cb(new Error('socket hang up'));
})
})

after(function() {
stub.restore();
})

it('retries the request', function(done) {

api_req.get('/devices/something', {}, function(err, resp, body) {
stub.callCount.should.equal(3);
done();
})

})

})

describe('on connection error', function() {

before(function() {
stub = sinon.stub(needle, 'request', function(method, url, data, opts, cb) {
cb(new Error('socket hang up'));
})
})

after(function() {
stub.restore();
})

it('retries the request', function(done) {

api_req.get('/devices/something', {}, function(err, resp, body) {
stub.callCount.should.equal(3);
done();
})

})

})

})
});

});
Loading