2018-10-22 08:28:53 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2018-10-25 19:51:00 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 08:28:53 +00:00
|
|
|
class ProfileConfigurationFactory {
|
2018-10-25 19:51:00 +00:00
|
|
|
static let shared = ProfileConfigurationFactory()
|
2018-10-22 08:28:53 +00:00
|
|
|
|
|
|
|
private let configurationsPath: URL
|
|
|
|
|
2018-10-25 19:51:00 +00:00
|
|
|
private init() {
|
2018-10-22 08:28:53 +00:00
|
|
|
let fm = FileManager.default
|
2018-10-25 19:51:00 +00:00
|
|
|
configurationsPath = fm.userURL(for: .documentDirectory, appending: nil)
|
2018-10-22 08:28:53 +00:00
|
|
|
try? fm.createDirectory(at: configurationsPath, withIntermediateDirectories: false, attributes: nil)
|
|
|
|
}
|
|
|
|
|
2018-10-25 19:51:00 +00:00
|
|
|
func save(url: URL, for profile: ProfileConfigurationSource) throws -> URL {
|
2018-10-22 09:16:05 +00:00
|
|
|
let savedUrl = targetConfigurationURL(for: profile)
|
2018-10-26 16:16:34 +00:00
|
|
|
let fm = FileManager.default
|
|
|
|
try? fm.removeItem(at: savedUrl)
|
|
|
|
try fm.copyItem(at: url, to: savedUrl)
|
2018-10-22 08:28:53 +00:00
|
|
|
return savedUrl
|
|
|
|
}
|
|
|
|
|
2018-10-25 19:51:00 +00:00
|
|
|
func configurationURL(for profile: ProfileConfigurationSource) -> URL? {
|
2018-10-22 09:16:05 +00:00
|
|
|
let url = targetConfigurationURL(for: profile)
|
|
|
|
guard FileManager.default.fileExists(atPath: url.path) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2018-10-25 19:51:00 +00:00
|
|
|
private func targetConfigurationURL(for profile: ProfileConfigurationSource) -> URL {
|
|
|
|
return configurationsPath.appendingPathComponent(profile.profileConfigurationPath)
|
2018-10-22 08:28:53 +00:00
|
|
|
}
|
|
|
|
}
|