2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// Profile.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/11/22.
|
2024-01-14 13:34:21 +00:00
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// 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
|
2023-05-24 16:19:47 +00:00
|
|
|
import PassepartoutCore
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
public protocol ProfileSubtype {
|
|
|
|
var vpnProtocols: [VPNProtocolType] { get }
|
|
|
|
}
|
|
|
|
|
|
|
|
public struct Profile: Identifiable, Codable, Equatable {
|
|
|
|
public var header: Header
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
public var currentVPNProtocol: VPNProtocolType
|
|
|
|
|
|
|
|
public var networkSettings = Profile.NetworkSettings()
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
public var onDemand = Profile.OnDemand()
|
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
public var connectionExpirationDate: Date?
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
var host: Host?
|
|
|
|
|
|
|
|
var provider: Provider?
|
|
|
|
|
|
|
|
init(_ header: Header) {
|
|
|
|
self.header = header
|
|
|
|
currentVPNProtocol = .openVPN
|
|
|
|
}
|
|
|
|
|
|
|
|
init(_ id: UUID = UUID(), name: String) {
|
|
|
|
header = Header(
|
|
|
|
uuid: id,
|
|
|
|
name: name,
|
|
|
|
providerName: nil
|
|
|
|
)
|
|
|
|
currentVPNProtocol = .openVPN
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
init(_ id: UUID = UUID(), name: String, provider: Profile.Provider) {
|
|
|
|
let header = Header(
|
|
|
|
uuid: id,
|
|
|
|
name: name,
|
|
|
|
providerName: provider.name
|
|
|
|
)
|
|
|
|
self.init(header, provider: provider)
|
|
|
|
}
|
|
|
|
|
|
|
|
public init(_ header: Header, provider: Profile.Provider) {
|
2022-06-23 21:31:01 +00:00
|
|
|
guard let firstVPNProtocol = provider.vpnSettings.keys.first else {
|
2022-04-12 13:09:14 +00:00
|
|
|
fatalError("No VPN protocols defined in provider")
|
|
|
|
}
|
|
|
|
self.header = header
|
|
|
|
currentVPNProtocol = firstVPNProtocol
|
|
|
|
self.provider = provider
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Identifiable
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
public var id: UUID {
|
2022-09-04 18:09:31 +00:00
|
|
|
header.id
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Profile {
|
|
|
|
public static let placeholder = Profile(
|
|
|
|
UUID(uuidString: "00000000-0000-0000-0000-000000000000")!,
|
|
|
|
name: ""
|
|
|
|
)
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
public static func isPlaceholder(_ id: UUID) -> Bool {
|
|
|
|
id == placeholder.id
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
public var isPlaceholder: Bool {
|
|
|
|
header.id == Self.placeholder.id
|
|
|
|
}
|
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
|
|
|
|
extension Profile {
|
|
|
|
public var isExpired: Bool {
|
|
|
|
guard let connectionExpirationDate else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return Date().distance(to: connectionExpirationDate) <= .zero
|
|
|
|
}
|
|
|
|
}
|