Add keys and message ID checking

This commit is contained in:
Terra 2022-04-24 23:32:49 +02:00
parent 6628e4f908
commit b85ea6585d
4 changed files with 41 additions and 20 deletions

View File

@ -86,7 +86,7 @@ port.write(b"w")
port.write(bytearray(config))
server_key = 0x2e29b257521dc792
client_key = 0x0#0x7ed64cce5b5d8e85
client_key = 0x7ed64cce5b5d8e85
port.write(b"k")
port.write(struct.pack("<QQ", client_key, server_key))

View File

@ -3,6 +3,9 @@ address = 0x0001
secret_key = 0x2e29b257521dc792
time_interval = 5
[node]
address = 0x1FFF
# Hexadecimal IDs
[node.1FFF]
secret_key = 0x7ed64cce5b5d8e85
[node.1234]
secret_key = 0x7ed64cce5b5d8e85

View File

@ -18,8 +18,7 @@ class MultiNode:
def __init__(self):
with open("Config.toml", "r") as config_file:
config = toml.loads(config_file.read())
print(config)
self.server_address = config["server"]["address"]
self.server_secret_key = config["server"]["secret_key"]
# How often to send the time
@ -27,6 +26,10 @@ class MultiNode:
self.devices = {}
self.last_time_message = time.time()
for node_id_str in config["node"]:
node_id = int(node_id_str, 16)
self.devices[node_id] = config["node"][node_id_str]
self.lora = LoRa(
0, # SPI channel
25, # Interrupt pin
@ -83,15 +86,30 @@ class MultiNode:
print(f"Invalid length! Expected {length + 9 + HASH_LENGTH} actual {len(payload.message)}")
return
hash_function = blake2s(key=0x0.to_bytes(8, "little"), digest_size=8)
if not tx_id in self.devices:
with open("Config.toml", "r") as config_file:
config = toml.loads(config_file.read())
for node_id_str in config["node"]:
node_id = int(node_id_str, 16)
if not node_id in self.devices:
self.devices[node_id] = config["node"][node_id_str]
if not tx_id in self.devices:
print(f"Error: Unrecognized device with ID {tx_id}")
return
hash_function = blake2s(key=self.devices[tx_id]["secret_key"].to_bytes(8, "little"), digest_size=8)
hash_function.update(payload.message[:-HASH_LENGTH])
if hash_function.digest() != data_hash:
print(f"Hash doesn't match! Expected {hash_function.digest()} got {data_hash}")
return
# self.devices[tx_id] = {}
if not tx_id in self.devices:
self.devices[tx_id] = {}
if "last_message_id" in self.devices[tx_id]:
if msg_id <= self.devices[tx_id]["last_message_id"]:
print(f'Error, expected an ID larger than {self.devices[tx_id]["last_message_id"]} but got {msg_id}')
return
self.devices[tx_id]["last_message_id"] = msg_id
self.decode_packet(self.devices[tx_id], data)
@ -122,19 +140,20 @@ class MultiNode:
def print_data(self):
for device_id in self.devices:
device = self.devices[device_id]
print(f'Node {device_id} @ ID {device["last_message_id"]}:')
if "last_message_id" in device:
print(f'Node {device_id} @ ID {device["last_message_id"]}:')
if "status" in device:
print(f'\t{device["status"]["battery"]:.3f} V {device["status"]["temperature"]:.1f} °C')
if "status" in device:
print(f'\t{device["status"]["battery"]:.3f} V {device["status"]["temperature"]:.1f} °C')
if "sensors" in device:
for sensor in device["sensors"]:
if sensor["value"] == math.nan:
print(f'\tCH {sensor["channel"]} on Pin {sensor["pin"]}: ERROR')
else:
print(f'\tCH {sensor["channel"]} on Pin {sensor["pin"]}: {sensor["value"]:.4f} {self.sensor_type_table[sensor["type"]]}')
print()
if "sensors" in device:
for sensor in device["sensors"]:
if sensor["value"] == math.nan:
print(f'\tCH {sensor["channel"]} on Pin {sensor["pin"]}: ERROR')
else:
print(f'\tCH {sensor["channel"]} on Pin {sensor["pin"]}: {sensor["value"]:.4f} {self.sensor_type_table[sensor["type"]]}')
print()
if __name__ == "__main__":

1
TODO
View File

@ -1,4 +1,3 @@
- Add password protection to configuration
- Webserver
- More sensor types
- Control of node outputs