aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/VPN/TunnelsManager.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/TunnelsManager.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/TunnelsManager.swift47
1 files changed, 11 insertions, 36 deletions
diff --git a/WireGuard/WireGuard/VPN/TunnelsManager.swift b/WireGuard/WireGuard/VPN/TunnelsManager.swift
index 8eb21123..d75e6c05 100644
--- a/WireGuard/WireGuard/VPN/TunnelsManager.swift
+++ b/WireGuard/WireGuard/VPN/TunnelsManager.swift
@@ -54,7 +54,9 @@ class TunnelsManager {
#endif
}
- func add(tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (TunnelContainer?, TunnelManagementError?) -> Void) {
+ func add(tunnelConfiguration: TunnelConfiguration,
+ activateOnDemandSetting: ActivateOnDemandSetting = ActivateOnDemandSetting.defaultSetting,
+ completionHandler: @escaping (TunnelContainer?, TunnelManagementError?) -> Void) {
let tunnelName = tunnelConfiguration.interface.name
if tunnelName.isEmpty {
completionHandler(nil, TunnelManagementError.tunnelAlreadyExistsWithThatName)
@@ -72,13 +74,7 @@ class TunnelsManager {
tunnelProviderManager.localizedDescription = tunnelName
tunnelProviderManager.isEnabled = true
- if (tunnelConfiguration.activationType == .activateManually) {
- tunnelProviderManager.onDemandRules = []
- tunnelProviderManager.isOnDemandEnabled = false
- } else {
- tunnelProviderManager.onDemandRules = onDemandRules(for: tunnelConfiguration.activationType)
- tunnelProviderManager.isOnDemandEnabled = true
- }
+ activateOnDemandSetting.apply(on: tunnelProviderManager)
tunnelProviderManager.saveToPreferences { [weak self] (error) in
defer { self?.isAddingTunnel = false }
@@ -114,7 +110,8 @@ class TunnelsManager {
}
}
- func modify(tunnel: TunnelContainer, with tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (TunnelManagementError?) -> Void) {
+ func modify(tunnel: TunnelContainer, tunnelConfiguration: TunnelConfiguration,
+ activateOnDemandSetting: ActivateOnDemandSetting, completionHandler: @escaping (TunnelManagementError?) -> Void) {
let tunnelName = tunnelConfiguration.interface.name
if tunnelName.isEmpty {
completionHandler(TunnelManagementError.tunnelAlreadyExistsWithThatName)
@@ -138,13 +135,7 @@ class TunnelsManager {
tunnelProviderManager.localizedDescription = tunnelName
tunnelProviderManager.isEnabled = true
- if (tunnelConfiguration.activationType == .activateManually) {
- tunnelProviderManager.onDemandRules = []
- tunnelProviderManager.isOnDemandEnabled = false
- } else {
- tunnelProviderManager.onDemandRules = onDemandRules(for: tunnelConfiguration.activationType)
- tunnelProviderManager.isOnDemandEnabled = true
- }
+ activateOnDemandSetting.apply(on: tunnelProviderManager)
tunnelProviderManager.saveToPreferences { [weak self] (error) in
defer { self?.isModifyingTunnel = false }
@@ -229,26 +220,6 @@ class TunnelsManager {
t.refreshConnectionStatus()
}
}
-
- func onDemandRules(for activationType: ActivationType) -> [NEOnDemandRule] {
- switch (activationType) {
- case .activateManually: return []
- case .useOnDemandOverWifiAndCellular:
- return [NEOnDemandRuleConnect()]
- case .useOnDemandOverWifiOnly:
- let connectOnWifiRule = NEOnDemandRuleConnect()
- connectOnWifiRule.interfaceTypeMatch = .wiFi
- let disconnectOnCellularRule = NEOnDemandRuleDisconnect()
- disconnectOnCellularRule.interfaceTypeMatch = .cellular
- return [connectOnWifiRule, disconnectOnCellularRule]
- case .useOnDemandOverCellularOnly:
- let connectOnCellularRule = NEOnDemandRuleConnect()
- connectOnCellularRule.interfaceTypeMatch = .cellular
- let disconnectOnWifiRule = NEOnDemandRuleDisconnect()
- disconnectOnWifiRule.interfaceTypeMatch = .wiFi
- return [connectOnCellularRule, disconnectOnWifiRule]
- }
- }
}
class TunnelContainer: NSObject {
@@ -275,6 +246,10 @@ class TunnelContainer: NSObject {
return (tunnelProvider.protocolConfiguration as! NETunnelProviderProtocol).tunnelConfiguration()
}
+ func activateOnDemandSetting() -> ActivateOnDemandSetting {
+ return ActivateOnDemandSetting(from: tunnelProvider)
+ }
+
func refreshConnectionStatus() {
let status = TunnelStatus(from: self.tunnelProvider.connection.status)
self.status = status