Removed SetTunnelStatus
Signed-off-by: Alessio Nossa <alessio.nossa@gmail.com>
This commit is contained in:
@@ -96,11 +96,11 @@
|
|||||||
<key>INIntentDefinitionNamespace</key>
|
<key>INIntentDefinitionNamespace</key>
|
||||||
<string>6NREiY</string>
|
<string>6NREiY</string>
|
||||||
<key>INIntentDefinitionSystemVersion</key>
|
<key>INIntentDefinitionSystemVersion</key>
|
||||||
<string>21C52</string>
|
<string>21G419</string>
|
||||||
<key>INIntentDefinitionToolsBuildVersion</key>
|
<key>INIntentDefinitionToolsBuildVersion</key>
|
||||||
<string>13C100</string>
|
<string>14C18</string>
|
||||||
<key>INIntentDefinitionToolsVersion</key>
|
<key>INIntentDefinitionToolsVersion</key>
|
||||||
<string>13.2.1</string>
|
<string>14.2</string>
|
||||||
<key>INIntents</key>
|
<key>INIntents</key>
|
||||||
<array>
|
<array>
|
||||||
<dict>
|
<dict>
|
||||||
@@ -447,271 +447,6 @@ Example
|
|||||||
<key>INIntentVerb</key>
|
<key>INIntentVerb</key>
|
||||||
<string>Do</string>
|
<string>Do</string>
|
||||||
</dict>
|
</dict>
|
||||||
<dict>
|
|
||||||
<key>INIntentCategory</key>
|
|
||||||
<string>toggle</string>
|
|
||||||
<key>INIntentConfigurable</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentDescription</key>
|
|
||||||
<string>Enables or disables a tunnel.
|
|
||||||
|
|
||||||
The action will not return an error if there are problems during the activation, it just launches the procedure.</string>
|
|
||||||
<key>INIntentDescriptionID</key>
|
|
||||||
<string>T7NEBw</string>
|
|
||||||
<key>INIntentEligibleForWidgets</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentLastParameterTag</key>
|
|
||||||
<integer>4</integer>
|
|
||||||
<key>INIntentManagedParameterCombinations</key>
|
|
||||||
<dict>
|
|
||||||
<key>state,tunnel,operation</key>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterCombinationTitle</key>
|
|
||||||
<string>${operation} ${tunnel} tunnel ${state}</string>
|
|
||||||
<key>INIntentParameterCombinationTitleID</key>
|
|
||||||
<string>juDVvv</string>
|
|
||||||
<key>INIntentParameterCombinationUpdatesLinked</key>
|
|
||||||
<true/>
|
|
||||||
</dict>
|
|
||||||
<key>tunnel,operation</key>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterCombinationTitle</key>
|
|
||||||
<string>${operation} ${tunnel} tunnel</string>
|
|
||||||
<key>INIntentParameterCombinationTitleID</key>
|
|
||||||
<string>kMEAI0</string>
|
|
||||||
</dict>
|
|
||||||
</dict>
|
|
||||||
<key>INIntentName</key>
|
|
||||||
<string>SetTunnelStatus</string>
|
|
||||||
<key>INIntentParameterCombinations</key>
|
|
||||||
<dict>
|
|
||||||
<key>state,tunnel,operation</key>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterCombinationIsLinked</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterCombinationTitle</key>
|
|
||||||
<string>${operation} ${tunnel} tunnel ${state}</string>
|
|
||||||
<key>INIntentParameterCombinationTitleID</key>
|
|
||||||
<string>SjQ7Ur</string>
|
|
||||||
</dict>
|
|
||||||
</dict>
|
|
||||||
<key>INIntentParameters</key>
|
|
||||||
<array>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterConfigurable</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterDisplayName</key>
|
|
||||||
<string>Tunnel</string>
|
|
||||||
<key>INIntentParameterDisplayNameID</key>
|
|
||||||
<string>mbhQcc</string>
|
|
||||||
<key>INIntentParameterDisplayPriority</key>
|
|
||||||
<integer>1</integer>
|
|
||||||
<key>INIntentParameterMetadata</key>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterMetadataCapitalization</key>
|
|
||||||
<string>Sentences</string>
|
|
||||||
<key>INIntentParameterMetadataDefaultValueID</key>
|
|
||||||
<string>iBD5fT</string>
|
|
||||||
</dict>
|
|
||||||
<key>INIntentParameterName</key>
|
|
||||||
<string>tunnel</string>
|
|
||||||
<key>INIntentParameterPromptDialogs</key>
|
|
||||||
<array>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterPromptDialogCustom</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterPromptDialogType</key>
|
|
||||||
<string>Configuration</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterPromptDialogCustom</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterPromptDialogType</key>
|
|
||||||
<string>Primary</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterPromptDialogCustom</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterPromptDialogFormatString</key>
|
|
||||||
<string>There are ${count} options matching ‘${tunnel}’.</string>
|
|
||||||
<key>INIntentParameterPromptDialogFormatStringID</key>
|
|
||||||
<string>73NvEc</string>
|
|
||||||
<key>INIntentParameterPromptDialogType</key>
|
|
||||||
<string>DisambiguationIntroduction</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterPromptDialogCustom</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterPromptDialogFormatString</key>
|
|
||||||
<string>Just to confirm, you wanted ‘${tunnel}’?</string>
|
|
||||||
<key>INIntentParameterPromptDialogFormatStringID</key>
|
|
||||||
<string>63TirR</string>
|
|
||||||
<key>INIntentParameterPromptDialogType</key>
|
|
||||||
<string>Confirmation</string>
|
|
||||||
</dict>
|
|
||||||
</array>
|
|
||||||
<key>INIntentParameterSupportsDynamicEnumeration</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterTag</key>
|
|
||||||
<integer>2</integer>
|
|
||||||
<key>INIntentParameterType</key>
|
|
||||||
<string>String</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterConfigurable</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterDisplayName</key>
|
|
||||||
<string>Operation</string>
|
|
||||||
<key>INIntentParameterDisplayNameID</key>
|
|
||||||
<string>U9YFTG</string>
|
|
||||||
<key>INIntentParameterDisplayPriority</key>
|
|
||||||
<integer>2</integer>
|
|
||||||
<key>INIntentParameterEnumType</key>
|
|
||||||
<string>Operation</string>
|
|
||||||
<key>INIntentParameterEnumTypeNamespace</key>
|
|
||||||
<string>6NREiY</string>
|
|
||||||
<key>INIntentParameterMetadata</key>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterMetadataDefaultValue</key>
|
|
||||||
<string>turn</string>
|
|
||||||
</dict>
|
|
||||||
<key>INIntentParameterName</key>
|
|
||||||
<string>operation</string>
|
|
||||||
<key>INIntentParameterPromptDialogs</key>
|
|
||||||
<array>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterPromptDialogCustom</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterPromptDialogType</key>
|
|
||||||
<string>Configuration</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterPromptDialogCustom</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterPromptDialogType</key>
|
|
||||||
<string>Primary</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterPromptDialogCustom</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterPromptDialogFormatString</key>
|
|
||||||
<string>There are ${count} options matching ‘${operation}’.</string>
|
|
||||||
<key>INIntentParameterPromptDialogFormatStringID</key>
|
|
||||||
<string>SALmBF</string>
|
|
||||||
<key>INIntentParameterPromptDialogType</key>
|
|
||||||
<string>DisambiguationIntroduction</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterPromptDialogCustom</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterPromptDialogFormatString</key>
|
|
||||||
<string>Just to confirm, you wanted ‘${operation}’?</string>
|
|
||||||
<key>INIntentParameterPromptDialogFormatStringID</key>
|
|
||||||
<string>vB13mD</string>
|
|
||||||
<key>INIntentParameterPromptDialogType</key>
|
|
||||||
<string>Confirmation</string>
|
|
||||||
</dict>
|
|
||||||
</array>
|
|
||||||
<key>INIntentParameterTag</key>
|
|
||||||
<integer>4</integer>
|
|
||||||
<key>INIntentParameterType</key>
|
|
||||||
<string>Integer</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterConfigurable</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentParameterDisplayName</key>
|
|
||||||
<string>State</string>
|
|
||||||
<key>INIntentParameterDisplayNameID</key>
|
|
||||||
<string>sLIh6r</string>
|
|
||||||
<key>INIntentParameterDisplayPriority</key>
|
|
||||||
<integer>3</integer>
|
|
||||||
<key>INIntentParameterEnumType</key>
|
|
||||||
<string>State</string>
|
|
||||||
<key>INIntentParameterEnumTypeNamespace</key>
|
|
||||||
<string>6NREiY</string>
|
|
||||||
<key>INIntentParameterMetadata</key>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterMetadataDefaultValue</key>
|
|
||||||
<string>on</string>
|
|
||||||
</dict>
|
|
||||||
<key>INIntentParameterName</key>
|
|
||||||
<string>state</string>
|
|
||||||
<key>INIntentParameterRelationship</key>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentParameterRelationshipParentName</key>
|
|
||||||
<string>operation</string>
|
|
||||||
<key>INIntentParameterRelationshipPredicateName</key>
|
|
||||||
<string>EnumHasExactValue</string>
|
|
||||||
<key>INIntentParameterRelationshipPredicateValue</key>
|
|
||||||
<string>turn</string>
|
|
||||||
</dict>
|
|
||||||
<key>INIntentParameterTag</key>
|
|
||||||
<integer>1</integer>
|
|
||||||
<key>INIntentParameterType</key>
|
|
||||||
<string>Integer</string>
|
|
||||||
</dict>
|
|
||||||
</array>
|
|
||||||
<key>INIntentResponse</key>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentResponseCodes</key>
|
|
||||||
<array>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentResponseCodeName</key>
|
|
||||||
<string>success</string>
|
|
||||||
<key>INIntentResponseCodeSuccess</key>
|
|
||||||
<true/>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentResponseCodeName</key>
|
|
||||||
<string>failure</string>
|
|
||||||
</dict>
|
|
||||||
</array>
|
|
||||||
<key>INIntentResponseLastParameterTag</key>
|
|
||||||
<integer>1</integer>
|
|
||||||
<key>INIntentResponseParameters</key>
|
|
||||||
<array>
|
|
||||||
<dict>
|
|
||||||
<key>INIntentResponseParameterConfigurable</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentResponseParameterCustomDisambiguation</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentResponseParameterDisplayName</key>
|
|
||||||
<string>State</string>
|
|
||||||
<key>INIntentResponseParameterDisplayNameID</key>
|
|
||||||
<string>TXqy56</string>
|
|
||||||
<key>INIntentResponseParameterDisplayPriority</key>
|
|
||||||
<integer>1</integer>
|
|
||||||
<key>INIntentResponseParameterEnumType</key>
|
|
||||||
<string>State</string>
|
|
||||||
<key>INIntentResponseParameterEnumTypeNamespace</key>
|
|
||||||
<string>6NREiY</string>
|
|
||||||
<key>INIntentResponseParameterName</key>
|
|
||||||
<string>state</string>
|
|
||||||
<key>INIntentResponseParameterSupportsResolution</key>
|
|
||||||
<true/>
|
|
||||||
<key>INIntentResponseParameterTag</key>
|
|
||||||
<integer>1</integer>
|
|
||||||
<key>INIntentResponseParameterType</key>
|
|
||||||
<string>Integer</string>
|
|
||||||
</dict>
|
|
||||||
</array>
|
|
||||||
</dict>
|
|
||||||
<key>INIntentTitle</key>
|
|
||||||
<string>Set Tunnel Status</string>
|
|
||||||
<key>INIntentTitleID</key>
|
|
||||||
<string>HULmDn</string>
|
|
||||||
<key>INIntentType</key>
|
|
||||||
<string>Custom</string>
|
|
||||||
<key>INIntentVerb</key>
|
|
||||||
<string>Toggle</string>
|
|
||||||
</dict>
|
|
||||||
</array>
|
</array>
|
||||||
<key>INTypes</key>
|
<key>INTypes</key>
|
||||||
<array/>
|
<array/>
|
||||||
|
|||||||
@@ -5,10 +5,6 @@ import Foundation
|
|||||||
import NetworkExtension
|
import NetworkExtension
|
||||||
import os.log
|
import os.log
|
||||||
|
|
||||||
#if os(iOS)
|
|
||||||
import Intents
|
|
||||||
#endif
|
|
||||||
|
|
||||||
protocol TunnelsManagerListDelegate: AnyObject {
|
protocol TunnelsManagerListDelegate: AnyObject {
|
||||||
func tunnelAdded(at index: Int)
|
func tunnelAdded(at index: Int)
|
||||||
func tunnelModified(at index: Int)
|
func tunnelModified(at index: Int)
|
||||||
@@ -311,12 +307,6 @@ class TunnelsManager {
|
|||||||
}
|
}
|
||||||
#elseif os(iOS)
|
#elseif os(iOS)
|
||||||
(tunnelProviderManager.protocolConfiguration as? NETunnelProviderProtocol)?.destroyConfigurationReference()
|
(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
|
#else
|
||||||
#error("Unimplemented")
|
#error("Unimplemented")
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
|
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
import Intents
|
|
||||||
|
|
||||||
class MainViewController: UISplitViewController {
|
class MainViewController: UISplitViewController {
|
||||||
|
|
||||||
@@ -97,25 +96,10 @@ extension MainViewController {
|
|||||||
if let tunnel = tunnelsManager.tunnel(named: tunnelName) {
|
if let tunnel = tunnelsManager.tunnel(named: tunnelName) {
|
||||||
tunnelsListVC.showTunnelDetail(for: tunnel, animated: false)
|
tunnelsListVC.showTunnelDetail(for: tunnel, animated: false)
|
||||||
if shouldToggleStatus {
|
if shouldToggleStatus {
|
||||||
|
|
||||||
let intent = SetTunnelStatusIntent()
|
|
||||||
intent.tunnel = tunnel.name
|
|
||||||
intent.operation = .turn
|
|
||||||
|
|
||||||
if tunnel.status == .inactive {
|
if tunnel.status == .inactive {
|
||||||
tunnelsManager.startActivation(of: tunnel)
|
tunnelsManager.startActivation(of: tunnel)
|
||||||
intent.state = .on
|
|
||||||
} else if tunnel.status == .active {
|
} else if tunnel.status == .active {
|
||||||
tunnelsManager.startDeactivation(of: tunnel)
|
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)")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -388,18 +388,6 @@ extension TunnelDetailTableViewController {
|
|||||||
cell.onSwitchToggled = { [weak self] isOn in
|
cell.onSwitchToggled = { [weak self] isOn in
|
||||||
guard let self = self else { return }
|
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 {
|
if self.tunnel.hasOnDemandRules {
|
||||||
self.tunnelsManager.setOnDemandEnabled(isOn, on: self.tunnel) { error in
|
self.tunnelsManager.setOnDemandEnabled(isOn, on: self.tunnel) { error in
|
||||||
if error == nil && !isOn {
|
if error == nil && !isOn {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
import UIKit
|
import UIKit
|
||||||
import MobileCoreServices
|
import MobileCoreServices
|
||||||
import UserNotifications
|
import UserNotifications
|
||||||
import Intents
|
|
||||||
|
|
||||||
class TunnelsListTableViewController: UIViewController {
|
class TunnelsListTableViewController: UIViewController {
|
||||||
|
|
||||||
@@ -318,19 +317,6 @@ extension TunnelsListTableViewController: UITableViewDataSource {
|
|||||||
cell.tunnel = tunnel
|
cell.tunnel = tunnel
|
||||||
cell.onSwitchToggled = { [weak self] isOn in
|
cell.onSwitchToggled = { [weak self] isOn in
|
||||||
guard let self = self, let tunnelsManager = self.tunnelsManager else { return }
|
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 {
|
if tunnel.hasOnDemandRules {
|
||||||
tunnelsManager.setOnDemandEnabled(isOn, on: tunnel) { error in
|
tunnelsManager.setOnDemandEnabled(isOn, on: tunnel) { error in
|
||||||
if error == nil && !isOn {
|
if error == nil && !isOn {
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ class IntentHandler: INExtension {
|
|||||||
|
|
||||||
override func handler(for intent: INIntent) -> Any {
|
override func handler(for intent: INIntent) -> Any {
|
||||||
guard intent is GetPeersIntent ||
|
guard intent is GetPeersIntent ||
|
||||||
intent is UpdateConfigurationIntent ||
|
intent is UpdateConfigurationIntent else {
|
||||||
intent is SetTunnelStatusIntent else {
|
|
||||||
fatalError("Unhandled intent type: \(intent)")
|
fatalError("Unhandled intent type: \(intent)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,8 +15,6 @@ class IntentHandling: NSObject {
|
|||||||
|
|
||||||
var onTunnelsManagerReady: ((TunnelsManager) -> Void)?
|
var onTunnelsManagerReady: ((TunnelsManager) -> Void)?
|
||||||
|
|
||||||
var onTunnelStatusActivationReturn: ((Bool) -> Void)?
|
|
||||||
|
|
||||||
override init() {
|
override init() {
|
||||||
super.init()
|
super.init()
|
||||||
|
|
||||||
@@ -29,8 +27,6 @@ class IntentHandling: NSObject {
|
|||||||
case .success(let tunnelsManager):
|
case .success(let tunnelsManager):
|
||||||
self.tunnelsManager = tunnelsManager
|
self.tunnelsManager = tunnelsManager
|
||||||
|
|
||||||
self.tunnelsManager?.activationDelegate = self
|
|
||||||
|
|
||||||
self.onTunnelsManagerReady?(tunnelsManager)
|
self.onTunnelsManagerReady?(tunnelsManager)
|
||||||
self.onTunnelsManagerReady = nil
|
self.onTunnelsManagerReady = nil
|
||||||
}
|
}
|
||||||
@@ -174,129 +170,3 @@ extension IntentHandling: UpdateConfigurationIntentHandling {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension IntentHandling: SetTunnelStatusIntentHandling {
|
|
||||||
|
|
||||||
@available(iOSApplicationExtension 14.0, *)
|
|
||||||
func provideTunnelOptionsCollection(for intent: SetTunnelStatusIntent, with completion: @escaping (INObjectCollection<NSString>?, Error?) -> Void) {
|
|
||||||
|
|
||||||
self.allTunnelNames { tunnelsNames in
|
|
||||||
let tunnelsNamesObjects = (tunnelsNames ?? []).map { NSString(string: $0) }
|
|
||||||
|
|
||||||
let objectCollection = INObjectCollection(items: tunnelsNamesObjects)
|
|
||||||
completion(objectCollection, nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func handle(intent: SetTunnelStatusIntent, completion: @escaping (SetTunnelStatusIntentResponse) -> Void) {
|
|
||||||
guard let tunnelName = intent.tunnel else {
|
|
||||||
return completion(SetTunnelStatusIntentResponse(code: .failure, userActivity: nil))
|
|
||||||
}
|
|
||||||
|
|
||||||
let setTunnelStatusResultBlock: (Bool) -> Void = { result in
|
|
||||||
if result {
|
|
||||||
completion(SetTunnelStatusIntentResponse(code: .success, userActivity: nil))
|
|
||||||
} else {
|
|
||||||
completion(SetTunnelStatusIntentResponse(code: .failure, userActivity: nil))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let updateStatusBlock: (TunnelsManager) -> Void = { tunnelsManager in
|
|
||||||
guard let tunnel = tunnelsManager.tunnel(named: tunnelName) else {
|
|
||||||
completion(SetTunnelStatusIntentResponse(code: .failure, userActivity: nil))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let operation = intent.operation
|
|
||||||
let isOn: Bool
|
|
||||||
|
|
||||||
if operation == .toggle {
|
|
||||||
switch tunnel.status {
|
|
||||||
case .inactive:
|
|
||||||
isOn = true
|
|
||||||
case .active:
|
|
||||||
isOn = false
|
|
||||||
default:
|
|
||||||
wg_log(.error, message: "SetTunnelStatusIntent action cannot be executed due to the current state of \(tunnelName) tunnel: \(tunnel.status)")
|
|
||||||
completion(SetTunnelStatusIntentResponse(code: .failure, userActivity: nil))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if operation == .turn {
|
|
||||||
if (tunnel.status == .inactive) || (tunnel.status == .active) {
|
|
||||||
isOn = (intent.state == .on)
|
|
||||||
|
|
||||||
if (isOn && tunnel.status == .active) || (!isOn && tunnel.status == .inactive) {
|
|
||||||
wg_log(.debug, message: "Tunnel \(tunnelName) is already \(isOn ? "active" : "inactive")")
|
|
||||||
completion(SetTunnelStatusIntentResponse(code: .success, userActivity: nil))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
wg_log(.error, message: "SetTunnelStatusIntent action cannot be executed due to the current state of \(tunnelName) tunnel: \(tunnel.status)")
|
|
||||||
completion(SetTunnelStatusIntentResponse(code: .failure, userActivity: nil))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
wg_log(.error, message: "Invalid 'operation' option in action")
|
|
||||||
completion(SetTunnelStatusIntentResponse(code: .failure, userActivity: nil))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if tunnel.hasOnDemandRules {
|
|
||||||
tunnelsManager.setOnDemandEnabled(isOn, on: tunnel) { error in
|
|
||||||
guard error == nil else {
|
|
||||||
wg_log(.error, message: "Error setting OnDemand status: \(error!.localizedDescription).")
|
|
||||||
completion(SetTunnelStatusIntentResponse(code: .failure, userActivity: nil))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isOn {
|
|
||||||
tunnelsManager.startDeactivation(of: tunnel)
|
|
||||||
}
|
|
||||||
|
|
||||||
completion(SetTunnelStatusIntentResponse(code: .success, userActivity: nil))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if isOn {
|
|
||||||
self.onTunnelStatusActivationReturn = setTunnelStatusResultBlock
|
|
||||||
tunnelsManager.startActivation(of: tunnel)
|
|
||||||
} else {
|
|
||||||
tunnelsManager.startDeactivation(of: tunnel)
|
|
||||||
completion(SetTunnelStatusIntentResponse(code: .success, userActivity: nil))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let tunnelsManager = tunnelsManager {
|
|
||||||
updateStatusBlock(tunnelsManager)
|
|
||||||
} else {
|
|
||||||
if onTunnelsManagerReady != nil {
|
|
||||||
wg_log(.error, message: "Overriding onTunnelsManagerReady action in allTunnelPeers function. This should not happen.")
|
|
||||||
}
|
|
||||||
onTunnelsManagerReady = updateStatusBlock
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
extension IntentHandling: TunnelsManagerActivationDelegate {
|
|
||||||
func tunnelActivationAttemptFailed(tunnel: TunnelContainer, error: TunnelsManagerActivationAttemptError) {
|
|
||||||
wg_log(.error, message: "Tunnel Activation Attempt Failed with error: \(error.localizedDescription)")
|
|
||||||
self.onTunnelStatusActivationReturn?(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func tunnelActivationAttemptSucceeded(tunnel: TunnelContainer) {
|
|
||||||
// Nothing to do, we wait tunnelActivationSucceeded to be sure all activation logic has been executed
|
|
||||||
}
|
|
||||||
|
|
||||||
func tunnelActivationFailed(tunnel: TunnelContainer, error: TunnelsManagerActivationError) {
|
|
||||||
wg_log(.error, message: "Tunnel Activation Failed with error: \(error.localizedDescription)")
|
|
||||||
self.onTunnelStatusActivationReturn?(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func tunnelActivationSucceeded(tunnel: TunnelContainer) {
|
|
||||||
self.onTunnelStatusActivationReturn?(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user