]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Implemented UpdateConfiguration intent
authorAlessio Nossa <alessio.nossa@gmail.com>
Tue, 1 Feb 2022 09:16:38 +0000 (10:16 +0100)
committerAlessio Nossa <alessio.nossa@gmail.com>
Tue, 1 Feb 2022 19:13:38 +0000 (20:13 +0100)
Signed-off-by: Alessio Nossa <alessio.nossa@gmail.com>
Sources/Shared/Intents.intentdefinition
Sources/WireGuardApp/UI/iOS/AppDelegate.swift
Sources/WireGuardApp/UI/iOS/Info.plist
Sources/WireGuardIntentsExtension/Info.plist
Sources/WireGuardIntentsExtension/IntentHandler.swift
Sources/WireGuardIntentsExtension/IntentHandling.swift

index 38f8e5467d5a0eb4502efd75c0f75b0d9ffd0d2f..590fa39c0a64dd7b4be9f9c1852d39864dc5de92 100644 (file)
                        <key>INIntentVerb</key>
                        <string>Do</string>
                </dict>
+               <dict>
+                       <key>INIntentCategory</key>
+                       <string>generic</string>
+                       <key>INIntentConfigurable</key>
+                       <true/>
+                       <key>INIntentDescription</key>
+                       <string>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"} }</string>
+                       <key>INIntentDescriptionID</key>
+                       <string>uTimVO</string>
+                       <key>INIntentIneligibleForSuggestions</key>
+                       <true/>
+                       <key>INIntentInput</key>
+                       <string>configuration</string>
+                       <key>INIntentLastParameterTag</key>
+                       <integer>5</integer>
+                       <key>INIntentManagedParameterCombinations</key>
+                       <dict>
+                               <key>tunnel,configuration,completionUrl</key>
+                               <dict>
+                                       <key>INIntentParameterCombinationSupportsBackgroundExecution</key>
+                                       <true/>
+                                       <key>INIntentParameterCombinationTitle</key>
+                                       <string>Update ${tunnel} configuration</string>
+                                       <key>INIntentParameterCombinationTitleID</key>
+                                       <string>2ASDIM</string>
+                                       <key>INIntentParameterCombinationUpdatesLinked</key>
+                                       <true/>
+                               </dict>
+                       </dict>
+                       <key>INIntentName</key>
+                       <string>UpdateConfiguration</string>
+                       <key>INIntentParameters</key>
+                       <array>
+                               <dict>
+                                       <key>INIntentParameterConfigurable</key>
+                                       <true/>
+                                       <key>INIntentParameterDisplayName</key>
+                                       <string>Tunnel</string>
+                                       <key>INIntentParameterDisplayNameID</key>
+                                       <string>TjOtzk</string>
+                                       <key>INIntentParameterDisplayPriority</key>
+                                       <integer>1</integer>
+                                       <key>INIntentParameterMetadata</key>
+                                       <dict>
+                                               <key>INIntentParameterMetadataCapitalization</key>
+                                               <string>Sentences</string>
+                                               <key>INIntentParameterMetadataDefaultValueID</key>
+                                               <string>h56bAD</string>
+                                       </dict>
+                                       <key>INIntentParameterName</key>
+                                       <string>tunnel</string>
+                                       <key>INIntentParameterPromptDialogs</key>
+                                       <array>
+                                               <dict>
+                                                       <key>INIntentParameterPromptDialogCustom</key>
+                                                       <true/>
+                                                       <key>INIntentParameterPromptDialogType</key>
+                                                       <string>Configuration</string>
+                                               </dict>
+                                               <dict>
+                                                       <key>INIntentParameterPromptDialogCustom</key>
+                                                       <true/>
+                                                       <key>INIntentParameterPromptDialogType</key>
+                                                       <string>Primary</string>
+                                               </dict>
+                                       </array>
+                                       <key>INIntentParameterSupportsDynamicEnumeration</key>
+                                       <true/>
+                                       <key>INIntentParameterTag</key>
+                                       <integer>1</integer>
+                                       <key>INIntentParameterType</key>
+                                       <string>String</string>
+                               </dict>
+                               <dict>
+                                       <key>INIntentParameterConfigurable</key>
+                                       <true/>
+                                       <key>INIntentParameterDisplayName</key>
+                                       <string>Configuration</string>
+                                       <key>INIntentParameterDisplayNameID</key>
+                                       <string>3SLMhb</string>
+                                       <key>INIntentParameterDisplayPriority</key>
+                                       <integer>2</integer>
+                                       <key>INIntentParameterMetadata</key>
+                                       <dict>
+                                               <key>INIntentParameterMetadataCapitalization</key>
+                                               <string>None</string>
+                                               <key>INIntentParameterMetadataDefaultValue</key>
+                                               <string>{"Peer Public Key": {"Endpoint":"1.2.3.4:5678"} }</string>
+                                               <key>INIntentParameterMetadataDefaultValueID</key>
+                                               <string>1J2FBa</string>
+                                               <key>INIntentParameterMetadataDisableAutocorrect</key>
+                                               <true/>
+                                               <key>INIntentParameterMetadataDisableSmartDashes</key>
+                                               <true/>
+                                               <key>INIntentParameterMetadataDisableSmartQuotes</key>
+                                               <true/>
+                                               <key>INIntentParameterMetadataMultiline</key>
+                                               <true/>
+                                       </dict>
+                                       <key>INIntentParameterName</key>
+                                       <string>configuration</string>
+                                       <key>INIntentParameterPromptDialogs</key>
+                                       <array>
+                                               <dict>
+                                                       <key>INIntentParameterPromptDialogCustom</key>
+                                                       <true/>
+                                                       <key>INIntentParameterPromptDialogType</key>
+                                                       <string>Configuration</string>
+                                               </dict>
+                                               <dict>
+                                                       <key>INIntentParameterPromptDialogCustom</key>
+                                                       <true/>
+                                                       <key>INIntentParameterPromptDialogType</key>
+                                                       <string>Primary</string>
+                                               </dict>
+                                       </array>
+                                       <key>INIntentParameterTag</key>
+                                       <integer>2</integer>
+                                       <key>INIntentParameterType</key>
+                                       <string>String</string>
+                               </dict>
+                               <dict>
+                                       <key>INIntentParameterConfigurable</key>
+                                       <true/>
+                                       <key>INIntentParameterDisplayName</key>
+                                       <string>Open URL when done</string>
+                                       <key>INIntentParameterDisplayNameID</key>
+                                       <string>dwpgmC</string>
+                                       <key>INIntentParameterDisplayPriority</key>
+                                       <integer>3</integer>
+                                       <key>INIntentParameterMetadata</key>
+                                       <dict>
+                                               <key>INIntentParameterMetadataCapitalization</key>
+                                               <string>None</string>
+                                               <key>INIntentParameterMetadataDefaultValue</key>
+                                               <string>shortcuts://</string>
+                                               <key>INIntentParameterMetadataDefaultValueID</key>
+                                               <string>cyr6LU</string>
+                                               <key>INIntentParameterMetadataDisableAutocorrect</key>
+                                               <true/>
+                                               <key>INIntentParameterMetadataDisableSmartDashes</key>
+                                               <true/>
+                                               <key>INIntentParameterMetadataDisableSmartQuotes</key>
+                                               <true/>
+                                       </dict>
+                                       <key>INIntentParameterName</key>
+                                       <string>completionUrl</string>
+                                       <key>INIntentParameterPromptDialogs</key>
+                                       <array>
+                                               <dict>
+                                                       <key>INIntentParameterPromptDialogCustom</key>
+                                                       <true/>
+                                                       <key>INIntentParameterPromptDialogType</key>
+                                                       <string>Configuration</string>
+                                               </dict>
+                                               <dict>
+                                                       <key>INIntentParameterPromptDialogCustom</key>
+                                                       <true/>
+                                                       <key>INIntentParameterPromptDialogType</key>
+                                                       <string>Primary</string>
+                                               </dict>
+                                       </array>
+                                       <key>INIntentParameterTag</key>
+                                       <integer>5</integer>
+                                       <key>INIntentParameterType</key>
+                                       <string>String</string>
+                               </dict>
+                       </array>
+                       <key>INIntentResponse</key>
+                       <dict>
+                               <key>INIntentResponseCodes</key>
+                               <array>
+                                       <dict>
+                                               <key>INIntentResponseCodeName</key>
+                                               <string>success</string>
+                                               <key>INIntentResponseCodeSuccess</key>
+                                               <true/>
+                                       </dict>
+                                       <dict>
+                                               <key>INIntentResponseCodeName</key>
+                                               <string>failure</string>
+                                       </dict>
+                                       <dict>
+                                               <key>INIntentResponseCodeConciseFormatString</key>
+                                               <string>The configuration update provided is not in the right format. Make sure you pass configuration as described in Action description.</string>
+                                               <key>INIntentResponseCodeConciseFormatStringID</key>
+                                               <string>xB99X4</string>
+                                               <key>INIntentResponseCodeFormatString</key>
+                                               <string>The configuration update provided is not in the right format. Make sure you pass configuration as described in Action description.</string>
+                                               <key>INIntentResponseCodeFormatStringID</key>
+                                               <string>UljpyD</string>
+                                               <key>INIntentResponseCodeName</key>
+                                               <string>wrongConfiguration</string>
+                                       </dict>
+                               </array>
+                       </dict>
+                       <key>INIntentTitle</key>
+                       <string>Update Configuration</string>
+                       <key>INIntentTitleID</key>
+                       <string>iYtEWT</string>
+                       <key>INIntentType</key>
+                       <string>Custom</string>
+                       <key>INIntentVerb</key>
+                       <string>Do</string>
+               </dict>
        </array>
        <key>INTypes</key>
        <array/>
