aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/VPN/ActivateOnDemandSetting.swift
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-11-12 14:02:09 +0530
committerRoopesh Chander <roop@roopc.net>2018-11-12 19:24:13 +0530
commitcc122d7463f98ab3f26d46ab849173ffd58d7951 (patch)
treec22e870308a68a26b4bdd7c0cd30bf5430dcc4ad /WireGuard/WireGuard/VPN/ActivateOnDemandSetting.swift
parentTunnelsManager: Support for on-demand rules (diff)
downloadwireguard-apple-cc122d7463f98ab3f26d46ab849173ffd58d7951.tar.xz
wireguard-apple-cc122d7463f98ab3f26d46ab849173ffd58d7951.zip
Model, Tunnels manager: Rewrite the model for VPN-on-demand
The VPN-on-demand settings should not be part of the tunnel configuration. Rather, the onDemandRules stored in the tunnel provider configuration serve as the one place where the VPN-on-demand settings are stored. Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to '')
-rw-r--r--WireGuard/WireGuard/VPN/ActivateOnDemandSetting.swift75
1 files changed, 75 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/VPN/ActivateOnDemandSetting.swift b/WireGuard/WireGuard/VPN/ActivateOnDemandSetting.swift
new file mode 100644
index 00000000..a2cbe00e
--- /dev/null
+++ b/WireGuard/WireGuard/VPN/ActivateOnDemandSetting.swift
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+import NetworkExtension
+
+struct ActivateOnDemandSetting {
+ var isActivateOnDemandEnabled: Bool
+ var activateOnDemandOption: ActivateOnDemandOption
+}
+
+enum ActivateOnDemandOption {
+ case none // Valid only when isActivateOnDemandEnabled is false
+ case useOnDemandOverWifiOrCellular
+ case useOnDemandOverWifiOnly
+ case useOnDemandOverCellularOnly
+}
+
+extension ActivateOnDemandSetting {
+ func apply(on tunnelProviderManager: NETunnelProviderManager) {
+ tunnelProviderManager.isOnDemandEnabled = isActivateOnDemandEnabled
+ let rules: [NEOnDemandRule]?
+ let connectRule = NEOnDemandRuleConnect()
+ let disconnectRule = NEOnDemandRuleDisconnect()
+ switch (activateOnDemandOption) {
+ case .none:
+ rules = nil
+ case .useOnDemandOverWifiOrCellular:
+ rules = [connectRule]
+ case .useOnDemandOverWifiOnly:
+ connectRule.interfaceTypeMatch = .wiFi
+ disconnectRule.interfaceTypeMatch = .cellular
+ rules = [connectRule, disconnectRule]
+ case .useOnDemandOverCellularOnly:
+ connectRule.interfaceTypeMatch = .cellular
+ disconnectRule.interfaceTypeMatch = .wiFi
+ rules = [connectRule, disconnectRule]
+ }
+ tunnelProviderManager.onDemandRules = rules
+ }
+
+ init(from tunnelProviderManager: NETunnelProviderManager) {
+ let rules = tunnelProviderManager.onDemandRules ?? []
+ let activateOnDemandOption: ActivateOnDemandOption
+ switch (rules.count) {
+ case 0:
+ activateOnDemandOption = .none
+ case 1:
+ let rule = rules[0]
+ precondition(rule.action == .connect)
+ activateOnDemandOption = .useOnDemandOverWifiOrCellular
+ case 2:
+ let connectRule = rules.first(where: { $0.action == .connect })!
+ let disconnectRule = rules.first(where: { $0.action == .disconnect })!
+ if (connectRule.interfaceTypeMatch == .wiFi && disconnectRule.interfaceTypeMatch == .cellular) {
+ activateOnDemandOption = .useOnDemandOverWifiOnly
+ } else if (connectRule.interfaceTypeMatch == .cellular && disconnectRule.interfaceTypeMatch == .wiFi) {
+ activateOnDemandOption = .useOnDemandOverCellularOnly
+ } else {
+ fatalError("Unexpected onDemandRules set on tunnel provider manager")
+ }
+ default:
+ fatalError("Unexpected number of onDemandRules set on tunnel provider manager")
+ }
+ self.activateOnDemandOption = activateOnDemandOption
+ if (activateOnDemandOption == .none) {
+ self.isActivateOnDemandEnabled = false
+ } else {
+ self.isActivateOnDemandEnabled = tunnelProviderManager.isOnDemandEnabled
+ }
+ }
+}
+
+extension ActivateOnDemandSetting {
+ static var defaultSetting = ActivateOnDemandSetting(isActivateOnDemandEnabled: false, activateOnDemandOption: .none)
+}