fix address
This commit is contained in:
parent
1c15c92c42
commit
2e9a3f2c97
@ -157,7 +157,7 @@ actor SDLContextActor {
|
||||
await self.puncherActor.start()
|
||||
await self.arpResolver.start()
|
||||
|
||||
let dnsService = DNSService(serverHost: self.config.serverHost, publicDnsServers: self.publicDnsServers) { [weak self] event in
|
||||
let dnsService = DNSService(serverAddress: self.config.serverAddress, publicDnsServers: self.publicDnsServers) { [weak self] event in
|
||||
await self?.handleDNSEvent(event)
|
||||
}
|
||||
self.dnsService = dnsService
|
||||
@ -173,7 +173,7 @@ actor SDLContextActor {
|
||||
await self.udpHoleServiceProxy.replace(udpHoleService)
|
||||
await udpHoleService.start(includeV6: false)
|
||||
|
||||
let superService = SDLSuperService(host: self.config.serverHost) { [weak self] message in
|
||||
let superService = SDLSuperService(serverName: self.config.serverName, serverAddress: self.config.serverAddress) { [weak self] message in
|
||||
await self?.handleSuperMessage(message: message)
|
||||
}
|
||||
await self.superServiceProxy.replace(superService)
|
||||
|
||||
@ -32,16 +32,28 @@ final class DNSCloudClient {
|
||||
private let packetContinuation: AsyncThrowingStream<Data, Error>.Continuation
|
||||
private var isPacketContinuationFinished: Bool = false
|
||||
|
||||
/// - Parameter host: 你的 sn-server 地址 (如 "8.8.8.8")
|
||||
/// - Parameter serverAddress: 你的 sn-server 地址 (如 "8.8.8.8")
|
||||
/// - Parameter port: 端口 (如 53)
|
||||
init(host: String, port: UInt16) {
|
||||
self.dnsServerAddress = .hostPort(host: NWEndpoint.Host(host), port: NWEndpoint.Port(integerLiteral: port))
|
||||
init(serverAddress: String, port: UInt16) {
|
||||
self.dnsServerAddress = .hostPort(host: Self.makeEndpointHost(address: serverAddress), port: NWEndpoint.Port(integerLiteral: port))
|
||||
|
||||
let packetPair = AsyncThrowingStream.makeStream(of: Data.self)
|
||||
self.packetFlow = packetPair.stream
|
||||
self.packetContinuation = packetPair.continuation
|
||||
}
|
||||
|
||||
private static func makeEndpointHost(address: String) -> NWEndpoint.Host {
|
||||
if let ipv4Address = IPv4Address(address) {
|
||||
return .ipv4(ipv4Address)
|
||||
}
|
||||
|
||||
if let ipv6Address = IPv6Address(address) {
|
||||
return .ipv6(ipv6Address)
|
||||
}
|
||||
|
||||
return .name(address, nil)
|
||||
}
|
||||
|
||||
func start() {
|
||||
// 1. 配置参数:这是解决环路的关键
|
||||
let parameters = NWParameters.udp
|
||||
|
||||
@ -8,7 +8,7 @@ actor DNSService {
|
||||
|
||||
typealias EventHandler = @Sendable (Event) async -> Void
|
||||
|
||||
private let serverHost: String
|
||||
private let serverAddress: String
|
||||
private let publicDnsServers: [String]
|
||||
private let onEvent: EventHandler
|
||||
|
||||
@ -18,8 +18,8 @@ actor DNSService {
|
||||
private var dnsLocalClient: DNSLocalClient?
|
||||
private var dnsLocalMonitorTask: Task<Void, Never>?
|
||||
|
||||
init(serverHost: String, publicDnsServers: [String], onEvent: @escaping EventHandler) {
|
||||
self.serverHost = serverHost
|
||||
init(serverAddress: String, publicDnsServers: [String], onEvent: @escaping EventHandler) {
|
||||
self.serverAddress = serverAddress
|
||||
self.publicDnsServers = publicDnsServers
|
||||
self.onEvent = onEvent
|
||||
}
|
||||
@ -88,7 +88,7 @@ actor DNSService {
|
||||
}
|
||||
|
||||
private func runCloud() async throws {
|
||||
let dnsClient = DNSCloudClient(host: self.serverHost, port: 15353)
|
||||
let dnsClient = DNSCloudClient(serverAddress: self.serverAddress, port: 15353)
|
||||
self.dnsClient = dnsClient
|
||||
dnsClient.start()
|
||||
|
||||
|
||||
@ -48,7 +48,8 @@ public class SDLConfiguration {
|
||||
// 当前的客户端版本
|
||||
let version: Int
|
||||
|
||||
let serverHost: String
|
||||
let serverName: String
|
||||
let serverAddress: String
|
||||
|
||||
let stunSocketAddress: SocketAddress
|
||||
|
||||
@ -64,7 +65,8 @@ public class SDLConfiguration {
|
||||
var exitNode: ExitNode?
|
||||
|
||||
public init(version: Int,
|
||||
serverHost: String,
|
||||
serverName: String,
|
||||
serverAddress: String,
|
||||
stunServers: [String],
|
||||
clientId: String,
|
||||
networkAddress: NetworkAddress,
|
||||
@ -73,8 +75,9 @@ public class SDLConfiguration {
|
||||
identityId: UInt32,
|
||||
exitNode: ExitNode?) {
|
||||
self.version = version
|
||||
self.serverHost = serverHost
|
||||
let stunHosts = stunServers.isEmpty ? [serverHost] : stunServers
|
||||
self.serverName = serverName
|
||||
self.serverAddress = serverAddress
|
||||
let stunHosts = stunServers.isEmpty ? [serverAddress] : stunServers
|
||||
self.stunSocketAddress = Self.makeStunSocketAddress(host: stunHosts[0], port: 1365)
|
||||
self.stunProbeSocketAddressArray = stunHosts.map { stunServer in
|
||||
[
|
||||
@ -94,6 +97,11 @@ public class SDLConfiguration {
|
||||
|
||||
private extension SDLConfiguration {
|
||||
|
||||
static func resolveHostAddress(host: String, port: Int) -> String {
|
||||
let address = try! SocketAddress.makeAddressResolvingHost(host, port: port)
|
||||
return address.ipAddress!
|
||||
}
|
||||
|
||||
static func makeStunSocketAddress(host: String, port: Int) -> SocketAddress {
|
||||
return try! SocketAddress.makeAddressResolvingHost(host, port: port)
|
||||
}
|
||||
@ -105,7 +113,7 @@ extension SDLConfiguration {
|
||||
|
||||
static func parse(options: [String: NSObject]) -> SDLConfiguration? {
|
||||
guard let version = options["version"] as? Int,
|
||||
let serverHost = options["server_host"] as? String,
|
||||
let serverName = options["server_host"] as? String,
|
||||
let stunAssistHost = options["stun_assist_host"] as? String,
|
||||
let accessToken = options["access_token"] as? String,
|
||||
let identityId = options["identity_id"] as? UInt32,
|
||||
@ -125,9 +133,12 @@ extension SDLConfiguration {
|
||||
exitNode = .init(exitNodeIp: exitNodeIp)
|
||||
}
|
||||
|
||||
let serverAddress = Self.resolveHostAddress(host: serverName, port: 1443)
|
||||
|
||||
return SDLConfiguration(version: version,
|
||||
serverHost: serverHost,
|
||||
stunServers: [serverHost, stunAssistHost],
|
||||
serverName: serverName,
|
||||
serverAddress: serverAddress,
|
||||
stunServers: [serverName, stunAssistHost],
|
||||
clientId: clientId,
|
||||
networkAddress: networkAddress,
|
||||
hostname: hostname,
|
||||
|
||||
@ -45,11 +45,13 @@ actor SDLSuperClient {
|
||||
|
||||
private var connection: NWConnection?
|
||||
|
||||
private let host: String
|
||||
private let serverName: String
|
||||
private let serverAddress: String
|
||||
private let port: UInt16
|
||||
|
||||
init(host: String, port: UInt16, maxBufferSize: Int = 2 * 1024 * 1024) {
|
||||
self.host = host
|
||||
init(serverName: String, serverAddress: String, port: UInt16, maxBufferSize: Int = 2 * 1024 * 1024) {
|
||||
self.serverName = serverName
|
||||
self.serverAddress = serverAddress
|
||||
self.port = port
|
||||
|
||||
self.frameParser = SDLQUICFrameParser(maxBufferSize: maxBufferSize)
|
||||
@ -57,10 +59,14 @@ actor SDLSuperClient {
|
||||
}
|
||||
|
||||
func start() {
|
||||
let host = self.host
|
||||
let serverName = self.serverName
|
||||
let serverAddress = self.serverAddress
|
||||
let queue = DispatchQueue(label: "com.sdl.SuperClient.queue") // 专用队列保证线程安全
|
||||
|
||||
let options = NWProtocolTLS.Options()
|
||||
serverName.withCString {
|
||||
sec_protocol_options_set_tls_server_name(options.securityProtocolOptions, $0)
|
||||
}
|
||||
sec_protocol_options_add_tls_application_protocol(
|
||||
options.securityProtocolOptions,
|
||||
"punchnet/1.0"
|
||||
@ -71,7 +77,7 @@ actor SDLSuperClient {
|
||||
options.securityProtocolOptions,
|
||||
{ _, trust, complete in
|
||||
// 执行公钥校验
|
||||
complete(TLSVerifier.verify(trust: trust, host: host))
|
||||
complete(TLSVerifier.verify(trust: trust, host: serverName))
|
||||
},
|
||||
queue
|
||||
)
|
||||
@ -81,7 +87,7 @@ actor SDLSuperClient {
|
||||
// 关键:让 Network.framework 忽略系统代理
|
||||
params.preferNoProxies = true
|
||||
|
||||
let connection = NWConnection(host: .init(host), port: .init(rawValue: port)!, using: params)
|
||||
let connection = NWConnection(host: Self.makeEndpointHost(address: serverAddress), port: .init(rawValue: port)!, using: params)
|
||||
|
||||
connection.stateUpdateHandler = { [weak self] state in
|
||||
SDLLogger.log("[SDLSuperClient] new state: \(state)", for: .debug)
|
||||
@ -94,6 +100,18 @@ actor SDLSuperClient {
|
||||
self.connection = connection
|
||||
}
|
||||
|
||||
private static func makeEndpointHost(address: String) -> NWEndpoint.Host {
|
||||
if let ipv4Address = IPv4Address(address) {
|
||||
return .ipv4(ipv4Address)
|
||||
}
|
||||
|
||||
if let ipv6Address = IPv6Address(address) {
|
||||
return .ipv6(ipv6Address)
|
||||
}
|
||||
|
||||
return .name(address, nil)
|
||||
}
|
||||
|
||||
private func handleConnectionState(state: NWConnection.State) {
|
||||
switch state {
|
||||
case .ready:
|
||||
|
||||
@ -3,15 +3,17 @@ import Foundation
|
||||
actor SDLSuperService {
|
||||
typealias MessageHandler = @Sendable (SDLQUICInboundMessage) async -> Void
|
||||
|
||||
private let host: String
|
||||
private let serverName: String
|
||||
private let serverAddress: String
|
||||
private let port: UInt16
|
||||
private let onMessage: MessageHandler
|
||||
|
||||
private var superClient: SDLSuperClient?
|
||||
private var monitorTask: Task<Void, Never>?
|
||||
|
||||
init(host: String, port: UInt16 = 1443, onMessage: @escaping MessageHandler) {
|
||||
self.host = host
|
||||
init(serverName: String, serverAddress: String, port: UInt16 = 1443, onMessage: @escaping MessageHandler) {
|
||||
self.serverName = serverName
|
||||
self.serverAddress = serverAddress
|
||||
self.port = port
|
||||
self.onMessage = onMessage
|
||||
}
|
||||
@ -49,7 +51,7 @@ actor SDLSuperService {
|
||||
}
|
||||
|
||||
private func runOnce() async throws {
|
||||
let superClient = SDLSuperClient(host: self.host, port: self.port)
|
||||
let superClient = SDLSuperClient(serverName: self.serverName, serverAddress: self.serverAddress, port: self.port)
|
||||
self.superClient = superClient
|
||||
await superClient.start()
|
||||
|
||||
@ -72,7 +74,7 @@ actor SDLSuperService {
|
||||
try await Task.sleep(for: .seconds(0.5))
|
||||
try Task.checkCancellation()
|
||||
|
||||
SDLLogger.log("[SDLSuperService] start super client: \(self.host)")
|
||||
SDLLogger.log("[SDLSuperService] start super client: \(self.serverAddress)")
|
||||
|
||||
try await withThrowingTaskGroup(of: Void.self) { group in
|
||||
defer {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user