Implemented GetPeers intent

Signed-off-by: Alessio Nossa <alessio.nossa@gmail.com>
This commit is contained in:
Alessio Nossa
2022-01-29 19:11:32 +01:00
parent 0dd0672844
commit ab1e96fcdc
6 changed files with 308 additions and 5 deletions
+157
View File
@@ -0,0 +1,157 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>INEnums</key>
<array/>
<key>INIntentDefinitionModelVersion</key>
<string>1.2</string>
<key>INIntentDefinitionNamespace</key>
<string>6NREiY</string>
<key>INIntentDefinitionSystemVersion</key>
<string>21C52</string>
<key>INIntentDefinitionToolsBuildVersion</key>
<string>13C100</string>
<key>INIntentDefinitionToolsVersion</key>
<string>13.2.1</string>
<key>INIntents</key>
<array>
<dict>
<key>INIntentCategory</key>
<string>generic</string>
<key>INIntentConfigurable</key>
<true/>
<key>INIntentDescription</key>
<string>Get list of public keys of peers in the selected configuration</string>
<key>INIntentDescriptionID</key>
<string>e4jxYU</string>
<key>INIntentIneligibleForSuggestions</key>
<true/>
<key>INIntentKeyParameter</key>
<string>tunnel</string>
<key>INIntentLastParameterTag</key>
<integer>1</integer>
<key>INIntentManagedParameterCombinations</key>
<dict>
<key>tunnel</key>
<dict>
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
<true/>
<key>INIntentParameterCombinationTitle</key>
<string>Get peers of ${tunnel}</string>
<key>INIntentParameterCombinationTitleID</key>
<string>5Ewt1o</string>
<key>INIntentParameterCombinationUpdatesLinked</key>
<true/>
</dict>
</dict>
<key>INIntentName</key>
<string>GetPeers</string>
<key>INIntentParameters</key>
<array>
<dict>
<key>INIntentParameterConfigurable</key>
<true/>
<key>INIntentParameterDisplayName</key>
<string>Tunnel</string>
<key>INIntentParameterDisplayNameID</key>
<string>tUPuxx</string>
<key>INIntentParameterDisplayPriority</key>
<integer>1</integer>
<key>INIntentParameterMetadata</key>
<dict>
<key>INIntentParameterMetadataCapitalization</key>
<string>Sentences</string>
<key>INIntentParameterMetadataDefaultValueID</key>
<string>0AfeHS</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>
</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>Failed to retrive configuration with the provided name.</string>
<key>INIntentResponseCodeConciseFormatStringID</key>
<string>ExhkKO</string>
<key>INIntentResponseCodeFormatString</key>
<string>Failed to retrive configuration with the provided name.</string>
<key>INIntentResponseCodeFormatStringID</key>
<string>lxnQb0</string>
<key>INIntentResponseCodeName</key>
<string>wrongTunnel</string>
</dict>
</array>
<key>INIntentResponseLastParameterTag</key>
<integer>2</integer>
<key>INIntentResponseOutput</key>
<string>peersPublicKeys</string>
<key>INIntentResponseParameters</key>
<array>
<dict>
<key>INIntentResponseParameterDisplayName</key>
<string>Peers Public Keys</string>
<key>INIntentResponseParameterDisplayNameID</key>
<string>zJOQgA</string>
<key>INIntentResponseParameterDisplayPriority</key>
<integer>1</integer>
<key>INIntentResponseParameterName</key>
<string>peersPublicKeys</string>
<key>INIntentResponseParameterSupportsMultipleValues</key>
<true/>
<key>INIntentResponseParameterTag</key>
<integer>2</integer>
<key>INIntentResponseParameterType</key>
<string>String</string>
</dict>
</array>
</dict>
<key>INIntentTitle</key>
<string>Get Peers</string>
<key>INIntentTitleID</key>
<string>V1ySW4</string>
<key>INIntentType</key>
<string>Custom</string>
<key>INIntentVerb</key>
<string>Do</string>
</dict>
</array>
<key>INTypes</key>
<array/>
</dict>
</plist>
+5 -1
View File
@@ -28,8 +28,12 @@
<dict>
<key>IntentsRestrictedWhileLocked</key>
<array/>
<key>IntentsSupported</key>
<key>IntentsRestrictedWhileProtectedDataUnavailable</key>
<array/>
<key>IntentsSupported</key>
<array>
<string>GetPeersIntent</string>
</array>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.intents-service</string>
@@ -5,11 +5,17 @@ import Intents
class IntentHandler: INExtension {
override func handler(for intent: INIntent) -> Any {
// This is the default implementation. If you want different objects to handle different intents,
// you can override this and return the handler you want for that particular intent.
override init() {
super.init()
Logger.configureGlobal(tagged: "INTENTS", withFilePath: FileManager.logFileURL?.path)
}
return self
override func handler(for intent: INIntent) -> Any {
guard intent is GetPeersIntent else {
fatalError("Unhandled intent type: \(intent)")
}
return IntentHandling()
}
}
@@ -0,0 +1,120 @@
// 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
}
}
private func allTunnelPeers(for tunnelName: String, completion: @escaping (Result<[String], IntentError>) -> Void) {
let getPeersFromConfigBlock: (TunnelsManager) -> Void = { tunnelsManager in
guard let tunnel = tunnelsManager.tunnel(named: tunnelName) else {
return completion(.failure(.wrongTunnel))
}
guard let publicKeys = tunnel.tunnelConfiguration?.peers.map({ $0.publicKey.base64Key }) else {
return completion(.failure(.unknown))
}
return completion(.success(publicKeys))
}
if let tunnelsManager = tunnelsManager {
getPeersFromConfigBlock(tunnelsManager)
} else {
if onTunnelsManagerReady != nil {
wg_log(.error, message: "Overriding onTunnelsManagerReady action in allTunnelPeers function. This should not happen.")
}
onTunnelsManagerReady = getPeersFromConfigBlock
}
}
}
extension IntentHandling: GetPeersIntentHandling {
@available(iOSApplicationExtension 14.0, *)
func provideTunnelOptionsCollection(for intent: GetPeersIntent, 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: GetPeersIntent, completion: @escaping (GetPeersIntentResponse) -> Void) {
guard let tunnel = intent.tunnel else {
return completion(GetPeersIntentResponse(code: .failure, userActivity: nil))
}
self.allTunnelPeers(for: tunnel) { peersResult in
switch peersResult {
case .success(let peers):
let response = GetPeersIntentResponse(code: .success, userActivity: nil)
response.peersPublicKeys = peers
completion(response)
case .failure(let error):
switch error {
case .wrongTunnel:
completion(GetPeersIntentResponse(code: .wrongTunnel, userActivity: nil))
default:
completion(GetPeersIntentResponse(code: .failure, userActivity: nil))
}
}
}
}
}
@@ -2,6 +2,10 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.networking.networkextension</key>
<array>
<string>packet-tunnel-provider</string>
</array>
<key>com.apple.security.application-groups</key>
<array>
<string>group.$(APP_ID_IOS)</string>
+12
View File
@@ -231,6 +231,10 @@
A64A79F427A544BC00F15B34 /* ActivateOnDemandOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FFA5DA32197085D0001E2F7 /* ActivateOnDemandOption.swift */; };
A64A79F527A544E800F15B34 /* TunnelConfiguration+UapiConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B707D8321F918D4000A8F73 /* TunnelConfiguration+UapiConfig.swift */; };
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 */; };
A6B8051F27A44F770088E750 /* IntentHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6B8051E27A44F770088E750 /* IntentHandler.swift */; };
@@ -478,6 +482,8 @@
6FFACD1E21E4D89600E9A2A5 /* ParseError+WireGuardAppError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ParseError+WireGuardAppError.swift"; sourceTree = "<group>"; };
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>"; };
@@ -524,6 +530,7 @@
buildActionMask = 2147483647;
files = (
A6B8051C27A44F770088E750 /* Intents.framework in Frameworks */,
A64A79FD27A54AA500F15B34 /* NetworkExtension.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -636,6 +643,7 @@
6F5A2B4421AFDE020081EDD8 /* FileManager+Extension.swift */,
6B5C5E26220A48D30024272E /* Keychain.swift */,
58233BCE2591F842002060A8 /* NotificationToken.swift */,
A64A79F827A5462900F15B34 /* Intents.intentdefinition */,
);
name = Shared;
path = Sources/Shared;
@@ -873,6 +881,7 @@
A6B8052727A454150088E750 /* WireGuardIntentsExtension_iOS.entitlements */,
A64A79D827A48A8E00F15B34 /* WireGuardIntentsExtension-Bridging-Header.h */,
A6B8051E27A44F770088E750 /* IntentHandler.swift */,
A64A79FB27A548A000F15B34 /* IntentHandling.swift */,
);
name = WireGuardIntentsExtension;
path = Sources/WireGuardIntentsExtension;
@@ -1533,6 +1542,7 @@
6F5A2B4821AFF49A0081EDD8 /* FileManager+Extension.swift in Sources */,
5F45418C21C2D48200994C13 /* TunnelEditKeyValueCell.swift in Sources */,
6FE254FB219C10800028284D /* ZipImporter.swift in Sources */,
A64A79F927A5462900F15B34 /* Intents.intentdefinition in Sources */,
585B107E2577E294004F691E /* PrivateKey.swift in Sources */,
6FDEF7FB21863B6100D8FBF6 /* unzip.c in Sources */,
6F29A9432278518D00DC6A6B /* RecentTunnelsTracker.swift in Sources */,
@@ -1575,11 +1585,13 @@
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 */,
A64A79F127A5447000F15B34 /* NETunnelProviderProtocol+Extension.swift in Sources */,
A64A79E327A5429100F15B34 /* PeerConfiguration.swift in Sources */,
A64A79FA27A5462900F15B34 /* Intents.intentdefinition in Sources */,
A64A79EE27A5442000F15B34 /* Keychain.swift in Sources */,
A64A79F327A544A500F15B34 /* String+ArrayConversion.swift in Sources */,
A64A79E227A5428C00F15B34 /* PrivateKey.swift in Sources */,