diff options
author | Roopesh Chander <roop@roopc.net> | 2018-12-14 02:24:53 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2018-12-14 02:24:53 +0530 |
commit | 48552d2663128b39a394127c0f564acd380179ea (patch) | |
tree | 8ec83ff378edcaa12a1d1c0147a1abee59b5e1db /WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift | |
parent | TunnelsManager: startActivation() need not take a tunnelConfiguration (diff) | |
download | wireguard-apple-48552d2663128b39a394127c0f564acd380179ea.tar.xz wireguard-apple-48552d2663128b39a394127c0f564acd380179ea.zip |
NE: Communicate last error to app through a shared file
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift')
-rw-r--r-- | WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift b/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift index 163535a..c392233 100644 --- a/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift +++ b/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift @@ -4,7 +4,17 @@ import NetworkExtension class ErrorNotifier { - static func errorMessage(for error: PacketTunnelProviderError) -> (String, String)? { + + let activationAttemptId: String? + weak var tunnelProvider: NEPacketTunnelProvider? + + init(activationAttemptId: String?, tunnelProvider: NEPacketTunnelProvider) { + self.activationAttemptId = activationAttemptId + self.tunnelProvider = tunnelProvider + ErrorNotifier.removeLastErrorFile() + } + + func errorMessage(for error: PacketTunnelProviderError) -> (String, String)? { switch error { case .savedProtocolConfigurationIsInvalid: return ("Activation failure", "Could not retrieve tunnel information from the saved configuration") @@ -17,9 +27,24 @@ class ErrorNotifier { } } - static func notify(_ error: PacketTunnelProviderError, from tunnelProvider: NEPacketTunnelProvider) { - guard let (title, message) = ErrorNotifier.errorMessage(for: error) else { return } - // displayMessage() is deprecated, but there's no better alternative to show the error to the user - tunnelProvider.displayMessage("\(title): \(message)") { _ in } + func notify(_ error: PacketTunnelProviderError) { + guard let (title, message) = errorMessage(for: error) else { return } + if let activationAttemptId = activationAttemptId, let lastErrorFilePath = FileManager.networkExtensionLastErrorFileURL?.path { + // The tunnel was started from the app + let errorMessageData = "\(activationAttemptId)\n\(title)\n\(message)".data(using: .utf8) + FileManager.default.createFile(atPath: lastErrorFilePath, contents: errorMessageData, attributes: nil) + } else { + // The tunnel was probably started from iOS Settings app + if let tunnelProvider = self.tunnelProvider { + // displayMessage() is deprecated, but there's no better alternative if invoked from iOS Settings + tunnelProvider.displayMessage("\(title): \(message)") { _ in } + } + } + } + + static func removeLastErrorFile() { + if let lastErrorFileURL = FileManager.networkExtensionLastErrorFileURL { + _ = FileManager.deleteFile(at: lastErrorFileURL) + } } } |