1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
//
// DebugViewModel.swift
// MullvadVPN
//
// Created by Jon Petersson on 2026-02-27.
// Copyright © 2026 Mullvad VPN AB. All rights reserved.
//
import MullvadSettings
import Network
import PacketTunnelCore
import SwiftUI
@MainActor
protocol DebugViewModel: ObservableObject {
typealias Item = (title: String, data: [String])
var tunnelSettings: LatestTunnelSettings { get }
var nwPathStatus: NWPath.Status { get }
var connection: [Item] { get }
var settings: [Item] { get }
}
class DebugViewModelImpl: DebugViewModel {
var tunnelManager: TunnelManager
var nwPathMonitor: NWPathMonitor
var appPreferences: AppPreferencesDataSource
var tunnelObserver: TunnelBlockObserver!
var tunnelSettings: LatestTunnelSettings
var tunnelStatus: TunnelStatus
var nwPathStatus = NWPath.Status.unsatisfied
@Published var connection = [Item]()
@Published var settings = [Item]()
init(
tunnelManager: TunnelManager,
nwPathMonitor: NWPathMonitor,
appPreferences: AppPreferencesDataSource
) {
self.tunnelManager = tunnelManager
self.nwPathMonitor = nwPathMonitor
self.appPreferences = appPreferences
tunnelSettings = tunnelManager.settings
tunnelStatus = tunnelManager.tunnelStatus
refreshData()
tunnelObserver = TunnelBlockObserver(
didUpdateTunnelStatus: { _, status in
self.tunnelStatus = status
self.refreshData()
},
didUpdateDeviceState: { _, _, _ in },
didUpdateTunnelSettings: { _, settings in
self.tunnelSettings = settings
self.refreshData()
}
)
self.tunnelManager.addObserver(tunnelObserver)
}
// Arrange these functions to get the desired section order in the view.
private func refreshData() {
// Connection
setRelays()
setObfuscation()
// Settings
setRelaySettings()
setMultihopSettings()
setDaitaSettings()
setObfuscationSettings()
setQuantumResistanceSettings()
setIncludeAllNetworksSettings()
setMullvadDnsBlockers()
setCustomDnsBlockers()
}
private func update(item: Item, in list: inout [Item]) {
guard let index = (list.firstIndex { $0.title == item.title }) else {
list.append(item)
return
}
list.remove(at: index)
list.insert(item, at: index)
}
}
// MARK: Connection
extension DebugViewModelImpl {
private func setRelays() {
let entry = tunnelStatus.state.relays?.entry?.debugDescription ?? "-"
let exit = tunnelStatus.state.relays?.exit.debugDescription ?? "-"
update(
item: (
title: "Relays",
data: [
"Entry: \(entry)",
"Exit: \(exit)",
]
), in: &connection
)
}
private func setObfuscation() {
update(
item: (
title: "Obfuscation",
data: [
tunnelStatus.observedState.connectionState?.obfuscationMethod.description ?? "-"
]
), in: &connection
)
}
}
// MARK: Settings
extension DebugViewModelImpl {
func setRelaySettings() {
let entry = tunnelSettings.relayConstraints.entryLocations.value?.locations.first?.stringRepresentation ?? "-"
let exit = tunnelSettings.relayConstraints.exitLocations.value?.locations.first?.stringRepresentation ?? "-"
update(
item: (
title: "Relays",
data: [
"Entry: \(entry)",
"Exit: \(exit)",
]
), in: &settings
)
}
func setMullvadDnsBlockers() {
let blockingOptions = tunnelSettings.dnsSettings.blockingOptions
var dnsBlockers = [String]()
if blockingOptions.contains(.blockAdvertising) {
dnsBlockers.append("Advertising (\(DNSBlockingOptions.blockAdvertising.serverAddress!))")
}
if blockingOptions.contains(.blockTracking) {
dnsBlockers.append("Tracking (\(DNSBlockingOptions.blockTracking.serverAddress!))")
}
if blockingOptions.contains(.blockMalware) {
dnsBlockers.append("Malware (\(DNSBlockingOptions.blockMalware.serverAddress!))")
}
if blockingOptions.contains(.blockAdultContent) {
dnsBlockers.append("Adult content (\(DNSBlockingOptions.blockAdultContent.serverAddress!))")
}
if blockingOptions.contains(.blockGambling) {
dnsBlockers.append("Gambling (\(DNSBlockingOptions.blockGambling.serverAddress!))")
}
if blockingOptions.contains(.blockSocialMedia) {
dnsBlockers.append("Social media (\(DNSBlockingOptions.blockSocialMedia.serverAddress!))")
}
update(
item: (
title: "Mullvad DNS blockers",
data: dnsBlockers.isEmpty ? ["-"] : dnsBlockers
), in: &settings
)
}
func setCustomDnsBlockers() {
let customAddresses = tunnelSettings.dnsSettings.customDNSDomains.map { $0.debugDescription }
update(
item: (
title: "Custom DNS blockers",
data: customAddresses.isEmpty ? ["-"] : customAddresses
), in: &settings
)
}
func setObfuscationSettings() {
let method = tunnelSettings.wireGuardObfuscation.state.description
let udpTcpPort = tunnelSettings.wireGuardObfuscation.udpOverTcpPort.description
let shadowSocksPort = tunnelSettings.wireGuardObfuscation.shadowsocksPort.description
update(
item: (
title: "Obfuscation",
data: [
"Method: \(method)",
"UDP over TCP port: \(udpTcpPort)",
"Shadowsocks port: \(shadowSocksPort)",
]
), in: &settings
)
}
func setQuantumResistanceSettings() {
update(
item: (
title: "Quantum resistance",
data: [
tunnelSettings.tunnelQuantumResistance.isEnabled ? "Enabled" : "Disabled"
]
), in: &settings
)
}
func setMultihopSettings() {
update(
item: (
title: "Multihop",
data: [
tunnelSettings.tunnelMultihopState.isEnabled ? "Enabled" : "Disabled"
]
), in: &settings
)
}
func setDaitaSettings() {
let daitaIsEnabled = tunnelSettings.daita.daitaState.isEnabled ? "Enabled" : "Disabled"
let directOnlyIsEnabled = tunnelSettings.daita.directOnlyState.isEnabled ? "Enabled" : "Disabled"
update(
item: (
title: "DAITA",
data: [
"DAITA: \(daitaIsEnabled)",
"Direct only: \(directOnlyIsEnabled)",
]
), in: &settings
)
}
func setIncludeAllNetworksSettings() {
let includeAllNetworksIsEnabled =
tunnelSettings.includeAllNetworks.includeAllNetworksIsEnabled ? "Enabled" : "Disabled"
let localNetworkSharingIsEnabled =
tunnelSettings.includeAllNetworks.localNetworkSharingIsEnabled ? "Enabled" : "Disabled"
let consent = appPreferences.includeAllNetworksConsent ? "True" : "False"
update(
item: (
title: "Force all apps",
data: [
"Force all apps: \(includeAllNetworksIsEnabled)",
"Local network sharing: \(localNetworkSharingIsEnabled)",
"Consent: \(consent)",
]
), in: &settings
)
}
}
// MARK: Mock
class MockDebugViewModel: DebugViewModel {
var tunnelSettings: LatestTunnelSettings = LatestTunnelSettings()
var nwPathStatus: NWPath.Status = NWPath.Status.unsatisfied
// Connection
var connection = [
(
title: "Relays",
data: [
"Entry: Sweden, Gothenburg, se-got-001",
"Exit: Sweden, Gothenburg, se-got-002",
]
),
(title: "Obfuscation", data: [""]),
]
// Settings
var settings = [
(title: "Relays", data: ["Entry: se-se-got-se-got-001", "Exit: se-se-got-se-got-001"]),
(
title: "Mullvad DNS blockers",
data: [
"Advertising (100.64.0.1)",
"Tracking (100.64.0.2)",
"Social Media (100.64.0.3)",
]
),
(title: "Custom DNS blockers", data: ["192.168.1.1", "192.168.1.2"]),
(title: "Obfuscation", data: ["QUIC", "UDP over TCP port: 53", "Shadowsocks port: 53"]),
(title: "Quantum resistance", data: ["Enabled"]),
(title: "Multihop", data: ["Disabled"]),
(title: "DAITA", data: ["DAITA: Enabled", "Direct only: Disabled"]),
(
title: "Force all apps",
data: [
"Force all apps: Disabled",
"Local network sharing: Disabled",
"Consent: False",
]
),
]
}
|