aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2019-02-23 14:24:30 +0530
committerJason A. Donenfeld <Jason@zx2c4.com>2019-03-18 06:46:55 +0100
commit3767a1298362caccd9844e9693428e7b1cc4b9f6 (patch)
treefeb202bf8f2c0b322303fae0f952bee3310fe689
parentmacOS: Localize tooltips (diff)
downloadwireguard-apple-3767a1298362caccd9844e9693428e7b1cc4b9f6.tar.xz
wireguard-apple-3767a1298362caccd9844e9693428e7b1cc4b9f6.zip
on-demand: Simplify OS-specific code for interface type selection
Previously, the enum values themselves were different for iOS and macOS. With this commit, the enum values are common, and only how they're handled is specific to iOS and macOS. Signed-off-by: Roopesh Chander <roop@roopc.net>
-rw-r--r--WireGuard/WireGuard/Tunnel/ActivateOnDemandSetting.swift67
-rw-r--r--WireGuard/WireGuard/UI/TunnelViewModel.swift35
-rw-r--r--WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift6
-rw-r--r--WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift6
4 files changed, 43 insertions, 71 deletions
diff --git a/WireGuard/WireGuard/Tunnel/ActivateOnDemandSetting.swift b/WireGuard/WireGuard/Tunnel/ActivateOnDemandSetting.swift
index 38359da4..0cb3f981 100644
--- a/WireGuard/WireGuard/Tunnel/ActivateOnDemandSetting.swift
+++ b/WireGuard/WireGuard/Tunnel/ActivateOnDemandSetting.swift
@@ -10,18 +10,19 @@ struct ActivateOnDemandSetting {
enum ActivateOnDemandOption {
case none // Valid only when isActivateOnDemandEnabled is false
- case useOnDemandOverWiFiOnly
- #if os(iOS)
- case useOnDemandOverWiFiOrCellular
- case useOnDemandOverCellularOnly
- #elseif os(macOS)
- case useOnDemandOverWiFiOrEthernet
- case useOnDemandOverEthernetOnly
- #else
- #error("Unimplemented")
- #endif
+ case wiFiInterfaceOnly
+ case nonWiFiInterfaceOnly
+ case anyInterface
}
+#if os(iOS)
+private let nonWiFiInterfaceType: NEOnDemandRuleInterfaceType = .cellular
+#elseif os(macOS)
+private let nonWiFiInterfaceType: NEOnDemandRuleInterfaceType = .ethernet
+#else
+#error("Unimplemented")
+#endif
+
extension ActivateOnDemandSetting {
func apply(on tunnelProviderManager: NETunnelProviderManager) {
tunnelProviderManager.isOnDemandEnabled = isActivateOnDemandEnabled
@@ -31,48 +32,22 @@ extension ActivateOnDemandSetting {
switch activateOnDemandOption {
case .none:
rules = nil
- #if os(iOS)
- case .useOnDemandOverWiFiOrCellular:
- rules = [connectRule]
- case .useOnDemandOverWiFiOnly:
+ case .wiFiInterfaceOnly:
connectRule.interfaceTypeMatch = .wiFi
- disconnectRule.interfaceTypeMatch = .cellular
+ disconnectRule.interfaceTypeMatch = nonWiFiInterfaceType
rules = [connectRule, disconnectRule]
- case .useOnDemandOverCellularOnly:
- connectRule.interfaceTypeMatch = .cellular
+ case .nonWiFiInterfaceOnly:
+ connectRule.interfaceTypeMatch = nonWiFiInterfaceType
disconnectRule.interfaceTypeMatch = .wiFi
rules = [connectRule, disconnectRule]
- #elseif os(macOS)
- case .useOnDemandOverWiFiOrEthernet:
+ case .anyInterface:
rules = [connectRule]
- case .useOnDemandOverWiFiOnly:
- connectRule.interfaceTypeMatch = .wiFi
- disconnectRule.interfaceTypeMatch = .ethernet
- rules = [connectRule, disconnectRule]
- case .useOnDemandOverEthernetOnly:
- connectRule.interfaceTypeMatch = .ethernet
- disconnectRule.interfaceTypeMatch = .wiFi
- rules = [connectRule, disconnectRule]
- #else
- #error("Unimplemented")
- #endif
}
tunnelProviderManager.onDemandRules = rules
}
init(from tunnelProviderManager: NETunnelProviderManager) {
let rules = tunnelProviderManager.onDemandRules ?? []
- #if os(iOS)
- let otherInterfaceType: NEOnDemandRuleInterfaceType = .cellular
- let useWiFiOrOtherOption: ActivateOnDemandOption = .useOnDemandOverWiFiOrCellular
- let useOtherOnlyOption: ActivateOnDemandOption = .useOnDemandOverCellularOnly
- #elseif os(macOS)
- let otherInterfaceType: NEOnDemandRuleInterfaceType = .ethernet
- let useWiFiOrOtherOption: ActivateOnDemandOption = .useOnDemandOverWiFiOrEthernet
- let useOtherOnlyOption: ActivateOnDemandOption = .useOnDemandOverEthernetOnly
- #else
- #error("Unimplemented")
- #endif
let activateOnDemandOption: ActivateOnDemandOption
switch rules.count {
case 0:
@@ -80,14 +55,14 @@ extension ActivateOnDemandSetting {
case 1:
let rule = rules[0]
precondition(rule.action == .connect)
- activateOnDemandOption = useWiFiOrOtherOption
+ activateOnDemandOption = .anyInterface
case 2:
let connectRule = rules.first(where: { $0.action == .connect })!
let disconnectRule = rules.first(where: { $0.action == .disconnect })!
- if connectRule.interfaceTypeMatch == .wiFi && disconnectRule.interfaceTypeMatch == otherInterfaceType {
- activateOnDemandOption = .useOnDemandOverWiFiOnly
- } else if connectRule.interfaceTypeMatch == otherInterfaceType && disconnectRule.interfaceTypeMatch == .wiFi {
- activateOnDemandOption = useOtherOnlyOption
+ if connectRule.interfaceTypeMatch == .wiFi && disconnectRule.interfaceTypeMatch == nonWiFiInterfaceType {
+ activateOnDemandOption = .wiFiInterfaceOnly
+ } else if connectRule.interfaceTypeMatch == nonWiFiInterfaceType && disconnectRule.interfaceTypeMatch == .wiFi {
+ activateOnDemandOption = .nonWiFiInterfaceOnly
} else {
fatalError("Unexpected onDemandRules set on tunnel provider manager")
}
diff --git a/WireGuard/WireGuard/UI/TunnelViewModel.swift b/WireGuard/WireGuard/UI/TunnelViewModel.swift
index e6b00a10..8a9b0070 100644
--- a/WireGuard/WireGuard/UI/TunnelViewModel.swift
+++ b/WireGuard/WireGuard/UI/TunnelViewModel.swift
@@ -627,21 +627,24 @@ extension TunnelViewModel {
switch activateOnDemandOption {
case .none:
return tr("tunnelOnDemandOptionOff")
- case .useOnDemandOverWiFiOnly:
+ case .wiFiInterfaceOnly:
return tr("tunnelOnDemandOptionWiFiOnly")
- #if os(iOS)
- case .useOnDemandOverWiFiOrCellular:
- return tr("tunnelOnDemandOptionWiFiOrCellular")
- case .useOnDemandOverCellularOnly:
+ case .nonWiFiInterfaceOnly:
+ #if os(iOS)
return tr("tunnelOnDemandOptionCellularOnly")
- #elseif os(macOS)
- case .useOnDemandOverWiFiOrEthernet:
- return tr("tunnelOnDemandOptionWiFiOrEthernet")
- case .useOnDemandOverEthernetOnly:
+ #elseif os(macOS)
return tr("tunnelOnDemandOptionEthernetOnly")
- #else
- #error("Unimplemented")
- #endif
+ #else
+ #error("Unimplemented")
+ #endif
+ case .anyInterface:
+ #if os(iOS)
+ return tr("tunnelOnDemandOptionWiFiOrCellular")
+ #elseif os(macOS)
+ return tr("tunnelOnDemandOptionWiFiOrEthernet")
+ #else
+ #error("Unimplemented")
+ #endif
}
}
@@ -658,13 +661,7 @@ extension TunnelViewModel {
}
static func defaultActivateOnDemandOption() -> ActivateOnDemandOption {
- #if os(iOS)
- return .useOnDemandOverWiFiOrCellular
- #elseif os(macOS)
- return .useOnDemandOverWiFiOrEthernet
- #else
- #error("Unimplemented")
- #endif
+ return .anyInterface
}
}
diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift
index a15d5fe3..1a20ffec 100644
--- a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift
+++ b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift
@@ -44,9 +44,9 @@ class TunnelEditTableViewController: UITableViewController {
]
let activateOnDemandOptions: [ActivateOnDemandOption] = [
- .useOnDemandOverWiFiOrCellular,
- .useOnDemandOverWiFiOnly,
- .useOnDemandOverCellularOnly
+ .anyInterface,
+ .wiFiInterfaceOnly,
+ .nonWiFiInterfaceOnly
]
let tunnelsManager: TunnelsManager
diff --git a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift
index 6e4a23a5..2c1c5384 100644
--- a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift
+++ b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift
@@ -82,9 +82,9 @@ class TunnelEditViewController: NSViewController {
let activateOnDemandOptions: [ActivateOnDemandOption] = [
.none,
- .useOnDemandOverWiFiOrEthernet,
- .useOnDemandOverWiFiOnly,
- .useOnDemandOverEthernetOnly
+ .anyInterface,
+ .wiFiInterfaceOnly,
+ .nonWiFiInterfaceOnly
]
let tunnelsManager: TunnelsManager