Simplify versioning of stored data

This commit is contained in:
Jason A. Donenfeld 2018-12-21 18:58:06 +01:00
parent df698658d2
commit d540c1811c
1 changed files with 10 additions and 26 deletions

View File

@ -3,20 +3,17 @@
import NetworkExtension import NetworkExtension
let tunnelConfigurationVersion = 2
extension NETunnelProviderProtocol { extension NETunnelProviderProtocol {
enum Keys: String { enum Keys: String {
case tunnelConfiguration = "TunnelConfiguration" case wgQuickConfig = "WgQuickConfigV1"
case tunnelConfigurationVersion = "TunnelConfigurationVersion"
} }
var tunnelConfiguration: TunnelConfiguration? { var tunnelConfiguration: TunnelConfiguration? {
migrateConfigurationIfNeeded() migrateConfigurationIfNeeded()
let tunnelConfigurationData: Data? let tunnelConfigurationData: Data?
if let configurationDictionary = providerConfiguration?[Keys.tunnelConfiguration.rawValue] { if let configurationDictionary = providerConfiguration?[Keys.wgQuickConfig.rawValue] {
tunnelConfigurationData = try? JSONSerialization.data(withJSONObject: configurationDictionary, options: []) tunnelConfigurationData = try? JSONSerialization.data(withJSONObject: configurationDictionary, options: [])
} else { } else {
tunnelConfigurationData = nil tunnelConfigurationData = nil
@ -36,10 +33,7 @@ extension NETunnelProviderProtocol {
let appId = Bundle.main.bundleIdentifier! let appId = Bundle.main.bundleIdentifier!
providerBundleIdentifier = "\(appId).network-extension" providerBundleIdentifier = "\(appId).network-extension"
providerConfiguration = [ providerConfiguration = [ Keys.wgQuickConfig.rawValue: tunnelConfigDictionary ]
Keys.tunnelConfiguration.rawValue: tunnelConfigDictionary,
Keys.tunnelConfigurationVersion.rawValue: tunnelConfigurationVersion
]
let endpoints = tunnelConfiguration.peers.compactMap { $0.endpoint } let endpoints = tunnelConfiguration.peers.compactMap { $0.endpoint }
if endpoints.count == 1 { if endpoints.count == 1 {
@ -60,20 +54,13 @@ extension NETunnelProviderProtocol {
@discardableResult @discardableResult
func migrateConfigurationIfNeeded() -> Bool { func migrateConfigurationIfNeeded() -> Bool {
guard let providerConfiguration = providerConfiguration else { return false } guard let configurationVersion = providerConfiguration?["tunnelConfigurationVersion"] as? Int else { return false }
guard let configurationVersion = providerConfiguration[Keys.tunnelConfigurationVersion.rawValue] as? Int ?? providerConfiguration["tunnelConfigurationVersion"] as? Int else { return false } if configurationVersion == 1 {
migrateFromConfigurationV1()
if configurationVersion < tunnelConfigurationVersion { } else {
switch configurationVersion { fatalError("No migration from configuration version \(configurationVersion) exists.")
case 1:
migrateFromConfigurationV1()
default:
fatalError("No migration from configuration version \(configurationVersion) exists.")
}
return true
} }
return true
return false
} }
private func migrateFromConfigurationV1() { private func migrateFromConfigurationV1() {
@ -82,10 +69,7 @@ extension NETunnelProviderProtocol {
guard let tunnelConfigData = try? JSONEncoder().encode(configuration.migrated) else { return } guard let tunnelConfigData = try? JSONEncoder().encode(configuration.migrated) else { return }
guard let tunnelConfigDictionary = try? JSONSerialization.jsonObject(with: tunnelConfigData, options: .allowFragments) else { return } guard let tunnelConfigDictionary = try? JSONSerialization.jsonObject(with: tunnelConfigData, options: .allowFragments) else { return }
providerConfiguration = [ providerConfiguration = [ Keys.wgQuickConfig.rawValue: tunnelConfigDictionary ]
Keys.tunnelConfiguration.rawValue: tunnelConfigDictionary,
Keys.tunnelConfigurationVersion.rawValue: tunnelConfigurationVersion
]
} }
} }