Skip to content

Commit

Permalink
Merge pull request #403 from prey/location-upgrades
Browse files Browse the repository at this point in the history
WIP: Add proxy for location service
  • Loading branch information
javo authored Nov 20, 2018
2 parents 3ecd89a + 948f796 commit 06ac3ee
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/agent/providers/geo/strategies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

var join = require('path').join,
needle = require('needle'),
get_agent = require('random-ua').generate,
platform = require(join(__dirname, process.platform)),
common = require('./../../common'),
config = common.config,
providers = require('./../../providers'),
keys = require('./../../plugins/control-panel/api/keys'),
logger = require('../../common').logger.prefix('geo');

var GEO_ENDPOINT = 'solid.preyproject.com/geo';
var proxy;

function geoip(cb) {
logger.debug("Getting location via geoip");
Expand Down Expand Up @@ -58,11 +60,15 @@ function wifi(cb) {
var data = {
"wifiAccessPoints": aps
}
proxy = config.get('try_proxy');

var opts = {
user_agent: common.system.user_agent,
username: keys.get().device,
password: keys.get().api,
json : true
}
if (proxy) opts.proxy = proxy;

needle.post(GEO_ENDPOINT, data, opts, function(err, resp, body) {
if (err) return cb(err);
Expand All @@ -82,6 +88,7 @@ function wifi(cb) {
user_agent: stdout.endpoint['user-agent'],
json : true
}
if (proxy) options.proxy = proxy;

// Get the location using the url and mac addresses data
needle.post(url, data, options, function(err, resp, body) {
Expand All @@ -100,6 +107,7 @@ function wifi(cb) {
user_agent: common.system.user_agent,
json : true
}
if (proxy) opts.proxy = proxy;

// Send the new location info and process it
needle.put(GEO_ENDPOINT, loc_data, opts, function(err, resp) {
Expand Down
2 changes: 1 addition & 1 deletion lib/agent/providers/network/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ exports.parse_access_points_list_netsh = function(out) {

// netsh groups access points by BSSID, so we need to separate each
// SSID block into the BSSID it contains and select one of them
var routers = block.split(/BSSID \d/);
var routers = block.split(/[BSSID BSSIDD] \d/);

// the first block will contain shared information: SSID, auth, encryption
// so parse those values first. insert the SSID part that was removed from line one
Expand Down
21 changes: 18 additions & 3 deletions test/lib/agent/providers/geo/wifi_strategy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
var helpers = require('./../../../../helpers'),
should = require('should'),
wifi_strat = helpers.load('providers/geo/strategies').wifi,
var join = require('path').join,
sinon = require('sinon'),
helpers = require('./../../../../helpers'),
should = require('should'),
wifi_strat = helpers.load('providers/geo/strategies').wifi,
location_response = require('../fixtures/wifi_location_response'),
lib_path = helpers.lib_path(),
api_path = join(lib_path, 'agent', 'plugins', 'control-panel', 'api'),
keys = require(join(api_path, 'keys')),
link_response = require('../fixtures/location_link_response');

describe('location', function() {
Expand Down Expand Up @@ -133,6 +138,16 @@ describe('location', function() {

describe('real endpoint', function() {

before(function() {
keys_get_stub = sinon.stub(keys, 'get', function() {
return { api: 'aaaaaaaaaa', device: 'bbbbbb' }
})
});

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

it('works', function(done) {
provider_stub.restore();
this.timeout(6000); // response may take longer
Expand Down

0 comments on commit 06ac3ee

Please sign in to comment.