|
| 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 UpdateTunnelConfiguration: AppIntent { |
| 8 | + |
| 9 | + static var title = LocalizedStringResource("updateTunnelConfigurationIntentName", table: "AppIntents") |
| 10 | + static var description = IntentDescription( |
| 11 | + LocalizedStringResource("updateTunnelConfigurationDescription", table: "AppIntents") |
| 12 | + ) |
| 13 | + |
| 14 | + @Parameter( |
| 15 | + title: LocalizedStringResource("updateTunnelConfigurationIntentTunnelParameterTitle", table: "AppIntents"), |
| 16 | + optionsProvider: TunnelsOptionsProvider() |
| 17 | + ) |
| 18 | + var tunnelName: String |
| 19 | + |
| 20 | + @Parameter( |
| 21 | + title: LocalizedStringResource("updateTunnelConfigurationIntentPeersParameterTitle", table: "AppIntents"), |
| 22 | + optionsProvider: AppIntentsPeerOptionsProvider() |
| 23 | + ) |
| 24 | + var peers: [AppIntentsPeer]? |
| 25 | + |
| 26 | + @Parameter( |
| 27 | + title: LocalizedStringResource("updateTunnelConfigurationIntentMergeParameterTitle", table: "AppIntents"), |
| 28 | + default: true |
| 29 | + ) |
| 30 | + var mergeConfiguration: Bool |
| 31 | + |
| 32 | + @Dependency |
| 33 | + var tunnelsManager: TunnelsManager |
| 34 | + |
| 35 | + func perform() async throws -> some IntentResult { |
| 36 | + guard let peers else { throw AppIntentConfigurationUpdateError.missingPeerParameter } |
| 37 | + |
| 38 | + guard let tunnelContainer = tunnelsManager.tunnel(named: tunnelName) else { |
| 39 | + throw AppIntentConfigurationUpdateError.wrongTunnel(name: tunnelName) |
| 40 | + } |
| 41 | + |
| 42 | + guard let tunnelConfiguration = tunnelContainer.tunnelConfiguration else { |
| 43 | + throw AppIntentConfigurationUpdateError.missingConfiguration |
| 44 | + } |
| 45 | + |
| 46 | + let newConfiguration = try buildNewConfiguration(from: tunnelConfiguration, peersUpdates: peers, mergeChanges: mergeConfiguration) |
| 47 | + |
| 48 | + do { |
| 49 | + try await tunnelsManager.modify(tunnel: tunnelContainer, tunnelConfiguration: newConfiguration, onDemandOption: tunnelContainer.onDemandOption) |
| 50 | + } catch { |
| 51 | + wg_log(.error, message: error.localizedDescription) |
| 52 | + throw error |
| 53 | + } |
| 54 | + |
| 55 | + wg_log(.debug, message: "Updated configuration of tunnel \(tunnelName)") |
| 56 | + |
| 57 | + return .result() |
| 58 | + } |
| 59 | + |
| 60 | + static var parameterSummary: some ParameterSummary { |
| 61 | + Summary("updateTunnelConfigurationIntentSummary \(\.$tunnelName)", table: "AppIntents") { |
| 62 | + \.$peers |
| 63 | + \.$mergeConfiguration |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private func buildNewConfiguration(from oldConfiguration: TunnelConfiguration, peersUpdates: [AppIntentsPeer], mergeChanges: Bool) throws -> TunnelConfiguration { |
| 68 | + var peers = oldConfiguration.peers |
| 69 | + |
| 70 | + for peerUpdate in peersUpdates { |
| 71 | + let peerIndex: Array<PeerConfiguration>.Index |
| 72 | + if let foundIndex = peers.firstIndex(where: { $0.publicKey.base64Key == peerUpdate.publicKey }) { |
| 73 | + peerIndex = foundIndex |
| 74 | + if mergeChanges == false { |
| 75 | + peers[peerIndex] = PeerConfiguration(publicKey: peers[peerIndex].publicKey) |
| 76 | + } |
| 77 | + } else { |
| 78 | + wg_log(.debug, message: "Failed to find peer \(peerUpdate.publicKey) in tunnel with name \(tunnelName). Adding it.") |
| 79 | + |
| 80 | + guard let pubKeyEncoded = PublicKey(base64Key: peerUpdate.publicKey) else { |
| 81 | + throw AppIntentConfigurationUpdateError.malformedPublicKey(key: peerUpdate.publicKey) |
| 82 | + } |
| 83 | + let newPeerConfig = PeerConfiguration(publicKey: pubKeyEncoded) |
| 84 | + peerIndex = peers.endIndex |
| 85 | + peers.append(newPeerConfig) |
| 86 | + } |
| 87 | + |
| 88 | + if let endpointString = peerUpdate.endpoint { |
| 89 | + if let newEntpoint = Endpoint(from: endpointString) { |
| 90 | + peers[peerIndex].endpoint = newEntpoint |
| 91 | + } else { |
| 92 | + wg_log(.debug, message: "Failed to convert \(endpointString) to Endpoint") |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + let newConfiguration = TunnelConfiguration(name: oldConfiguration.name, interface: oldConfiguration.interface, peers: peers) |
| 98 | + return newConfiguration |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) |
| 103 | +struct AppIntentsPeerOptionsProvider: DynamicOptionsProvider { |
| 104 | + |
| 105 | + func results() async throws -> ItemCollection<AppIntentsPeer> { |
| 106 | + // The error thrown here is not displayed correctly to the user. A Feedback |
| 107 | + // has been opened (FB12098463). |
| 108 | + throw AppIntentConfigurationUpdateError.peerOptionsUnavailable |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) |
| 113 | +enum AppIntentConfigurationUpdateError: Swift.Error, CustomLocalizedStringResourceConvertible { |
| 114 | + case wrongTunnel(name: String) |
| 115 | + case missingConfiguration |
| 116 | + case peerOptionsUnavailable |
| 117 | + case missingPeerParameter |
| 118 | + case malformedPublicKey(key: String) |
| 119 | + |
| 120 | + var localizedStringResource: LocalizedStringResource { |
| 121 | + switch self { |
| 122 | + case .wrongTunnel(let name): |
| 123 | + return LocalizedStringResource("wireguardAppIntentsWrongTunnelError \(name)", table: "AppIntents") |
| 124 | + case .missingConfiguration: |
| 125 | + return LocalizedStringResource("wireguardAppIntentsMissingConfigurationError", table: "AppIntents") |
| 126 | + case .peerOptionsUnavailable: |
| 127 | + return LocalizedStringResource("updateTunnelConfigurationIntentPeerOptionsUnavailableError", table: "AppIntents") |
| 128 | + case .missingPeerParameter: |
| 129 | + return LocalizedStringResource("updateTunnelConfigurationIntentMissingPeerParameterError", table: "AppIntents") |
| 130 | + case .malformedPublicKey(let malformedKey): |
| 131 | + return LocalizedStringResource("updateTunnelConfigurationIntentMalformedPublicKeyError \(malformedKey)", table: "AppIntents") |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments