diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2023-02-01 12:06:10 +0100 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2023-02-03 10:58:39 +0100 |
| commit | 1f255382d34bf68f88b0b9b6c33117522578e33a (patch) | |
| tree | 78a18aca17a7544fac07edd8ef2f8d65b3de033f | |
| parent | 8a65e796f38b9af7f1992e98969ac3e4dfc97ad1 (diff) | |
| download | mullvadvpn-1f255382d34bf68f88b0b9b6c33117522578e33a.tar.xz mullvadvpn-1f255382d34bf68f88b0b9b6c33117522578e33a.zip | |
Add configuration failure cause
| -rw-r--r-- | ios/MullvadTypes/PacketTunnelErrorWrapper.swift | 41 | ||||
| -rw-r--r-- | ios/PacketTunnel/PacketTunnelProvider.swift | 49 |
2 files changed, 59 insertions, 31 deletions
diff --git a/ios/MullvadTypes/PacketTunnelErrorWrapper.swift b/ios/MullvadTypes/PacketTunnelErrorWrapper.swift index e70bdb0c9f..46d796dcf6 100644 --- a/ios/MullvadTypes/PacketTunnelErrorWrapper.swift +++ b/ios/MullvadTypes/PacketTunnelErrorWrapper.swift @@ -9,31 +9,36 @@ import Foundation public enum PacketTunnelErrorWrapper: Codable, Equatable, LocalizedError { - /// Failure that indicates wire guard errors. - case wireguard(error: String) + public enum ConfigurationFailureCause: Codable, Equatable { + /// Settings schema is outdated. + case outdatedSchema + + /// No relay satisfying constraints. + case noRelaysSatisfyingConstraints + + /// Read error. + case readFailure + } + + /// Failure that indicates WireGuard errors. + case wireguard(String) /// Failure to read stored settings. - case readConfiguration + case configuration(ConfigurationFailureCause) public var errorDescription: String? { switch self { case let .wireguard(error): return error - case .readConfiguration: - return "Failure to read settings." - } - } - - public static func == (lhs: PacketTunnelErrorWrapper, rhs: PacketTunnelErrorWrapper) -> Bool { - switch (lhs, rhs) { - case (.readConfiguration, .readConfiguration): - return true - - case let (.wireguard(error: lhsError), .wireguard(error: rhsError)): - return lhsError == rhsError - - default: - return false + case let .configuration(cause): + switch cause { + case .outdatedSchema: + return "Settings schema is outdated." + case .readFailure: + return "Failure to read VPN configuration." + case .noRelaysSatisfyingConstraints: + return "No relays satisfying constraints." + } } } } diff --git a/ios/PacketTunnel/PacketTunnelProvider.swift b/ios/PacketTunnel/PacketTunnelProvider.swift index 13f7646b9f..6c28d845d9 100644 --- a/ios/PacketTunnel/PacketTunnelProvider.swift +++ b/ios/PacketTunnel/PacketTunnelProvider.swift @@ -45,10 +45,10 @@ class PacketTunnelProvider: NEPacketTunnelProvider, TunnelMonitorDelegate { private var numberOfFailedAttempts: UInt = 0 /// Last wireguard error. - private var wgError: PacketTunnelErrorWrapper? + private var wgError: WireGuardAdapterError? - /// Last tunnel provider error. - private var tunnelProviderError: PacketTunnelErrorWrapper? + /// Last configuration read error. + private var configurationError: Error? /// Relay cache. private let relayCache = RelayCache( @@ -89,8 +89,13 @@ class PacketTunnelProvider: NEPacketTunnelProvider, TunnelMonitorDelegate { /// Returns `PacketTunnelStatus` used for sharing with main bundle process. private var packetTunnelStatus: PacketTunnelStatus { + let errors: [PacketTunnelErrorWrapper?] = [ + wgError.flatMap { PacketTunnelErrorWrapper(error: $0) }, + configurationError.flatMap { PacketTunnelErrorWrapper(error: $0) }, + ] + return PacketTunnelStatus( - lastErrors: [wgError, tunnelProviderError].compactMap { $0 }, + lastErrors: errors.compactMap { $0 }, isNetworkReachable: isNetworkReachable, deviceCheck: deviceCheck, tunnelRelay: selectorResult?.packetTunnelRelay @@ -189,7 +194,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider, TunnelMonitorDelegate { message: "Failed to read tunnel configuration when starting the tunnel." ) - tunnelProviderError = .readConfiguration + configurationError = error startEmptyTunnel(completionHandler: completionHandler) return @@ -446,9 +451,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider, TunnelMonitorDelegate { { let tunnelSettings = try SettingsManager.readSettings() let deviceState = try SettingsManager.readDeviceState() - - tunnelProviderError = nil - let selectorResult: RelaySelectorResult switch nextRelay { @@ -474,11 +476,15 @@ class PacketTunnelProvider: NEPacketTunnelProvider, TunnelMonitorDelegate { let tunnelConfiguration: PacketTunnelConfiguration do { tunnelConfiguration = try makeConfiguration(nextRelay) + configurationError = nil } catch { providerLogger.error( error: error, message: "Failed produce new configuration." ) + + configurationError = error + completionHandler?() return } @@ -496,11 +502,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider, TunnelMonitorDelegate { adapter.update(tunnelConfiguration: tunnelConfiguration.wgTunnelConfig) { error in self.dispatchQueue.async { if let error = error { - let wrappedError: PacketTunnelErrorWrapper = .wireguard( - error: error.localizedDescription - ) - - self.wgError = wrappedError + self.wgError = error } if let error = error { @@ -712,3 +714,24 @@ extension DeviceCheck { } } } + +extension PacketTunnelErrorWrapper { + init?(error: Error) { + switch error { + case let error as WireGuardAdapterError: + self = .wireguard(error.localizedDescription) + + case is UnsupportedSettingsVersionError: + self = .configuration(.outdatedSchema) + + case is ReadSettingsVersionError: + self = .configuration(.readFailure) + + case is NoRelaysSatisfyingConstraintsError: + self = .configuration(.noRelaysSatisfyingConstraints) + + default: + return nil + } + } +} |
