Model for an endpoint
Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
parent
8ec73025f2
commit
c12f481b70
|
@ -7,6 +7,7 @@
|
||||||
objects = {
|
objects = {
|
||||||
|
|
||||||
/* Begin PBXBuildFile section */
|
/* Begin PBXBuildFile section */
|
||||||
|
6F693A562179E556008551C1 /* Endpoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F693A552179E556008551C1 /* Endpoint.swift */; };
|
||||||
6F7774E1217181B1006A79B3 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774DF217181B1006A79B3 /* MainViewController.swift */; };
|
6F7774E1217181B1006A79B3 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774DF217181B1006A79B3 /* MainViewController.swift */; };
|
||||||
6F7774E2217181B1006A79B3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E0217181B1006A79B3 /* AppDelegate.swift */; };
|
6F7774E2217181B1006A79B3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E0217181B1006A79B3 /* AppDelegate.swift */; };
|
||||||
6F7774E421718281006A79B3 /* TunnelsListTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E321718281006A79B3 /* TunnelsListTableViewController.swift */; };
|
6F7774E421718281006A79B3 /* TunnelsListTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E321718281006A79B3 /* TunnelsListTableViewController.swift */; };
|
||||||
|
@ -19,6 +20,7 @@
|
||||||
/* End PBXBuildFile section */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
|
6F693A552179E556008551C1 /* Endpoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Endpoint.swift; sourceTree = "<group>"; };
|
||||||
6F7774DF217181B1006A79B3 /* MainViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = "<group>"; };
|
6F7774DF217181B1006A79B3 /* MainViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = "<group>"; };
|
||||||
6F7774E0217181B1006A79B3 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
|
6F7774E0217181B1006A79B3 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
|
||||||
6F7774E321718281006A79B3 /* TunnelsListTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TunnelsListTableViewController.swift; sourceTree = "<group>"; };
|
6F7774E321718281006A79B3 /* TunnelsListTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TunnelsListTableViewController.swift; sourceTree = "<group>"; };
|
||||||
|
@ -69,6 +71,7 @@
|
||||||
children = (
|
children = (
|
||||||
6F7774E72172020C006A79B3 /* Configuration.swift */,
|
6F7774E72172020C006A79B3 /* Configuration.swift */,
|
||||||
6F7774E9217229DB006A79B3 /* IPAddressRange.swift */,
|
6F7774E9217229DB006A79B3 /* IPAddressRange.swift */,
|
||||||
|
6F693A552179E556008551C1 /* Endpoint.swift */,
|
||||||
);
|
);
|
||||||
path = Model;
|
path = Model;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
|
@ -198,6 +201,7 @@
|
||||||
files = (
|
files = (
|
||||||
6F7774E421718281006A79B3 /* TunnelsListTableViewController.swift in Sources */,
|
6F7774E421718281006A79B3 /* TunnelsListTableViewController.swift in Sources */,
|
||||||
6F7774EF21722D97006A79B3 /* TunnelsManager.swift in Sources */,
|
6F7774EF21722D97006A79B3 /* TunnelsManager.swift in Sources */,
|
||||||
|
6F693A562179E556008551C1 /* Endpoint.swift in Sources */,
|
||||||
6F7774E2217181B1006A79B3 /* AppDelegate.swift in Sources */,
|
6F7774E2217181B1006A79B3 /* AppDelegate.swift in Sources */,
|
||||||
6F7774EA217229DB006A79B3 /* IPAddressRange.swift in Sources */,
|
6F7774EA217229DB006A79B3 /* IPAddressRange.swift in Sources */,
|
||||||
6F7774E82172020C006A79B3 /* Configuration.swift in Sources */,
|
6F7774E82172020C006A79B3 /* Configuration.swift in Sources */,
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
//
|
||||||
|
// Endpoint.swift
|
||||||
|
// WireGuard
|
||||||
|
//
|
||||||
|
// Created by Roopesh Chander on 19/10/18.
|
||||||
|
// Copyright © 2018 WireGuard LLC. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import Network
|
||||||
|
|
||||||
|
@available(OSX 10.14, iOS 12.0, *)
|
||||||
|
struct Endpoint {
|
||||||
|
let host: NWEndpoint.Host
|
||||||
|
let port: NWEndpoint.Port
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: Converting to and from String
|
||||||
|
// For use in the UI
|
||||||
|
|
||||||
|
extension Endpoint {
|
||||||
|
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
|
||||||
|
guard (!string.isEmpty) else { return nil }
|
||||||
|
if (string.first! == "[") {
|
||||||
|
// Look for IPv6-style endpoint, like [::1]:80
|
||||||
|
let startOfHost = string.index(after: string.startIndex)
|
||||||
|
guard let endOfHost = string.dropFirst().firstIndex(of: "]") else { return nil }
|
||||||
|
let afterEndOfHost = string.index(after: endOfHost)
|
||||||
|
guard (string[afterEndOfHost] == ":") else { return nil }
|
||||||
|
let startOfPort = string.index(after: afterEndOfHost)
|
||||||
|
let hostString = String(string[startOfHost ..< endOfHost])
|
||||||
|
guard let endpointPort = NWEndpoint.Port(String(string[startOfPort ..< string.endIndex])) else { return nil }
|
||||||
|
host = NWEndpoint.Host(hostString)
|
||||||
|
port = endpointPort
|
||||||
|
} else {
|
||||||
|
// Look for an IPv4-style endpoint, like 127.0.0.1:80
|
||||||
|
guard let endOfHost = string.firstIndex(of: ":") else { return nil }
|
||||||
|
let startOfPort = string.index(after: endOfHost)
|
||||||
|
let hostString = String(string[string.startIndex ..< endOfHost])
|
||||||
|
guard let endpointPort = NWEndpoint.Port(String(string[startOfPort ..< string.endIndex])) else { return nil }
|
||||||
|
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
|
||||||
|
// For serializing to disk
|
||||||
|
|
||||||
|
@available(OSX 10.14, iOS 12.0, *)
|
||||||
|
extension Endpoint: Codable {
|
||||||
|
public func encode(to encoder: Encoder) throws {
|
||||||
|
var container = encoder.singleValueContainer()
|
||||||
|
try container.encode(self.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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue