2018-11-10 09:17:36 +00:00
|
|
|
//
|
2022-03-03 14:34:57 +00:00
|
|
|
// Endpoint.swift
|
2018-11-10 09:17:36 +00:00
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 11/10/18.
|
2022-02-04 11:42:58 +00:00
|
|
|
// Copyright (c) 2022 Davide De Rosa. All rights reserved.
|
2018-11-10 09:17:36 +00:00
|
|
|
//
|
2019-05-14 08:58:47 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-11-10 09:17:36 +00:00
|
|
|
//
|
|
|
|
// This file is part of TunnelKit.
|
|
|
|
//
|
|
|
|
// TunnelKit is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// TunnelKit is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2022-10-25 09:29:36 +00:00
|
|
|
import __TunnelKitUtils
|
2018-11-10 09:17:36 +00:00
|
|
|
|
2022-03-03 14:34:57 +00:00
|
|
|
/// Represents an endpoint.
|
2022-03-18 17:40:58 +00:00
|
|
|
public struct Endpoint: RawRepresentable, Codable, Equatable, CustomStringConvertible {
|
2022-10-25 09:29:36 +00:00
|
|
|
|
|
|
|
// XXX: simplistic match
|
2022-10-26 17:02:09 +00:00
|
|
|
private static let rx = NSRegularExpression("^([^\\s]+):(UDP[46]?|TCP[46]?):(\\d+)$")
|
2022-10-25 09:29:36 +00:00
|
|
|
|
2022-03-03 14:34:57 +00:00
|
|
|
public let address: String
|
|
|
|
|
|
|
|
public let proto: EndpointProtocol
|
|
|
|
|
2022-10-17 07:00:23 +00:00
|
|
|
public init(_ address: String, _ proto: EndpointProtocol) {
|
2022-03-03 14:34:57 +00:00
|
|
|
self.address = address
|
|
|
|
self.proto = proto
|
|
|
|
}
|
2022-10-17 07:00:23 +00:00
|
|
|
|
|
|
|
public var isIPv4: Bool {
|
|
|
|
var addr = in_addr()
|
|
|
|
let result = address.withCString {
|
|
|
|
inet_pton(AF_INET, $0, &addr)
|
|
|
|
}
|
|
|
|
return result > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
public var isIPv6: Bool {
|
|
|
|
var addr = in_addr()
|
|
|
|
let result = address.withCString {
|
|
|
|
inet_pton(AF_INET6, $0, &addr)
|
|
|
|
}
|
|
|
|
return result > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
public var isHostname: Bool {
|
|
|
|
!isIPv4 && !isIPv6
|
|
|
|
}
|
|
|
|
|
|
|
|
public func withRandomPrefixLength(_ length: Int) throws -> Endpoint {
|
|
|
|
guard isHostname else {
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
let prefix = try SecureRandom.data(length: length)
|
|
|
|
let prefixedAddress = "\(prefix.toHex()).\(address)"
|
|
|
|
return Endpoint(prefixedAddress, proto)
|
|
|
|
}
|
2022-03-03 14:34:57 +00:00
|
|
|
|
2022-03-18 17:40:58 +00:00
|
|
|
// MARK: RawRepresentable
|
|
|
|
|
|
|
|
public init?(rawValue: String) {
|
2022-10-25 09:29:36 +00:00
|
|
|
let components = Self.rx.groups(in: rawValue)
|
2022-03-18 17:40:58 +00:00
|
|
|
guard components.count == 3 else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let address = components[0]
|
|
|
|
guard let socketType = SocketType(rawValue: components[1]) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
guard let port = UInt16(components[2]) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
self.init(address, EndpointProtocol(socketType, port))
|
|
|
|
}
|
|
|
|
|
|
|
|
public var rawValue: String {
|
|
|
|
return "\(address):\(proto.socketType.rawValue):\(proto.port)"
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:34:57 +00:00
|
|
|
// MARK: CustomStringConvertible
|
|
|
|
|
|
|
|
public var description: String {
|
2022-03-18 17:40:58 +00:00
|
|
|
return "\(address.maskedDescription):\(proto.rawValue)"
|
2022-03-03 14:34:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 09:17:36 +00:00
|
|
|
/// Defines the communication protocol of an endpoint.
|
|
|
|
public struct EndpointProtocol: RawRepresentable, Equatable, CustomStringConvertible {
|
|
|
|
|
|
|
|
/// The socket type.
|
|
|
|
public let socketType: SocketType
|
|
|
|
|
|
|
|
/// The remote port.
|
|
|
|
public let port: UInt16
|
2022-03-03 14:34:57 +00:00
|
|
|
|
2022-09-23 19:45:04 +00:00
|
|
|
public init(_ socketType: SocketType, _ port: UInt16) {
|
2018-11-10 09:17:36 +00:00
|
|
|
self.socketType = socketType
|
|
|
|
self.port = port
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: RawRepresentable
|
|
|
|
|
|
|
|
public init?(rawValue: String) {
|
|
|
|
let components = rawValue.components(separatedBy: ":")
|
|
|
|
guard components.count == 2 else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
guard let socketType = SocketType(rawValue: components[0]) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
guard let port = UInt16(components[1]) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
self.init(socketType, port)
|
|
|
|
}
|
|
|
|
|
|
|
|
public var rawValue: String {
|
|
|
|
return "\(socketType.rawValue):\(port)"
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: CustomStringConvertible
|
|
|
|
|
|
|
|
public var description: String {
|
|
|
|
return rawValue
|
|
|
|
}
|
|
|
|
}
|
2019-05-19 10:16:09 +00:00
|
|
|
|
|
|
|
extension EndpointProtocol: Codable {
|
|
|
|
public init(from decoder: Decoder) throws {
|
|
|
|
let container = try decoder.singleValueContainer()
|
2021-11-23 11:20:47 +00:00
|
|
|
let rawValue = try container.decode(String.self)
|
|
|
|
let proto = EndpointProtocol(rawValue: rawValue) ?? EndpointProtocol(.udp, 1198)
|
2019-05-19 10:16:09 +00:00
|
|
|
self.init(proto.socketType, proto.port)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func encode(to encoder: Encoder) throws {
|
|
|
|
var container = encoder.singleValueContainer()
|
|
|
|
try container.encode(rawValue)
|
|
|
|
}
|
|
|
|
}
|