aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-12-21 18:58:06 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2018-12-21 18:58:06 +0100
commit78b38a4eba1e1d47b59d8e95fc4883e5edbab340 (patch)
tree199bfcfcf044062f2596b99a02b4725b9a31fe92 /WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
parentGet rid of superflous isActivateOnDemandEnabled key (diff)
downloadwireguard-apple-78b38a4eba1e1d47b59d8e95fc4883e5edbab340.tar.xz
wireguard-apple-78b38a4eba1e1d47b59d8e95fc4883e5edbab340.zip
Simplify versioning of stored data
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to '')
-rw-r--r--WireGuard/Shared/NETunnelProviderProtocol+Extension.swift36
1 files changed, 10 insertions, 26 deletions
diff --git a/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift b/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
index 3a9bc38b..7d6e412d 100644
--- a/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
+++ b/WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
@@ -3,20 +3,17 @@
import NetworkExtension
-let tunnelConfigurationVersion = 2
-
extension NETunnelProviderProtocol {
enum Keys: String {
- case tunnelConfiguration = "TunnelConfiguration"
- case tunnelConfigurationVersion = "TunnelConfigurationVersion"
+ case wgQuickConfig = "WgQuickConfigV1"
}
var tunnelConfiguration: TunnelConfiguration? {
migrateConfigurationIfNeeded()
let tunnelConfigurationData: Data?
- if let configurationDictionary = providerConfiguration?[Keys.tunnelConfiguration.rawValue] {
+ if let configurationDictionary = providerConfiguration?[Keys.wgQuickConfig.rawValue] {
tunnelConfigurationData = try? JSONSerialization.data(withJSONObject: configurationDictionary, options: [])
} else {
tunnelConfigurationData = nil
@@ -36,10 +33,7 @@ extension NETunnelProviderProtocol {
let appId = Bundle.main.bundleIdentifier!
providerBundleIdentifier = "\(appId).network-extension"
- providerConfiguration = [
- Keys.tunnelConfiguration.rawValue: tunnelConfigDictionary,
- Keys.tunnelConfigurationVersion.rawValue: tunnelConfigurationVersion
- ]
+ providerConfiguration = [ Keys.wgQuickConfig.rawValue: tunnelConfigDictionary ]
let endpoints = tunnelConfiguration.peers.compactMap { $0.endpoint }
if endpoints.count == 1 {
@@ -60,20 +54,13 @@ extension NETunnelProviderProtocol {
@discardableResult
func migrateConfigurationIfNeeded() -> Bool {
- guard let providerConfiguration = providerConfiguration else { return false }
- guard let configurationVersion = providerConfiguration[Keys.tunnelConfigurationVersion.rawValue] as? Int ?? providerConfiguration["tunnelConfigurationVersion"] as? Int else { return false }
-
- if configurationVersion < tunnelConfigurationVersion {
- switch configurationVersion {
- case 1:
- migrateFromConfigurationV1()
- default:
- fatalError("No migration from configuration version \(configurationVersion) exists.")
- }
- return true
+ guard let configurationVersion = providerConfiguration?["tunnelConfigurationVersion"] as? Int else { return false }
+ if configurationVersion == 1 {
+ migrateFromConfigurationV1()
+ } else {
+ fatalError("No migration from configuration version \(configurationVersion) exists.")
}
-
- return false
+ return true
}
private func migrateFromConfigurationV1() {
@@ -82,10 +69,7 @@ extension NETunnelProviderProtocol {
guard let tunnelConfigData = try? JSONEncoder().encode(configuration.migrated) else { return }
guard let tunnelConfigDictionary = try? JSONSerialization.jsonObject(with: tunnelConfigData, options: .allowFragments) else { return }
- providerConfiguration = [
- Keys.tunnelConfiguration.rawValue: tunnelConfigDictionary,
- Keys.tunnelConfigurationVersion.rawValue: tunnelConfigurationVersion
- ]
+ providerConfiguration = [ Keys.wgQuickConfig.rawValue: tunnelConfigDictionary ]
}
}