Migrate to statusfile

This commit is contained in:
dadav
2019-10-13 22:39:03 +02:00
parent e15d0f3323
commit 87a3fb5f0c
4 changed files with 70 additions and 167 deletions

View File

@ -1,5 +1,5 @@
__author__ = 'zenzen san'
__version__ = '1.0.0'
__version__ = '2.0.0'
__name__ = 'net-pos'
__license__ = 'GPL3'
__description__ = """Saves a json file with the access points with more signal
@ -11,33 +11,24 @@ import logging
import json
import os
import requests
from pwnagotchi.utils import StatusFile
MOZILLA_API_URL = 'https://location.services.mozilla.com/v1/geolocate?key={api}'
ALREADY_SAVED = None
SKIP = None
REPORT = StatusFile('/root/.net_pos_saved', data_format='json')
SKIP = list()
READY = False
OPTIONS = {}
OPTIONS = dict()
def on_loaded():
global ALREADY_SAVED
global SKIP
global READY
SKIP = list()
if 'api_key' not in OPTIONS or ('api_key' in OPTIONS and OPTIONS['api_key'] is None):
logging.error("NET-POS: api_key isn't set. Can't use mozilla's api.")
return
try:
with open('/root/.net_pos_saved', 'r') as f:
ALREADY_SAVED = f.read().splitlines()
except OSError:
logging.warning('NET-POS: No net-pos-file found.')
ALREADY_SAVED = []
READY = True
logging.info("net-pos plugin loaded.")
def _append_saved(path):
@ -55,20 +46,22 @@ def _append_saved(path):
def on_internet_available(agent):
global SKIP
global REPORT
if READY:
config = agent.config()
display = agent.view()
reported = REPORT.data_field_or('reported', default=list())
handshake_dir = config['bettercap']['handshakes']
all_files = os.listdir(handshake_dir)
all_np_files = [os.path.join(handshake_dir, filename)
for filename in all_files
if filename.endswith('.net-pos.json')]
new_np_files = set(all_np_files) - set(ALREADY_SAVED) - set(SKIP)
new_np_files = set(all_np_files) - set(reported) - set(SKIP)
if new_np_files:
logging.info("NET-POS: Found {num} new net-pos files. Fetching positions ...", len(new_np_files))
logging.info("NET-POS: Found %d new net-pos files. Fetching positions ...", len(new_np_files))
display.set('status', f"Found {len(new_np_files)} new net-pos files. Fetching positions ...")
display.update(force=True)
for idx, np_file in enumerate(new_np_files):
@ -76,8 +69,8 @@ def on_internet_available(agent):
geo_file = np_file.replace('.net-pos.json', '.geo.json')
if os.path.exists(geo_file):
# got already the position
ALREADY_SAVED.append(np_file)
_append_saved(np_file)
reported.append(np_file)
REPORT.update(data={'reported': reported})
continue
try:
@ -85,18 +78,21 @@ def on_internet_available(agent):
except requests.exceptions.RequestException as req_e:
logging.error("NET-POS: %s", req_e)
SKIP += np_file
continue
except json.JSONDecodeError as js_e:
logging.error("NET-POS: %s", js_e)
SKIP += np_file
continue
except OSError as os_e:
logging.error("NET-POS: %s", os_e)
SKIP += np_file
continue
with open(geo_file, 'w+t') as sf:
json.dump(geo_data, sf)
ALREADY_SAVED.append(np_file)
_append_saved(np_file)
reported.append(np_file)
REPORT.update(data={'reported': reported})
display.set('status', f"Fetching positions ({idx+1}/{len(new_np_files)})")
display.update(force=True)
@ -108,15 +104,15 @@ def on_handshake(agent, filename, access_point, client_station):
logging.info("NET-POS: Saving net-location to %s", netpos_filename)
try:
with open(netpos_filename, 'w+t') as fp:
json.dump(netpos, fp)
with open(netpos_filename, 'w+t') as net_pos_file:
json.dump(netpos, net_pos_file)
except OSError as os_e:
logging.error("NET-POS: %s", os_e)
def _get_netpos(agent):
aps = agent.get_access_points()
netpos = {}
netpos = dict()
netpos['wifiAccessPoints'] = list()
# 6 seems a good number to save a wifi networks location
for access_point in sorted(aps, key=lambda i: i['rssi'], reverse=True)[:6]: