mirror of
https://github.com/passepartoutvpn/wireguard-apple.git
synced 2025-01-31 13:02:08 +00:00
Get rid of superflous isActivateOnDemandEnabled key
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
8553723e04
commit
ec031b1f19
@ -10,7 +10,6 @@ extension NETunnelProviderProtocol {
|
|||||||
enum Keys: String {
|
enum Keys: String {
|
||||||
case tunnelConfiguration = "TunnelConfiguration"
|
case tunnelConfiguration = "TunnelConfiguration"
|
||||||
case tunnelConfigurationVersion = "TunnelConfigurationVersion"
|
case tunnelConfigurationVersion = "TunnelConfigurationVersion"
|
||||||
case isActivateOnDemandEnabled = "IsActivateOnDemandEnabled"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var tunnelConfiguration: TunnelConfiguration? {
|
var tunnelConfiguration: TunnelConfiguration? {
|
||||||
@ -27,11 +26,7 @@ extension NETunnelProviderProtocol {
|
|||||||
return try? JSONDecoder().decode(TunnelConfiguration.self, from: tunnelConfigurationData!)
|
return try? JSONDecoder().decode(TunnelConfiguration.self, from: tunnelConfigurationData!)
|
||||||
}
|
}
|
||||||
|
|
||||||
var isActivateOnDemandEnabled: Bool {
|
convenience init?(tunnelConfiguration: TunnelConfiguration) {
|
||||||
return providerConfiguration?[Keys.isActivateOnDemandEnabled.rawValue] as? Bool ?? false
|
|
||||||
}
|
|
||||||
|
|
||||||
convenience init?(tunnelConfiguration: TunnelConfiguration, isActivateOnDemandEnabled: Bool) {
|
|
||||||
assert(!tunnelConfiguration.interface.name.isEmpty)
|
assert(!tunnelConfiguration.interface.name.isEmpty)
|
||||||
|
|
||||||
guard let tunnelConfigData = try? JSONEncoder().encode(tunnelConfiguration) else { return nil }
|
guard let tunnelConfigData = try? JSONEncoder().encode(tunnelConfiguration) else { return nil }
|
||||||
@ -43,8 +38,7 @@ extension NETunnelProviderProtocol {
|
|||||||
providerBundleIdentifier = "\(appId).network-extension"
|
providerBundleIdentifier = "\(appId).network-extension"
|
||||||
providerConfiguration = [
|
providerConfiguration = [
|
||||||
Keys.tunnelConfiguration.rawValue: tunnelConfigDictionary,
|
Keys.tunnelConfiguration.rawValue: tunnelConfigDictionary,
|
||||||
Keys.tunnelConfigurationVersion.rawValue: tunnelConfigurationVersion,
|
Keys.tunnelConfigurationVersion.rawValue: tunnelConfigurationVersion
|
||||||
Keys.isActivateOnDemandEnabled.rawValue: isActivateOnDemandEnabled
|
|
||||||
]
|
]
|
||||||
|
|
||||||
let endpoints = tunnelConfiguration.peers.compactMap { $0.endpoint }
|
let endpoints = tunnelConfiguration.peers.compactMap { $0.endpoint }
|
||||||
@ -85,14 +79,12 @@ extension NETunnelProviderProtocol {
|
|||||||
private func migrateFromConfigurationV1() {
|
private func migrateFromConfigurationV1() {
|
||||||
guard let serializedTunnelConfiguration = providerConfiguration?["tunnelConfiguration"] as? Data else { return }
|
guard let serializedTunnelConfiguration = providerConfiguration?["tunnelConfiguration"] as? Data else { return }
|
||||||
guard let configuration = try? JSONDecoder().decode(LegacyTunnelConfiguration.self, from: serializedTunnelConfiguration) 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 tunnelConfigData = try? JSONEncoder().encode(configuration.migrated) else { return }
|
||||||
guard let tunnelConfigDictionary = try? JSONSerialization.jsonObject(with: tunnelConfigData, options: .allowFragments) else { return }
|
guard let tunnelConfigDictionary = try? JSONSerialization.jsonObject(with: tunnelConfigData, options: .allowFragments) else { return }
|
||||||
|
|
||||||
providerConfiguration = [
|
providerConfiguration = [
|
||||||
Keys.tunnelConfiguration.rawValue: tunnelConfigDictionary,
|
Keys.tunnelConfiguration.rawValue: tunnelConfigDictionary,
|
||||||
Keys.tunnelConfigurationVersion.rawValue: tunnelConfigurationVersion,
|
Keys.tunnelConfigurationVersion.rawValue: tunnelConfigurationVersion
|
||||||
Keys.isActivateOnDemandEnabled.rawValue: isActivateOnDemandEnabled
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,6 +177,7 @@
|
|||||||
|
|
||||||
"alertTunnelActivationFailureTitle" = "Activation failure";
|
"alertTunnelActivationFailureTitle" = "Activation failure";
|
||||||
"alertTunnelActivationFailureMessage" = "The tunnel could not be activated. Please ensure that you are connected to the Internet.";
|
"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";
|
"alertTunnelNameEmptyTitle" = "No name provided";
|
||||||
"alertTunnelNameEmptyMessage" = "Cannot create tunnel with an empty name";
|
"alertTunnelNameEmptyMessage" = "Cannot create tunnel with an empty name";
|
||||||
|
@ -37,7 +37,7 @@ class MockTunnels {
|
|||||||
let tunnelConfiguration = TunnelConfiguration(interface: interface, peers: [peer])
|
let tunnelConfiguration = TunnelConfiguration(interface: interface, peers: [peer])
|
||||||
|
|
||||||
let tunnelProviderManager = NETunnelProviderManager()
|
let tunnelProviderManager = NETunnelProviderManager()
|
||||||
tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration, isActivateOnDemandEnabled: false)
|
tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration)
|
||||||
tunnelProviderManager.localizedDescription = tunnelName
|
tunnelProviderManager.localizedDescription = tunnelName
|
||||||
tunnelProviderManager.isEnabled = true
|
tunnelProviderManager.isEnabled = true
|
||||||
|
|
||||||
|
@ -51,15 +51,15 @@ enum TunnelsManagerActivationAttemptError: WireGuardAppError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum TunnelsManagerActivationError: WireGuardAppError {
|
enum TunnelsManagerActivationError: WireGuardAppError {
|
||||||
case activationFailed
|
case activationFailed(wasOnDemandEnabled: Bool)
|
||||||
case activationFailedWithExtensionError(title: String, message: String)
|
case activationFailedWithExtensionError(title: String, message: String, wasOnDemandEnabled: Bool)
|
||||||
|
|
||||||
var alertText: AlertText {
|
var alertText: AlertText {
|
||||||
switch self {
|
switch self {
|
||||||
case .activationFailed:
|
case .activationFailed(let wasOnDemandEnabled):
|
||||||
return (tr("alertTunnelActivationFailureTitle"), tr("alertTunnelActivationFailureMessage"))
|
return (tr("alertTunnelActivationFailureTitle"), tr("alertTunnelActivationFailureMessage") + (wasOnDemandEnabled ? tr("alertTunnelActivationFailureOnDemandAddendum") : ""))
|
||||||
case .activationFailedWithExtensionError(let title, let message):
|
case .activationFailedWithExtensionError(let title, let message, let wasOnDemandEnabled):
|
||||||
return (title, message)
|
return (title, message + (wasOnDemandEnabled ? tr("alertTunnelActivationFailureOnDemandAddendum") : ""))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ class TunnelsManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let tunnelProviderManager = NETunnelProviderManager()
|
let tunnelProviderManager = NETunnelProviderManager()
|
||||||
tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration, isActivateOnDemandEnabled: activateOnDemandSetting.isActivateOnDemandEnabled)
|
tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration)
|
||||||
tunnelProviderManager.localizedDescription = tunnelName
|
tunnelProviderManager.localizedDescription = tunnelName
|
||||||
tunnelProviderManager.isEnabled = true
|
tunnelProviderManager.isEnabled = true
|
||||||
|
|
||||||
@ -125,7 +125,7 @@ class TunnelsManager {
|
|||||||
|
|
||||||
let shouldRestartIfActive = !((tunnelProviderManager.protocolConfiguration as? NETunnelProviderProtocol)?.hasTunnelConfiguration(tunnelConfiguration: tunnelConfiguration) ?? false)
|
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.localizedDescription = tunnelName
|
||||||
tunnelProviderManager.isEnabled = true
|
tunnelProviderManager.isEnabled = true
|
||||||
|
|
||||||
@ -277,9 +277,9 @@ class TunnelsManager {
|
|||||||
} else if session.status == .disconnected {
|
} else if session.status == .disconnected {
|
||||||
tunnel.isAttemptingActivation = false
|
tunnel.isAttemptingActivation = false
|
||||||
if let (title, message) = self.lastErrorTextFromNetworkExtension(for: tunnel) {
|
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 {
|
} else {
|
||||||
self.activationDelegate?.tunnelActivationFailed(tunnel: tunnel, error: .activationFailed)
|
self.activationDelegate?.tunnelActivationFailed(tunnel: tunnel, error: .activationFailed(wasOnDemandEnabled: tunnelProvider.isOnDemandEnabled))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ class ErrorNotifier {
|
|||||||
weak var tunnelProvider: NEPacketTunnelProvider?
|
weak var tunnelProvider: NEPacketTunnelProvider?
|
||||||
|
|
||||||
var tunnelName: String?
|
var tunnelName: String?
|
||||||
var isActivateOnDemandEnabled = false
|
|
||||||
|
|
||||||
init(activationAttemptId: String?, tunnelProvider: NEPacketTunnelProvider) {
|
init(activationAttemptId: String?, tunnelProvider: NEPacketTunnelProvider) {
|
||||||
self.activationAttemptId = activationAttemptId
|
self.activationAttemptId = activationAttemptId
|
||||||
@ -31,21 +30,9 @@ class ErrorNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func notify(_ error: PacketTunnelProviderError) {
|
func notify(_ error: PacketTunnelProviderError) {
|
||||||
guard let (title, message) = errorMessage(for: error) else { return }
|
guard let (title, message) = errorMessage(for: error), let activationAttemptId = activationAttemptId, let lastErrorFilePath = FileManager.networkExtensionLastErrorFileURL?.path else { return }
|
||||||
if let activationAttemptId = activationAttemptId, let lastErrorFilePath = FileManager.networkExtensionLastErrorFileURL?.path {
|
let errorMessageData = "\(activationAttemptId)\n\(title)\n\(message)".data(using: .utf8)
|
||||||
// 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)
|
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 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static func removeLastErrorFile() {
|
static func removeLastErrorFile() {
|
||||||
|
@ -24,7 +24,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
|||||||
networkMonitor?.cancel()
|
networkMonitor?.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
//swiftlint:disable:next function_body_length
|
|
||||||
override func startTunnel(options: [String: NSObject]?, completionHandler startTunnelCompletionHandler: @escaping (Error?) -> Void) {
|
override func startTunnel(options: [String: NSObject]?, completionHandler startTunnelCompletionHandler: @escaping (Error?) -> Void) {
|
||||||
let activationAttemptId = options?["activationAttemptId"] as? String
|
let activationAttemptId = options?["activationAttemptId"] as? String
|
||||||
let errorNotifier = ErrorNotifier(activationAttemptId: activationAttemptId, tunnelProvider: self)
|
let errorNotifier = ErrorNotifier(activationAttemptId: activationAttemptId, tunnelProvider: self)
|
||||||
@ -39,22 +38,8 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
|||||||
configureLogger()
|
configureLogger()
|
||||||
|
|
||||||
let tunnelName = tunnelConfiguration.interface.name
|
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
|
errorNotifier.tunnelName = tunnelName
|
||||||
|
|
||||||
let endpoints = tunnelConfiguration.peers.map { $0.endpoint }
|
let endpoints = tunnelConfiguration.peers.map { $0.endpoint }
|
||||||
|
Loading…
Reference in New Issue
Block a user