Rename tunnelConfiguration to baseConfiguration

It's much less confusing. Migrate too.
This commit is contained in:
Davide De Rosa 2018-10-25 18:42:11 +02:00
parent 09685e6994
commit c73c2e3826
4 changed files with 27 additions and 15 deletions

View File

@ -41,7 +41,7 @@ class AppConstants {
} }
class VPN { class VPN {
static func tunnelConfiguration() -> TunnelKitProvider.Configuration { static func baseConfiguration() -> TunnelKitProvider.Configuration {
let sessionBuilder = SessionProxy.ConfigurationBuilder(ca: CryptoContainer(pem: "")) let sessionBuilder = SessionProxy.ConfigurationBuilder(ca: CryptoContainer(pem: ""))
var builder = TunnelKitProvider.ConfigurationBuilder(sessionConfiguration: sessionBuilder.build()) var builder = TunnelKitProvider.ConfigurationBuilder(sessionConfiguration: sessionBuilder.build())
builder.mtu = 1250 builder.mtu = 1250

View File

@ -47,11 +47,12 @@ extension ConnectionService {
// replace migration logic here // replace migration logic here
try migrateToWrappedSessionConfiguration(&json) try migrateToWrappedSessionConfiguration(&json)
try migrateToBaseConfiguration(&json)
return try JSONSerialization.data(withJSONObject: json, options: []) return try JSONSerialization.data(withJSONObject: json, options: [])
} }
private static func migrateToWrappedSessionConfiguration(_ json: inout [String: Any]) throws { static func migrateToWrappedSessionConfiguration(_ json: inout [String: Any]) throws {
guard let profiles = json["profiles"] as? [[String: Any]] else { guard let profiles = json["profiles"] as? [[String: Any]] else {
throw ApplicationError.migration throw ApplicationError.migration
} }
@ -76,6 +77,17 @@ extension ConnectionService {
json["profiles"] = newProfiles json["profiles"] = newProfiles
} }
static func migrateToBaseConfiguration(_ json: inout [String: Any]) throws {
guard var baseConfiguration = json["tunnelConfiguration"] as? [String: Any] else {
return
}
migrateSessionConfiguration(in: &baseConfiguration)
json["baseConfiguration"] = baseConfiguration
json.removeValue(forKey: "tunnelConfiguration")
}
// MARK: Helpers
private static func migrateSessionConfiguration(in map: inout [String: Any]) { private static func migrateSessionConfiguration(in map: inout [String: Any]) {
let scKeys = [ let scKeys = [
"cipher", "cipher",

View File

@ -40,7 +40,7 @@ class ConnectionService: Codable {
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case appGroup case appGroup
case tunnelConfiguration case baseConfiguration
case profiles case profiles
@ -55,7 +55,7 @@ class ConnectionService: Codable {
private let keychain: Keychain private let keychain: Keychain
var tunnelConfiguration: TunnelKitProvider.Configuration var baseConfiguration: TunnelKitProvider.Configuration
private var profiles: [String: ConnectionProfile] private var profiles: [String: ConnectionProfile]
@ -83,7 +83,7 @@ class ConnectionService: Codable {
weak var delegate: ConnectionServiceDelegate? weak var delegate: ConnectionServiceDelegate?
init(withAppGroup appGroup: String, tunnelConfiguration: TunnelKitProvider.Configuration) { init(withAppGroup appGroup: String, baseConfiguration: TunnelKitProvider.Configuration) {
guard let defaults = UserDefaults(suiteName: appGroup) else { guard let defaults = UserDefaults(suiteName: appGroup) else {
fatalError("No entitlements for group '\(appGroup)'") fatalError("No entitlements for group '\(appGroup)'")
} }
@ -91,7 +91,7 @@ class ConnectionService: Codable {
self.defaults = defaults self.defaults = defaults
keychain = Keychain(group: appGroup) keychain = Keychain(group: appGroup)
self.tunnelConfiguration = tunnelConfiguration self.baseConfiguration = baseConfiguration
profiles = [:] profiles = [:]
activeProfileId = nil activeProfileId = nil
preferences = EditablePreferences() preferences = EditablePreferences()
@ -109,7 +109,7 @@ class ConnectionService: Codable {
self.defaults = defaults self.defaults = defaults
keychain = Keychain(group: appGroup) keychain = Keychain(group: appGroup)
tunnelConfiguration = try container.decode(TunnelKitProvider.Configuration.self, forKey: .tunnelConfiguration) baseConfiguration = try container.decode(TunnelKitProvider.Configuration.self, forKey: .baseConfiguration)
let profilesArray = try container.decode([ConnectionProfileHolder].self, forKey: .profiles).map { $0.contained } let profilesArray = try container.decode([ConnectionProfileHolder].self, forKey: .profiles).map { $0.contained }
var profiles: [String: ConnectionProfile] = [:] var profiles: [String: ConnectionProfile] = [:]
profilesArray.forEach { profilesArray.forEach {
@ -126,7 +126,7 @@ class ConnectionService: Codable {
func encode(to encoder: Encoder) throws { func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self) var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(appGroup, forKey: .appGroup) try container.encode(appGroup, forKey: .appGroup)
try container.encode(tunnelConfiguration, forKey: .tunnelConfiguration) try container.encode(baseConfiguration, forKey: .baseConfiguration)
try container.encode(profiles.map { ConnectionProfileHolder($0.value) }, forKey: .profiles) try container.encode(profiles.map { ConnectionProfileHolder($0.value) }, forKey: .profiles)
try container.encodeIfPresent(activeProfileId, forKey: .activeProfileId) try container.encodeIfPresent(activeProfileId, forKey: .activeProfileId)
try container.encode(preferences, forKey: .preferences) try container.encode(preferences, forKey: .preferences)
@ -224,7 +224,7 @@ class ConnectionService: Codable {
} }
} }
let cfg = try profile.generate(from: tunnelConfiguration, preferences: preferences) let cfg = try profile.generate(from: baseConfiguration, preferences: preferences)
let protocolConfiguration = try cfg.generatedTunnelProtocol( let protocolConfiguration = try cfg.generatedTunnelProtocol(
withBundleIdentifier: GroupConstants.App.tunnelIdentifier, withBundleIdentifier: GroupConstants.App.tunnelIdentifier,
appGroup: appGroup, appGroup: appGroup,
@ -267,11 +267,11 @@ class ConnectionService: Codable {
} }
var vpnLog: String { var vpnLog: String {
return tunnelConfiguration.existingLog(in: appGroup) ?? "" return baseConfiguration.existingLog(in: appGroup) ?? ""
} }
var vpnLastError: TunnelKitProvider.ProviderError? { var vpnLastError: TunnelKitProvider.ProviderError? {
guard let key = tunnelConfiguration.lastErrorKey else { guard let key = baseConfiguration.lastErrorKey else {
return nil return nil
} }
guard let rawValue = defaults.string(forKey: key) else { guard let rawValue = defaults.string(forKey: key) else {
@ -281,7 +281,7 @@ class ConnectionService: Codable {
} }
func clearVpnLastError() { func clearVpnLastError() {
guard let key = tunnelConfiguration.lastErrorKey else { guard let key = baseConfiguration.lastErrorKey else {
return return
} }
defaults.removeObject(forKey: key) defaults.removeObject(forKey: key)

View File

@ -53,7 +53,7 @@ class TransientStore {
for: .documentDirectory, for: .documentDirectory,
appending: AppConstants.Store.serviceFilename appending: AppConstants.Store.serviceFilename
) )
let cfg = AppConstants.VPN.tunnelConfiguration() let cfg = AppConstants.VPN.baseConfiguration()
do { do {
ConnectionService.migrateJSON(at: servicePath, to: servicePath) ConnectionService.migrateJSON(at: servicePath, to: servicePath)
@ -63,12 +63,12 @@ class TransientStore {
log.verbose(content) log.verbose(content)
} }
service = try JSONDecoder().decode(ConnectionService.self, from: data) service = try JSONDecoder().decode(ConnectionService.self, from: data)
service.tunnelConfiguration = cfg service.baseConfiguration = cfg
} catch let e { } catch let e {
log.error("Could not decode service: \(e)") log.error("Could not decode service: \(e)")
service = ConnectionService( service = ConnectionService(
withAppGroup: GroupConstants.App.appGroup, withAppGroup: GroupConstants.App.appGroup,
tunnelConfiguration: cfg baseConfiguration: cfg
) )
// // hardcoded loading // // hardcoded loading