From e78e49c45bdb110f70c2dcb6a5973e277eb11062 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Wed, 20 May 2026 15:20:25 +0800 Subject: [PATCH] fix superServiceProxy --- Tun/Punchnet/Context/SDLContextActor.swift | 18 ++++++-------- Tun/Punchnet/Policy/PolicyService.swift | 8 ++---- Tun/Punchnet/Super/SDLSuperService.swift | 29 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/Tun/Punchnet/Context/SDLContextActor.swift b/Tun/Punchnet/Context/SDLContextActor.swift index f4963a5..7c60f51 100644 --- a/Tun/Punchnet/Context/SDLContextActor.swift +++ b/Tun/Punchnet/Context/SDLContextActor.swift @@ -70,8 +70,8 @@ actor SDLContextActor { private var udpHoleService: SDLUDPHoleService? private var dnsService: SDLDNSService? - private var superService: SDLSuperService? private var packetReaderService: SDLPacketReaderService? + private let superServiceProxy = SDLSuperServiceProxy() private let publicDnsServers = ["223.5.5.5", "119.29.29.29"] @@ -136,7 +136,7 @@ actor SDLContextActor { let superService = SDLSuperService(host: self.config.serverHost) { [weak self] message in await self?.handleSuperMessage(message: message) } - self.superService = superService + await self.superServiceProxy.replace(superService) await superService.start() } @@ -165,9 +165,7 @@ actor SDLContextActor { self.dnsService = nil await dnsService?.stop() - let superService = self.superService - self.superService = nil - await superService?.stop() + await self.superServiceProxy.stop() self.sessionToken = nil self.dataCipher = nil @@ -320,7 +318,7 @@ extension SDLContextActor { while true { try await Task.sleep(for: .seconds(300)) SDLLogger.log("[SDLContext] updatePolicyTask execute") - await self.policyService.updatePolicy(superService: self.superService) + await self.policyService.updatePolicy(superServiceProxy: self.superServiceProxy) } } catch let err { SDLLogger.log("[SDLContext] updatePolicyTask stop with err: \(err)") @@ -389,7 +387,7 @@ extension SDLContextActor { if let registerSuperData = try? registerSuper.serializedData() { SDLLogger.log("[SDLContext] will send register super") - await self.superService?.send(type: .registerSuper, data: registerSuperData) + await self.superServiceProxy.send(type: .registerSuper, data: registerSuperData) } } @@ -505,7 +503,7 @@ extension SDLContextActor { case .requestPolicy(let srcIdentityID): SDLLogger.log("[SDLContext] not found identity: \(srcIdentityID) ruleMap", for: .debug) if let queryData = await self.policyService.identifyStore.makePolicyRequest(srcIdentityId: srcIdentityID, dstIdentityId: self.config.identityId) { - await self.superService?.send(type: .policyRequest, data: queryData) + await self.superServiceProxy.send(type: .policyRequest, data: queryData) } case .none: () @@ -721,7 +719,7 @@ extension SDLContextActor { else { SDLLogger.log("[SDLContext] dstIp: \(asIpAddress(ip)) arp query not found, broadcast", for: .trace) if let arpRequest = try? await self.arpServer.makeArpRequest(targetIp: ip) { - await self.superService?.send(type: .arpRequest, data: arpRequest) + await self.superServiceProxy.send(type: .arpRequest, data: arpRequest) } } } @@ -762,7 +760,7 @@ extension SDLContextActor { // 尝试打洞 if let queryData = await self.puncherActor.makeQueryInfoRequest(request: request) { - await self.superService?.send(type: .queryInfo, data: queryData) + await self.superServiceProxy.send(type: .queryInfo, data: queryData) } } } diff --git a/Tun/Punchnet/Policy/PolicyService.swift b/Tun/Punchnet/Policy/PolicyService.swift index d5742dd..b50b83a 100644 --- a/Tun/Punchnet/Policy/PolicyService.swift +++ b/Tun/Punchnet/Policy/PolicyService.swift @@ -58,14 +58,10 @@ actor PolicyService { return false } - func updatePolicy(superService: SDLSuperService?) async { - guard let superService else { - return - } - + func updatePolicy(superServiceProxy: SDLSuperServiceProxy) async { let requests = await self.identifyStore.makeBatchPolicyRequests(dstIdentityID: self.identityId) for request in requests { - await superService.send(type: .policyRequest, data: request) + await superServiceProxy.send(type: .policyRequest, data: request) } } diff --git a/Tun/Punchnet/Super/SDLSuperService.swift b/Tun/Punchnet/Super/SDLSuperService.swift index 7974c46..0c7036d 100644 --- a/Tun/Punchnet/Super/SDLSuperService.swift +++ b/Tun/Punchnet/Super/SDLSuperService.swift @@ -109,3 +109,32 @@ actor SDLSuperService { SDLLogger.log("[SDLSuperService] cleanup") } } + +actor SDLSuperServiceProxy { + private var superService: SDLSuperService? + private var generation: UInt64 = 0 + + func replace(_ superService: SDLSuperService?) async { + self.generation &+= 1 + + let oldSuperService = self.superService + self.superService = superService + + if oldSuperService !== superService { + await oldSuperService?.stop() + } + } + + func stop() async { + self.generation &+= 1 + + let superService = self.superService + self.superService = nil + + await superService?.stop() + } + + func send(type: SDLPacketType, data: Data) async { + await self.superService?.send(type: type, data: data) + } +}