diff --git a/Tun/Punchnet/DNS/DNSCloudClient.swift b/Tun/Punchnet/DNS/DNSCloudClient.swift index 58040ac..ac4539a 100644 --- a/Tun/Punchnet/DNS/DNSCloudClient.swift +++ b/Tun/Punchnet/DNS/DNSCloudClient.swift @@ -42,16 +42,16 @@ final class DNSCloudClient { self.packetContinuation = packetPair.continuation } - private static func makeEndpointHost(address: String) -> NWEndpoint.Host { - if let ipv4Address = IPv4Address(address) { + private static func makeEndpointHost(address ip: String) -> NWEndpoint.Host { + if let ipv4Address = IPv4Address(ip) { return .ipv4(ipv4Address) } - if let ipv6Address = IPv6Address(address) { + if let ipv6Address = IPv6Address(ip) { return .ipv6(ipv6Address) } - return .name(address, nil) + preconditionFailure("invalid DNS cloud server IP: \(ip)") } func start() { diff --git a/Tun/Punchnet/DNS/DNSLocalClient.swift b/Tun/Punchnet/DNS/DNSLocalClient.swift index 749d8b0..05cd0e0 100644 --- a/Tun/Punchnet/DNS/DNSLocalClient.swift +++ b/Tun/Punchnet/DNS/DNSLocalClient.swift @@ -44,7 +44,7 @@ actor DNSLocalClient { private var nextTransactionID: UInt16 = 1 init(host: String) { - self.dnsServerEndpoint = .hostPort(host: NWEndpoint.Host(host), port: 53) + self.dnsServerEndpoint = .hostPort(host: Self.makeEndpointHost(ip: host), port: 53) let (stream, continuation) = AsyncThrowingStream.makeStream(of: Data.self, bufferingPolicy: .bufferingNewest(256)) self.packetFlow = stream @@ -54,6 +54,18 @@ actor DNSLocalClient { SDLLogger.log("[DNSLocalClient] packetFlow terminated: \(termination)") } } + + private static func makeEndpointHost(ip: String) -> NWEndpoint.Host { + if let ipv4Address = IPv4Address(ip) { + return .ipv4(ipv4Address) + } + + if let ipv6Address = IPv6Address(ip) { + return .ipv6(ipv6Address) + } + + preconditionFailure("invalid public DNS server IP: \(ip)") + } func start() { guard self.state == .idle else { diff --git a/Tun/Punchnet/SDLIPV6AssistClient.swift b/Tun/Punchnet/SDLIPV6AssistClient.swift index 66d7fa9..13d7d02 100644 --- a/Tun/Punchnet/SDLIPV6AssistClient.swift +++ b/Tun/Punchnet/SDLIPV6AssistClient.swift @@ -33,10 +33,12 @@ actor SDLIPV6AssistClient { private var pendingRequests: [UInt32: PendingRequest] = [:] init?(assistServerInfo: SDLV6Info) { - guard assistServerInfo.port <= UInt32(UInt16.max), let host = SDLUtil.ipv6DataToString(assistServerInfo.v6) else { + guard assistServerInfo.port <= UInt32(UInt16.max), + let host = SDLUtil.ipv6DataToString(assistServerInfo.v6), + let address = IPv6Address(host) else { return nil } - self.assistServerAddress = .hostPort(host: NWEndpoint.Host(host), port: NWEndpoint.Port(integerLiteral: UInt16(assistServerInfo.port))) + self.assistServerAddress = .hostPort(host: .ipv6(address), port: NWEndpoint.Port(integerLiteral: UInt16(assistServerInfo.port))) } func start() { diff --git a/Tun/Punchnet/SDLUtil.swift b/Tun/Punchnet/SDLUtil.swift index fc8f244..526838e 100644 --- a/Tun/Punchnet/SDLUtil.swift +++ b/Tun/Punchnet/SDLUtil.swift @@ -7,7 +7,6 @@ import Foundation import SystemConfiguration -import Network import Darwin struct SDLUtil { @@ -111,31 +110,4 @@ struct SDLUtil { } return results } - - // 域名解析 - static func resolveHostname(host: String) async -> String? { - let endpoint = NWEndpoint.hostPort(host: NWEndpoint.Host(host), port: 53) - let parameters = NWParameters.udp - // 即使还没正式开始,也加上这个,确保不会被残留的旧 utun 路由卡死 - parameters.prohibitedInterfaceTypes = [.other] - - let connection = NWConnection(to: endpoint, using: parameters) - - return await withCheckedContinuation { continuation in - connection.stateUpdateHandler = { state in - if case .ready = state { - if let path = connection.currentPath, - case .hostPort(let resolvedHost, _) = path.remoteEndpoint { - let ip = String(describing: resolvedHost) - continuation.resume(returning: ip) - connection.cancel() - } - } else if case .failed = state { - continuation.resume(returning: nil) - } - } - connection.start(queue: .global()) - } - } - } diff --git a/Tun/Punchnet/Super/SDLSuperClient.swift b/Tun/Punchnet/Super/SDLSuperClient.swift index 86b1acd..aab0530 100644 --- a/Tun/Punchnet/Super/SDLSuperClient.swift +++ b/Tun/Punchnet/Super/SDLSuperClient.swift @@ -97,16 +97,16 @@ actor SDLSuperClient { self.connection = connection } - private static func makeEndpointHost(address: String) -> NWEndpoint.Host { - if let ipv4Address = IPv4Address(address) { + private static func makeEndpointHost(address ip: String) -> NWEndpoint.Host { + if let ipv4Address = IPv4Address(ip) { return .ipv4(ipv4Address) } - if let ipv6Address = IPv6Address(address) { + if let ipv6Address = IPv6Address(ip) { return .ipv6(ipv6Address) } - return .name(address, nil) + preconditionFailure("invalid super server IP: \(ip)") } private func handleConnectionState(state: NWConnection.State) {