Skip to content

Commit df77456

Browse files
committed
Implement GetPeers AppIntent
Signed-off-by: Alessio Nossa <[email protected]>
1 parent 2952206 commit df77456

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

Sources/WireGuardApp/UI/iOS/AppDelegate.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import UIKit
55
import os.log
66
import Intents
7+
import AppIntents
78

89
@UIApplicationMain
910
class AppDelegate: UIResponder, UIApplicationDelegate {
@@ -47,6 +48,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
4748

4849
tunnelsManager.activationDelegate = self.mainVC
4950

51+
if #available(iOS 16.0, *) {
52+
AppDependencyManager.shared.add(dependency: tunnelsManager)
53+
}
54+
5055
NotificationCenter.default.post(name: AppDelegate.tunnelsManagerReadyNotificationName,
5156
object: self,
5257
userInfo: nil)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
3+
4+
// App Intents Common
5+
"wireguardAppIntentsWrongTunnelError %@" = "Cannot find a tunnel with the name \"%1$@\" :(((";
6+
"wireguardAppIntentsMissingConfigurationError" = "The selected tunnel has no configuration.";
7+
8+
// Get peers Intent
9+
"getPeersIntentName" = "Get Peers";
10+
"getPeersIntentDescription" = "Get list of public keys of peers in the selected configuration";
11+
"getPeersIntentTunnelParameterTitle" = "Tunnel";
12+
"getPeersIntentSummary ${tunnelName}" = "Get peers of ${tunnelName}";
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
3+
4+
import Foundation
5+
import AppIntents
6+
7+
@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *)
8+
struct GetPeers: AppIntent {
9+
10+
static var title = LocalizedStringResource("getPeersIntentName", table: "AppIntents")
11+
static var description = IntentDescription(
12+
LocalizedStringResource("getPeersIntentDescription", table: "AppIntents")
13+
)
14+
15+
@Parameter(
16+
title: LocalizedStringResource("getPeersIntentTunnelParameterTitle", table: "AppIntents"),
17+
optionsProvider: TunnelsOptionsProvider()
18+
)
19+
var tunnelName: String
20+
21+
@Dependency
22+
var tunnelsManager: TunnelsManager
23+
24+
func perform() async throws -> some ReturnsValue {
25+
guard let tunnelContainer = tunnelsManager.tunnel(named: tunnelName) else {
26+
throw GetPeersIntentError.wrongTunnel(name: tunnelName)
27+
}
28+
29+
guard let tunnelConfiguration = tunnelContainer.tunnelConfiguration else {
30+
throw GetPeersIntentError.missingConfiguration
31+
}
32+
33+
let publicKeys = tunnelConfiguration.peers.map { $0.publicKey.base64Key }
34+
return .result(value: publicKeys)
35+
}
36+
37+
static var parameterSummary: some ParameterSummary {
38+
Summary("getPeersIntentSummary \(\.$tunnelName)", table: "AppIntents")
39+
}
40+
}
41+
42+
@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *)
43+
enum GetPeersIntentError: Swift.Error, CustomLocalizedStringResourceConvertible {
44+
case wrongTunnel(name: String)
45+
case missingConfiguration
46+
47+
var localizedStringResource: LocalizedStringResource {
48+
switch self {
49+
case .wrongTunnel(let name):
50+
return LocalizedStringResource("wireguardAppIntentsWrongTunnelError \(name)", table: "AppIntents")
51+
case .missingConfiguration:
52+
return LocalizedStringResource("wireguardAppIntentsMissingConfigurationError", table: "AppIntents")
53+
}
54+
}
55+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
3+
4+
import AppIntents
5+
6+
@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *)
7+
struct TunnelsOptionsProvider: DynamicOptionsProvider {
8+
@Dependency
9+
var tunnelsManager: TunnelsManager
10+
11+
func results() async throws -> [String] {
12+
let tunnelsNames = tunnelsManager.mapTunnels { $0.name }
13+
return tunnelsNames
14+
}
15+
}

WireGuard.xcodeproj/project.pbxproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
6FFA5DA021958ECC0001E2F7 /* ErrorNotifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FFA5D9F21958ECC0001E2F7 /* ErrorNotifier.swift */; };
206206
6FFA5DA42197085D0001E2F7 /* ActivateOnDemandOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FFA5DA32197085D0001E2F7 /* ActivateOnDemandOption.swift */; };
207207
6FFACD2021E4D8D500E9A2A5 /* ParseError+WireGuardAppError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FFACD1E21E4D89600E9A2A5 /* ParseError+WireGuardAppError.swift */; };
208+
A625F05529C4C627005EF23D /* GetPeers.swift in Sources */ = {isa = PBXBuildFile; fileRef = A625F05029C4C627005EF23D /* GetPeers.swift */; };
208209
A64A79DC27A5411700F15B34 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FF3526E21C23FA10008484E /* Logger.swift */; };
209210
A64A79DD27A5411E00F15B34 /* ringlogger.c in Sources */ = {isa = PBXBuildFile; fileRef = 6FF3526C21C23F960008484E /* ringlogger.c */; };
210211
A64A79DE27A541F500F15B34 /* TunnelsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774EE21722D97006A79B3 /* TunnelsManager.swift */; };
@@ -239,6 +240,8 @@
239240
A6B8051C27A44F770088E750 /* Intents.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A6B8051B27A44F770088E750 /* Intents.framework */; };
240241
A6B8051F27A44F770088E750 /* IntentHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6B8051E27A44F770088E750 /* IntentHandler.swift */; };
241242
A6B8052327A44F770088E750 /* WireGuardIntentsExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = A6B8051A27A44F770088E750 /* WireGuardIntentsExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
243+
A6E361F829D8758500FFF234 /* AppIntents.strings in Resources */ = {isa = PBXBuildFile; fileRef = A6E361F729D8758500FFF234 /* AppIntents.strings */; };
244+
A6E361FE29D9B18C00FFF234 /* TunnelsOptionsProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6E361FD29D9B18C00FFF234 /* TunnelsOptionsProvider.swift */; };
242245
/* End PBXBuildFile section */
243246

