From dc00d8d118c18f88967d769934541710123cdd9d Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sat, 19 Apr 2025 06:49:15 -0500 Subject: [PATCH 1/3] Add delegate to fix tls issues with cocoamqtt --- Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift index cac34de8..1fa45f2e 100644 --- a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift +++ b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift @@ -40,8 +40,8 @@ class MqttClientProxyManager { if let host = host { let port = defaultServerPort - var username = node.mqttConfig?.username - var password = node.mqttConfig?.password + let username = node.mqttConfig?.username + let password = node.mqttConfig?.password // if host == defaultServerAddress { //username = ProcessInfo.processInfo.environment["PUBLIC_MQTT_USERNAME"] //password = ProcessInfo.processInfo.environment["PUBLIC_MQTT_PASSWORD"] @@ -130,6 +130,9 @@ extension MqttClientProxyManager: CocoaMQTTDelegate { self.disconnect() } } + func mqtt(_ mqtt: CocoaMQTT, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void) { + completionHandler(true) + } func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) { Logger.mqtt.debug("📲 [MQTT Client Proxy] disconnected: \(err?.localizedDescription ?? "", privacy: .public)") if let error = err { From 5e6486b8d4f8c4634b42f078605cf52e3762047e Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sat, 19 Apr 2025 07:06:56 -0500 Subject: [PATCH 2/3] Update Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../Helpers/Mqtt/MqttClientProxyManager.swift | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift index 1fa45f2e..450dbc52 100644 --- a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift +++ b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift @@ -131,8 +131,24 @@ extension MqttClientProxyManager: CocoaMQTTDelegate { } } func mqtt(_ mqtt: CocoaMQTT, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void) { - completionHandler(true) + var isValid = false + #if canImport(Security) + if #available(macOS 10.15, iOS 13.0, *) { + isValid = SecTrustEvaluateWithError(trust, nil) + } else { + var result: SecTrustResultType = .invalid + let status = SecTrustEvaluate(trust, &result) + isValid = (status == errSecSuccess) && (result == .unspecified || result == .proceed) } + #endif + if isValid { + Logger.mqtt.info("📲 [MQTT Client Proxy] TLS validation succeeded.") + completionHandler(true) + } else { + Logger.mqtt.error("📲 [MQTT Client Proxy] TLS validation failed.") + completionHandler(false) + } + } func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) { Logger.mqtt.debug("📲 [MQTT Client Proxy] disconnected: \(err?.localizedDescription ?? "", privacy: .public)") if let error = err { From d4ece62cce1bfd0b6654a89d0ac9c5c3d3e39b3f Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sat, 19 Apr 2025 07:09:08 -0500 Subject: [PATCH 3/3] Complete anyway --- Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift index 450dbc52..569379b5 100644 --- a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift +++ b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift @@ -145,8 +145,8 @@ extension MqttClientProxyManager: CocoaMQTTDelegate { Logger.mqtt.info("📲 [MQTT Client Proxy] TLS validation succeeded.") completionHandler(true) } else { - Logger.mqtt.error("📲 [MQTT Client Proxy] TLS validation failed.") - completionHandler(false) + Logger.mqtt.warning("📲 [MQTT Client Proxy] TLS validation failed.") + completionHandler(true) } } func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {