From: Alessio Nossa Date: Sat, 29 Jan 2022 18:11:32 +0000 (+0100) Subject: Implemented GetPeers intent X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ab1e96fcdcaa5128f619ca565b0f5303935b4518;p=thirdparty%2Fwireguard-apple.git Implemented GetPeers intent Signed-off-by: Alessio Nossa --- diff --git a/Sources/Shared/Intents.intentdefinition b/Sources/Shared/Intents.intentdefinition new file mode 100644 index 0000000..38f8e54 --- /dev/null +++ b/Sources/Shared/Intents.intentdefinition @@ -0,0 +1,157 @@ + + + + + INEnums + + INIntentDefinitionModelVersion + 1.2 + INIntentDefinitionNamespace + 6NREiY + INIntentDefinitionSystemVersion + 21C52 + INIntentDefinitionToolsBuildVersion + 13C100 + INIntentDefinitionToolsVersion + 13.2.1 + INIntents + + + INIntentCategory + generic + INIntentConfigurable + + INIntentDescription + Get list of public keys of peers in the selected configuration + INIntentDescriptionID + e4jxYU + INIntentIneligibleForSuggestions + + INIntentKeyParameter + tunnel + INIntentLastParameterTag + 1 + INIntentManagedParameterCombinations + + tunnel + + INIntentParameterCombinationSupportsBackgroundExecution + + INIntentParameterCombinationTitle + Get peers of ${tunnel} + INIntentParameterCombinationTitleID + 5Ewt1o + INIntentParameterCombinationUpdatesLinked + + + + INIntentName + GetPeers + INIntentParameters + + + INIntentParameterConfigurable + + INIntentParameterDisplayName + Tunnel + INIntentParameterDisplayNameID + tUPuxx + INIntentParameterDisplayPriority + 1 + INIntentParameterMetadata + + INIntentParameterMetadataCapitalization + Sentences + INIntentParameterMetadataDefaultValueID + 0AfeHS + + INIntentParameterName + tunnel + INIntentParameterPromptDialogs + + + INIntentParameterPromptDialogCustom + + INIntentParameterPromptDialogType + Configuration + + + INIntentParameterPromptDialogCustom + + INIntentParameterPromptDialogType + Primary + + + INIntentParameterSupportsDynamicEnumeration + + INIntentParameterTag + 1 + INIntentParameterType + String + + + INIntentResponse + + INIntentResponseCodes + + + INIntentResponseCodeName + success + INIntentResponseCodeSuccess + + + + INIntentResponseCodeName + failure + + + INIntentResponseCodeConciseFormatString + Failed to retrive configuration with the provided name. + INIntentResponseCodeConciseFormatStringID + ExhkKO + INIntentResponseCodeFormatString + Failed to retrive configuration with the provided name. + INIntentResponseCodeFormatStringID + lxnQb0 + INIntentResponseCodeName + wrongTunnel + + + INIntentResponseLastParameterTag + 2 + INIntentResponseOutput + peersPublicKeys + INIntentResponseParameters + + + INIntentResponseParameterDisplayName + Peers Public Keys + INIntentResponseParameterDisplayNameID + zJOQgA + INIntentResponseParameterDisplayPriority + 1 + INIntentResponseParameterName + peersPublicKeys + INIntentResponseParameterSupportsMultipleValues + + INIntentResponseParameterTag + 2 + INIntentResponseParameterType + String + + + + INIntentTitle + Get Peers + INIntentTitleID + V1ySW4 + INIntentType + Custom + INIntentVerb + Do + + + INTypes + + + diff --git a/Sources/WireGuardIntentsExtension/Info.plist b/Sources/WireGuardIntentsExtension/Info.plist index dac8df5..35910e1 100644 --- a/Sources/WireGuardIntentsExtension/Info.plist +++ b/Sources/WireGuardIntentsExtension/Info.plist @@ -28,8 +28,12 @@ IntentsRestrictedWhileLocked - IntentsSupported + IntentsRestrictedWhileProtectedDataUnavailable + IntentsSupported + + GetPeersIntent + NSExtensionPointIdentifier com.apple.intents-service diff --git a/Sources/WireGuardIntentsExtension/IntentHandler.swift b/Sources/WireGuardIntentsExtension/IntentHandler.swift index fc4899c..4567b49 100644 --- a/Sources/WireGuardIntentsExtension/IntentHandler.swift +++ b/Sources/WireGuardIntentsExtension/IntentHandler.swift @@ -5,11 +5,17 @@ import Intents class IntentHandler: INExtension { + override init() { + super.init() + Logger.configureGlobal(tagged: "INTENTS", withFilePath: FileManager.logFileURL?.path) + } + 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. + guard intent is GetPeersIntent else { + fatalError("Unhandled intent type: \(intent)") + } - return self + return IntentHandling() } } diff --git a/Sources/WireGuardIntentsExtension/IntentHandling.swift b/Sources/WireGuardIntentsExtension/IntentHandling.swift new file mode 100644 index 0000000..d946160 --- /dev/null +++ b/Sources/WireGuardIntentsExtension/IntentHandling.swift @@ -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?, 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)) + } + } + } + } + +} diff --git a/Sources/WireGuardIntentsExtension/WireGuardIntentsExtension_iOS.entitlements b/Sources/WireGuardIntentsExtension/WireGuardIntentsExtension_iOS.entitlements index 75c276b..33ce9fc 100644 --- a/Sources/WireGuardIntentsExtension/WireGuardIntentsExtension_iOS.entitlements +++ b/Sources/WireGuardIntentsExtension/WireGuardIntentsExtension_iOS.entitlements @@ -2,6 +2,10 @@ + com.apple.developer.networking.networkextension + + packet-tunnel-provider + com.apple.security.application-groups group.$(APP_ID_IOS) diff --git a/WireGuard.xcodeproj/project.pbxproj b/WireGuard.xcodeproj/project.pbxproj index 8c9ff87..53db590 100644 --- a/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard.xcodeproj/project.pbxproj @@ -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 = ""; }; A64A79D727A462FC00F15B34 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; A64A79D827A48A8E00F15B34 /* WireGuardIntentsExtension-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WireGuardIntentsExtension-Bridging-Header.h"; sourceTree = ""; }; + A64A79F827A5462900F15B34 /* Intents.intentdefinition */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; path = Intents.intentdefinition; sourceTree = ""; }; + A64A79FB27A548A000F15B34 /* IntentHandling.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntentHandling.swift; sourceTree = ""; }; 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 = ""; }; @@ -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 */,