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:
parent
86646448ac
commit
33cdc14070
|
@ -7,22 +7,28 @@ import PromiseKit
|
|||
import NetworkExtension
|
||||
|
||||
enum GoVersionCoordinatorError: Error {
|
||||
case noSession
|
||||
case noEnabledSession
|
||||
case noResponse
|
||||
}
|
||||
|
||||
extension AppCoordinator: SettingsTableViewControllerDelegate {
|
||||
func goVersionInformation() -> Promise<String> {
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue