Skip to content

Commit

Permalink
cleanup and paho calbackAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
pdxlocations committed Nov 19, 2024
1 parent ad8b454 commit b69edbf
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 12 deletions.
3 changes: 1 addition & 2 deletions encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from utils import generate_hash


def decrypt_packet(mp, key):
"""Decrypt the encrypted message payload and return the decoded data."""
try:
Expand Down Expand Up @@ -52,4 +51,4 @@ def encrypt_packet(channel, key, mp, encoded_message):

except Exception as e:
logging.error(f"Failed to encrypt: {e}")
return None
return None
2 changes: 1 addition & 1 deletion load_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
HOP_MODIFIER = config['hop_modifier']

# Get the full default key
EXPANDED_KEY = "1PG7OiApB1nwvP+rz05pAQ==" if KEY == "AQ==" else KEY
KEY = "1PG7OiApB1nwvP+rz05pAQ==" if KEY == "AQ==" else KEY
3 changes: 1 addition & 2 deletions logger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging

# Enable logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s %(message)s',
Expand All @@ -27,4 +26,4 @@ def log_skipped_message(from_topic, portnum, action) -> None:
f"Portnum : {portnum}\n"
f"Action : {action}\n"
f"{DIVIDER}"
)
)
1 change: 0 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import logging
import sys

Expand Down
10 changes: 5 additions & 5 deletions message_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def on_message(client, userdata, msg) -> None:

# Decrypt the payload if necessary
if original_mp.HasField("encrypted") and not original_mp.HasField("decoded"):
decoded_data = decrypt_packet(original_mp, load_config.EXPANDED_KEY)
decoded_data = decrypt_packet(original_mp, load_config.KEY)
if decoded_data is None: # Check if decryption failed
logging.error("Decryption failed; skipping message")
return # Skip processing this message if decryption failed
Expand Down Expand Up @@ -81,16 +81,16 @@ def on_message(client, userdata, msg) -> None:
forward_to_preset = target_topic.split("/")[-1]
target_topic =f"{target_topic}/{gateway_node_id}"

new_channel = generate_hash(forward_to_preset, load_config.EXPANDED_KEY)
new_channel = generate_hash(forward_to_preset, load_config.KEY)
modified_mp.channel = new_channel
original_channel = msg.topic
original_channel = original_channel.split("/")[-2]
original_channel = generate_hash(original_channel, load_config.EXPANDED_KEY)
original_channel = generate_hash(original_channel, load_config.KEY)

if load_config.EXPANDED_KEY == "":
if load_config.KEY == "":
modified_mp.decoded.CopyFrom(original_mp.decoded)
else:
modified_mp.encrypted = encrypt_packet(forward_to_preset, load_config.EXPANDED_KEY, modified_mp, original_mp.decoded)
modified_mp.encrypted = encrypt_packet(forward_to_preset, load_config.KEY, modified_mp, original_mp.decoded)


# Package the modified packet for publishing
Expand Down
2 changes: 1 addition & 1 deletion mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def on_connect(client, userdata, flags, reason_code, properties=None):
logging.error(f"Failed to connect with reason code {reason_code}")

def create_mqtt_client():
client = mqtt.Client()
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
client.username_pw_set(load_config.USER, load_config.PASSWORD)
client.on_connect = on_connect
return client

0 comments on commit b69edbf

Please sign in to comment.