From 6d6d242346d2ffe0207f3558bbf3f1b63c38b216 Mon Sep 17 00:00:00 2001 From: Jonathan Flueren <11487762+JonOfUs@users.noreply.github.com> Date: Thu, 17 Aug 2023 21:23:49 +0200 Subject: [PATCH] Remove example data, catch errors --- scanner/upload.py | 19 +++++++++---------- website/identities.json | 1 - website/index.php | 12 +++++++++--- website/present.json | 1 - 4 files changed, 18 insertions(+), 15 deletions(-) delete mode 100644 website/identities.json delete mode 100644 website/present.json diff --git a/scanner/upload.py b/scanner/upload.py index a1b778b..c809fb4 100644 --- a/scanner/upload.py +++ b/scanner/upload.py @@ -7,12 +7,13 @@ import json import hmac import hashlib -IDENTITIES_URL = "http://localhost:8080/identities.json" -WEBHOOK_URL = "http://localhost:8080/update.php" +BASE_URL = "http://localhost:8080/" WEBHOOK_SECRET = "CHANGE-THIS" +WIFI_MAP_PATH = "wifi_map.yaml" def get_identities(): - resp = requests.get(IDENTITIES_URL) + identities_url = BASE_URL.rstrip('/') + '/identities.json' + resp = requests.get(identities_url) return resp.json() @@ -47,12 +48,8 @@ def parse_wifi_map(map_path): return filtered_identities -if __name__ == '__main__': - wifi_map_path = 'wifi_map.yaml' - if len(sys.argv) > 1: - wifi_map_path = sys.argv[1] - - filtered_identities = parse_wifi_map(wifi_map_path) +if __name__ == '__main__': + filtered_identities = parse_wifi_map(WIFI_MAP_PATH) # build request json_payload = json.dumps(list(filtered_identities)).encode("utf-8") @@ -68,8 +65,10 @@ if __name__ == '__main__': 'x-hmac-hash': signature } + webhook_url = BASE_URL.rstrip('/') + '/update.php' + # send request - r = requests.post(url=WEBHOOK_URL, data=json_payload, headers=req_headers) + r = requests.post(url=webhook_url, data=json_payload, headers=req_headers) print("Upload response:") print(r.status_code) diff --git a/website/identities.json b/website/identities.json deleted file mode 100644 index b8134c7..0000000 --- a/website/identities.json +++ /dev/null @@ -1 +0,0 @@ -[{"name":"Null","mac_hash":"38fbdde984330e50c02382e647c576b71f41cc5c45b193d4f3177e6ee8f22a78"},{"name":"BROADCAST","mac_hash":"ef85d972b07fccdd79085ddb4713cd487c3838e128a7c4d11092909675c2022d"}] \ No newline at end of file diff --git a/website/index.php b/website/index.php index d4576bb..1451f70 100644 --- a/website/index.php +++ b/website/index.php @@ -8,7 +8,9 @@ define('NAME_PATTERN', "^([ a-zA-Z0-9'\-]){1,30}$"); define('MAC_PATTERN', "^([0-9A-Fa-f]{2}[:-s]){5}([0-9A-Fa-f]{2})$"); define('PAGE_TITLE', 'CZI Presence Detector'); -$present = json_decode(file_get_contents(PRESENT_FILE), true); +$present = file_exists(PRESENT_FILE) + ? json_decode(file_get_contents(PRESENT_FILE), true) + : ["timestamp" => 0, "names" => []]; $tz = 'Europe/Berlin'; $datetime = new DateTime("now", new DateTimeZone($tz)); @@ -34,7 +36,9 @@ function add_identity($name, $mac) $hashed_mac = hash_mac($mac); - $identities = json_decode(file_get_contents(IDENTITIES_FILE), true); + $identities = file_exists(IDENTITIES_FILE) + ? json_decode(file_get_contents(IDENTITIES_FILE), true) + : []; $url = strtok($_SERVER['REQUEST_URI'], '?'); @@ -71,7 +75,9 @@ function remove_identity($name) $url = strtok($_SERVER['REQUEST_URI'], '?'); - $identities = json_decode(file_get_contents(IDENTITIES_FILE), true); + $identities = file_exists(IDENTITIES_FILE) + ? json_decode(file_get_contents(IDENTITIES_FILE), true) + : []; $new_identities = array(); diff --git a/website/present.json b/website/present.json deleted file mode 100644 index 05b8986..0000000 --- a/website/present.json +++ /dev/null @@ -1 +0,0 @@ -{"timestamp":1691957288,"names":["Jon"]} \ No newline at end of file