CZI_presence_detector/scanner/uploader.py

88 lines
2.3 KiB
Python
Raw Normal View History

2023-08-17 21:46:32 +02:00
#!/usr/bin/python
# some credits: https://gist.github.com/calebmadrigal/fdb8855a6d05c87bbb0254a1424ee582
2023-08-12 15:36:29 +02:00
import yaml
2023-08-12 17:23:33 +02:00
import requests
import json
import hmac
import hashlib
2023-08-17 21:46:32 +02:00
import time
2023-08-12 15:36:29 +02:00
2023-08-17 21:23:49 +02:00
BASE_URL = "http://localhost:8080/"
2023-08-12 17:23:33 +02:00
WEBHOOK_SECRET = "CHANGE-THIS"
2023-08-17 21:23:49 +02:00
WIFI_MAP_PATH = "wifi_map.yaml"
2023-08-17 21:46:32 +02:00
UPLOAD_INTERVAL = 30
2023-08-12 15:36:29 +02:00
2023-08-13 22:08:35 +02:00
def get_identities():
2023-08-17 21:23:49 +02:00
identities_url = BASE_URL.rstrip('/') + '/identities.json'
resp = requests.get(identities_url)
2023-08-17 21:46:32 +02:00
if resp.status_code == 200:
return resp.json()
else:
return []
2023-08-12 15:36:29 +02:00
def parse_wifi_map(map_path):
2023-08-12 17:23:33 +02:00
# read scan results
2023-08-12 15:36:29 +02:00
with open(map_path, 'r') as f:
wifi_map = yaml.safe_load(f)
2023-08-12 17:23:33 +02:00
# read known identities
2023-08-13 22:08:35 +02:00
identities = get_identities()
2023-08-12 15:36:29 +02:00
2023-08-17 21:46:32 +02:00
#print("Known identities:")
#for identity in identities:
# print('mac hash = {}, name = {}'.format(identity['mac_hash'],identity['name']))
2023-08-12 15:36:29 +02:00
2023-08-12 20:15:43 +02:00
filtered_identities = set()
2023-08-12 15:36:29 +02:00
2023-08-12 17:23:33 +02:00
# filter scan results for known identities
2023-08-12 15:36:29 +02:00
for ssid in wifi_map:
ssid_node = wifi_map[ssid]
for bssid in ssid_node:
bssid_node = ssid_node[bssid]
if 'devices' in bssid_node:
for device in bssid_node['devices']:
for identity in identities:
2023-08-13 22:08:35 +02:00
mac_hash = hashlib.sha256(device.encode()).hexdigest()
if identity['mac_hash'] == mac_hash:
2023-08-12 20:26:11 +02:00
filtered_identities |= {identity['name']}
2023-08-12 15:36:29 +02:00
2023-08-17 21:46:32 +02:00
#print('\nFiltered identities:',filtered_identities)
2023-08-12 15:36:29 +02:00
2023-08-12 20:15:43 +02:00
return filtered_identities
2023-08-12 17:23:33 +02:00
2023-08-17 21:46:32 +02:00
def upload_identities(identities):
2023-08-12 17:23:33 +02:00
# build request
2023-08-17 21:46:32 +02:00
json_payload = json.dumps(list(identities)).encode("utf-8")
2023-08-12 17:23:33 +02:00
signature = hmac.new(
key=bytes(WEBHOOK_SECRET, "utf-8"),
msg=json_payload,
digestmod=hashlib.sha256
).hexdigest()
req_headers = {
'Content-Type': 'application/json',
'x-hmac-hash': signature
}
2023-08-17 21:23:49 +02:00
webhook_url = BASE_URL.rstrip('/') + '/update.php'
2023-08-12 17:23:33 +02:00
# send request
2023-08-17 21:23:49 +02:00
r = requests.post(url=webhook_url, data=json_payload, headers=req_headers)
2023-08-17 21:46:32 +02:00
#print("Upload response:", r.status_code)
if __name__ == '__main__':
while(1):
filtered_identities = parse_wifi_map(WIFI_MAP_PATH)
upload_identities(filtered_identities)
time.sleep(UPLOAD_INTERVAL)
2023-08-12 15:36:29 +02:00