From e13bc40f88a498e0b76e1725919993aec9f5f909 Mon Sep 17 00:00:00 2001 From: Alessio Nossa Date: Tue, 11 Apr 2023 23:41:31 +0200 Subject: Implement BuildPeerConfigurationUpdate App Intent Signed-off-by: Alessio Nossa --- Sources/WireguardAppIntents/AppIntents.strings | 12 ++++ .../BuildPeerConfigurationUpdate.swift | 77 ++++++++++++++++++++++ WireGuard.xcodeproj/project.pbxproj | 4 ++ 3 files changed, 93 insertions(+) create mode 100644 Sources/WireguardAppIntents/BuildPeerConfigurationUpdate.swift diff --git a/Sources/WireguardAppIntents/AppIntents.strings b/Sources/WireguardAppIntents/AppIntents.strings index e539499..9ebdc8e 100644 --- a/Sources/WireguardAppIntents/AppIntents.strings +++ b/Sources/WireguardAppIntents/AppIntents.strings @@ -10,3 +10,15 @@ "getPeersIntentDescription" = "Get list of public keys of peers in the selected configuration"; "getPeersIntentTunnelParameterTitle" = "Tunnel"; "getPeersIntentSummary ${tunnelName}" = "Get peers of ${tunnelName}"; + +// Build Peer Configuration +"buildPeerConfigurationUpdateIntentName" = "Build Peer Configuration"; +"buildPeerConfigurationUpdateIntentDescription" = ""; +"buildPeerConfigurationUpdateIntentSummary ${publicKey}" = "Build configuration for peer ${publicKey}"; +"buildPeerConfigurationUpdateIntentPublicKeyParameterTitle" = "Peer Public Key"; +"buildPeerConfigurationUpdateIntentEndpointParameterTitle" = "Endpoint"; + +// Peer Configuration Update Entity +"peerConfigurationUpdateEntityName" = "Configuration Update - Peer"; +"peerConfigurationUpdateEntityPropertyPublicKeyTitle" = "Public Key"; +"peerConfigurationUpdateEntityPropertyEndpointTitle" = "Endpoint"; diff --git a/Sources/WireguardAppIntents/BuildPeerConfigurationUpdate.swift b/Sources/WireguardAppIntents/BuildPeerConfigurationUpdate.swift new file mode 100644 index 0000000..d6c0032 --- /dev/null +++ b/Sources/WireguardAppIntents/BuildPeerConfigurationUpdate.swift @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved. + +import AppIntents + +let kEndpointConfigurationUpdateDictionaryKey = "Endpoint" + +@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) +struct BuildPeerConfigurationUpdate: AppIntent { + + static var title = LocalizedStringResource("buildPeerConfigurationUpdateIntentName", table: "AppIntents") + static var description = IntentDescription( + LocalizedStringResource("buildPeerConfigurationUpdateIntentDescription", table: "AppIntents") + ) + + @Parameter( + title: LocalizedStringResource("buildPeerConfigurationUpdateIntentPublicKeyParameterTitle", table: "AppIntents") + ) + var publicKey: String + + @Parameter( + title: LocalizedStringResource("buildPeerConfigurationUpdateIntentEndpointParameterTitle", table: "AppIntents") + ) + var endpoint: String + + func perform() async throws -> some IntentResult { + let peerConfigurationUpdate = AppIntentsPeer() + peerConfigurationUpdate.publicKey = publicKey + peerConfigurationUpdate.endpoint = endpoint + + return .result(value: peerConfigurationUpdate) + } + + static var parameterSummary: some ParameterSummary { + Summary("buildPeerConfigurationUpdateIntentSummary \(\.$publicKey)", table: "AppIntents") { + \.$endpoint + } + } +} + +@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) +struct AppIntentsPeer: TransientAppEntity { + + static var typeDisplayRepresentation = TypeDisplayRepresentation( + name: LocalizedStringResource("peerConfigurationUpdateEntityName", table: "AppIntents") + ) + + @Property( + title: LocalizedStringResource("peerConfigurationUpdateEntityPropertyPublicKeyTitle", table: "AppIntents") + ) + var publicKey: String + + @Property( + title: LocalizedStringResource("peerConfigurationUpdateEntityPropertyEndpointTitle", table: "AppIntents") + ) + var endpoint: String? + + var displayRepresentation: DisplayRepresentation { + var dictionary: [String: [String: String]] = [:] + dictionary[publicKey] = [:] + + if let endpoint { + dictionary[publicKey]?.updateValue(endpoint, forKey: kEndpointConfigurationUpdateDictionaryKey) + } + + let jsonData: Data + do { + jsonData = try JSONSerialization.data(withJSONObject: dictionary) + } catch { + return DisplayRepresentation(stringLiteral: error.localizedDescription) + } + + let jsonString = String(data: jsonData, encoding: .utf8)! + + return DisplayRepresentation(stringLiteral: jsonString) + } +} diff --git a/WireGuard.xcodeproj/project.pbxproj b/WireGuard.xcodeproj/project.pbxproj index 8c97b5c..ad1ef98 100644 --- a/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard.xcodeproj/project.pbxproj @@ -207,6 +207,7 @@ 6FFACD2021E4D8D500E9A2A5 /* ParseError+WireGuardAppError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FFACD1E21E4D89600E9A2A5 /* ParseError+WireGuardAppError.swift */; }; A625F05529C4C627005EF23D /* GetPeers.swift in Sources */ = {isa = PBXBuildFile; fileRef = A625F05029C4C627005EF23D /* GetPeers.swift */; }; A6E361F829D8758500FFF234 /* AppIntents.strings in Resources */ = {isa = PBXBuildFile; fileRef = A6E361F729D8758500FFF234 /* AppIntents.strings */; }; + A6E361FC29D9AEEA00FFF234 /* BuildPeerConfigurationUpdate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6E361FB29D9AEEA00FFF234 /* BuildPeerConfigurationUpdate.swift */; }; A6E361FE29D9B18C00FFF234 /* TunnelsOptionsProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6E361FD29D9B18C00FFF234 /* TunnelsOptionsProvider.swift */; }; /* End PBXBuildFile section */ @@ -443,6 +444,7 @@ 6FFACD1E21E4D89600E9A2A5 /* ParseError+WireGuardAppError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ParseError+WireGuardAppError.swift"; sourceTree = ""; }; A625F05029C4C627005EF23D /* GetPeers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetPeers.swift; sourceTree = ""; }; A6E361F729D8758500FFF234 /* AppIntents.strings */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; path = AppIntents.strings; sourceTree = ""; }; + A6E361FB29D9AEEA00FFF234 /* BuildPeerConfigurationUpdate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BuildPeerConfigurationUpdate.swift; sourceTree = ""; }; A6E361FD29D9B18C00FFF234 /* TunnelsOptionsProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelsOptionsProvider.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -821,6 +823,7 @@ A625F04C29C4C627005EF23D /* WireguardAppIntents */ = { isa = PBXGroup; children = ( + A6E361FB29D9AEEA00FFF234 /* BuildPeerConfigurationUpdate.swift */, A625F05029C4C627005EF23D /* GetPeers.swift */, A6E361F729D8758500FFF234 /* AppIntents.strings */, A6E361FD29D9B18C00FFF234 /* TunnelsOptionsProvider.swift */, @@ -1456,6 +1459,7 @@ 6F919EC3218A2AE90023B400 /* ErrorPresenter.swift in Sources */, 6B62E45F220A6FA900EF34A6 /* PrivateDataConfirmation.swift in Sources */, 6F5A2B4821AFF49A0081EDD8 /* FileManager+Extension.swift in Sources */, + A6E361FC29D9AEEA00FFF234 /* BuildPeerConfigurationUpdate.swift in Sources */, 5F45418C21C2D48200994C13 /* TunnelEditKeyValueCell.swift in Sources */, 6FE254FB219C10800028284D /* ZipImporter.swift in Sources */, 585B107E2577E294004F691E /* PrivateKey.swift in Sources */, -- cgit v1.2.3-59-g8ed1b