Robust Meshtastic MQTT binary and JSON message parsers: Safe protobuf handling, TEXT_MESSAGE_APP support, updated requirements
This commit is contained in:
1 parent
0d42d5781a
commit
3e3e9ab72f
3 files changed
+172
No files matched your search
+64
@@ -0,0 +1,64 @@
|
||||
import paho.mqtt.client as mqtt
|
||||
import config
|
||||
import logging
|
||||
import json
|
||||
import datetime
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
datefmt='%Y-%m-%d %H:%M:%S'
|
||||
)
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
try:
|
||||
payload = msg.payload.decode('utf-8')
|
||||
data = json.loads(payload)
|
||||
# New format: type == 'text' and payload['text']
|
||||
if data.get("type") == "text" and "payload" in data and "text" in data["payload"]:
|
||||
text = data["payload"]["text"]
|
||||
from_id = data.get("from")
|
||||
to_id = data.get("to")
|
||||
sender = data.get("sender")
|
||||
rssi = data.get("rssi")
|
||||
hop_start = data.get("hop_start")
|
||||
hops_away = data.get("hops_away")
|
||||
timestamp = data.get("timestamp")
|
||||
if timestamp:
|
||||
ts_str = datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
|
||||
else:
|
||||
ts_str = "N/A"
|
||||
logging.info(f"Text message from {from_id} ({sender}) to {to_id}: {text} | RSSI: {rssi} | Hop start: {hop_start} | Hops away: {hops_away} | Time: {ts_str}")
|
||||
# Old format (if encountered)
|
||||
elif "decoded" in data:
|
||||
decoded = data["decoded"]
|
||||
portnum = decoded.get("portnum")
|
||||
if portnum == 1 or portnum == "TEXT_MESSAGE_APP":
|
||||
text = decoded.get("payload")
|
||||
from_id = data.get("from")
|
||||
to_id = data.get("to")
|
||||
rssi = data.get("rssi")
|
||||
hop_start = data.get("hop_start")
|
||||
hops_away = data.get("hops_away")
|
||||
timestamp = data.get("timestamp")
|
||||
if timestamp:
|
||||
ts_str = datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
|
||||
else:
|
||||
ts_str = "N/A"
|
||||
logging.info(f"Text message from {from_id} to {to_id}: {text} | RSSI: {rssi} | Hop start: {hop_start} | Hops away: {hops_away} | Time: {ts_str}")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def main():
|
||||
client = mqtt.Client()
|
||||
client.username_pw_set(config.MQTT_USERNAME, config.MQTT_PASSWORD)
|
||||
client.on_message = on_message
|
||||
client.connect(config.MQTT_BROKER, config.MQTT_PORT, 60)
|
||||
topic = config.MQTT_JSON_TOPIC
|
||||
client.subscribe(topic)
|
||||
logging.info(f"Subscribed to topic: {topic}")
|
||||
logging.info("Started Meshtastic JSON text message logger.")
|
||||
client.loop_forever()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
import paho.mqtt.client as mqtt
|
||||
import config
|
||||
import logging
|
||||
import base64
|
||||
from Crypto.Cipher import AES
|
||||
import datetime
|
||||
|
||||
try:
|
||||
from meshtastic import mesh_pb2, portnums_pb2, mqtt_pb2
|
||||
except ImportError:
|
||||
import meshtastic.mesh_pb2 as mesh_pb2
|
||||
import meshtastic.portnums_pb2 as portnums_pb2
|
||||
import meshtastic.mqtt_pb2 as mqtt_pb2
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s [%(levelname)s] %(message)s',
|
||||
datefmt='%Y-%m-%d %H:%M:%S'
|
||||
)
|
||||
|
||||
BROADCAST_KEY = b'\x01' * 16
|
||||
|
||||
|
||||
def decrypt_payload_ctr(encrypted, packet_id, from_id, key=BROADCAST_KEY):
|
||||
try:
|
||||
# nonce = id (8 bytes LE) + from (8 bytes LE)
|
||||
nonce = packet_id.to_bytes(8, 'little') + from_id.to_bytes(8, 'little')
|
||||
cipher = AES.new(key, AES.MODE_CTR, nonce=nonce)
|
||||
decrypted = cipher.decrypt(encrypted)
|
||||
return decrypted
|
||||
except Exception as e:
|
||||
logging.warning(f"Decryption error (CTR): {e}")
|
||||
return None
|
||||
|
||||
|
||||
def parse_and_log_message(packet):
|
||||
try:
|
||||
logging.info(f"Protobuf packet structure:\n{packet}")
|
||||
if packet.HasField("decoded"):
|
||||
decoded = packet.decoded
|
||||
if decoded.portnum == portnums_pb2.TEXT_MESSAGE_APP:
|
||||
text = decoded.payload.decode(errors='replace')
|
||||
from_id = getattr(packet, 'from')
|
||||
to_id = packet.to
|
||||
rssi = packet.rx_rssi
|
||||
hop_start = packet.hop_start
|
||||
hops_away = getattr(packet, 'hops_away', None)
|
||||
timestamp = packet.rx_time
|
||||
if timestamp:
|
||||
ts_str = datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
|
||||
else:
|
||||
ts_str = "N/A"
|
||||
logging.info(f"Text message from {from_id} to {to_id}: {text} | RSSI: {rssi} | Hop start: {hop_start} | Hops away: {hops_away} | Time: {ts_str}")
|
||||
else:
|
||||
logging.info(f"Not a text message, portnum={decoded.portnum}")
|
||||
else:
|
||||
logging.info("No 'decoded' field in packet")
|
||||
except Exception as e:
|
||||
logging.warning(f"Protobuf parsing error: {e}")
|
||||
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
logging.info(f"Received binary message from topic {msg.topic}, length: {len(msg.payload)} bytes")
|
||||
# 1. Parse as ServiceEnvelope
|
||||
service_envelope = mqtt_pb2.ServiceEnvelope()
|
||||
try:
|
||||
service_envelope.ParseFromString(msg.payload)
|
||||
packet = service_envelope.packet
|
||||
# 2. If encrypted, decrypt via AES-CTR
|
||||
if packet.HasField("encrypted") and not packet.HasField("decoded"):
|
||||
encrypted = packet.encrypted
|
||||
packet_id = packet.id if packet.HasField("id") else 0
|
||||
from_id = packet.from if packet.HasField("from") else 0
|
||||
decrypted_payload = decrypt_payload_ctr(encrypted, packet_id, from_id)
|
||||
if decrypted_payload:
|
||||
try:
|
||||
decoded = mesh_pb2.DecodedPacket()
|
||||
decoded.ParseFromString(decrypted_payload)
|
||||
packet.decoded.CopyFrom(decoded)
|
||||
parse_and_log_message(packet)
|
||||
except Exception as e:
|
||||
logging.warning(f"Error parsing decrypted payload: {e}")
|
||||
else:
|
||||
logging.info("Failed to decrypt payload.")
|
||||
else:
|
||||
# If not encrypted or already has decoded
|
||||
parse_and_log_message(packet)
|
||||
except Exception as e:
|
||||
logging.warning(f"ServiceEnvelope parsing error: {e}")
|
||||
|
||||
|
||||
def main():
|
||||
client = mqtt.Client()
|
||||
client.username_pw_set(config.MQTT_USERNAME, config.MQTT_PASSWORD)
|
||||
client.on_message = on_message
|
||||
client.connect(config.MQTT_BROKER, config.MQTT_PORT, 60)
|
||||
topic = config.MQTT_TOPIC
|
||||
client.subscribe(topic)
|
||||
logging.info(f"Subscribed to binary topic: {topic}")
|
||||
logging.info("Started Meshtastic binary message logger.")
|
||||
client.loop_forever()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,4 @@
|
||||
python-telegram-bot
|
||||
paho-mqtt
|
||||
meshtastic
|
||||
cryptography
|
||||
Reference in new issue
Block a user