From ff9aaceefef100f4e00f520f0b355a764d4a93c6 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Wed, 6 May 2026 17:58:17 +0800 Subject: [PATCH] rename SDLQuicClient -> SDLSuperClient --- Tun/Punchnet/Actors/ArpServer.swift | 6 +- Tun/Punchnet/Actors/SDLContextActor.swift | 60 +++++++++---------- Tun/Punchnet/Actors/SDLPuncherActor.swift | 6 +- ...LQuicClient.swift => SDLSuperClient.swift} | 26 ++++---- Tun/Punchnet/Policy/IdentityStore.swift | 14 ++--- 5 files changed, 54 insertions(+), 58 deletions(-) rename Tun/Punchnet/Actors/{SDLQuicClient.swift => SDLSuperClient.swift} (94%) diff --git a/Tun/Punchnet/Actors/ArpServer.swift b/Tun/Punchnet/Actors/ArpServer.swift index e748a7c..5607ac6 100644 --- a/Tun/Punchnet/Actors/ArpServer.swift +++ b/Tun/Punchnet/Actors/ArpServer.swift @@ -70,8 +70,8 @@ actor ArpServer { self.coolingDown = [:] } - func arpRequest(targetIp: UInt32, use quicClient: SDLQUICClient?) async throws { - guard let quicClient, self.coolingDown[targetIp] == nil else { + func arpRequest(targetIp: UInt32, use superClient: SDLSuperClient?) async throws { + guard let superClient, self.coolingDown[targetIp] == nil else { return } @@ -82,7 +82,7 @@ actor ArpServer { var arpRequest = SDLArpRequest() arpRequest.targetIp = targetIp - await quicClient.send(type: .arpRequest, data: try arpRequest.serializedData()) + await superClient.send(type: .arpRequest, data: try arpRequest.serializedData()) } func handleArpResponse(arpResponse: SDLArpResponse) { diff --git a/Tun/Punchnet/Actors/SDLContextActor.swift b/Tun/Punchnet/Actors/SDLContextActor.swift index 9448d71..1526595 100644 --- a/Tun/Punchnet/Actors/SDLContextActor.swift +++ b/Tun/Punchnet/Actors/SDLContextActor.swift @@ -71,7 +71,7 @@ actor SDLContextActor { private var dnsLocalClient: DNSLocalClient? private var dnsLocalMonitorTask: Task? - private var quicClient: SDLQUICClient? + private var superClient: SDLSuperClient? private var superMonitorTask: Task? nonisolated private let puncherActor: SDLPuncherActor @@ -161,20 +161,20 @@ actor SDLContextActor { private func startSuperClient() async throws { // 启动monitor - let quicClient = SDLQUICClient(host: self.config.serverHost, port: 1443) - self.quicClient = quicClient - await quicClient.start() + let superClient = SDLSuperClient(host: self.config.serverHost, port: 1443) + self.superClient = superClient + await superClient.start() defer { - if self.quicClient === quicClient { - self.quicClient = nil + if self.superClient === superClient { + self.superClient = nil } SDLLogger.log("[SDLContext] startSuperClient defer") } // 这里必须等待quic的协商完成 try await Task.sleep(for: .seconds(0.5)) - SDLLogger.log("[SDLContext] start quic client: \(self.config.serverHost)") + SDLLogger.log("[SDLContext] start super client: \(self.config.serverHost)") do { try await withTaskCancellationHandler { @@ -184,7 +184,7 @@ actor SDLContextActor { } group.addTask { - for try await message in await quicClient.messageStream { + for try await message in await superClient.messageStream { try Task.checkCancellation() await self.handleQUICMessage(message: message) } @@ -194,21 +194,21 @@ actor SDLContextActor { while true { try await Task.sleep(for: .seconds(5)) try Task.checkCancellation() - await quicClient.send(type: .ping, data: Data()) + await superClient.send(type: .ping, data: Data()) } } try await group.next() } } onCancel: { - SDLLogger.log("[SDLQUICClient] startSuperClient taskGroup cancel", for: .debug) + SDLLogger.log("[SDLSuperClient] startSuperClient taskGroup cancel", for: .debug) Task { SDLLogger.log("[SDLContext] startSuperClient onCancel") - await quicClient.stop() + await superClient.stop() } } } catch let err { - await quicClient.stop() + await superClient.stop() SDLLogger.log("[SDLContext] startSuperClient catch err: \(err)") throw err } @@ -294,23 +294,19 @@ actor SDLContextActor { dnsClient.start() defer { + dnsClient.stop() self.dnsClient = nil } - do { - try await withTaskCancellationHandler { - for try await packet in dnsClient.packetFlow { - try Task.checkCancellation() - - let nePacket = NEPacket(data: packet, protocolFamily: 2) - self.provider.packetFlow.writePacketObjects([nePacket]) - } - } onCancel: { - dnsClient.stop() + try await withTaskCancellationHandler { + for try await packet in dnsClient.packetFlow { + try Task.checkCancellation() + + let nePacket = NEPacket(data: packet, protocolFamily: 2) + self.provider.packetFlow.writePacketObjects([nePacket]) } - } catch let err { + } onCancel: { dnsClient.stop() - throw err } } @@ -522,11 +518,11 @@ actor SDLContextActor { dnsClient?.stop() await dnsLocalClient?.stop() - let quicClient = self.quicClient - self.quicClient = nil + let superClient = self.superClient + self.superClient = nil self.superMonitorTask?.cancel() self.superMonitorTask = nil - await quicClient?.stop() + await superClient?.stop() self.readTask?.cancel() self.readTask = nil @@ -565,7 +561,7 @@ actor SDLContextActor { while true { try await Task.sleep(for: .seconds(300)) SDLLogger.log("[SDLContext] updatePolicyTask execute") - await self.identifyStore.batUpdatePolicy(using: self.quicClient, dstIdentityID: self.config.identityId) + await self.identifyStore.batUpdatePolicy(using: self.superClient, dstIdentityID: self.config.identityId) } } catch let err { SDLLogger.log("[SDLContext] updatePolicyTask stop with err: \(err)") @@ -899,7 +895,7 @@ extension SDLContextActor { if let registerSuperData = try? registerSuper.serializedData() { SDLLogger.log("[SDLContext] will send register super") - await self.quicClient?.send(type: .registerSuper, data: registerSuperData) + await self.superClient?.send(type: .registerSuper, data: registerSuperData) } } @@ -977,7 +973,7 @@ extension SDLContextActor { case .requestPolicy(let srcIdentityID): SDLLogger.log("[SDLContext] not found identity: \(srcIdentityID) ruleMap", for: .debug) // 向服务器请求权限逻辑 - await self.identifyStore.policyRequest(srcIdentityId: srcIdentityID, dstIdentityId: self.config.identityId, using: self.quicClient) + await self.identifyStore.policyRequest(srcIdentityId: srcIdentityID, dstIdentityId: self.config.identityId, using: self.superClient) case .none: () } @@ -1049,7 +1045,7 @@ extension SDLContextActor { // let arpReqeust = ARPPacket.arpRequest(senderIP: networkAddr.ip, senderMAC: networkAddr.mac, targetIP: dstIp) // await self.routeLayerPacket(dstMac: ARPPacket.broadcastMac , type: .arp, data: arpReqeust.marshal()) - try? await self.arpServer.arpRequest(targetIp: ip, use: self.quicClient) + try? await self.arpServer.arpRequest(targetIp: ip, use: self.superClient) } } @@ -1088,7 +1084,7 @@ extension SDLContextActor { self.flowTracer.inc(num: payload.count, type: .forward) // 尝试打洞 - await self.puncherActor.submitRegisterRequest(quicClient: self.quicClient, request: request) + await self.puncherActor.submitRegisterRequest(superClient: self.superClient, request: request) } } diff --git a/Tun/Punchnet/Actors/SDLPuncherActor.swift b/Tun/Punchnet/Actors/SDLPuncherActor.swift index 1a8ea83..4f1bc4c 100644 --- a/Tun/Punchnet/Actors/SDLPuncherActor.swift +++ b/Tun/Punchnet/Actors/SDLPuncherActor.swift @@ -64,8 +64,8 @@ actor SDLPuncherActor { } } - func submitRegisterRequest(quicClient: SDLQUICClient?, request: RegisterRequest) async { - guard let quicClient else { + func submitRegisterRequest(superClient: SDLSuperClient?, request: RegisterRequest) async { + guard let superClient else { return } @@ -90,7 +90,7 @@ actor SDLPuncherActor { phase: .waitingPeerInfo(deadline: now.addingTimeInterval(self.peerInfoTimeout)) ) - await quicClient.send(type: .queryInfo, data: queryData) + await superClient.send(type: .queryInfo, data: queryData) } func handlePeerInfo(using udpHole: SDLUDPHole?, udpHoleV6: SDLUDPHoleV6?, peerInfo: SDLPeerInfo) async { diff --git a/Tun/Punchnet/Actors/SDLQuicClient.swift b/Tun/Punchnet/Actors/SDLSuperClient.swift similarity index 94% rename from Tun/Punchnet/Actors/SDLQuicClient.swift rename to Tun/Punchnet/Actors/SDLSuperClient.swift index 2c40c42..0d48e04 100644 --- a/Tun/Punchnet/Actors/SDLQuicClient.swift +++ b/Tun/Punchnet/Actors/SDLSuperClient.swift @@ -1,5 +1,5 @@ // -// SDLQuicClient.swift +// SDLSuperClient.swift // Tun // // Created by 安礼成 on 2026/2/13. @@ -25,7 +25,7 @@ enum SDLQUICError: Error { case dataStreamClosed } -actor SDLQUICClient { +actor SDLSuperClient { enum State { case idle case running @@ -58,7 +58,7 @@ actor SDLQUICClient { func start() { let host = self.host - let queue = DispatchQueue(label: "com.sdl.QUICClient.queue") // 专用队列保证线程安全 + let queue = DispatchQueue(label: "com.sdl.SuperClient.queue") // 专用队列保证线程安全 let options = NWProtocolTLS.Options() sec_protocol_options_add_tls_application_protocol( @@ -75,7 +75,7 @@ actor SDLQUICClient { }, queue ) - SDLLogger.log("[SDLQUICClient] start with tls protocol", for: .debug) + SDLLogger.log("[SDLSuperClient] start with tls protocol", for: .debug) let params = NWParameters(tls: options) // 关键:让 Network.framework 忽略系统代理 @@ -84,7 +84,7 @@ actor SDLQUICClient { let connection = NWConnection(host: .init(host), port: .init(rawValue: port)!, using: params) connection.stateUpdateHandler = { [weak self] state in - SDLLogger.log("[SDLQUICClient] new state: \(state)", for: .debug) + SDLLogger.log("[SDLSuperClient] new state: \(state)", for: .debug) Task { await self?.handleConnectionState(state: state) } @@ -156,7 +156,7 @@ actor SDLQUICClient { connection.send(content: packet, completion: .contentProcessed { [weak self] error in if let error { Task { - SDLLogger.log("[SDLQUICClient] send data get error: \(error)", for: .debug) + SDLLogger.log("[SDLSuperClient] send data get error: \(error)", for: .debug) await self?.finishMessageContinuationIfNeed(throwing: .writeFailed(error)) } } @@ -201,17 +201,17 @@ actor SDLQUICClient { self.finishMessageContinuationIfNeed(throwing: nil) - SDLLogger.log("[SDLQUICClient] stopped") + SDLLogger.log("[SDLSuperClient] stopped") } deinit { - SDLLogger.log("[SDLQUICClient] deinit") + SDLLogger.log("[SDLSuperClient] deinit") } } // --MARK: 数据累加器 -extension SDLQUICClient { +extension SDLSuperClient { class SDLQUICFrameParser { private let allocator = ByteBufferAllocator() @@ -266,7 +266,7 @@ extension SDLQUICClient { } // --MARK: 编解码器 -extension SDLQUICClient { +extension SDLSuperClient { enum SDLQUICCodec { public static func decode(frame: ByteBuffer) -> SDLQUICInboundMessage? { @@ -317,14 +317,14 @@ extension SDLQUICClient { case .event: guard let bytes = buffer.readBytes(length: buffer.readableBytes), let event = try? SDLEvent(serializedBytes: bytes) else { - SDLLogger.log("SDLQUICClient decode Event Error", for: .debug) + SDLLogger.log("SDLSuperClient decode Event Error", for: .debug) return nil } return .event(event) case .pong: return .pong default: - SDLLogger.log("SDLQUICClient decode miss type: \(type)", for: .debug) + SDLLogger.log("SDLSuperClient decode miss type: \(type)", for: .debug) return nil } @@ -333,7 +333,7 @@ extension SDLQUICClient { } // --MARK: tls验证 -extension SDLQUICClient { +extension SDLSuperClient { enum TLSVerifier { // 你的 Base64 公钥指纹 diff --git a/Tun/Punchnet/Policy/IdentityStore.swift b/Tun/Punchnet/Policy/IdentityStore.swift index 716ddb6..e5e9d3a 100644 --- a/Tun/Punchnet/Policy/IdentityStore.swift +++ b/Tun/Punchnet/Policy/IdentityStore.swift @@ -25,9 +25,9 @@ actor IdentityStore { self.publisher = publisher } - // 批量更新, 有外部任务驱动,因为这里依赖于当前的quicClient - func batUpdatePolicy(using quicClient: SDLQUICClient?, dstIdentityID: UInt32) async { - guard let quicClient else { + // 批量更新, 有外部任务驱动,因为这里依赖于当前的superClient + func batUpdatePolicy(using superClient: SDLSuperClient?, dstIdentityID: UInt32) async { + guard let superClient else { return } @@ -39,14 +39,14 @@ actor IdentityStore { // 发送请求 if let queryData = try? policyRequest.serializedData() { - await quicClient.send(type: .policyRequest, data: queryData) + await superClient.send(type: .policyRequest, data: queryData) } } } // 提交权限请求 - func policyRequest(srcIdentityId: UInt32, dstIdentityId: UInt32, using quicClient: SDLQUICClient?) async { - guard let quicClient, !coolingDown.contains(srcIdentityId) else { + func policyRequest(srcIdentityId: UInt32, dstIdentityId: UInt32, using superClient: SDLSuperClient?) async { + guard let superClient, !coolingDown.contains(srcIdentityId) else { return } @@ -59,7 +59,7 @@ actor IdentityStore { coolingDown.insert(srcIdentityId) // 发送请求 if let queryData = try? policyRequest.serializedData() { - await quicClient.send(type: .policyRequest, data: queryData) + await superClient.send(type: .policyRequest, data: queryData) } Task {