index 4172b33aac3ee80cc1efd380fdbd891e6401bc7e..45ffa2b253b1bbaf06436421fcac09ab1e07bc3a 100644 (file)
@@ -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)")
+        }
+    }
+}
index 754d12c7e07ecf7da9df37e77722493b84e05679..101d0a79edb229cad91e8ff8b20b89ed2d4a6cf6 100644 (file)
        <string>Localized</string>
        <key>NSFaceIDUsageDescription</key>
        <string>Localized</string>
+       <key>NSUserActivityTypes</key>
+       <array>
+               <string>UpdateConfigurationIntent</string>
+       </array>
        <key>UILaunchStoryboardName</key>
        <string>LaunchScreen</string>
        <key>UIRequiredDeviceCapabilities</key>
index 35910e1c6889a4f6252815887729ca0783603580..06ec3ab2cd049373f43c817e7f0112d65e390a89 100644 (file)
@@ -33,6 +33,7 @@
                        <key>IntentsSupported</key>
                        <array>
                                <string>GetPeersIntent</string>
+                               <string>UpdateConfigurationIntent</string>
                        </array>
                </dict>
                <key>NSExtensionPointIdentifier</key>
index 4567b499ccff3b7c7103d70cc5031c9389a13c9d..62eb5e23f0fc7a424630094adcf868ae3cead979 100644 (file)
@@ -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)")
         }
 
index d946160325c1ad0fefb519a8ebecef3fb79dd4e0..1de3d46ade8b430b502a71d6a8ae6aea0c86a45c 100644 (file)
@@ -118,3 +118,55 @@ extension IntentHandling: GetPeersIntentHandling {
     }
 
 }
+
+extension IntentHandling: UpdateConfigurationIntentHandling {
+
+    @available(iOSApplicationExtension 14.0, *)
+    func provideTunnelOptionsCollection(for intent: UpdateConfigurationIntent, with completion: @escaping (INObjectCollection<NSString>?, 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))
+    }
+
+}