From ec031b1f19c7b85b52ef478b8dfe3f8e173cf046 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 21 Dec 2018 18:50:32 +0100 Subject: [PATCH] Get rid of superflous isActivateOnDemandEnabled key Signed-off-by: Jason A. Donenfeld --- .../NETunnelProviderProtocol+Extension.swift | 14 +++----------- .../WireGuard/Base.lproj/Localizable.strings | 1 + WireGuard/WireGuard/Tunnel/MockTunnels.swift | 2 +- WireGuard/WireGuard/Tunnel/TunnelErrors.swift | 12 ++++++------ .../WireGuard/Tunnel/TunnelsManager.swift | 8 ++++---- .../ErrorNotifier.swift | 19 +++---------------- .../PacketTunnelProvider.swift | 17 +---------------- 7 files changed, 19 insertions(+), 54 deletions(-) diff --git a/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift b/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift index e5cfac8..3a9bc38 100644 --- a/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift +++ b/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift @@ -10,7 +10,6 @@ extension NETunnelProviderProtocol { enum Keys: String { case tunnelConfiguration = "TunnelConfiguration" case tunnelConfigurationVersion = "TunnelConfigurationVersion" - case isActivateOnDemandEnabled = "IsActivateOnDemandEnabled" } var tunnelConfiguration: TunnelConfiguration? { @@ -27,11 +26,7 @@ extension NETunnelProviderProtocol { return try? JSONDecoder().decode(TunnelConfiguration.self, from: tunnelConfigurationData!) } - var isActivateOnDemandEnabled: Bool { - return providerConfiguration?[Keys.isActivateOnDemandEnabled.rawValue] as? Bool ?? false - } - - convenience init?(tunnelConfiguration: TunnelConfiguration, isActivateOnDemandEnabled: Bool) { + convenience init?(tunnelConfiguration: TunnelConfiguration) { assert(!tunnelConfiguration.interface.name.isEmpty) guard let tunnelConfigData = try? JSONEncoder().encode(tunnelConfiguration) else { return nil } @@ -43,8 +38,7 @@ extension NETunnelProviderProtocol { providerBundleIdentifier = "\(appId).network-extension" providerConfiguration = [ Keys.tunnelConfiguration.rawValue: tunnelConfigDictionary, - Keys.tunnelConfigurationVersion.rawValue: tunnelConfigurationVersion, - Keys.isActivateOnDemandEnabled.rawValue: isActivateOnDemandEnabled + Keys.tunnelConfigurationVersion.rawValue: tunnelConfigurationVersion ] let endpoints = tunnelConfiguration.peers.compactMap { $0.endpoint } @@ -85,14 +79,12 @@ extension NETunnelProviderProtocol { private func migrateFromConfigurationV1() { guard let serializedTunnelConfiguration = providerConfiguration?["tunnelConfiguration"] as? Data else { return } guard let configuration = try? JSONDecoder().decode(LegacyTunnelConfiguration.self, from: serializedTunnelConfiguration) else { return } - guard let isActivateOnDemandEnabled = providerConfiguration?["isActivateOnDemandEnabled"] as? Bool else { return } guard let tunnelConfigData = try? JSONEncoder().encode(configuration.migrated) else { return } guard let tunnelConfigDictionary = try? JSONSerialization.jsonObject(with: tunnelConfigData, options: .allowFragments) else { return } providerConfiguration = [ Keys.tunnelConfiguration.rawValue: tunnelConfigDictionary, - Keys.tunnelConfigurationVersion.rawValue: tunnelConfigurationVersion, - Keys.isActivateOnDemandEnabled.rawValue: isActivateOnDemandEnabled + Keys.tunnelConfigurationVersion.rawValue: tunnelConfigurationVersion ] } diff --git a/WireGuard/WireGuard/Base.lproj/Localizable.strings b/WireGuard/WireGuard/Base.lproj/Localizable.strings index 524cfdc..58eb9d4 100644 --- a/WireGuard/WireGuard/Base.lproj/Localizable.strings +++ b/WireGuard/WireGuard/Base.lproj/Localizable.strings @@ -177,6 +177,7 @@ "alertTunnelActivationFailureTitle" = "Activation failure"; "alertTunnelActivationFailureMessage" = "The tunnel could not be activated. Please ensure that you are connected to the Internet."; +"alertTunnelActivationFailureOnDemandAddendum" = " This tunnel has Activate On Demand enabled, so this tunnel might be re-activated automatically by the OS. You may turn off Activate On Demand in this app by editing the tunnel configuration."; "alertTunnelNameEmptyTitle" = "No name provided"; "alertTunnelNameEmptyMessage" = "Cannot create tunnel with an empty name"; diff --git a/WireGuard/WireGuard/Tunnel/MockTunnels.swift b/WireGuard/WireGuard/Tunnel/MockTunnels.swift index 7be6f5c..764d860 100644 --- a/WireGuard/WireGuard/Tunnel/MockTunnels.swift +++ b/WireGuard/WireGuard/Tunnel/MockTunnels.swift @@ -37,7 +37,7 @@ class MockTunnels { let tunnelConfiguration = TunnelConfiguration(interface: interface, peers: [peer]) let tunnelProviderManager = NETunnelProviderManager() - tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration, isActivateOnDemandEnabled: false) + tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration) tunnelProviderManager.localizedDescription = tunnelName tunnelProviderManager.isEnabled = true diff --git a/WireGuard/WireGuard/Tunnel/TunnelErrors.swift b/WireGuard/WireGuard/Tunnel/TunnelErrors.swift index c3d15b2..9b2b574 100644 --- a/WireGuard/WireGuard/Tunnel/TunnelErrors.swift +++ b/WireGuard/WireGuard/Tunnel/TunnelErrors.swift @@ -51,15 +51,15 @@ enum TunnelsManagerActivationAttemptError: WireGuardAppError { } enum TunnelsManagerActivationError: WireGuardAppError { - case activationFailed - case activationFailedWithExtensionError(title: String, message: String) + case activationFailed(wasOnDemandEnabled: Bool) + case activationFailedWithExtensionError(title: String, message: String, wasOnDemandEnabled: Bool) var alertText: AlertText { switch self { - case .activationFailed: - return (tr("alertTunnelActivationFailureTitle"), tr("alertTunnelActivationFailureMessage")) - case .activationFailedWithExtensionError(let title, let message): - return (title, message) + case .activationFailed(let wasOnDemandEnabled): + return (tr("alertTunnelActivationFailureTitle"), tr("alertTunnelActivationFailureMessage") + (wasOnDemandEnabled ? tr("alertTunnelActivationFailureOnDemandAddendum") : "")) + case .activationFailedWithExtensionError(let title, let message, let wasOnDemandEnabled): + return (title, message + (wasOnDemandEnabled ? tr("alertTunnelActivationFailureOnDemandAddendum") : "")) } } } diff --git a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift index b56cb37..51f0d37 100644 --- a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift +++ b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift @@ -66,7 +66,7 @@ class TunnelsManager { } let tunnelProviderManager = NETunnelProviderManager() - tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration, isActivateOnDemandEnabled: activateOnDemandSetting.isActivateOnDemandEnabled) + tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration) tunnelProviderManager.localizedDescription = tunnelName tunnelProviderManager.isEnabled = true @@ -125,7 +125,7 @@ class TunnelsManager { let shouldRestartIfActive = !((tunnelProviderManager.protocolConfiguration as? NETunnelProviderProtocol)?.hasTunnelConfiguration(tunnelConfiguration: tunnelConfiguration) ?? false) - tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration, isActivateOnDemandEnabled: activateOnDemandSetting.isActivateOnDemandEnabled) + tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration) tunnelProviderManager.localizedDescription = tunnelName tunnelProviderManager.isEnabled = true @@ -277,9 +277,9 @@ class TunnelsManager { } else if session.status == .disconnected { tunnel.isAttemptingActivation = false if let (title, message) = self.lastErrorTextFromNetworkExtension(for: tunnel) { - self.activationDelegate?.tunnelActivationFailed(tunnel: tunnel, error: .activationFailedWithExtensionError(title: title, message: message)) + self.activationDelegate?.tunnelActivationFailed(tunnel: tunnel, error: .activationFailedWithExtensionError(title: title, message: message, wasOnDemandEnabled: tunnelProvider.isOnDemandEnabled)) } else { - self.activationDelegate?.tunnelActivationFailed(tunnel: tunnel, error: .activationFailed) + self.activationDelegate?.tunnelActivationFailed(tunnel: tunnel, error: .activationFailed(wasOnDemandEnabled: tunnelProvider.isOnDemandEnabled)) } } } diff --git a/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift b/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift index 1b74d5d..a9bcc14 100644 --- a/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift +++ b/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift @@ -9,7 +9,6 @@ class ErrorNotifier { weak var tunnelProvider: NEPacketTunnelProvider? var tunnelName: String? - var isActivateOnDemandEnabled = false init(activationAttemptId: String?, tunnelProvider: NEPacketTunnelProvider) { self.activationAttemptId = activationAttemptId @@ -31,21 +30,9 @@ class ErrorNotifier { } 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 onDemandMessage = isActivateOnDemandEnabled ? " This tunnel has Activate On Demand enabled, so this tunnel might be activated automatically. You may turn off Activate On Demand in the WireGuard app by navigating to: '\(tunnelName ?? "tunnel")' > Edit." : "" - let errorMessageData = "\(activationAttemptId)\n\(title)\n\(message)\(onDemandMessage)".data(using: .utf8) - FileManager.default.createFile(atPath: lastErrorFilePath, contents: errorMessageData, attributes: nil) - } else { - // The tunnel was probably started from iOS Settings app or activated on-demand - if let tunnelProvider = self.tunnelProvider { - // displayMessage() is deprecated, but there's no better alternative if invoked from iOS Settings - if !isActivateOnDemandEnabled { // If using activate-on-demand, don't use displayMessage - tunnelProvider.displayMessage("\(title): \(message)") { _ in } - } - } - } + guard let (title, message) = errorMessage(for: error), let activationAttemptId = activationAttemptId, let lastErrorFilePath = FileManager.networkExtensionLastErrorFileURL?.path else { return } + let errorMessageData = "\(activationAttemptId)\n\(title)\n\(message)".data(using: .utf8) + FileManager.default.createFile(atPath: lastErrorFilePath, contents: errorMessageData, attributes: nil) } static func removeLastErrorFile() { diff --git a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift index f32a004..27a42c5 100644 --- a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift +++ b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift @@ -24,7 +24,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider { networkMonitor?.cancel() } - //swiftlint:disable:next function_body_length override func startTunnel(options: [String: NSObject]?, completionHandler startTunnelCompletionHandler: @escaping (Error?) -> Void) { let activationAttemptId = options?["activationAttemptId"] as? String let errorNotifier = ErrorNotifier(activationAttemptId: activationAttemptId, tunnelProvider: self) @@ -39,22 +38,8 @@ class PacketTunnelProvider: NEPacketTunnelProvider { configureLogger() let tunnelName = tunnelConfiguration.interface.name - wg_log(.info, message: "Starting tunnel '\(tunnelName)'") + wg_log(.info, message: "Starting tunnel '\(tunnelName)' from the " + (activationAttemptId == nil ? "OS directly, rather than the app" : "app")) - if activationAttemptId != nil { - wg_log(.info, staticMessage: "Tunnel activated from the app") - } else { - wg_log(.info, staticMessage: "Tunnel not activated from the app") - } - - let isActivateOnDemandEnabled = tunnelProviderProtocol.isActivateOnDemandEnabled - if isActivateOnDemandEnabled { - wg_log(.info, staticMessage: "Tunnel has Activate On Demand enabled") - } else { - wg_log(.info, staticMessage: "Tunnel has Activate On Demand disabled") - } - - errorNotifier.isActivateOnDemandEnabled = isActivateOnDemandEnabled errorNotifier.tunnelName = tunnelName let endpoints = tunnelConfiguration.peers.map { $0.endpoint }