Implemented UpdateConfiguration intent
Signed-off-by: Alessio Nossa <alessio.nossa@gmail.com>
This commit is contained in:
@@ -150,6 +150,216 @@
|
|||||||
<key>INIntentVerb</key>
|
<key>INIntentVerb</key>
|
||||||
<string>Do</string>
|
<string>Do</string>
|
||||||
</dict>
|
</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>
|
</array>
|
||||||
<key>INTypes</key>
|
<key>INTypes</key>
|
||||||
<array/>
|
<array/>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
import os.log
|
import os.log
|
||||||
|
import Intents
|
||||||
|
|
||||||
@UIApplicationMain
|
@UIApplicationMain
|
||||||
class AppDelegate: UIResponder, UIApplicationDelegate {
|
class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||||
@@ -105,3 +106,92 @@ extension AppDelegate {
|
|||||||
return nil
|
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)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -82,6 +82,10 @@
|
|||||||
<string>Localized</string>
|
<string>Localized</string>
|
||||||
<key>NSFaceIDUsageDescription</key>
|
<key>NSFaceIDUsageDescription</key>
|
||||||
<string>Localized</string>
|
<string>Localized</string>
|
||||||
|
<key>NSUserActivityTypes</key>
|
||||||
|
<array>
|
||||||
|
<string>UpdateConfigurationIntent</string>
|
||||||
|
</array>
|
||||||
<key>UILaunchStoryboardName</key>
|
<key>UILaunchStoryboardName</key>
|
||||||
<string>LaunchScreen</string>
|
<string>LaunchScreen</string>
|
||||||
<key>UIRequiredDeviceCapabilities</key>
|
<key>UIRequiredDeviceCapabilities</key>
|
||||||
|
|||||||
@@ -33,6 +33,7 @@
|
|||||||
<key>IntentsSupported</key>
|
<key>IntentsSupported</key>
|
||||||
<array>
|
<array>
|
||||||
<string>GetPeersIntent</string>
|
<string>GetPeersIntent</string>
|
||||||
|
<string>UpdateConfigurationIntent</string>
|
||||||
</array>
|
</array>
|
||||||
</dict>
|
</dict>
|
||||||
<key>NSExtensionPointIdentifier</key>
|
<key>NSExtensionPointIdentifier</key>
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class IntentHandler: INExtension {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override func handler(for intent: INIntent) -> Any {
|
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)")
|
fatalError("Unhandled intent type: \(intent)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user