2018-10-24 01:37:28 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-17 14:56:46 +00:00
|
|
|
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
|
2018-10-19 14:03:17 +00:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Network
|
|
|
|
|
2020-11-05 11:23:06 +00:00
|
|
|
public struct Endpoint {
|
|
|
|
public let host: NWEndpoint.Host
|
|
|
|
public let port: NWEndpoint.Port
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2020-11-05 11:23:06 +00:00
|
|
|
public init(host: NWEndpoint.Host, port: NWEndpoint.Port) {
|
2018-12-21 04:52:45 +00:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
}
|
2018-10-19 14:03:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-22 04:41:54 +00:00
|
|
|
extension Endpoint: Equatable {
|
2020-11-05 11:23:06 +00:00
|
|
|
public static func == (lhs: Endpoint, rhs: Endpoint) -> Bool {
|
2018-12-22 04:41:54 +00:00
|
|
|
return lhs.host == rhs.host && lhs.port == rhs.port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Endpoint: Hashable {
|
2020-11-05 11:23:06 +00:00
|
|
|
public func hash(into hasher: inout Hasher) {
|
2018-12-22 04:41:54 +00:00
|
|
|
hasher.combine(host)
|
|
|
|
hasher.combine(port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 14:03:17 +00:00
|
|
|
extension Endpoint {
|
2020-11-05 11:23:06 +00:00
|
|
|
public var stringRepresentation: String {
|
2018-12-21 04:52:45 +00:00
|
|
|
switch host {
|
|
|
|
case .name(let hostname, _):
|
|
|
|
return "\(hostname):\(port)"
|
|
|
|
case .ipv4(let address):
|
|
|
|
return "\(address):\(port)"
|
|
|
|
case .ipv6(let address):
|
|
|
|
return "[\(address)]:\(port)"
|
2019-04-08 09:48:26 +00:00
|
|
|
@unknown default:
|
|
|
|
fatalError()
|
2018-12-21 04:52:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2020-11-05 11:23:06 +00:00
|
|
|
public init?(from string: String) {
|
2018-10-19 14:03:17 +00:00
|
|
|
// Separation of host and port is based on 'parse_endpoint' function in
|
2019-12-30 10:54:13 +00:00
|
|
|
// https://git.zx2c4.com/wireguard-tools/tree/src/config.c
|
2018-12-12 18:28:27 +00:00
|
|
|
guard !string.isEmpty else { return nil }
|
2018-10-23 10:44:10 +00:00
|
|
|
let startOfPort: String.Index
|
|
|
|
let hostString: String
|
2018-12-12 18:28:27 +00:00
|
|
|
if string.first! == "[" {
|
2018-10-19 14:03:17 +00:00
|
|
|
// 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)
|
2020-12-23 13:06:26 +00:00
|
|
|
if afterEndOfHost == string.endIndex { return nil }
|
2018-12-12 18:28:27 +00:00
|
|
|
guard string[afterEndOfHost] == ":" else { return nil }
|
2018-10-23 10:44:10 +00:00
|
|
|
startOfPort = string.index(after: afterEndOfHost)
|
|
|
|
hostString = String(string[startOfHost ..< endOfHost])
|
2018-10-19 14:03:17 +00:00
|
|
|
} else {
|
|
|
|
// Look for an IPv4-style endpoint, like 127.0.0.1:80
|
|
|
|
guard let endOfHost = string.firstIndex(of: ":") else { return nil }
|
2018-10-23 10:44:10 +00:00
|
|
|
startOfPort = string.index(after: endOfHost)
|
|
|
|
hostString = String(string[string.startIndex ..< endOfHost])
|
2018-10-19 14:03:17 +00:00
|
|
|
}
|
2018-10-23 10:44:10 +00:00
|
|
|
guard let endpointPort = NWEndpoint.Port(String(string[startOfPort ..< string.endIndex])) else { return nil }
|
2018-12-12 17:40:57 +00:00
|
|
|
let invalidCharacterIndex = hostString.unicodeScalars.firstIndex { char in
|
|
|
|
return !CharacterSet.urlHostAllowed.contains(char)
|
2018-10-23 10:44:10 +00:00
|
|
|
}
|
2018-12-12 18:28:27 +00:00
|
|
|
guard invalidCharacterIndex == nil else { return nil }
|
2018-10-23 10:44:10 +00:00
|
|
|
host = NWEndpoint.Host(hostString)
|
|
|
|
port = endpointPort
|
2018-10-19 14:03:17 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-06 18:06:48 +00:00
|
|
|
|
|
|
|
extension Endpoint {
|
2020-11-05 11:23:06 +00:00
|
|
|
public func hasHostAsIPAddress() -> Bool {
|
2018-12-12 18:28:27 +00:00
|
|
|
switch host {
|
2018-12-12 17:40:57 +00:00
|
|
|
case .name:
|
2018-11-06 18:06:48 +00:00
|
|
|
return false
|
2018-12-12 17:40:57 +00:00
|
|
|
case .ipv4:
|
2018-11-06 18:06:48 +00:00
|
|
|
return true
|
2018-12-12 17:40:57 +00:00
|
|
|
case .ipv6:
|
2018-11-06 18:06:48 +00:00
|
|
|
return true
|
2019-04-08 09:48:26 +00:00
|
|
|
@unknown default:
|
|
|
|
fatalError()
|
2018-11-06 18:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-08 10:56:17 +00:00
|
|
|
|
2020-11-05 11:23:06 +00:00
|
|
|
public func hostname() -> String? {
|
2018-12-12 18:28:27 +00:00
|
|
|
switch host {
|
2018-11-08 10:56:17 +00:00
|
|
|
case .name(let hostname, _):
|
|
|
|
return hostname
|
2018-12-12 17:40:57 +00:00
|
|
|
case .ipv4:
|
2018-11-08 10:56:17 +00:00
|
|
|
return nil
|
2018-12-12 17:40:57 +00:00
|
|
|
case .ipv6:
|
2018-11-08 10:56:17 +00:00
|
|
|
return nil
|
2019-04-08 09:48:26 +00:00
|
|
|
@unknown default:
|
|
|
|
fatalError()
|
2018-11-08 10:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-06 18:06:48 +00:00
|
|
|
}
|