fix: MQTT proxy connection and probe test failures (#5215)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
James Rich
2026-04-22 11:05:24 -05:00
committed by GitHub
parent ba559549ba
commit 765594f7ee
2 changed files with 11 additions and 7 deletions

View File

@@ -44,6 +44,7 @@ import org.meshtastic.mqtt.ProbeResult
import org.meshtastic.mqtt.probe
import org.meshtastic.proto.MqttClientProxyMessage
import org.meshtastic.proto.ToRadio
import kotlin.time.Clock
@Single
class MqttManagerImpl(
@@ -125,6 +126,8 @@ class MqttManagerImpl(
val endpoint = resolveEndpoint(address, tlsEnabled)
val result =
MqttClient.probe(endpoint = endpoint) {
// Provide a valid client ID for the probe; brokers reject empty identifiers
clientId = "MeshtasticProbe-${Clock.System.now().toEpochMilliseconds()}"
val user = username?.takeUnless { it.isEmpty() }
val pass = password?.takeUnless { it.isEmpty() }
if (user != null) this.username = user

View File

@@ -219,15 +219,17 @@ class MQTTRepositoryImpl(
}
}
private const val MQTT_PORT_PLAIN = 1883
private const val MQTT_PORT_TLS = 8883
/**
* Resolve a user-supplied broker address into an [MqttEndpoint].
*
* Address resolution rules:
* - If [rawAddress] already contains a URI scheme (`scheme://…`), parse it directly via [MqttEndpoint.parse] and
* respect whatever transport / port the user encoded.
* - Otherwise wrap it as a WebSocket endpoint (`ws[s]://host${WEBSOCKET_PATH}`) so the proxy works over CDNs and
* firewall-restricted networks where raw 1883/8883 may be blocked. The scheme is `wss` when [tlsEnabled] is `true`,
* `ws` otherwise.
* - Otherwise wrap it as a TCP endpoint using standard MQTT ports: port 8883 if [tlsEnabled] is `true`, port 1883
* otherwise. This allows standard MQTT brokers to work out of the box.
*
* Extracted as a top-level function so [MQTTRepositoryImplTest] can exercise every branch without spinning up the full
* repository, and so `MqttManagerImpl` (in `:core:data`) can reuse the same parsing rules for the probe API. Visibility
@@ -236,8 +238,7 @@ class MQTTRepositoryImpl(
fun resolveEndpoint(rawAddress: String, tlsEnabled: Boolean): MqttEndpoint = if (rawAddress.contains("://")) {
MqttEndpoint.parse(rawAddress)
} else {
val scheme = if (tlsEnabled) "wss" else "ws"
MqttEndpoint.parse("$scheme://$rawAddress$WEBSOCKET_PATH")
val port = if (tlsEnabled) MQTT_PORT_TLS else MQTT_PORT_PLAIN
val scheme = if (tlsEnabled) "ssl" else "tcp"
MqttEndpoint.parse("$scheme://$rawAddress:$port")
}
private const val WEBSOCKET_PATH = "/mqtt"