From 0604e8f5e837d413e581523db9671193c0d09cd0 Mon Sep 17 00:00:00 2001 From: johaoRosasRosillo Date: Wed, 13 Apr 2022 15:40:16 -0400 Subject: [PATCH 1/2] send info location when api geo return error 429 --- lib/agent/providers/geo/strategies.js | 34 ++++++++++++++++++++++++++- package.json | 2 +- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/agent/providers/geo/strategies.js b/lib/agent/providers/geo/strategies.js index 020dfadcc..e56eaa431 100644 --- a/lib/agent/providers/geo/strategies.js +++ b/lib/agent/providers/geo/strategies.js @@ -7,7 +7,8 @@ var join = require('path').join, config = common.config, providers = require('./../../providers'), keys = require('./../../plugins/control-panel/api/keys'), - logger = require('../../common').logger.prefix('geo'); + logger = require('../../common').logger.prefix('geo'), + storage = require('./../../utils/storage'); var GEO_ENDPOINT = 'https://solid.preyproject.com/geo', proxy; @@ -32,6 +33,12 @@ function geoip(cb) { }); } +var save_data = (data) => { + storage.do('set', {type: 'keys', id: 'geo', data: {value: JSON.stringify(data)}}, (err) => { + if (err) logger.error('Unable to save geo data'); + }); +} + function wifi(cb) { logger.info("Getting location via wifi strategy"); @@ -71,6 +78,21 @@ function wifi(cb) { if (proxy) opts.proxy = proxy; needle.post(GEO_ENDPOINT, data, opts, function(err, resp, body) { + + if (resp.statusCode == 429) { + storage.do('query', {type: 'keys', column: 'id', data: 'geo'}, (err, stored_data) => { + if (err) return cb(new Error('Unable to read geo data')); + if (stored_data.length == 0) return cb(new Error('There is no geo data in DB')); + else{ + try { + stored_data = JSON.parse(stored_data[0].value); + return cb(null, stored_data); + } catch (e) { + return cb(new Error("Couldn't get data in sqlite storage geo")); + } + } + }) + } if (err) return cb(err); check_response(body, function(err, stdout) { @@ -154,6 +176,16 @@ function wifi(cb) { method: 'wifi' }; + storage.do('query', {type: 'keys', column: 'id', data: 'geo'}, (err, stored_data) => { + if (err) logger.error('Unable to read geo data'); + if (stored_data.length == 0) save_data(data); + else{ + storage.do('del', {type: 'keys', id: 'geo'}, (err) => { + if (err) logger.error('Unable to delete geo data'); + save_data(data); + }) + } + }) return cb(null, data); } } diff --git a/package.json b/package.json index ba814eebd..7259426b7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prey", - "version": "1.9.22", + "version": "1.9.23", "author": "Client Engineering ", "keywords": [ "prey", From d2cc36590582ab5c2418440e66475b7932c51478 Mon Sep 17 00:00:00 2001 From: johaoRosasRosillo Date: Mon, 18 Apr 2022 10:28:47 -0400 Subject: [PATCH 2/2] specif name strategy wifi --- lib/agent/providers/geo/strategies.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/agent/providers/geo/strategies.js b/lib/agent/providers/geo/strategies.js index e56eaa431..cc474fc93 100644 --- a/lib/agent/providers/geo/strategies.js +++ b/lib/agent/providers/geo/strategies.js @@ -34,8 +34,8 @@ function geoip(cb) { } var save_data = (data) => { - storage.do('set', {type: 'keys', id: 'geo', data: {value: JSON.stringify(data)}}, (err) => { - if (err) logger.error('Unable to save geo data'); + storage.do('set', {type: 'keys', id: 'last_wifi_location', data: {value: JSON.stringify(data)}}, (err) => { + if (err) logger.error('Unable to save last_wifi_location data'); }); } @@ -80,7 +80,7 @@ function wifi(cb) { needle.post(GEO_ENDPOINT, data, opts, function(err, resp, body) { if (resp.statusCode == 429) { - storage.do('query', {type: 'keys', column: 'id', data: 'geo'}, (err, stored_data) => { + storage.do('query', {type: 'keys', column: 'id', data: 'last_wifi_location'}, (err, stored_data) => { if (err) return cb(new Error('Unable to read geo data')); if (stored_data.length == 0) return cb(new Error('There is no geo data in DB')); else{ @@ -176,12 +176,12 @@ function wifi(cb) { method: 'wifi' }; - storage.do('query', {type: 'keys', column: 'id', data: 'geo'}, (err, stored_data) => { - if (err) logger.error('Unable to read geo data'); + storage.do('query', {type: 'keys', column: 'id', data: 'last_wifi_location'}, (err, stored_data) => { + if (err) logger.error('Unable to read last_wifi_location data'); if (stored_data.length == 0) save_data(data); else{ - storage.do('del', {type: 'keys', id: 'geo'}, (err) => { - if (err) logger.error('Unable to delete geo data'); + storage.do('del', {type: 'keys', id: 'last_wifi_location'}, (err) => { + if (err) logger.error('Unable to delete last_wifi_location data'); save_data(data); }) }