aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/TunnelViewModel.swift
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2019-02-15 16:20:04 +0530
committerRoopesh Chander <roop@roopc.net>2019-02-16 17:35:33 +0530
commita355232e094798dfcaf837742dbe064cd8fbc008 (patch)
treefa0f779cd0b130fac2f5d5ce1737692f6e62a141 /WireGuard/WireGuard/UI/TunnelViewModel.swift
parentConfTextStorage: lowercase only once (diff)
downloadwireguard-apple-a355232e094798dfcaf837742dbe064cd8fbc008.tar.xz
wireguard-apple-a355232e094798dfcaf837742dbe064cd8fbc008.zip
TunnelViewModel: Minor refactoring of exclude private IPs handling
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to '')
-rw-r--r--WireGuard/WireGuard/UI/TunnelViewModel.swift43
1 files changed, 23 insertions, 20 deletions
diff --git a/WireGuard/WireGuard/UI/TunnelViewModel.swift b/WireGuard/WireGuard/UI/TunnelViewModel.swift
index 0657a16d..1864cad6 100644
--- a/WireGuard/WireGuard/UI/TunnelViewModel.swift
+++ b/WireGuard/WireGuard/UI/TunnelViewModel.swift
@@ -401,39 +401,42 @@ class TunnelViewModel {
"193.0.0.0/8", "194.0.0.0/7", "196.0.0.0/6", "200.0.0.0/5", "208.0.0.0/4"
]
+ static func excludePrivateIPsFieldStates(isSinglePeer: Bool, allowedIPs: Set<String>) -> (shouldAllowExcludePrivateIPsControl: Bool, excludePrivateIPsValue: Bool) {
+ guard isSinglePeer else {
+ return (shouldAllowExcludePrivateIPsControl: false, excludePrivateIPsValue: false)
+ }
+ let allowedIPStrings = Set<String>(allowedIPs)
+ if allowedIPStrings.contains(TunnelViewModel.PeerData.ipv4DefaultRouteString) {
+ return (shouldAllowExcludePrivateIPsControl: true, excludePrivateIPsValue: false)
+ } else if allowedIPStrings.isSuperset(of: TunnelViewModel.PeerData.ipv4DefaultRouteModRFC1918String) {
+ return (shouldAllowExcludePrivateIPsControl: true, excludePrivateIPsValue: true)
+ } else {
+ return (shouldAllowExcludePrivateIPsControl: false, excludePrivateIPsValue: false)
+ }
+ }
+
func updateExcludePrivateIPsFieldState() {
if scratchpad.isEmpty {
populateScratchpad()
}
let allowedIPStrings = Set<String>(scratchpad[.allowedIPs].splitToArray(trimmingCharacters: .whitespacesAndNewlines))
+ (shouldAllowExcludePrivateIPsControl, excludePrivateIPsValue) = TunnelViewModel.PeerData.excludePrivateIPsFieldStates(isSinglePeer: numberOfPeers == 1, allowedIPs: allowedIPStrings)
shouldStronglyRecommendDNS = allowedIPStrings.contains(TunnelViewModel.PeerData.ipv4DefaultRouteString) || allowedIPStrings.isSuperset(of: TunnelViewModel.PeerData.ipv4DefaultRouteModRFC1918String)
- guard numberOfPeers == 1 else {
- shouldAllowExcludePrivateIPsControl = false
- excludePrivateIPsValue = false
- return
- }
- if allowedIPStrings.contains(TunnelViewModel.PeerData.ipv4DefaultRouteString) {
- shouldAllowExcludePrivateIPsControl = true
- excludePrivateIPsValue = false
- } else if allowedIPStrings.isSuperset(of: TunnelViewModel.PeerData.ipv4DefaultRouteModRFC1918String) {
- shouldAllowExcludePrivateIPsControl = true
- excludePrivateIPsValue = true
+ }
+
+ static func modifiedAllowedIPs(currentAllowedIPs: [String], excludePrivateIPs: Bool, dnsServers: [String]) -> [String] {
+ let ipv6Addresses = currentAllowedIPs.filter { $0.contains(":") }
+ if excludePrivateIPs {
+ return ipv6Addresses + TunnelViewModel.PeerData.ipv4DefaultRouteModRFC1918String + dnsServers
} else {
- shouldAllowExcludePrivateIPsControl = false
- excludePrivateIPsValue = false
+ return ipv6Addresses + [TunnelViewModel.PeerData.ipv4DefaultRouteString]
}
}
func excludePrivateIPsValueChanged(isOn: Bool, dnsServers: String) {
let allowedIPStrings = scratchpad[.allowedIPs].splitToArray(trimmingCharacters: .whitespacesAndNewlines)
let dnsServerStrings = dnsServers.splitToArray(trimmingCharacters: .whitespacesAndNewlines)
- let ipv6Addresses = allowedIPStrings.filter { $0.contains(":") }
- let modifiedAllowedIPStrings: [String]
- if isOn {
- modifiedAllowedIPStrings = ipv6Addresses + TunnelViewModel.PeerData.ipv4DefaultRouteModRFC1918String + dnsServerStrings
- } else {
- modifiedAllowedIPStrings = ipv6Addresses + [TunnelViewModel.PeerData.ipv4DefaultRouteString]
- }
+ let modifiedAllowedIPStrings = TunnelViewModel.PeerData.modifiedAllowedIPs(currentAllowedIPs: allowedIPStrings, excludePrivateIPs: isOn, dnsServers: dnsServerStrings)
scratchpad[.allowedIPs] = modifiedAllowedIPStrings.joined(separator: ", ")
validatedConfiguration = nil
excludePrivateIPsValue = isOn