aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/Model/Endpoint.swift
diff options
context:
space:
mode:
Diffstat (limited to 'WireGuard/Shared/Model/Endpoint.swift')
-rw-r--r--WireGuard/Shared/Model/Endpoint.swift68
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 {