Discard old migration code

This commit is contained in:
Davide De Rosa 2021-01-03 17:43:44 +01:00
parent 4575d660b1
commit ed8acb619f
3 changed files with 3 additions and 193 deletions

View File

@ -52,109 +52,6 @@ public extension ConnectionService {
return try JSONSerialization.data(withJSONObject: json, options: [])
}
func migrateProvidersToLowercase() {
// migrate providers to lowercase names
guard let files = try? FileManager.default.contentsOfDirectory(at: providersURL, includingPropertiesForKeys: nil, options: []) else {
log.debug("No providers to migrate")
return
}
for entry in files {
let filename = entry.lastPathComponent
// old names contain at least an uppercase letter
guard let _ = filename.rangeOfCharacter(from: .uppercaseLetters) else {
continue
}
log.debug("Migrating provider in \(filename) to new name")
do {
let data = try Data(contentsOf: entry)
guard var obj = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], let name = obj["name"] as? String else {
log.warning("Skipping provider \(filename), not a JSON or no 'name' key found")
continue
}
// replace name and overwrite
obj["name"] = name.lowercased()
let migratedData = try JSONSerialization.data(withJSONObject: obj, options: [])
try? migratedData.write(to: entry)
// rename file if it makes sense
let newEntry = entry.deletingLastPathComponent().appendingPathComponent(filename.lowercased())
try? FileManager.default.moveItem(at: entry, to: newEntry)
log.debug("Migrated provider: \(name)")
} catch let e {
log.warning("Unable to migrate provider \(filename): \(e)")
}
}
}
func migrateHostsToUUID() {
guard let files = try? FileManager.default.contentsOfDirectory(at: hostsURL, includingPropertiesForKeys: nil, options: []) else {
log.debug("No hosts to migrate")
return
}
// initialize titles mapping
hostTitles = [:]
for entry in files {
let filename = entry.lastPathComponent
guard filename.hasSuffix(".json") else {
continue
}
log.debug("Migrating host \(filename) to UUID-based")
do {
let data = try Data(contentsOf: entry)
guard var obj = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], let title = obj["title"] as? String else {
log.warning("Skipping host \(filename), not a JSON or no 'title' key found")
continue
}
// pick unique id
let uuid = UUID().uuidString
// remove title from JSON (will move to index)
obj["id"] = uuid
// obj.removeValue(forKey: "title")
// save mapping for later
hostTitles[uuid] = title
// migrate active profile if necessary (= it's a host)
if let key = activeProfileKey, key.context == .host && key.id == title {
activeProfileKey = ProfileKey(.host, uuid)
}
// replace name and overwrite
let migratedData = try JSONSerialization.data(withJSONObject: obj, options: [])
try? migratedData.write(to: entry)
let parent = entry.deletingLastPathComponent()
// rename file to UUID
let newFilename = "\(uuid).json"
let newEntry = parent.appendingPathComponent(newFilename)
try? FileManager.default.moveItem(at: entry, to: newEntry)
// rename associated .ovpn (if any)
let ovpnFilename = "\(title).ovpn"
let ovpnNewFilename = "\(uuid).ovpn"
try? FileManager.default.moveItem(
at: parent.appendingPathComponent(ovpnFilename),
to: parent.appendingPathComponent(ovpnNewFilename)
)
log.debug("Migrated host: \(filename) -> \(newFilename)")
} catch let e {
log.warning("Unable to migrate host \(filename): \(e)")
}
}
}
func migrateKeychainContext() {
for key in allProfileKeys() {
guard let profile = profile(withKey: key), let username = profile.username else {

View File

@ -356,39 +356,6 @@ public class ConnectionService: Codable {
return url.deletingPathExtension().lastPathComponent
}
func reloadHostProfilesFromConfigurationFiles() -> Bool {
var anyReloaded = false
for entry in cache {
guard entry.value.context == .host else {
continue
}
guard let host = profile(withKey: entry.key) as? HostConnectionProfile else {
log.warning("Host context but not a HostConnectionProfile?")
continue
}
guard let url = configurationURL(for: entry.key) else {
continue
}
// can fail due to passphrase (migration is non-interactive)
if let result = try? OpenVPN.ConfigurationParser.parsed(fromURL: url) {
host.parameters = OpenVPNTunnelProvider.ConfigurationBuilder(sessionConfiguration: result.configuration).build()
} else {
// fall back to the safer option
var builder = host.parameters.builder()
var sessionBuilder = builder.sessionConfiguration.builder()
sessionBuilder.routingPolicies = [.IPv4]
builder.sessionConfiguration = sessionBuilder.build()
host.parameters = builder.build()
}
cache[entry.key] = host
anyReloaded = true
}
return anyReloaded
}
// MARK: Profiles
public func hasProfiles() -> Bool {

View File

@ -72,33 +72,6 @@ public class TransientStore {
}
}
public static var didMigrateHostsRoutingPolicies: Bool {
get {
return UserDefaults.standard.bool(forKey: Keys.didMigrateHostsRoutingPolicies)
}
set {
UserDefaults.standard.set(newValue, forKey: Keys.didMigrateHostsRoutingPolicies)
}
}
public static var didMigrateDynamicProviders: Bool {
get {
return UserDefaults.standard.bool(forKey: Keys.didMigrateDynamicProviders)
}
set {
UserDefaults.standard.set(newValue, forKey: Keys.didMigrateDynamicProviders)
}
}
public static var didMigrateHostsToUUID: Bool {
get {
return UserDefaults.standard.bool(forKey: Keys.didMigrateHostsToUUID)
}
set {
UserDefaults.standard.set(newValue, forKey: Keys.didMigrateHostsToUUID)
}
}
public static var didMigrateKeychainContext: Bool {
get {
return UserDefaults.standard.bool(forKey: Keys.didMigrateKeychainContext)
@ -125,8 +98,6 @@ public class TransientStore {
Keys.masksPrivateData: true
])
TransientStore.migrateDocumentsToAppGroup()
// this must be graceful
ConnectionService.migrateJSON(from: TransientStore.serviceURL, to: TransientStore.serviceURL)
@ -147,29 +118,16 @@ public class TransientStore {
service.baseConfiguration = cfg
// pre-load migrations
if !TransientStore.didMigrateDynamicProviders {
service.migrateProvidersToLowercase()
TransientStore.didMigrateDynamicProviders = true
}
if !TransientStore.didMigrateHostsToUUID {
service.migrateHostsToUUID()
TransientStore.didMigrateHostsToUUID = true
}
service.loadProfiles()
// post-load migrations
#if os(iOS)
if !TransientStore.didMigrateKeychainContext {
service.migrateKeychainContext()
TransientStore.didMigrateKeychainContext = true
}
// post-load migrations
if !TransientStore.didMigrateHostsRoutingPolicies {
if service.reloadHostProfilesFromConfigurationFiles() {
service.saveProfiles()
}
TransientStore.didMigrateHostsRoutingPolicies = true
}
#endif
} catch let e {
log.error("Could not decode service: \(e)")
service = ConnectionService(
@ -178,15 +136,7 @@ public class TransientStore {
)
// fresh install, skip all migrations
TransientStore.didMigrateHostsRoutingPolicies = true
TransientStore.didMigrateDynamicProviders = true
TransientStore.didMigrateHostsToUUID = true
TransientStore.didMigrateKeychainContext = true
// // hardcoded loading
// _ = service.addProfile(ProviderConnectionProfile(name: .pia), credentials: nil)
// _ = service.addProfile(HostConnectionProfile(title: "vps"), credentials: Credentials(username: "foo", password: "bar"))
// service.activateProfile(service.profiles.first!)
}
service.observeVPNDataCount(milliseconds: GroupConstants.VPN.dataCountInterval)
}
@ -230,10 +180,6 @@ public class TransientStore {
}
private static func migratedDataIfNecessary(fromData data: Data) -> Data? {
guard !TransientStore.didMigrateHostsToUUID else {
return data
}
guard var json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {
return nil
}