Use environment variables for default MQTT server connection info for client proxy

This commit is contained in:
Garth Vander Houwen 2025-01-01 11:56:25 -08:00
parent abaa913d86
commit 3a79e10182
4 changed files with 70 additions and 45 deletions

View file

@ -146,6 +146,7 @@
DD994B69295F88B60013760A /* IntervalEnums.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD994B68295F88B60013760A /* IntervalEnums.swift */; };
DDA0B6B2294CDC55001356EC /* Channels.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDA0B6B1294CDC55001356EC /* Channels.swift */; };
DDA1C48E28DB49D3009933EC /* ChannelRoles.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDA1C48D28DB49D3009933EC /* ChannelRoles.swift */; };
DDA28B1A2D25CF7800EF726F /* EnvironmentValues.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDA28B192D25CF6E00EF726F /* EnvironmentValues.swift */; };
DDA6B2E928419CF2003E8C16 /* MeshPackets.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDA6B2E828419CF2003E8C16 /* MeshPackets.swift */; };
DDA9515A2BC6624100CEA535 /* TelemetryWeather.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDA951592BC6624100CEA535 /* TelemetryWeather.swift */; };
DDA9515C2BC6631200CEA535 /* TelemetryEnums.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDA9515B2BC6631200CEA535 /* TelemetryEnums.swift */; };
@ -429,6 +430,7 @@
DD9A1A912BA2D2D3001E602E /* MeshtasticDataModelV 30.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "MeshtasticDataModelV 30.xcdatamodel"; sourceTree = "<group>"; };
DDA0B6B1294CDC55001356EC /* Channels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Channels.swift; sourceTree = "<group>"; };
DDA1C48D28DB49D3009933EC /* ChannelRoles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelRoles.swift; sourceTree = "<group>"; };
DDA28B192D25CF6E00EF726F /* EnvironmentValues.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnvironmentValues.swift; sourceTree = "<group>"; };
DDA6B2E828419CF2003E8C16 /* MeshPackets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeshPackets.swift; sourceTree = "<group>"; };
DDA951592BC6624100CEA535 /* TelemetryWeather.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TelemetryWeather.swift; sourceTree = "<group>"; };
DDA9515B2BC6631200CEA535 /* TelemetryEnums.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TelemetryEnums.swift; sourceTree = "<group>"; };
@ -1096,6 +1098,7 @@
DDDB445329F8AD1600EE2349 /* Data.swift */,
DDDB445129F8ACF900EE2349 /* Date.swift */,
DDDB444129F8A88700EE2349 /* Double.swift */,
DDA28B192D25CF6E00EF726F /* EnvironmentValues.swift */,
DDDB444329F8A8DD00EE2349 /* Float.swift */,
DDDB444D29F8AB0E00EE2349 /* Int.swift */,
DDDB444729F8A9C900EE2349 /* String.swift */,
@ -1497,6 +1500,7 @@
DDB6ABE228B13FB500384BA1 /* PositionConfigEnums.swift in Sources */,
DD994B69295F88B60013760A /* IntervalEnums.swift in Sources */,
DDDCD5702BB26F5C00BE6B60 /* NodeListFilter.swift in Sources */,
DDA28B1A2D25CF7800EF726F /* EnvironmentValues.swift in Sources */,
DD6F65742C6CB80A0053C113 /* View.swift in Sources */,
DD1933762B0835D500771CD5 /* PositionAltitudeChart.swift in Sources */,
DD415828285859C4009B0E59 /* TelemetryConfig.swift in Sources */,

View file

@ -0,0 +1,12 @@
//
// EnvironmentValues.swift
// Meshtastic
//
// Copyright(c) by Garth Vander Houwen on 1/1/25.
//
import SwiftUI
extension EnvironmentValues {
@Entry var publicMqttUsername: String = "meshdev"
@Entry var publicMqttPsk: String = "large4cats"
}

View file

