From b0c1b7b7b5fd295161164e9696673e482160941f Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Fri, 16 Aug 2024 10:10:08 -0500 Subject: [PATCH] MQTT PKI fixes --- src/mqtt/MQTT.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 1d2d9bca0..d3a2bb92c 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -152,7 +152,7 @@ void MQTT::onReceive(char *topic, byte *payload, size_t length) LOG_INFO("Ignoring downlink message we originally sent.\n"); } else { // Find channel by channel_id and check downlink_enabled - if ((strcmp(e.channel_id, "PKI") && e.packet) || + if ((strcmp(e.channel_id, "PKI") == 0 && e.packet) || (strcmp(e.channel_id, channels.getGlobalId(ch.index)) == 0 && e.packet && ch.settings.downlink_enabled)) { LOG_INFO("Received MQTT topic %s, len=%u\n", topic, length); meshtastic_MeshPacket *p = packetPool.allocCopy(*e.packet); @@ -163,7 +163,8 @@ void MQTT::onReceive(char *topic, byte *payload, size_t length) } // PKI messages get accepted even if we can't decrypt - if (router && p->which_payload_variant == meshtastic_MeshPacket_encrypted_tag && p->channel == 0) + if (router && p->which_payload_variant == meshtastic_MeshPacket_encrypted_tag && + strcmp(e.channel_id, "PKI") == 0) router->enqueueReceivedMessage(p); // ignore messages if we don't have the channel key else if (router && perhapsDecode(p)) @@ -365,10 +366,12 @@ void MQTT::reconnect() void MQTT::sendSubscriptions() { #if HAS_NETWORKING + bool hasDownlink = false; size_t numChan = channels.getNumChannels(); for (size_t i = 0; i < numChan; i++) { const auto &ch = channels.getByIndex(i); if (ch.settings.downlink_enabled) { + hasDownlink = true; std::string topic = cryptTopic + channels.getGlobalId(i) + "/#"; LOG_INFO("Subscribing to %s\n", topic.c_str()); pubSub.subscribe(topic.c_str(), 1); // FIXME, is QOS 1 right? @@ -382,9 +385,11 @@ void MQTT::sendSubscriptions() } } #if !MESHTASTIC_EXCLUDE_PKI - std::string topic = cryptTopic + "PKI/#"; - LOG_INFO("Subscribing to %s\n", topic.c_str()); - pubSub.subscribe(topic.c_str(), 1); + if (hasDownlink) { + std::string topic = cryptTopic + "PKI/#"; + LOG_INFO("Subscribing to %s\n", topic.c_str()); + pubSub.subscribe(topic.c_str(), 1); + } #endif #endif } @@ -496,7 +501,13 @@ void MQTT::onSend(const meshtastic_MeshPacket &mp, const meshtastic_MeshPacket & { if (mp.via_mqtt) return; // Don't send messages that came from MQTT back into MQTT - + bool uplinkEnabled = false; + for (int i = 0; i <= 7; i++) { + if (channels.getByIndex(i).settings.uplink_enabled) + uplinkEnabled = true; + } + if (!uplinkEnabled) + return; // no channels have an uplink enabled auto &ch = channels.getByIndex(chIndex); if (mp_decoded.which_payload_variant != meshtastic_MeshPacket_decoded_tag) { @@ -511,7 +522,7 @@ void MQTT::onSend(const meshtastic_MeshPacket &mp, const meshtastic_MeshPacket & return; } - if (ch.settings.uplink_enabled) { + if (ch.settings.uplink_enabled || mp.pki_encrypted) { const char *channelId = channels.getGlobalId(chIndex); // FIXME, for now we just use the human name for the channel meshtastic_ServiceEnvelope *env = mqttPool.allocZeroed();