Removed UpdateConfigurationIntent SiriKit Intent

Signed-off-by: Alessio Nossa <alessio.nossa@gmail.com>
This commit is contained in:
Alessio Nossa
2023-04-05 15:40:51 +02:00
parent bee5d346b1
commit d05a1693e2
6 changed files with 3 additions and 418 deletions
+1 -212
View File
@@ -15,218 +15,7 @@
<key>INIntentDefinitionToolsVersion</key>
<string>14.2</string>
<key>INIntents</key>
<array>
<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>
<array/>
</dict>
@@ -115,88 +115,6 @@ extension AppDelegate {
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)")
}
}
}
+1 -3
View File
@@ -83,9 +83,7 @@
<key>NSFaceIDUsageDescription</key>
<string>Localized</string>
<key>NSUserActivityTypes</key>
<array>
<string>UpdateConfigurationIntent</string>
</array>
<array/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIRequiredDeviceCapabilities</key>
@@ -11,11 +11,7 @@ class IntentHandler: INExtension {
}
override func handler(for intent: INIntent) -> Any {
guard intent is UpdateConfigurationIntent else {
fatalError("Unhandled intent type: \(intent)")
}
return IntentHandling()
fatalError("Unhandled intent type: \(intent)")
}
}
@@ -1,112 +0,0 @@
// SPDX-License-Identifier: MIT
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
import Intents
class IntentHandling: NSObject {
public enum IntentError: Error {
case failedDecode
case wrongTunnel
case unknown
}
var tunnelsManager: TunnelsManager?
var onTunnelsManagerReady: ((TunnelsManager) -> Void)?
override init() {
super.init()
TunnelsManager.create { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
wg_log(.error, message: error.localizedDescription)
case .success(let tunnelsManager):
self.tunnelsManager = tunnelsManager
self.onTunnelsManagerReady?(tunnelsManager)
self.onTunnelsManagerReady = nil
}
}
}
init(tunnelsManager: TunnelsManager) {
super.init()
self.tunnelsManager = tunnelsManager
}
}
extension IntentHandling {
private func allTunnelNames(completion: @escaping ([String]?) -> Void) {
let getTunnelsNameBlock: (TunnelsManager) -> Void = { tunnelsManager in
let tunnelsNames = tunnelsManager.mapTunnels { $0.name }
return completion(tunnelsNames)
}
if let tunnelsManager = tunnelsManager {
getTunnelsNameBlock(tunnelsManager)
} else {
if onTunnelsManagerReady != nil {
wg_log(.error, message: "Overriding onTunnelsManagerReady action in allTunnelNames function. This should not happen.")
}
onTunnelsManagerReady = getTunnelsNameBlock
}
}
}
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))
}
}
-4
View File
@@ -234,7 +234,6 @@
A64A79F627A5450000F15B34 /* WireGuardResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F61F1EA21B937EF00483816 /* WireGuardResult.swift */; };
A64A79F927A5462900F15B34 /* Intents.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = A64A79F827A5462900F15B34 /* Intents.intentdefinition */; };
A64A79FA27A5462900F15B34 /* Intents.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = A64A79F827A5462900F15B34 /* Intents.intentdefinition */; };
A64A79FC27A548A000F15B34 /* IntentHandling.swift in Sources */ = {isa = PBXBuildFile; fileRef = A64A79FB27A548A000F15B34 /* IntentHandling.swift */; };
A64A79FD27A54AA500F15B34 /* NetworkExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6FF4AC462120B9E0002C96EB /* NetworkExtension.framework */; };
A64A79FE27A58F1800F15B34 /* MockTunnels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FB1017821C57DE600766195 /* MockTunnels.swift */; };
A6B8051C27A44F770088E750 /* Intents.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A6B8051B27A44F770088E750 /* Intents.framework */; };
@@ -487,7 +486,6 @@
A64A79D727A462FC00F15B34 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A64A79D827A48A8E00F15B34 /* WireGuardIntentsExtension-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WireGuardIntentsExtension-Bridging-Header.h"; sourceTree = "<group>"; };
A64A79F827A5462900F15B34 /* Intents.intentdefinition */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; path = Intents.intentdefinition; sourceTree = "<group>"; };
A64A79FB27A548A000F15B34 /* IntentHandling.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntentHandling.swift; sourceTree = "<group>"; };
A6B8051A27A44F770088E750 /* WireGuardIntentsExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = WireGuardIntentsExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; };
A6B8051B27A44F770088E750 /* Intents.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Intents.framework; path = System/Library/Frameworks/Intents.framework; sourceTree = SDKROOT; };
A6B8051E27A44F770088E750 /* IntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntentHandler.swift; sourceTree = "<group>"; };
@@ -899,7 +897,6 @@
A6B8052727A454150088E750 /* WireGuardIntentsExtension_iOS.entitlements */,
A64A79D827A48A8E00F15B34 /* WireGuardIntentsExtension-Bridging-Header.h */,
A6B8051E27A44F770088E750 /* IntentHandler.swift */,
A64A79FB27A548A000F15B34 /* IntentHandling.swift */,
);
name = WireGuardIntentsExtension;
path = Sources/WireGuardIntentsExtension;
@@ -1606,7 +1603,6 @@
A64A79E827A542F800F15B34 /* key.c in Sources */,
A64A79E127A5426C00F15B34 /* TunnelConfiguration.swift in Sources */,
A64A79DE27A541F500F15B34 /* TunnelsManager.swift in Sources */,
A64A79FC27A548A000F15B34 /* IntentHandling.swift in Sources */,
A64A79ED27A5440E00F15B34 /* TunnelStatus.swift in Sources */,
A64A79EB27A543D400F15B34 /* WireGuardAppError.swift in Sources */,
A64A79E527A542C000F15B34 /* InterfaceConfiguration.swift in Sources */,