From b858be731030667e0104a923296be33ecd0c9f8d Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Wed, 6 May 2026 16:05:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=8F=98=E9=87=8F=E7=9A=84AR?= =?UTF-8?q?C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tun/Punchnet/Actors/SDLContextActor.swift | 21 ++++++++++++++++----- Tun/Punchnet/DNS/DNSLocalClient.swift | 1 + Tun/Punchnet/UDPHole/SDLUDPHole.swift | 18 +++++++++++------- Tun/Punchnet/UDPHole/SDLUDPHoleV6.swift | 7 +++++++ 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/Tun/Punchnet/Actors/SDLContextActor.swift b/Tun/Punchnet/Actors/SDLContextActor.swift index fc05cc9..6757622 100644 --- a/Tun/Punchnet/Actors/SDLContextActor.swift +++ b/Tun/Punchnet/Actors/SDLContextActor.swift @@ -374,7 +374,10 @@ actor SDLContextActor { self.udpHoleLocalAddress = localAddress defer { - self.udpHole = nil + if self.udpHole === udpHole { + self.udpHole = nil + self.udpHoleLocalAddress = nil + } } // 开始探测nat的类型 @@ -430,8 +433,10 @@ actor SDLContextActor { } defer { - self.udpHoleV6?.stop() - self.udpHoleV6 = nil + if self.udpHoleV6 === udpHoleV6 { + udpHoleV6.stop() + self.udpHoleV6 = nil + } } try await withThrowingTaskGroup { group in @@ -485,12 +490,18 @@ actor SDLContextActor { self.flowSessionManager.clear() - self.udpHoleMonitorTask?.cancel() + let udpHole = self.udpHole + self.udpHole = nil self.udpHoleLocalAddress = nil - self.udpHoleV6?.stop() + let udpHoleV6 = self.udpHoleV6 self.udpHoleV6 = nil + self.udpHoleMonitorTask?.cancel() + self.udpHoleMonitorTask = nil + udpHoleV6?.stop() + await udpHole?.stop() + self.dnsMonitorTask?.cancel() self.dnsLocalMonitorTask?.cancel() diff --git a/Tun/Punchnet/DNS/DNSLocalClient.swift b/Tun/Punchnet/DNS/DNSLocalClient.swift index ec5e5f1..749d8b0 100644 --- a/Tun/Punchnet/DNS/DNSLocalClient.swift +++ b/Tun/Punchnet/DNS/DNSLocalClient.swift @@ -82,6 +82,7 @@ actor DNSLocalClient { self.connection = connection self.cleanupTask = cleanupTask + connection.start(queue: .global()) } diff --git a/Tun/Punchnet/UDPHole/SDLUDPHole.swift b/Tun/Punchnet/UDPHole/SDLUDPHole.swift index 49f213e..453d1eb 100644 --- a/Tun/Punchnet/UDPHole/SDLUDPHole.swift +++ b/Tun/Punchnet/UDPHole/SDLUDPHole.swift @@ -76,7 +76,7 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler { private let messageContinuation: AsyncThrowingStream<(SocketAddress, SDLHoleMessage), Error>.Continuation private var isMessageContinuationFinished: Bool = false - private var counterActor: SDLUDPCounter + private let counterActor: SDLUDPCounter // 启动函数 init() throws { @@ -102,8 +102,9 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler { self.channel = channel + let counterActor = self.counterActor Task { - await self.counterActor.start() + await counterActor.start() } return localAddress @@ -117,8 +118,9 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler { let remoteAddress = envelope.remoteAddress let bytesCount = buffer.readableBytes + let counterActor = self.counterActor Task { - await self.counterActor.increment(direction: .inbound, from: remoteAddress.description, bytes: bytesCount) + await counterActor.increment(direction: .inbound, from: remoteAddress.description, bytes: bytesCount) } do { @@ -147,8 +149,9 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler { return } + let counterActor = self.counterActor Task { - await self.counterActor.increment(direction: .outbound, from: remoteAddress.description, bytes: data.count) + await counterActor.increment(direction: .outbound, from: remoteAddress.description, bytes: data.count) } var buffer = channel.allocator.buffer(capacity: data.count + 1) @@ -162,8 +165,8 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler { channel.writeAndFlush(envelope, promise: promise) } - promise.futureResult.whenFailure { err in - self.finishMessageContinuationIfNeed(throwing: .sendFaied(err)) + promise.futureResult.whenFailure { [weak self] err in + self?.finishMessageContinuationIfNeed(throwing: .sendFaied(err)) } } @@ -173,8 +176,9 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler { self.channel = nil try? self.group.syncShutdownGracefully() + let counterActor = self.counterActor Task { - await self.counterActor.stop() + await counterActor.stop() } SDLLogger.log("[SDLUDPHole] stopped", for: .debug) diff --git a/Tun/Punchnet/UDPHole/SDLUDPHoleV6.swift b/Tun/Punchnet/UDPHole/SDLUDPHoleV6.swift index d798e09..a82d747 100644 --- a/Tun/Punchnet/UDPHole/SDLUDPHoleV6.swift +++ b/Tun/Punchnet/UDPHole/SDLUDPHoleV6.swift @@ -118,9 +118,16 @@ final class SDLUDPHoleV6: ChannelInboundHandler { self.messageContinuation.finish() self.eventContinuation.finish() + + let channel = self.channel self.channel = nil + try? channel?.close().wait() try? self.group.syncShutdownGracefully() } + + deinit { + SDLLogger.log("[SDLUDPHoleV6] deinit", for: .debug) + } }