244247
/* Begin PBXContainerItemProxy section */
@@ -480,6 +483,7 @@
480483
6FFA5D9F21958ECC0001E2F7 /* ErrorNotifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorNotifier.swift; sourceTree = "<group>"; };
481484
6FFA5DA32197085D0001E2F7 /* ActivateOnDemandOption.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivateOnDemandOption.swift; sourceTree = "<group>"; };
482485
6FFACD1E21E4D89600E9A2A5 /* ParseError+WireGuardAppError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ParseError+WireGuardAppError.swift"; sourceTree = "<group>"; };
486+
A625F05029C4C627005EF23D /* GetPeers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetPeers.swift; sourceTree = "<group>"; };
483487
A64A79D727A462FC00F15B34 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
484488
A64A79D827A48A8E00F15B34 /* WireGuardIntentsExtension-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WireGuardIntentsExtension-Bridging-Header.h"; sourceTree = "<group>"; };
485489
A64A79F827A5462900F15B34 /* Intents.intentdefinition */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; path = Intents.intentdefinition; sourceTree = "<group>"; };
@@ -488,6 +492,8 @@
488492
A6B8051B27A44F770088E750 /* Intents.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Intents.framework; path = System/Library/Frameworks/Intents.framework; sourceTree = SDKROOT; };
489493
A6B8051E27A44F770088E750 /* IntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntentHandler.swift; sourceTree = "<group>"; };
490494
A6B8052727A454150088E750 /* WireGuardIntentsExtension_iOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = WireGuardIntentsExtension_iOS.entitlements; sourceTree = "<group>"; };
495+
A6E361F729D8758500FFF234 /* AppIntents.strings */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; path = AppIntents.strings; sourceTree = "<group>"; };
496+
A6E361FD29D9B18C00FFF234 /* TunnelsOptionsProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelsOptionsProvider.swift; sourceTree = "<group>"; };
491497
/* End PBXFileReference section */
492498

493499
/* Begin PBXFrameworksBuildPhase section */
@@ -820,6 +826,7 @@
820826
6FF4AC0B211EC46F002C96EB = {
821827
isa = PBXGroup;
822828
children = (
829+
A625F04C29C4C627005EF23D /* WireguardAppIntents */,
823830
6F70E20C221058DF008BDFB4 /* InfoPlist.strings */,
824831
6FE1765421C90BBE002690EA /* Localizable.strings */,
825832
6F5D0C432183B4A4000F85AD /* Shared */,
@@ -874,6 +881,17 @@
874881
name = Frameworks;
875882
sourceTree = "<group>";
876883
};
884+
A625F04C29C4C627005EF23D /* WireguardAppIntents */ = {
885+
isa = PBXGroup;
886+
children = (
887+
A625F05029C4C627005EF23D /* GetPeers.swift */,
888+
A6E361F729D8758500FFF234 /* AppIntents.strings */,
889+
A6E361FD29D9B18C00FFF234 /* TunnelsOptionsProvider.swift */,
890+
);
891+
name = WireguardAppIntents;
892+
path = Sources/WireguardAppIntents;
893+
sourceTree = "<group>";
894+
};
877895
A6B8051D27A44F770088E750 /* WireGuardIntentsExtension */ = {
878896
isa = PBXGroup;
879897
children = (
@@ -1193,6 +1211,7 @@
11931211
6FE1765621C90BBE002690EA /* Localizable.strings in Resources */,
11941212
6FF4AC22211EC472002C96EB /* LaunchScreen.storyboard in Resources */,
11951213
6F919EDA218C65C50023B400 /* wireguard_doc_logo_44x58.png in Resources */,
1214+
A6E361F829D8758500FFF234 /* AppIntents.strings in Resources */,
11961215
6FF4AC1F211EC472002C96EB /* Assets.xcassets in Resources */,
11971216
6F919EDB218C65C50023B400 /* wireguard_doc_logo_64x64.png in Resources */,
11981217
6F919EDC218C65C50023B400 /* wireguard_doc_logo_320x320.png in Resources */,
@@ -1524,8 +1543,10 @@
15241543
6FDB6D18224CC05A00EE4BC3 /* LogViewController.swift in Sources */,
15251544
6FFA5D952194454A0001E2F7 /* NETunnelProviderProtocol+Extension.swift in Sources */,
15261545
5F4541A921C451D100994C13 /* TunnelStatus.swift in Sources */,
1546+
A625F05529C4C627005EF23D /* GetPeers.swift in Sources */,
15271547
6F8F0D7422267AD2000E8335 /* ChevronCell.swift in Sources */,
15281548
6F61F1E921B932F700483816 /* WireGuardAppError.swift in Sources */,
1549+
A6E361FE29D9B18C00FFF234 /* TunnelsOptionsProvider.swift in Sources */,
15291550
6F7774E2217181B1006A79B3 /* AppDelegate.swift in Sources */,
15301551
6FDEF80021863C0100D8FBF6 /* ioapi.c in Sources */,
15311552
6F7F7E5F21C7D74B00527607 /* TunnelErrors.swift in Sources */,

0 commit comments

Comments
 (0)