@ -6,6 +6,7 @@
//
import Foundation
import SwiftUI
import CocoaMQTT
import OSLog
@ -26,7 +27,7 @@ class MqttClientProxyManager {
var debugLog = false
func connectFromConfigSettings(node: NodeInfoEntity) {
let defaultServerAddress = "mqtt.meshtastic.org"
let useSsl = node.mqttConfig?.tlsEnabled == true
var useSsl = node.mqttConfig?.tlsEnabled == false
var defaultServerPort = useSsl ? 8883 : 1883
var host = node.mqttConfig?.address
if host == nil || host!.isEmpty {
@ -43,8 +44,15 @@ class MqttClientProxyManager {
if let host = host {
let port = defaultServerPort
let username = node.mqttConfig?.username
let password = node.mqttConfig?.password
var username = node.mqttConfig?.username
var password = node.mqttConfig?.password
if host == defaultServerAddress {
@Environment(\.publicMqttUsername) var publicMqttUsername
@Environment(\.publicMqttPsk) var publicMqttPsk
username = publicMqttUsername
password = publicMqttPsk
useSsl = false
}
let root = node.mqttConfig?.root?.count ?? 0 > 0 ? node.mqttConfig?.root : "msh"
let prefix = root!
topic = prefix + (supportedVersion ? "/2/e" : "/2/c") + "/#"

View file

@ -174,51 +174,52 @@ struct MQTTConfig: View {
.keyboardType(.default)
}
.autocorrectionDisabled()
HStack {
Label("mqtt.username", systemImage: "person.text.rectangle")
TextField("mqtt.username", text: $username)
.foregroundColor(.gray)
.autocapitalization(.none)
.disableAutocorrection(true)
.onChange(of: username) {
var totalBytes = username.utf8.count
// Only mess with the value if it is too big
while totalBytes > 62 {
username = String(username.dropLast())
totalBytes = username.utf8.count
if !proxyToClientEnabled && address != "mqtt.meshtastic.org" {
HStack {
Label("mqtt.username", systemImage: "person.text.rectangle")
TextField("mqtt.username", text: $username)
.foregroundColor(.gray)
.autocapitalization(.none)
.disableAutocorrection(true)
.onChange(of: username) {
var totalBytes = username.utf8.count
// Only mess with the value if it is too big
while totalBytes > 62 {
username = String(username.dropLast())
totalBytes = username.utf8.count
}
hasChanges = true
}
hasChanges = true
}
.foregroundColor(.gray)
}
.keyboardType(.default)
.scrollDismissesKeyboard(.interactively)
HStack {
Label("password", systemImage: "wallet.pass")
TextField("password", text: $password)
.foregroundColor(.gray)
.autocapitalization(.none)
.disableAutocorrection(true)
.onChange(of: password) {
var totalBytes = password.utf8.count
// Only mess with the value if it is too big
while totalBytes > 62 {
password = String(password.dropLast())
totalBytes = password.utf8.count
.foregroundColor(.gray)
}
.keyboardType(.default)
.scrollDismissesKeyboard(.interactively)
HStack {
Label("password", systemImage: "wallet.pass")
TextField("password", text: $password)
.foregroundColor(.gray)
.autocapitalization(.none)
.disableAutocorrection(true)
.onChange(of: password) {
var totalBytes = password.utf8.count
// Only mess with the value if it is too big
while totalBytes > 62 {
password = String(password.dropLast())
totalBytes = password.utf8.count
}
hasChanges = true
}
hasChanges = true
}
.foregroundColor(.gray)
.foregroundColor(.gray)
}
.keyboardType(.default)
.scrollDismissesKeyboard(.interactively)
.listRowSeparator(/*@START_MENU_TOKEN@*/.visible/*@END_MENU_TOKEN@*/)
Toggle(isOn: $tlsEnabled) {
Label("TLS Enabled", systemImage: "checkmark.shield.fill")
Text("Your MQTT Server must support TLS. Not available via the public mqtt server.")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
}
.keyboardType(.default)
.scrollDismissesKeyboard(.interactively)
.listRowSeparator(/*@START_MENU_TOKEN@*/.visible/*@END_MENU_TOKEN@*/)
Toggle(isOn: $tlsEnabled) {
Label("TLS Enabled", systemImage: "checkmark.shield.fill")
Text("Your MQTT Server must support TLS. Not available via the public mqtt server.")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
}
Text("For all Mqtt functionality other than the map report you must also set uplink and downlink for each channel you want to bridge over Mqtt.")
.font(.callout)