From ddf3dc313f181d54e2ebe558cf061c78335622c2 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sun, 2 Jul 2023 13:27:46 +0200 Subject: [PATCH] Move VPNProtocolType serialization to Data layer (#318) It's crucial that the data layer has full control over how entities are (de)serialized. Do not leave this choice up to the domain layer by defining an enum as a raw value type, because any change in the enum raw value would literally be a disaster, i.e. _any_ serialized data would break instantly. --- CHANGELOG.md | 8 +-- .../Domain/VPNProtocolType.swift | 7 +-- .../Extensions/Domain+Identifiable.swift | 2 +- .../VPNProtocolType+RawRepresentable.swift | 56 +++++++++++++++++++ 4 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 PassepartoutLibrary/Sources/PassepartoutProvidersImpl/Strategies/VPNProtocolType+RawRepresentable.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 212d218f..1b076ddd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,13 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased -### Changed - -- Internal error handling. [#310](https://github.com/passepartoutvpn/passepartout-apple/pull/310) - ### Fixed - Allow wildcards in proxy bypass domains. [#296](https://github.com/passepartoutvpn/passepartout-apple/issues/296) +- Fail gracefully when refreshing infrastructure. [#307](https://github.com/passepartoutvpn/passepartout-apple/pull/307) +- Only show 'Reconnect' on active profile. [#311](https://github.com/passepartoutvpn/passepartout-apple/pull/311) +- IPv4/6 address validation. [#308](https://github.com/passepartoutvpn/passepartout-apple/pull/308) +- Domain name validation. [#297](https://github.com/passepartoutvpn/passepartout-apple/pull/297) ## 2.1.1 (2023-04-19) diff --git a/PassepartoutLibrary/Sources/PassepartoutCore/Domain/VPNProtocolType.swift b/PassepartoutLibrary/Sources/PassepartoutCore/Domain/VPNProtocolType.swift index b867b8d8..ed77120c 100644 --- a/PassepartoutLibrary/Sources/PassepartoutCore/Domain/VPNProtocolType.swift +++ b/PassepartoutLibrary/Sources/PassepartoutCore/Domain/VPNProtocolType.swift @@ -25,11 +25,10 @@ import Foundation -// FIXME: arch, provide a well-defined serialization property rather than .rawValue -public enum VPNProtocolType: String, CaseIterable, Codable { - case openVPN = "ovpn" +public enum VPNProtocolType: CaseIterable, Codable { + case openVPN - case wireGuard = "wg" + case wireGuard } public protocol VPNProtocolProviding { diff --git a/PassepartoutLibrary/Sources/PassepartoutProviders/Extensions/Domain+Identifiable.swift b/PassepartoutLibrary/Sources/PassepartoutProviders/Extensions/Domain+Identifiable.swift index 534a9e97..e795795e 100644 --- a/PassepartoutLibrary/Sources/PassepartoutProviders/Extensions/Domain+Identifiable.swift +++ b/PassepartoutLibrary/Sources/PassepartoutProviders/Extensions/Domain+Identifiable.swift @@ -57,7 +57,7 @@ extension ProviderServer: Identifiable { } public static func id(withName providerName: ProviderName, vpnProtocol: VPNProtocolType, apiId: String) -> String? { - let idSource = [providerName, vpnProtocol.rawValue, apiId].joined(separator: ":") + let idSource = [providerName, "\(vpnProtocol)", apiId].joined(separator: ":") guard let data = idSource.data(using: .utf8) else { return nil } diff --git a/PassepartoutLibrary/Sources/PassepartoutProvidersImpl/Strategies/VPNProtocolType+RawRepresentable.swift b/PassepartoutLibrary/Sources/PassepartoutProvidersImpl/Strategies/VPNProtocolType+RawRepresentable.swift new file mode 100644 index 00000000..50e04b0c --- /dev/null +++ b/PassepartoutLibrary/Sources/PassepartoutProvidersImpl/Strategies/VPNProtocolType+RawRepresentable.swift @@ -0,0 +1,56 @@ +// +// VPNProtocolType+RawRepresentable.swift +// Passepartout +// +// Created by Davide De Rosa on 7/2/23. +// Copyright (c) 2023 Davide De Rosa. All rights reserved. +// +// 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 . +// + +import Foundation +import PassepartoutCore + +extension VPNProtocolType: RawRepresentable { + private static let openVPNString = "ovpn" + + private static let wireGuardString = "wg" + + public init?(rawValue: String) { + switch rawValue { + case Self.openVPNString: + self = .openVPN + + case Self.wireGuardString: + self = .wireGuard + + default: + return nil + } + } + + public var rawValue: String { + switch self { + case .openVPN: + return Self.openVPNString + + case .wireGuard: + return Self.wireGuardString + } + } +}