From 33cdc14070b4c3217b816f22cb74cc0f88168ea0 Mon Sep 17 00:00:00 2001 From: Jeroen Leenarts Date: Mon, 1 Oct 2018 15:37:15 +0200 Subject: [PATCH] Don't crash on failure to obtain version info and inform user on what is needed to be able to retrieve version info from the Go client. Signed-off-by: Jason A. Donenfeld --- ...+SettingsTableViewControllerDelegate.swift | 24 ++++++++++++------- .../SetttingsTableViewController.swift | 18 ++++++++++++-- .../PacketTunnelProvider.swift | 4 +++- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/WireGuard/Coordinators/AppCoordinator+SettingsTableViewControllerDelegate.swift b/WireGuard/Coordinators/AppCoordinator+SettingsTableViewControllerDelegate.swift index 5784f5d..d19422c 100644 --- a/WireGuard/Coordinators/AppCoordinator+SettingsTableViewControllerDelegate.swift +++ b/WireGuard/Coordinators/AppCoordinator+SettingsTableViewControllerDelegate.swift @@ -7,22 +7,28 @@ import PromiseKit import NetworkExtension enum GoVersionCoordinatorError: Error { - case noSession + case noEnabledSession + case noResponse } extension AppCoordinator: SettingsTableViewControllerDelegate { func goVersionInformation() -> Promise { return Promise(resolver: { (resolver) in - guard let session = self.providerManagers?.first?.connection as? NETunnelProviderSession else { - resolver.reject(GoVersionCoordinatorError.noSession) + guard let session = self.providerManagers?.first(where: { $0.isEnabled })?.connection as? NETunnelProviderSession else { + resolver.reject(GoVersionCoordinatorError.noEnabledSession) return } - try session.sendProviderMessage(ExtensionMessage.requestVersion.data, responseHandler: { (data) in - guard let responseString = String(data: data!, encoding: .utf8) else { - return - } - resolver.fulfill(responseString) - }) + do { + try session.sendProviderMessage(ExtensionMessage.requestVersion.data, responseHandler: { (data) in + guard let data = data, let responseString = String(data: data, encoding: .utf8) else { + resolver.reject(GoVersionCoordinatorError.noResponse) + return + } + resolver.fulfill(responseString) + }) + } catch { + resolver.reject(error) + } }) } diff --git a/WireGuard/ViewControllers/SetttingsTableViewController.swift b/WireGuard/ViewControllers/SetttingsTableViewController.swift index 0421815..8f87bb7 100644 --- a/WireGuard/ViewControllers/SetttingsTableViewController.swift +++ b/WireGuard/ViewControllers/SetttingsTableViewController.swift @@ -35,8 +35,15 @@ class SettingsTableViewController: UITableViewController { label.text = goVersion } return Guarantee.value(()) - }.recover({ (_) in - self.goVersionInfoLabel.text = NSLocalizedString("Unknown", comment: "") + }.recover({ (error) in + switch error { + case GoVersionCoordinatorError.noEnabledSession: + self.goVersionInfoLabel.text = NSLocalizedString("Unavailable", comment: "") + self.showNotEnabledAlert() + default: + self.goVersionInfoLabel.text = NSLocalizedString("Unknown", comment: "") + } + }) } @@ -69,6 +76,13 @@ class SettingsTableViewController: UITableViewController { return self.delegate?.goVersionInformation() ?? Promise(error: GoVersionError.noDelegate) } + private func showNotEnabledAlert() { + let notEnabledAlertController = UIAlertController(title: NSLocalizedString("Go version", comment: ""), message: NSLocalizedString("Enable a WireGuard config by connecting or by selecting one in the VPN section of the device Settings app.", comment: ""), preferredStyle: .alert) + notEnabledAlertController.addAction(UIAlertAction(title: NSLocalizedString("Ok", comment: "Generic OK button"), style: .default, handler: nil)) + + present(notEnabledAlertController, animated: true, completion: nil) + } + private func showCopyConfirmation() { let confirmationAlertController = UIAlertController(title: NSLocalizedString("Copied version information", comment: ""), message: UIPasteboard.general.string, preferredStyle: .alert) confirmationAlertController.addAction(UIAlertAction(title: NSLocalizedString("Ok", comment: "Generic OK button"), style: .default, handler: nil)) diff --git a/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuardNetworkExtension/PacketTunnelProvider.swift index e0536b2..103dc90 100644 --- a/WireGuardNetworkExtension/PacketTunnelProvider.swift +++ b/WireGuardNetworkExtension/PacketTunnelProvider.swift @@ -112,7 +112,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider { override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) { let responseData: Data? - switch ExtensionMessage(messageData) { + let message = ExtensionMessage(messageData) + + switch message { case ExtensionMessage.requestVersion: responseData = (wgVersion().flatMap { String(cString: $0) } ?? "").data(using: .utf8) default: