fix address

This commit is contained in:
anlicheng 2026-05-21 13:49:10 +08:00
parent 95e20fc6fe
commit 3cfe139512
5 changed files with 25 additions and 39 deletions

View File

@ -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() {

View File

@ -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
@ -55,6 +55,18 @@ actor DNSLocalClient {
}
}
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 {
return

View File

@ -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() {

View File

@ -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())
}
}
}

View File

@ -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) {