From: Alessio Nossa Date: Tue, 1 Feb 2022 09:16:38 +0000 (+0100) Subject: Implemented UpdateConfiguration intent X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fe3f2d089bec0346c5ab338905081de59e116697;p=thirdparty%2Fwireguard-apple.git Implemented UpdateConfiguration intent Signed-off-by: Alessio Nossa --- diff --git a/Sources/Shared/Intents.intentdefinition b/Sources/Shared/Intents.intentdefinition index 38f8e54..590fa39 100644 --- a/Sources/Shared/Intents.intentdefinition +++ b/Sources/Shared/Intents.intentdefinition @@ -150,6 +150,216 @@ INIntentVerb Do + + INIntentCategory + generic + INIntentConfigurable + + INIntentDescription + Update peers configuration. Configuration must be provided with the same format as the following example. The fields you can update are: "Endpoint". +The fields and the peers you omit will not be modified. + +Example +{ "Peer1 Public Key (Base64)": { "Endpoint": "1.2.3.4:4321" }, + "Peer2 Public Key (Base64)": {"Endpoint": "10.11.12.13:6789"} } + INIntentDescriptionID + uTimVO + INIntentIneligibleForSuggestions + + INIntentInput + configuration + INIntentLastParameterTag + 5 + INIntentManagedParameterCombinations + + tunnel,configuration,completionUrl + + INIntentParameterCombinationSupportsBackgroundExecution + + INIntentParameterCombinationTitle + Update ${tunnel} configuration + INIntentParameterCombinationTitleID + 2ASDIM + INIntentParameterCombinationUpdatesLinked + + + + INIntentName + UpdateConfiguration + INIntentParameters + + + INIntentParameterConfigurable + + INIntentParameterDisplayName + Tunnel + INIntentParameterDisplayNameID + TjOtzk + INIntentParameterDisplayPriority + 1 + INIntentParameterMetadata + + INIntentParameterMetadataCapitalization + Sentences + INIntentParameterMetadataDefaultValueID + h56bAD + + INIntentParameterName + tunnel + INIntentParameterPromptDialogs + + + INIntentParameterPromptDialogCustom + + INIntentParameterPromptDialogType + Configuration + + + INIntentParameterPromptDialogCustom + + INIntentParameterPromptDialogType + Primary + + + INIntentParameterSupportsDynamicEnumeration + + INIntentParameterTag + 1 + INIntentParameterType + String + + + INIntentParameterConfigurable + + INIntentParameterDisplayName + Configuration + INIntentParameterDisplayNameID + 3SLMhb + INIntentParameterDisplayPriority + 2 + INIntentParameterMetadata + + INIntentParameterMetadataCapitalization + None + INIntentParameterMetadataDefaultValue + {"Peer Public Key": {"Endpoint":"1.2.3.4:5678"} } + INIntentParameterMetadataDefaultValueID + 1J2FBa + INIntentParameterMetadataDisableAutocorrect + + INIntentParameterMetadataDisableSmartDashes + + INIntentParameterMetadataDisableSmartQuotes + + INIntentParameterMetadataMultiline + + + INIntentParameterName + configuration + INIntentParameterPromptDialogs + + + INIntentParameterPromptDialogCustom + + INIntentParameterPromptDialogType + Configuration + + + INIntentParameterPromptDialogCustom + + INIntentParameterPromptDialogType + Primary + + + INIntentParameterTag + 2 + INIntentParameterType + String + + + INIntentParameterConfigurable + + INIntentParameterDisplayName + Open URL when done + INIntentParameterDisplayNameID + dwpgmC + INIntentParameterDisplayPriority + 3 + INIntentParameterMetadata + + INIntentParameterMetadataCapitalization + None + INIntentParameterMetadataDefaultValue + shortcuts:// + INIntentParameterMetadataDefaultValueID + cyr6LU + INIntentParameterMetadataDisableAutocorrect + + INIntentParameterMetadataDisableSmartDashes + + INIntentParameterMetadataDisableSmartQuotes + + + INIntentParameterName + completionUrl + INIntentParameterPromptDialogs + + + INIntentParameterPromptDialogCustom + + INIntentParameterPromptDialogType + Configuration + + + INIntentParameterPromptDialogCustom + + INIntentParameterPromptDialogType + Primary + + + INIntentParameterTag + 5 + INIntentParameterType + String + + + INIntentResponse + + INIntentResponseCodes + + + INIntentResponseCodeName + success + INIntentResponseCodeSuccess + + + + INIntentResponseCodeName + failure + + + INIntentResponseCodeConciseFormatString + The configuration update provided is not in the right format. Make sure you pass configuration as described in Action description. + INIntentResponseCodeConciseFormatStringID + xB99X4 + INIntentResponseCodeFormatString + The configuration update provided is not in the right format. Make sure you pass configuration as described in Action description. + INIntentResponseCodeFormatStringID + UljpyD + INIntentResponseCodeName + wrongConfiguration + + + + INIntentTitle + Update Configuration + INIntentTitleID + iYtEWT + INIntentType + Custom + INIntentVerb + Do + INTypes diff --git a/Sources/WireGuardApp/UI/iOS/AppDelegate.swift b/Sources/WireGuardApp/UI/iOS/AppDelegate.swift index 4172b33..45ffa2b 100644 --- a/Sources/WireGuardApp/UI/iOS/AppDelegate.swift +++ b/Sources/WireGuardApp/UI/iOS/AppDelegate.swift @@ -3,6 +3,7 @@ import UIKit import os.log +import Intents @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { @@ -105,3 +106,92 @@ extension AppDelegate { return nil } } + +extension AppDelegate { + + func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { + + guard let interaction = userActivity.interaction else { + return false + } + + if interaction.intent is UpdateConfigurationIntent { + if let tunnelsManager = tunnelsManager { + self.handleupdateConfigurationIntent(interaction: interaction, tunnelsManager: tunnelsManager) + } else { + var token: NSObjectProtocol? + token = NotificationCenter.default.addObserver(forName: AppDelegate.tunnelsManagerReadyNotificationName, object: nil, queue: .main) { [weak self] _ in + guard let tunnelsManager = self?.tunnelsManager else { return } + + self?.handleupdateConfigurationIntent(interaction: interaction, tunnelsManager: tunnelsManager) + NotificationCenter.default.removeObserver(token!) + } + } + + return true + } + + return false + } + + func handleupdateConfigurationIntent(interaction: INInteraction, tunnelsManager: TunnelsManager) { + + guard let updateConfigurationIntent = interaction.intent as? UpdateConfigurationIntent, + let configurationUpdates = interaction.intentResponse?.userActivity?.userInfo else { + return + } + + guard let tunnelName = updateConfigurationIntent.tunnel, + let configurations = configurationUpdates["Configuration"] as? [String: [String: String]] else { + wg_log(.error, message: "Failed to get informations to update the configuration") + return + } + + guard let tunnel = tunnelsManager.tunnel(named: tunnelName), + let tunnelConfiguration = tunnel.tunnelConfiguration else { + wg_log(.error, message: "Failed to get tunnel configuration with name \(tunnelName)") + ErrorPresenter.showErrorAlert(title: "Tunnel not found", + message: "Tunnel with name '\(tunnelName)' is not present.", + from: self.mainVC) + return + } + + var peers = tunnelConfiguration.peers + + for (peerPubKey, valuesToUpdate) in configurations { + guard let peerIndex = peers.firstIndex(where: { $0.publicKey.base64Key == peerPubKey }) else { + wg_log(.debug, message: "Failed to find peer \(peerPubKey) in tunnel with name \(tunnelName)") + ErrorPresenter.showErrorAlert(title: "Peer not found", + message: "Peer '\(peerPubKey)' is not present in '\(tunnelName)' tunnel.", + from: self.mainVC) + continue + } + + if let endpointString = valuesToUpdate["Endpoint"] { + if let newEntpoint = Endpoint(from: endpointString) { + peers[peerIndex].endpoint = newEntpoint + } else { + wg_log(.debug, message: "Failed to convert \(endpointString) to Endpoint") + } + } + } + + let newConfiguration = TunnelConfiguration(name: tunnel.name, interface: tunnelConfiguration.interface, peers: peers) + + tunnelsManager.modify(tunnel: tunnel, tunnelConfiguration: newConfiguration, onDemandOption: tunnel.onDemandOption) { error in + guard error == nil else { + wg_log(.error, message: error!.localizedDescription) + ErrorPresenter.showErrorAlert(error: error!, from: self.mainVC) + return + } + + if let completionUrlString = updateConfigurationIntent.completionUrl, + !completionUrlString.isEmpty, + let completionUrl = URL(string: completionUrlString) { + UIApplication.shared.open(completionUrl, options: [:], completionHandler: nil) + } + + wg_log(.debug, message: "Updated configuration of tunnel \(tunnelName)") + } + } +} diff --git a/Sources/WireGuardApp/UI/iOS/Info.plist b/Sources/WireGuardApp/UI/iOS/Info.plist index 754d12c..101d0a7 100644 --- a/Sources/WireGuardApp/UI/iOS/Info.plist +++ b/Sources/WireGuardApp/UI/iOS/Info.plist @@ -82,6 +82,10 @@ Localized NSFaceIDUsageDescription Localized + NSUserActivityTypes + + UpdateConfigurationIntent + UILaunchStoryboardName LaunchScreen UIRequiredDeviceCapabilities diff --git a/Sources/WireGuardIntentsExtension/Info.plist b/Sources/WireGuardIntentsExtension/Info.plist index 35910e1..06ec3ab 100644 --- a/Sources/WireGuardIntentsExtension/Info.plist +++ b/Sources/WireGuardIntentsExtension/Info.plist @@ -33,6 +33,7 @@ IntentsSupported GetPeersIntent + UpdateConfigurationIntent NSExtensionPointIdentifier diff --git a/Sources/WireGuardIntentsExtension/IntentHandler.swift b/Sources/WireGuardIntentsExtension/IntentHandler.swift index 4567b49..62eb5e2 100644 --- a/Sources/WireGuardIntentsExtension/IntentHandler.swift +++ b/Sources/WireGuardIntentsExtension/IntentHandler.swift @@ -11,7 +11,7 @@ class IntentHandler: INExtension { } override func handler(for intent: INIntent) -> Any { - guard intent is GetPeersIntent else { + guard intent is GetPeersIntent || intent is UpdateConfigurationIntent else { fatalError("Unhandled intent type: \(intent)") } diff --git a/Sources/WireGuardIntentsExtension/IntentHandling.swift b/Sources/WireGuardIntentsExtension/IntentHandling.swift index d946160..1de3d46 100644 --- a/Sources/WireGuardIntentsExtension/IntentHandling.swift +++ b/Sources/WireGuardIntentsExtension/IntentHandling.swift @@ -118,3 +118,55 @@ extension IntentHandling: GetPeersIntentHandling { } } + +extension IntentHandling: UpdateConfigurationIntentHandling { + + @available(iOSApplicationExtension 14.0, *) + func provideTunnelOptionsCollection(for intent: UpdateConfigurationIntent, with completion: @escaping (INObjectCollection?, Error?) -> Void) { + self.allTunnelNames { tunnelsNames in + let tunnelsNamesObjects = (tunnelsNames ?? []).map { NSString(string: $0) } + + let objectCollection = INObjectCollection(items: tunnelsNamesObjects) + completion(objectCollection, nil) + } + } + + func handle(intent: UpdateConfigurationIntent, completion: @escaping (UpdateConfigurationIntentResponse) -> Void) { + // Due to an Apple bug (https://developer.apple.com/forums/thread/96020) we can't update VPN + // configuration from extensions at the moment, so we should handle the action in the app. + // We check that the configuration update data is valid and then launch the main app. + + guard let tunnelName = intent.tunnel, + let configurationString = intent.configuration else { + wg_log(.error, message: "Failed to get informations to update the configuration") + completion(UpdateConfigurationIntentResponse(code: .failure, userActivity: nil)) + return + } + + var configurations: [String: [String: String]] + + let configurationsData = Data(configurationString.utf8) + do { + // Make sure this JSON is in the format we expect + if let decodedJson = try JSONSerialization.jsonObject(with: configurationsData, options: []) as? [String: [String: String]] { + configurations = decodedJson + } else { + throw IntentError.failedDecode + } + } catch _ { + wg_log(.error, message: "Failed to decode configuration data in JSON format for \(tunnelName)") + completion(UpdateConfigurationIntentResponse(code: .wrongConfiguration, userActivity: nil)) + return + } + + var activity: NSUserActivity? + if let bundleIdentifier = Bundle.main.bundleIdentifier { + activity = NSUserActivity(activityType: "\(bundleIdentifier).activity.update-tunnel-config") + activity?.userInfo = ["TunnelName": tunnelName, + "Configuration": configurations] + } + + completion(UpdateConfigurationIntentResponse(code: .continueInApp, userActivity: activity)) + } + +}