aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuardNetworkExtension
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-11-09 16:53:52 +0530
committerRoopesh Chander <roop@roopc.net>2018-11-09 17:07:42 +0530
commit3136fe0e2c3a4c0c35b239853ebbeae5e2ff4ea3 (patch)
tree507416f6914114d162088ef9d1a1e04c2c12fae3 /WireGuard/WireGuardNetworkExtension
parentRemove unused file PacketTunnelOptionKey.swift (diff)
downloadwireguard-apple-3136fe0e2c3a4c0c35b239853ebbeae5e2ff4ea3.tar.xz
wireguard-apple-3136fe0e2c3a4c0c35b239853ebbeae5e2ff4ea3.zip
NE: When there's an error starting the tunnel, show it to the user using displayMessage()
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuardNetworkExtension')
-rw-r--r--WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift25
-rw-r--r--WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift6
2 files changed, 30 insertions, 1 deletions
diff --git a/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift b/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift
new file mode 100644
index 0000000..4087fe7
--- /dev/null
+++ b/WireGuard/WireGuardNetworkExtension/ErrorNotifier.swift
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+import NetworkExtension
+
+class ErrorNotifier {
+ static func errorMessage(for error: PacketTunnelProviderError) -> (String, String)? {
+ switch (error) {
+ case .savedProtocolConfigurationIsInvalid:
+ return ("Activation failure", "Could not retrieve tunnel information from the saved configuration")
+ case .dnsResolutionFailure(_):
+ return ("DNS resolution failure", "One or more endpoint domains could not be resolved")
+ case .couldNotStartWireGuard:
+ return ("Activation failure", "WireGuard backend could not be started")
+ case .coultNotSetNetworkSettings:
+ return ("Activation failure", "Error applying network settings on the tunnel")
+ }
+ }
+
+ 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)", completionHandler: { (_) in })
+ }
+}
diff --git a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
index 02c087d..e131e74 100644
--- a/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
+++ b/WireGuard/WireGuardNetworkExtension/PacketTunnelProvider.swift
@@ -5,7 +5,6 @@ import NetworkExtension
import os.log
enum PacketTunnelProviderError: Error {
- case invalidOptions
case savedProtocolConfigurationIsInvalid
case dnsResolutionFailure(hostnames: [String])
case couldNotStartWireGuard
@@ -28,6 +27,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
guard let tunnelProviderProtocol = self.protocolConfiguration as? NETunnelProviderProtocol,
let tunnelConfiguration = tunnelProviderProtocol.tunnelConfiguration() else {
+ ErrorNotifier.notify(PacketTunnelProviderError.savedProtocolConfigurationIsInvalid, from: self)
startTunnelCompletionHandler(PacketTunnelProviderError.savedProtocolConfigurationIsInvalid)
return
}
@@ -41,6 +41,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
} catch DNSResolverError.dnsResolutionFailed(let hostnames) {
os_log("Starting tunnel failed: DNS resolution failure for %{public}d hostnames (%{public}s)", log: OSLog.default,
type: .error, hostnames.count, hostnames.joined(separator: ", "))
+ ErrorNotifier.notify(PacketTunnelProviderError.dnsResolutionFailure(hostnames: hostnames), from: self)
startTunnelCompletionHandler(PacketTunnelProviderError.dnsResolutionFailure(hostnames: hostnames))
return
} catch {
@@ -61,6 +62,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
let fd = packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int32
if fd < 0 {
os_log("Starting tunnel failed: Could not determine file descriptor", log: OSLog.default, type: .error)
+ ErrorNotifier.notify(PacketTunnelProviderError.couldNotStartWireGuard, from: self)
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotStartWireGuard)
return
}
@@ -70,6 +72,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
if handle < 0 {
os_log("Starting tunnel failed: Could not start WireGuard", log: OSLog.default, type: .error)
+ ErrorNotifier.notify(PacketTunnelProviderError.couldNotStartWireGuard, from: self)
startTunnelCompletionHandler(PacketTunnelProviderError.couldNotStartWireGuard)
return
}
@@ -82,6 +85,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
setTunnelNetworkSettings(networkSettings) { (error) in
if let error = error {
os_log("Starting tunnel failed: Error setting network settings: %s", log: OSLog.default, type: .error, error.localizedDescription)
+ ErrorNotifier.notify(PacketTunnelProviderError.coultNotSetNetworkSettings, from: self)
startTunnelCompletionHandler(PacketTunnelProviderError.coultNotSetNetworkSettings)
} else {
startTunnelCompletionHandler(nil /* No errors */)