Refactor configurations in service extension

Reuse same directories of ConnectionService for storing
configuration files.
This commit is contained in:
Davide De Rosa 2018-10-27 00:08:48 +02:00
parent 4388dfe6ae
commit 821393af70
6 changed files with 64 additions and 95 deletions

View File

@ -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)")
}

View File

@ -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:

View File

@ -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 = "<group>"; };
0E2B493F20FCFF990094784C /* Theme+Titles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Theme+Titles.swift"; sourceTree = "<group>"; };
0E2B494120FD16540094784C /* TransientStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransientStore.swift; sourceTree = "<group>"; };
0E2D11B9217DBEDE0096822C /* ProfileConfigurationFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileConfigurationFactory.swift; sourceTree = "<group>"; };
0E2D11B9217DBEDE0096822C /* ConnectionService+Configurations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ConnectionService+Configurations.swift"; sourceTree = "<group>"; };
0E39BCEF214B9EF10035E9DE /* WebServices.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebServices.swift; sourceTree = "<group>"; };
0E39BCF2214DA9310035E9DE /* AppConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppConstants.swift; sourceTree = "<group>"; };
0E3DA370215CB5BF00B40FC9 /* VersionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionViewController.swift; sourceTree = "<group>"; };
@ -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 */,

View File

@ -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 <http://www.gnu.org/licenses/>.
//
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")
}
}

View File

@ -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))
}

View File

@ -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 <http://www.gnu.org/licenses/>.
//
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)
}
}