CZI_presence_detector/scanner/upload.py

76 lines
2 KiB
Python
Raw Normal View History

2023-08-12 15:36:29 +02:00
# credits: https://gist.github.com/calebmadrigal/fdb8855a6d05c87bbb0254a1424ee582
import sys
import yaml
2023-08-12 17:23:33 +02:00
import requests
import json
import hmac
import hashlib
2023-08-12 15:36:29 +02:00
2023-08-13 22:08:35 +02:00
IDENTITIES_URL = "http://localhost:8080/identities.json"
WEBHOOK_URL = "http://localhost:8080/update.php"
2023-08-12 17:23:33 +02:00
WEBHOOK_SECRET = "CHANGE-THIS"
2023-08-12 15:36:29 +02:00
2023-08-13 22:08:35 +02:00
def get_identities():
resp = requests.get(IDENTITIES_URL)
return resp.json()
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-12 17:23:33 +02:00
print("Known identities:")
2023-08-12 15:36:29 +02:00
for identity in identities:
2023-08-13 22:08:35 +02:00
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-12 20:15:43 +02:00
print('\nFiltered identities:')
print(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-12 15:36:29 +02:00
if __name__ == '__main__':
wifi_map_path = 'wifi_map.yaml'
if len(sys.argv) > 1:
wifi_map_path = sys.argv[1]
2023-08-12 17:23:33 +02:00
2023-08-12 20:15:43 +02:00
filtered_identities = parse_wifi_map(wifi_map_path)
2023-08-12 17:23:33 +02:00
# build request
2023-08-12 20:26:11 +02:00
json_payload = json.dumps(list(filtered_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
}
# send request
r = requests.post(url=WEBHOOK_URL, data=json_payload, headers=req_headers)
2023-08-12 15:36:29 +02:00
2023-08-12 17:23:33 +02:00
print("Upload response:")
print(r.status_code)