diff options
author | Alessio Nossa <alessio.nossa@gmail.com> | 2022-02-01 17:51:18 +0100 |
---|---|---|
committer | Alessio Nossa <alessio.nossa@gmail.com> | 2022-02-01 20:14:21 +0100 |
commit | c29787f898da0c27a16b0e61149f34a8f031bffd (patch) | |
tree | fbbb7cc5f69341d4b065af8b223cf65291401520 | |
parent | Implemented SetTunnelStatus intent (diff) | |
download | wireguard-apple-c29787f898da0c27a16b0e61149f34a8f031bffd.tar.xz wireguard-apple-c29787f898da0c27a16b0e61149f34a8f031bffd.zip |
WireguardApp: iOS: Added Siri shortcuts donations for SetTunnelStatus intent
Signed-off-by: Alessio Nossa <alessio.nossa@gmail.com>
4 files changed, 53 insertions, 0 deletions
diff --git a/Sources/WireGuardApp/Tunnel/TunnelsManager.swift b/Sources/WireGuardApp/Tunnel/TunnelsManager.swift index 7751e54..fbf29c4 100644 --- a/Sources/WireGuardApp/Tunnel/TunnelsManager.swift +++ b/Sources/WireGuardApp/Tunnel/TunnelsManager.swift @@ -5,6 +5,10 @@ import Foundation import NetworkExtension import os.log +#if os(iOS) +import Intents +#endif + protocol TunnelsManagerListDelegate: AnyObject { func tunnelAdded(at index: Int) func tunnelModified(at index: Int) @@ -307,6 +311,12 @@ class TunnelsManager { } #elseif os(iOS) (tunnelProviderManager.protocolConfiguration as? NETunnelProviderProtocol)?.destroyConfigurationReference() + + INInteraction.delete(with: "com.wireguard.intents.tunnel.\(tunnel.name)") { error in + if let error = error { + wg_log(.error, message: "Error deleting donated interactions for tunnel \(tunnel.name): \(error.localizedDescription)") + } + } #else #error("Unimplemented") #endif diff --git a/Sources/WireGuardApp/UI/iOS/ViewController/MainViewController.swift b/Sources/WireGuardApp/UI/iOS/ViewController/MainViewController.swift index 6aab559..74a8149 100644 --- a/Sources/WireGuardApp/UI/iOS/ViewController/MainViewController.swift +++ b/Sources/WireGuardApp/UI/iOS/ViewController/MainViewController.swift @@ -2,6 +2,7 @@ // Copyright © 2018-2021 WireGuard LLC. All Rights Reserved. import UIKit +import Intents class MainViewController: UISplitViewController { @@ -96,10 +97,25 @@ extension MainViewController { if let tunnel = tunnelsManager.tunnel(named: tunnelName) { tunnelsListVC.showTunnelDetail(for: tunnel, animated: false) if shouldToggleStatus { + + let intent = SetTunnelStatusIntent() + intent.tunnel = tunnel.name + intent.operation = .turn + if tunnel.status == .inactive { tunnelsManager.startActivation(of: tunnel) + intent.state = .on } else if tunnel.status == .active { tunnelsManager.startDeactivation(of: tunnel) + intent.state = .off + } + + let interaction = INInteraction(intent: intent, response: nil) + interaction.groupIdentifier = "com.wireguard.intents.tunnel.\(tunnel.name)" + interaction.donate { error in + if let error = error { + wg_log(.error, message: "Error donating interaction for SetTunnelStatusIntent: \(error.localizedDescription)") + } } } } diff --git a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelDetailTableViewController.swift b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelDetailTableViewController.swift index 196de0c..0361d44 100644 --- a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelDetailTableViewController.swift +++ b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelDetailTableViewController.swift @@ -2,6 +2,7 @@ // Copyright © 2018-2021 WireGuard LLC. All Rights Reserved. import UIKit +import Intents class TunnelDetailTableViewController: UITableViewController { @@ -387,6 +388,18 @@ extension TunnelDetailTableViewController { cell.onSwitchToggled = { [weak self] isOn in guard let self = self else { return } + let intent = SetTunnelStatusIntent() + intent.tunnel = self.tunnel.name + intent.operation = .turn + intent.state = isOn ? .on : .off + let interaction = INInteraction(intent: intent, response: nil) + interaction.groupIdentifier = "com.wireguard.intents.tunnel.\(self.tunnel.name)" + interaction.donate { error in + if let error = error { + wg_log(.error, message: "Error donating interaction for SetTunnelStatusIntent: \(error.localizedDescription)") + } + } + if self.tunnel.hasOnDemandRules { self.tunnelsManager.setOnDemandEnabled(isOn, on: self.tunnel) { error in if error == nil && !isOn { diff --git a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift index 85e64ce..c5132a2 100644 --- a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift +++ b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift @@ -4,6 +4,7 @@ import UIKit import MobileCoreServices import UserNotifications +import Intents class TunnelsListTableViewController: UIViewController { @@ -317,6 +318,19 @@ extension TunnelsListTableViewController: UITableViewDataSource { cell.tunnel = tunnel cell.onSwitchToggled = { [weak self] isOn in guard let self = self, let tunnelsManager = self.tunnelsManager else { return } + + let intent = SetTunnelStatusIntent() + intent.tunnel = tunnel.name + intent.operation = .turn + intent.state = isOn ? .on : .off + let interaction = INInteraction(intent: intent, response: nil) + interaction.groupIdentifier = "com.wireguard.intents.tunnel.\(tunnel.name)" + interaction.donate { error in + if let error = error { + wg_log(.error, message: "Error donating interaction for SetTunnelStatusIntent: \(error.localizedDescription)") + } + } + if tunnel.hasOnDemandRules { tunnelsManager.setOnDemandEnabled(isOn, on: tunnel) { error in if error == nil && !isOn { |