From f1bdc8490cea6398924c46345cb11b98050878e6 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Tue, 22 Mar 2022 07:14:57 +0100 Subject: [PATCH] Use "struct" in data models to leverage Equatable This way Configuration objects can be compared for changes. --- Sources/TunnelKitCore/BidirectionalState.swift | 4 ++-- Sources/TunnelKitCore/IPv4Settings.swift | 2 +- Sources/TunnelKitCore/IPv6Settings.swift | 2 +- Sources/TunnelKitCore/Proxy.swift | 2 +- Sources/TunnelKitOpenVPNCore/Configuration.swift | 2 +- Sources/TunnelKitOpenVPNCore/StaticKey.swift | 10 +++++----- Sources/TunnelKitOpenVPNCore/TLSWrap.swift | 4 ++-- Sources/TunnelKitOpenVPNProtocol/Packet.swift | 2 +- Sources/TunnelKitWireGuardCore/Configuration.swift | 2 +- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Sources/TunnelKitCore/BidirectionalState.swift b/Sources/TunnelKitCore/BidirectionalState.swift index a0742f6..8c17bb8 100644 --- a/Sources/TunnelKitCore/BidirectionalState.swift +++ b/Sources/TunnelKitCore/BidirectionalState.swift @@ -26,7 +26,7 @@ import Foundation /// A generic structure holding a pair of inbound/outbound states. -public class BidirectionalState { +public struct BidirectionalState { private let resetValue: T /// The inbound state. @@ -58,7 +58,7 @@ public class BidirectionalState { /** Resets state to the value provided with `init(withResetValue:)`. */ - public func reset() { + public mutating func reset() { inbound = resetValue outbound = resetValue } diff --git a/Sources/TunnelKitCore/IPv4Settings.swift b/Sources/TunnelKitCore/IPv4Settings.swift index 7accaa8..b876a57 100644 --- a/Sources/TunnelKitCore/IPv4Settings.swift +++ b/Sources/TunnelKitCore/IPv4Settings.swift @@ -26,7 +26,7 @@ import Foundation /// Encapsulates the IPv4 settings for the tunnel. -public struct IPv4Settings: Codable, CustomStringConvertible { +public struct IPv4Settings: Codable, Equatable, CustomStringConvertible { /// Represents an IPv4 route in the routing table. public struct Route: Codable, Hashable, CustomStringConvertible { diff --git a/Sources/TunnelKitCore/IPv6Settings.swift b/Sources/TunnelKitCore/IPv6Settings.swift index 728a5c8..5d83e5f 100644 --- a/Sources/TunnelKitCore/IPv6Settings.swift +++ b/Sources/TunnelKitCore/IPv6Settings.swift @@ -26,7 +26,7 @@ import Foundation /// Encapsulates the IPv6 settings for the tunnel. -public struct IPv6Settings: Codable, CustomStringConvertible { +public struct IPv6Settings: Codable, Equatable, CustomStringConvertible { /// Represents an IPv6 route in the routing table. public struct Route: Codable, Hashable, CustomStringConvertible { diff --git a/Sources/TunnelKitCore/Proxy.swift b/Sources/TunnelKitCore/Proxy.swift index 061bcf1..13399f7 100644 --- a/Sources/TunnelKitCore/Proxy.swift +++ b/Sources/TunnelKitCore/Proxy.swift @@ -26,7 +26,7 @@ import Foundation /// Encapsulates a proxy setting. -public struct Proxy: Codable, RawRepresentable, CustomStringConvertible { +public struct Proxy: Codable, Equatable, RawRepresentable, CustomStringConvertible { /// The proxy address. public let address: String diff --git a/Sources/TunnelKitOpenVPNCore/Configuration.swift b/Sources/TunnelKitOpenVPNCore/Configuration.swift index 5faa6c7..9093b1a 100644 --- a/Sources/TunnelKitOpenVPNCore/Configuration.swift +++ b/Sources/TunnelKitOpenVPNCore/Configuration.swift @@ -356,7 +356,7 @@ extension OpenVPN { } /// The immutable configuration for `OpenVPNSession`. - public struct Configuration: Codable { + public struct Configuration: Codable, Equatable { /// - Seealso: `ConfigurationBuilder.cipher` public let cipher: Cipher? diff --git a/Sources/TunnelKitOpenVPNCore/StaticKey.swift b/Sources/TunnelKitOpenVPNCore/StaticKey.swift index 2cc6809..372d39f 100644 --- a/Sources/TunnelKitOpenVPNCore/StaticKey.swift +++ b/Sources/TunnelKitOpenVPNCore/StaticKey.swift @@ -30,7 +30,7 @@ import CTunnelKitCore extension OpenVPN { /// Represents an OpenVPN static key file (as generated with --genkey) - public class StaticKey: Codable { + public struct StaticKey: Codable, Equatable { enum CodingKeys: CodingKey { case data @@ -147,12 +147,12 @@ extension OpenVPN { - Parameter file: The text file containing the key. - Parameter direction: The key direction, or bidirectional if nil. */ - public convenience init?(file: String, direction: Direction?) { + public init?(file: String, direction: Direction?) { let lines = file.split(separator: "\n") self.init(lines: lines, direction: direction) } - public convenience init?(lines: [Substring], direction: Direction?) { + public init?(lines: [Substring], direction: Direction?) { var isHead = true var hexLines: [Substring] = [] @@ -196,7 +196,7 @@ extension OpenVPN { - Parameter biData: The key data. */ - public convenience init(biData data: Data) { + public init(biData data: Data) { self.init(data: data, direction: nil) } @@ -216,7 +216,7 @@ extension OpenVPN { // MARK: Codable - public required init(from decoder: Decoder) throws { + public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) secureData = Z(try container.decode(Data.self, forKey: .data)) direction = try container.decodeIfPresent(Direction.self, forKey: .dir) diff --git a/Sources/TunnelKitOpenVPNCore/TLSWrap.swift b/Sources/TunnelKitOpenVPNCore/TLSWrap.swift index edeb181..5d704a3 100644 --- a/Sources/TunnelKitOpenVPNCore/TLSWrap.swift +++ b/Sources/TunnelKitOpenVPNCore/TLSWrap.swift @@ -28,10 +28,10 @@ import Foundation extension OpenVPN { /// Holds parameters for TLS wrapping. - public class TLSWrap: Codable { + public struct TLSWrap: Codable, Equatable { /// The wrapping strategy. - public enum Strategy: String, Codable { + public enum Strategy: String, Codable, Equatable { /// Authenticates payload (--tls-auth). case auth diff --git a/Sources/TunnelKitOpenVPNProtocol/Packet.swift b/Sources/TunnelKitOpenVPNProtocol/Packet.swift index 01113c5..5ccb777 100644 --- a/Sources/TunnelKitOpenVPNProtocol/Packet.swift +++ b/Sources/TunnelKitOpenVPNProtocol/Packet.swift @@ -63,7 +63,7 @@ extension ControlPacket { } extension OpenVPN { - class DataPacket { + struct DataPacket { static let pingString = Data(hex: "2a187bf3641eb4cb07ed2d0a981fc748") } diff --git a/Sources/TunnelKitWireGuardCore/Configuration.swift b/Sources/TunnelKitWireGuardCore/Configuration.swift index 8d87b72..a0b60ef 100644 --- a/Sources/TunnelKitWireGuardCore/Configuration.swift +++ b/Sources/TunnelKitWireGuardCore/Configuration.swift @@ -203,7 +203,7 @@ extension WireGuard { } } - public struct Configuration: Codable, WireGuardConfigurationProviding { + public struct Configuration: Codable, Equatable, WireGuardConfigurationProviding { public let tunnelConfiguration: TunnelConfiguration public var interface: InterfaceConfiguration {