diff options
author | Eric Kuck <eric@bluelinelabs.com> | 2018-12-20 22:52:45 -0600 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-12-21 16:42:16 +0100 |
commit | 8553723e04c4d63b99e669df2e43fc4a914b7f9d (patch) | |
tree | e3b7b25777e47455a787fa590cf00d1c25cbdc5d /WireGuard/Shared/Model/Endpoint.swift | |
parent | NE: simplify logic (diff) | |
download | wireguard-apple-8553723e04c4d63b99e669df2e43fc4a914b7f9d.tar.xz wireguard-apple-8553723e04c4d63b99e669df2e43fc4a914b7f9d.zip |
Updated NETunnelProvider save format
Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
Diffstat (limited to 'WireGuard/Shared/Model/Endpoint.swift')
-rw-r--r-- | WireGuard/Shared/Model/Endpoint.swift | 68 |
1 files changed, 35 insertions, 33 deletions
diff --git a/WireGuard/Shared/Model/Endpoint.swift b/WireGuard/Shared/Model/Endpoint.swift index 3a4beee..891c564 100644 --- a/WireGuard/Shared/Model/Endpoint.swift +++ b/WireGuard/Shared/Model/Endpoint.swift @@ -4,15 +4,48 @@ import Foundation import Network -@available(OSX 10.14, iOS 12.0, *) struct Endpoint { let host: NWEndpoint.Host let port: NWEndpoint.Port + + init(host: NWEndpoint.Host, port: NWEndpoint.Port) { + self.host = host + self.port = port + } } -// MARK: Converting to and from String +extension Endpoint: Codable { + init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + let endpointString = try container.decode(String.self) + guard let endpoint = Endpoint(from: endpointString) else { + throw DecodingError.invalidData + } + self = endpoint + } + + func encode(to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(stringRepresentation) + } + + enum DecodingError: Error { + case invalidData + } +} extension Endpoint { + var stringRepresentation: String { + switch host { + case .name(let hostname, _): + return "\(hostname):\(port)" + case .ipv4(let address): + return "\(address):\(port)" + case .ipv6(let address): + return "[\(address)]:\(port)" + } + } + init?(from string: String) { // Separation of host and port is based on 'parse_endpoint' function in // https://git.zx2c4.com/WireGuard/tree/src/tools/config.c @@ -41,37 +74,6 @@ extension Endpoint { host = NWEndpoint.Host(hostString) port = endpointPort } - func stringRepresentation() -> String { - switch host { - case .name(let hostname, _): - return "\(hostname):\(port)" - case .ipv4(let address): - return "\(address):\(port)" - case .ipv6(let address): - return "[\(address)]:\(port)" - } - } -} - -// MARK: Codable - -@available(OSX 10.14, iOS 12.0, *) -extension Endpoint: Codable { - public func encode(to encoder: Encoder) throws { - var container = encoder.singleValueContainer() - try container.encode(stringRepresentation()) - } - public init(from decoder: Decoder) throws { - let container = try decoder.singleValueContainer() - let endpointString = try container.decode(String.self) - guard let endpoint = Endpoint(from: endpointString) else { - throw DecodingError.invalidData - } - self = endpoint - } - enum DecodingError: Error { - case invalidData - } } extension Endpoint { |