From 821393af70f70c0ff22dd127a21bc274e19e1cd1 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sat, 27 Oct 2018 00:08:48 +0200 Subject: [PATCH] Refactor configurations in service extension Reuse same directories of ConnectionService for storing configuration files. --- .../Organizer/WizardHostViewController.swift | 4 +- .../Scenes/ServiceViewController.swift | 2 +- Passepartout.xcodeproj/project.pbxproj | 8 +- .../ConnectionService+Configurations.swift | 49 +++++++++++ .../Sources/Model/ConnectionService.swift | 14 ++-- .../Model/ProfileConfigurationFactory.swift | 82 ------------------- 6 files changed, 64 insertions(+), 95 deletions(-) create mode 100644 Passepartout/Sources/Model/ConnectionService+Configurations.swift delete mode 100644 Passepartout/Sources/Model/ProfileConfigurationFactory.swift diff --git a/Passepartout-iOS/Scenes/Organizer/WizardHostViewController.swift b/Passepartout-iOS/Scenes/Organizer/WizardHostViewController.swift index dd2e731d..2fb94b15 100644 --- a/Passepartout-iOS/Scenes/Organizer/WizardHostViewController.swift +++ b/Passepartout-iOS/Scenes/Organizer/WizardHostViewController.swift @@ -159,8 +159,8 @@ class WizardHostViewController: UITableViewController, TableModelHost, Wizard { } if let url = parsedFile?.url { do { - let savedUrl = try ProfileConfigurationFactory.shared.save(url: url, for: profile) - log.debug("Associated .ovpn configuration file to profile '\(profile.id)': \(savedUrl)") + let savedURL = try TransientStore.shared.service.save(configurationURL: url, for: profile) + log.debug("Associated .ovpn configuration file to profile '\(profile.id)': \(savedURL)") } catch let e { log.error("Could not associate .ovpn configuration file to profile: \(e)") } diff --git a/Passepartout-iOS/Scenes/ServiceViewController.swift b/Passepartout-iOS/Scenes/ServiceViewController.swift index 36349032..be20305f 100644 --- a/Passepartout-iOS/Scenes/ServiceViewController.swift +++ b/Passepartout-iOS/Scenes/ServiceViewController.swift @@ -154,7 +154,7 @@ class ServiceViewController: UIViewController, TableModelHost { let vc = destination as? ConfigurationViewController vc?.title = L10n.Service.Cells.Host.Parameters.caption vc?.initialConfiguration = uncheckedHostProfile.parameters.sessionConfiguration - vc?.originalConfigurationURL = ProfileConfigurationFactory.shared.configurationURL(for: uncheckedHostProfile) + vc?.originalConfigurationURL = service.configurationURL(for: uncheckedHostProfile) vc?.delegate = self case .debugLogSegueIdentifier: diff --git a/Passepartout.xcodeproj/project.pbxproj b/Passepartout.xcodeproj/project.pbxproj index 5172f742..7ae60691 100644 --- a/Passepartout.xcodeproj/project.pbxproj +++ b/Passepartout.xcodeproj/project.pbxproj @@ -20,7 +20,7 @@ 0E1D72B4213C118500BA1586 /* ConfigurationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E1D72B3213C118500BA1586 /* ConfigurationViewController.swift */; }; 0E2B494020FCFF990094784C /* Theme+Titles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E2B493F20FCFF990094784C /* Theme+Titles.swift */; }; 0E2B494220FD16540094784C /* TransientStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E2B494120FD16540094784C /* TransientStore.swift */; }; - 0E2D11BA217DBEDE0096822C /* ProfileConfigurationFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E2D11B9217DBEDE0096822C /* ProfileConfigurationFactory.swift */; }; + 0E2D11BA217DBEDE0096822C /* ConnectionService+Configurations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E2D11B9217DBEDE0096822C /* ConnectionService+Configurations.swift */; }; 0E39BCF0214B9EF10035E9DE /* WebServices.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E39BCEF214B9EF10035E9DE /* WebServices.swift */; }; 0E39BCF3214DA9310035E9DE /* AppConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E39BCF2214DA9310035E9DE /* AppConstants.swift */; }; 0E3DA371215CB5BF00B40FC9 /* VersionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E3DA370215CB5BF00B40FC9 /* VersionViewController.swift */; }; @@ -137,7 +137,7 @@ 0E1D72B3213C118500BA1586 /* ConfigurationViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurationViewController.swift; sourceTree = ""; }; 0E2B493F20FCFF990094784C /* Theme+Titles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Theme+Titles.swift"; sourceTree = ""; }; 0E2B494120FD16540094784C /* TransientStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransientStore.swift; sourceTree = ""; }; - 0E2D11B9217DBEDE0096822C /* ProfileConfigurationFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileConfigurationFactory.swift; sourceTree = ""; }; + 0E2D11B9217DBEDE0096822C /* ConnectionService+Configurations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ConnectionService+Configurations.swift"; sourceTree = ""; }; 0E39BCEF214B9EF10035E9DE /* WebServices.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebServices.swift; sourceTree = ""; }; 0E39BCF2214DA9310035E9DE /* AppConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppConstants.swift; sourceTree = ""; }; 0E3DA370215CB5BF00B40FC9 /* VersionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionViewController.swift; sourceTree = ""; }; @@ -382,12 +382,12 @@ 0EBE3AA2213DC1B000BFA2F5 /* Profiles */, 0EBE3A9E213DC1A100BFA2F5 /* ConnectionProfile.swift */, 0EBE3A9F213DC1A100BFA2F5 /* ConnectionService.swift */, + 0E2D11B9217DBEDE0096822C /* ConnectionService+Configurations.swift */, 0EBBE8F42182361700106008 /* ConnectionService+Migration.swift */, 0EDE8DE620C93945004C739C /* Credentials.swift */, 0EC7F20420E24308004EA58E /* DebugLog.swift */, 0ED38AE621404F100004D387 /* EndpointDataSource.swift */, 0E89DFC4213DF7AE00741BA1 /* Preferences.swift */, - 0E2D11B9217DBEDE0096822C /* ProfileConfigurationFactory.swift */, 0E89DFC7213E8FC500741BA1 /* SessionProxy+Communication.swift */, 0E2B494120FD16540094784C /* TransientStore.swift */, 0E4C9CB820DB9BC600A0C59C /* TrustedNetworks.swift */, @@ -842,7 +842,7 @@ 0ED31C1220CF0ABA0027975F /* Infrastructure.swift in Sources */, 0EC7F20520E24308004EA58E /* DebugLog.swift in Sources */, 0E4FD7E120D3E4C5002221FF /* MockVPNProvider.swift in Sources */, - 0E2D11BA217DBEDE0096822C /* ProfileConfigurationFactory.swift in Sources */, + 0E2D11BA217DBEDE0096822C /* ConnectionService+Configurations.swift in Sources */, 0EBE3A90213C6F4000BFA2F5 /* TrustPolicy.swift in Sources */, 0E6BE13F20CFBAB300A6DD36 /* DebugLogViewController.swift in Sources */, 0E89DFC8213E8FC500741BA1 /* SessionProxy+Communication.swift in Sources */, diff --git a/Passepartout/Sources/Model/ConnectionService+Configurations.swift b/Passepartout/Sources/Model/ConnectionService+Configurations.swift new file mode 100644 index 00000000..c22f1dc8 --- /dev/null +++ b/Passepartout/Sources/Model/ConnectionService+Configurations.swift @@ -0,0 +1,49 @@ +// +// ConnectionService+Configurations.swift +// Passepartout +// +// Created by Davide De Rosa on 10/22/18. +// Copyright (c) 2018 Davide De Rosa. All rights reserved. +// +// https://github.com/keeshux +// +// This file is part of Passepartout. +// +// Passepartout is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Passepartout is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Passepartout. If not, see . +// + +import Foundation + +extension ConnectionService { + func save(configurationURL: URL, for profile: ConnectionProfile) throws -> URL { + let destinationURL = targetConfigurationURL(for: profile) + let fm = FileManager.default + try? fm.removeItem(at: destinationURL) + try fm.copyItem(at: configurationURL, to: destinationURL) + return destinationURL + } + + func configurationURL(for profile: ConnectionProfile) -> URL? { + let url = targetConfigurationURL(for: profile) + guard FileManager.default.fileExists(atPath: url.path) else { + return nil + } + return url + } + + private func targetConfigurationURL(for profile: ConnectionProfile) -> URL { + let contextURL = ConnectionService.ProfileKey(profile).contextURL(in: self) + return contextURL.appendingPathComponent("\(profile.id).ovpn") + } +} diff --git a/Passepartout/Sources/Model/ConnectionService.swift b/Passepartout/Sources/Model/ConnectionService.swift index 03502b2a..cf04e423 100644 --- a/Passepartout/Sources/Model/ConnectionService.swift +++ b/Passepartout/Sources/Model/ConnectionService.swift @@ -64,19 +64,21 @@ class ConnectionService: Codable { id = profile.id } - fileprivate func profileURL(in service: ConnectionService) -> URL { - let contextURL: URL + func contextURL(in service: ConnectionService) -> URL { switch context { case .provider: - contextURL = service.providersURL + return service.providersURL case .host: - contextURL = service.hostsURL + return service.hostsURL } - return ConnectionService.url(in: contextURL, forProfileId: id) + } + + func profileURL(in service: ConnectionService) -> URL { + return ConnectionService.url(in: contextURL(in: service), forProfileId: id) } - fileprivate func profileData(in service: ConnectionService) throws -> Data { + func profileData(in service: ConnectionService) throws -> Data { return try Data(contentsOf: profileURL(in: service)) } diff --git a/Passepartout/Sources/Model/ProfileConfigurationFactory.swift b/Passepartout/Sources/Model/ProfileConfigurationFactory.swift deleted file mode 100644 index c97d3ce1..00000000 --- a/Passepartout/Sources/Model/ProfileConfigurationFactory.swift +++ /dev/null @@ -1,82 +0,0 @@ -// -// ProfileConfigurationFactory.swift -// Passepartout -// -// Created by Davide De Rosa on 10/22/18. -// Copyright (c) 2018 Davide De Rosa. All rights reserved. -// -// https://github.com/keeshux -// -// This file is part of Passepartout. -// -// Passepartout is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// Passepartout is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Passepartout. If not, see . -// - -import Foundation - -protocol ProfileConfigurationSource { - var id: String { get } - - var profileDirectory: String { get } -} - -extension ProfileConfigurationSource { - var profileConfigurationPath: String { - return "\(profileDirectory)/\(id).ovpn" - } -} - -extension ProviderConnectionProfile: ProfileConfigurationSource { - var profileDirectory: String { - return AppConstants.Store.providersDirectory - } -} - -extension HostConnectionProfile: ProfileConfigurationSource { - var profileDirectory: String { - return AppConstants.Store.hostsDirectory - } -} - -class ProfileConfigurationFactory { - static let shared = ProfileConfigurationFactory() - - private let configurationsPath: URL - - private init() { - let fm = FileManager.default - configurationsPath = fm.userURL(for: .documentDirectory, appending: nil) - try? fm.createDirectory(at: configurationsPath, withIntermediateDirectories: false, attributes: nil) - } - - func save(url: URL, for profile: ProfileConfigurationSource) throws -> URL { - let savedUrl = targetConfigurationURL(for: profile) - let fm = FileManager.default - try? fm.removeItem(at: savedUrl) - try fm.copyItem(at: url, to: savedUrl) - return savedUrl - } - - func configurationURL(for profile: ProfileConfigurationSource) -> URL? { - let url = targetConfigurationURL(for: profile) - guard FileManager.default.fileExists(atPath: url.path) else { - return nil - } - return url - } - - private func targetConfigurationURL(for profile: ProfileConfigurationSource) -> URL { - return configurationsPath.appendingPathComponent(profile.profileConfigurationPath) - } -}