diff --git a/Meshtastic.xcodeproj/project.pbxproj b/Meshtastic.xcodeproj/project.pbxproj index d2a6449a..bb3dc1f9 100644 --- a/Meshtastic.xcodeproj/project.pbxproj +++ b/Meshtastic.xcodeproj/project.pbxproj @@ -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 = ""; }; DDA0B6B1294CDC55001356EC /* Channels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Channels.swift; sourceTree = ""; }; DDA1C48D28DB49D3009933EC /* ChannelRoles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelRoles.swift; sourceTree = ""; }; + DDA28B192D25CF6E00EF726F /* EnvironmentValues.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnvironmentValues.swift; sourceTree = ""; }; DDA6B2E828419CF2003E8C16 /* MeshPackets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeshPackets.swift; sourceTree = ""; }; DDA951592BC6624100CEA535 /* TelemetryWeather.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TelemetryWeather.swift; sourceTree = ""; }; DDA9515B2BC6631200CEA535 /* TelemetryEnums.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TelemetryEnums.swift; sourceTree = ""; }; @@ -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 */, diff --git a/Meshtastic/Extensions/EnvironmentValues.swift b/Meshtastic/Extensions/EnvironmentValues.swift new file mode 100644 index 00000000..236042d4 --- /dev/null +++ b/Meshtastic/Extensions/EnvironmentValues.swift @@ -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" +} diff --git a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift index 77cd1088..e6a4e8bf 100644 --- a/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift +++ b/Meshtastic/Helpers/Mqtt/MqttClientProxyManager.swift @@ -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") + "/#" diff --git a/Meshtastic/Views/Settings/Config/Module/MQTTConfig.swift b/Meshtastic/Views/Settings/Config/Module/MQTTConfig.swift index fc93e6f9..ca54a40e 100644 --- a/Meshtastic/Views/Settings/Config/Module/MQTTConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/MQTTConfig.swift @@ -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)