From 3807b4754b1f2ccd466b6a04bddce7f9ffe7ded1 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Mon, 7 Mar 2022 23:50:38 +0100 Subject: [PATCH] Review some Core/OpenVPN entities - Drop redundant Equatable (automatic in structs) - Make IPv4/6 routes Hashable - Expose StaticKey as hex String - Mask PAC URL --- Sources/TunnelKitCore/DataCount.swift | 6 ------ Sources/TunnelKitCore/Endpoint.swift | 12 ------------ Sources/TunnelKitCore/IPv4Settings.swift | 10 +++++++++- Sources/TunnelKitCore/IPv6Settings.swift | 10 +++++++++- .../OpenVPNTunnelProvider.swift | 2 +- Sources/TunnelKitOpenVPNCore/Configuration.swift | 6 ------ Sources/TunnelKitOpenVPNCore/CryptoContainer.swift | 6 ------ Sources/TunnelKitOpenVPNCore/StaticKey.swift | 4 ++++ 8 files changed, 23 insertions(+), 33 deletions(-) diff --git a/Sources/TunnelKitCore/DataCount.swift b/Sources/TunnelKitCore/DataCount.swift index 7e36f66..c98824b 100644 --- a/Sources/TunnelKitCore/DataCount.swift +++ b/Sources/TunnelKitCore/DataCount.swift @@ -35,10 +35,4 @@ public struct DataCount: Equatable { self.received = received self.sent = sent } - - // MARK: Equatable - - public static func ==(lhs: DataCount, rhs: DataCount) -> Bool { - return lhs.up == rhs.up && lhs.down == rhs.down - } } diff --git a/Sources/TunnelKitCore/Endpoint.swift b/Sources/TunnelKitCore/Endpoint.swift index 0f2302b..eecbbff 100644 --- a/Sources/TunnelKitCore/Endpoint.swift +++ b/Sources/TunnelKitCore/Endpoint.swift @@ -37,12 +37,6 @@ public struct Endpoint: Codable, Equatable, CustomStringConvertible { self.proto = proto } - // MARK: Equatable - - public static func ==(lhs: Endpoint, rhs: Endpoint) -> Bool { - return lhs.address == rhs.address && lhs.proto == rhs.proto - } - // MARK: CustomStringConvertible public var description: String { @@ -85,12 +79,6 @@ public struct EndpointProtocol: RawRepresentable, Equatable, CustomStringConvert return "\(socketType.rawValue):\(port)" } - // MARK: Equatable - - public static func ==(lhs: EndpointProtocol, rhs: EndpointProtocol) -> Bool { - return (lhs.socketType == rhs.socketType) && (lhs.port == rhs.port) - } - // MARK: CustomStringConvertible public var description: String { diff --git a/Sources/TunnelKitCore/IPv4Settings.swift b/Sources/TunnelKitCore/IPv4Settings.swift index b1fbea9..7accaa8 100644 --- a/Sources/TunnelKitCore/IPv4Settings.swift +++ b/Sources/TunnelKitCore/IPv4Settings.swift @@ -29,7 +29,7 @@ import Foundation public struct IPv4Settings: Codable, CustomStringConvertible { /// Represents an IPv4 route in the routing table. - public struct Route: Codable, CustomStringConvertible { + public struct Route: Codable, Hashable, CustomStringConvertible { /// The destination host or subnet. public let destination: String @@ -46,6 +46,14 @@ public struct IPv4Settings: Codable, CustomStringConvertible { self.gateway = gateway } + // MARK: Hashable + + public func hash(into hasher: inout Hasher) { + hasher.combine(destination) + hasher.combine(mask) + hasher.combine(gateway) + } + // MARK: CustomStringConvertible public var description: String { diff --git a/Sources/TunnelKitCore/IPv6Settings.swift b/Sources/TunnelKitCore/IPv6Settings.swift index 9aad8da..728a5c8 100644 --- a/Sources/TunnelKitCore/IPv6Settings.swift +++ b/Sources/TunnelKitCore/IPv6Settings.swift @@ -29,7 +29,7 @@ import Foundation public struct IPv6Settings: Codable, CustomStringConvertible { /// Represents an IPv6 route in the routing table. - public struct Route: Codable, CustomStringConvertible { + public struct Route: Codable, Hashable, CustomStringConvertible { /// The destination host or subnet. public let destination: String @@ -46,6 +46,14 @@ public struct IPv6Settings: Codable, CustomStringConvertible { self.gateway = gateway } + // MARK: Hashable + + public func hash(into hasher: inout Hasher) { + hasher.combine(destination) + hasher.combine(prefixLength) + hasher.combine(gateway) + } + // MARK: CustomStringConvertible public var description: String { diff --git a/Sources/TunnelKitOpenVPNAppExtension/OpenVPNTunnelProvider.swift b/Sources/TunnelKitOpenVPNAppExtension/OpenVPNTunnelProvider.swift index 3bd9a7f..c106dfd 100644 --- a/Sources/TunnelKitOpenVPNAppExtension/OpenVPNTunnelProvider.swift +++ b/Sources/TunnelKitOpenVPNAppExtension/OpenVPNTunnelProvider.swift @@ -751,7 +751,7 @@ extension OpenVPNTunnelProvider: OpenVPNSessionDelegate { } proxySettings?.proxyAutoConfigurationURL = pacURL proxySettings?.autoProxyConfigurationEnabled = true - log.info("Routing: Setting PAC \(pacURL)") + log.info("Routing: Setting PAC \(pacURL.maskedDescription)") } // only set if there is a proxy (proxySettings set to non-nil above) diff --git a/Sources/TunnelKitOpenVPNCore/Configuration.swift b/Sources/TunnelKitOpenVPNCore/Configuration.swift index 563c025..5faa6c7 100644 --- a/Sources/TunnelKitOpenVPNCore/Configuration.swift +++ b/Sources/TunnelKitOpenVPNCore/Configuration.swift @@ -56,12 +56,6 @@ extension OpenVPN { self.username = username self.password = password } - - // MARK: Equatable - - public static func ==(lhs: Credentials, rhs: Credentials) -> Bool { - return (lhs.username == rhs.username) && (lhs.password == rhs.password) - } } /// Encryption algorithm. diff --git a/Sources/TunnelKitOpenVPNCore/CryptoContainer.swift b/Sources/TunnelKitOpenVPNCore/CryptoContainer.swift index 03a3620..4b10ea4 100644 --- a/Sources/TunnelKitOpenVPNCore/CryptoContainer.swift +++ b/Sources/TunnelKitOpenVPNCore/CryptoContainer.swift @@ -72,12 +72,6 @@ extension OpenVPN { return CryptoContainer(pem: decryptedPEM) } - // MARK: Equatable - - public static func ==(lhs: CryptoContainer, rhs: CryptoContainer) -> Bool { - return lhs.pem == rhs.pem - } - // MARK: Codable public init(from decoder: Decoder) throws { diff --git a/Sources/TunnelKitOpenVPNCore/StaticKey.swift b/Sources/TunnelKitOpenVPNCore/StaticKey.swift index 85d0f69..2cc6809 100644 --- a/Sources/TunnelKitOpenVPNCore/StaticKey.swift +++ b/Sources/TunnelKitOpenVPNCore/StaticKey.swift @@ -227,5 +227,9 @@ extension OpenVPN { try container.encode(secureData.toData(), forKey: .data) try container.encodeIfPresent(direction, forKey: .dir) } + + public var hexString: String { + return secureData.toHex() + } } }