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)) port.write(bytearray(config))
server_key = 0x2e29b257521dc792 server_key = 0x2e29b257521dc792
client_key = 0x0#0x7ed64cce5b5d8e85 client_key = 0x7ed64cce5b5d8e85
port.write(b"k") port.write(b"k")
port.write(struct.pack("<QQ", client_key, server_key)) port.write(struct.pack("<QQ", client_key, server_key))

View File

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

View File

@ -19,7 +19,6 @@ class MultiNode:
with open("Config.toml", "r") as config_file: with open("Config.toml", "r") as config_file:
config = toml.loads(config_file.read()) config = toml.loads(config_file.read())
print(config)
self.server_address = config["server"]["address"] self.server_address = config["server"]["address"]
self.server_secret_key = config["server"]["secret_key"] self.server_secret_key = config["server"]["secret_key"]
# How often to send the time # How often to send the time
@ -27,6 +26,10 @@ class MultiNode:
self.devices = {} self.devices = {}
self.last_time_message = time.time() 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( self.lora = LoRa(
0, # SPI channel 0, # SPI channel
25, # Interrupt pin 25, # Interrupt pin
@ -83,15 +86,30 @@ class MultiNode:
print(f"Invalid length! Expected {length + 9 + HASH_LENGTH} actual {len(payload.message)}") print(f"Invalid length! Expected {length + 9 + HASH_LENGTH} actual {len(payload.message)}")
return 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]) hash_function.update(payload.message[:-HASH_LENGTH])
if hash_function.digest() != data_hash: if hash_function.digest() != data_hash:
print(f"Hash doesn't match! Expected {hash_function.digest()} got {data_hash}") print(f"Hash doesn't match! Expected {hash_function.digest()} got {data_hash}")
return return
# self.devices[tx_id] = {}
if not tx_id in self.devices: if "last_message_id" in self.devices[tx_id]:
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.devices[tx_id]["last_message_id"] = msg_id
self.decode_packet(self.devices[tx_id], data) self.decode_packet(self.devices[tx_id], data)
@ -122,6 +140,7 @@ class MultiNode:
def print_data(self): def print_data(self):
for device_id in self.devices: for device_id in self.devices:
device = self.devices[device_id] device = self.devices[device_id]
if "last_message_id" in device:
print(f'Node {device_id} @ ID {device["last_message_id"]}:') print(f'Node {device_id} @ ID {device["last_message_id"]}:')
if "status" in device: if "status" in device:

1
TODO
View File

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