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 <Jason@zx2c4.com>
This commit is contained in:
Jeroen Leenarts 2018-10-01 15:37:15 +02:00
parent 86646448ac
commit 33cdc14070
3 changed files with 34 additions and 12 deletions

View File

@ -7,22 +7,28 @@ import PromiseKit
import NetworkExtension import NetworkExtension
enum GoVersionCoordinatorError: Error { enum GoVersionCoordinatorError: Error {
case noSession case noEnabledSession
case noResponse
} }
extension AppCoordinator: SettingsTableViewControllerDelegate { extension AppCoordinator: SettingsTableViewControllerDelegate {
func goVersionInformation() -> Promise<String> { func goVersionInformation() -> Promise<String> {
return Promise(resolver: { (resolver) in return Promise(resolver: { (resolver) in
guard let session = self.providerManagers?.first?.connection as? NETunnelProviderSession else { guard let session = self.providerManagers?.first(where: { $0.isEnabled })?.connection as? NETunnelProviderSession else {
resolver.reject(GoVersionCoordinatorError.noSession) resolver.reject(GoVersionCoordinatorError.noEnabledSession)
return return
} }
try session.sendProviderMessage(ExtensionMessage.requestVersion.data, responseHandler: { (data) in do {
guard let responseString = String(data: data!, encoding: .utf8) else { try session.sendProviderMessage(ExtensionMessage.requestVersion.data, responseHandler: { (data) in
return guard let data = data, let responseString = String(data: data, encoding: .utf8) else {
} resolver.reject(GoVersionCoordinatorError.noResponse)
resolver.fulfill(responseString) return
}) }
resolver.fulfill(responseString)
})
} catch {
resolver.reject(error)
}
}) })
} }

View File

@ -35,8 +35,15 @@ class SettingsTableViewController: UITableViewController {
label.text = goVersion label.text = goVersion
} }
return Guarantee.value(()) return Guarantee.value(())
}.recover({ (_) in }.recover({ (error) in
self.goVersionInfoLabel.text = NSLocalizedString("Unknown", comment: "") 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) 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() { private func showCopyConfirmation() {
let confirmationAlertController = UIAlertController(title: NSLocalizedString("Copied version information", comment: ""), message: UIPasteboard.general.string, preferredStyle: .alert) 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)) confirmationAlertController.addAction(UIAlertAction(title: NSLocalizedString("Ok", comment: "Generic OK button"), style: .default, handler: nil))

View File

@ -112,7 +112,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) { override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) {
let responseData: Data? let responseData: Data?
switch ExtensionMessage(messageData) { let message = ExtensionMessage(messageData)
switch message {
case ExtensionMessage.requestVersion: case ExtensionMessage.requestVersion:
responseData = (wgVersion().flatMap { String(cString: $0) } ?? "").data(using: .utf8) responseData = (wgVersion().flatMap { String(cString: $0) } ?? "").data(using: .utf8)
default: default: