diff --git a/Tun/Punchnet/Actors/SDLContextActor.swift b/Tun/Punchnet/Actors/SDLContextActor.swift index 6f3383c..038fc3b 100644 --- a/Tun/Punchnet/Actors/SDLContextActor.swift +++ b/Tun/Punchnet/Actors/SDLContextActor.swift @@ -76,7 +76,6 @@ actor SDLContextActor { // 处理权限控制 private let identifyStore: IdentityStore private let snapshotPublisher: SnapshotPublisher - private let policyRequesterActor: PolicyRequesterActor // 注册任务 private var registerTask: Task? @@ -97,7 +96,6 @@ actor SDLContextActor { let snapshotPublisher = SnapshotPublisher(initial: IdentitySnapshot.empty()) self.identifyStore = IdentityStore(publisher: snapshotPublisher) self.snapshotPublisher = snapshotPublisher - self.policyRequesterActor = PolicyRequesterActor() } public func start() { @@ -578,7 +576,7 @@ actor SDLContextActor { policyRequest.srcIdentityID = data.identityID policyRequest.dstIdentityID = self.config.identityId - await self.policyRequesterActor.submitPolicyRequest(using: self.quicClient, request: &policyRequest) + await self.identifyStore.policyRequest(using: self.quicClient, request: &policyRequest) } default: SDLLogger.shared.log("[SDLContext] get invalid packet", level: .debug) diff --git a/Tun/Punchnet/Policy/IdentityStore.swift b/Tun/Punchnet/Policy/IdentityStore.swift index abc8288..cdfadfb 100644 --- a/Tun/Punchnet/Policy/IdentityStore.swift +++ b/Tun/Punchnet/Policy/IdentityStore.swift @@ -10,6 +10,13 @@ import NIO actor IdentityStore { typealias IdentityID = UInt32 + // 处理权限的请求问题 + nonisolated private let cooldown: Duration = .seconds(5) + // identityId + private var coolingDown: Set = [] + // 处理各个请求的版本问题, map[identityId] = version + private var versions: [UInt32: UInt32] = [:] + nonisolated private let alloctor = ByteBufferAllocator() private let publisher: SnapshotPublisher @@ -19,6 +26,32 @@ actor IdentityStore { self.publisher = publisher } + // 提交权限请求 + func policyRequest(using quicClient: SDLQUICClient?, request: inout SDLPolicyRequest) { + let identityId = request.srcIdentityID + guard let quicClient, !coolingDown.contains(identityId) else { + return + } + + // 触发一次打洞 + coolingDown.insert(identityId) + + let version = self.versions[identityId, default: 1] + request.version = version + // 更新请求的版本问题 + self.versions[identityId] = version + 1 + // 发送请求 + if let queryData = try? request.serializedData() { + quicClient.send(type: .policyRequest, data: queryData) + } + + Task { + // 启动冷却期 + try? await Task.sleep(for: .seconds(5)) + self.endCooldown(for: identityId) + } + } + func apply(policyResponse: SDLPolicyResponse) { let id = policyResponse.srcIdentityID let version = policyResponse.version @@ -50,4 +83,8 @@ actor IdentityStore { return IdentitySnapshot(identityMap: identityMap) } + private func endCooldown(for key: UInt32) { + self.coolingDown.remove(key) + } + } diff --git a/Tun/Punchnet/Policy/PolicyRequesterActor.swift b/Tun/Punchnet/Policy/PolicyRequesterActor.swift deleted file mode 100644 index 26bc99c..0000000 --- a/Tun/Punchnet/Policy/PolicyRequesterActor.swift +++ /dev/null @@ -1,54 +0,0 @@ -// -// SDLPuncherActor.swift -// Tun -// -// Created by 安礼成 on 2026/1/7. -// - -import Foundation -import NIOCore - -actor PolicyRequesterActor { - nonisolated private let cooldown: Duration = .seconds(5) - - // identityId - private var coolingDown: Set = [] - - // 处理各个请求的版本问题, map[identityId] = version - private var versions: [UInt32: UInt32] = [:] - - init() { - - } - - // 提交权限请求 - func submitPolicyRequest(using quicClient: SDLQUICClient?, request: inout SDLPolicyRequest) { - let identityId = request.srcIdentityID - guard let quicClient, !coolingDown.contains(identityId) else { - return - } - - // 触发一次打洞 - coolingDown.insert(identityId) - - let version = self.versions[identityId, default: 1] - request.version = version - // 更新请求的版本问题 - self.versions[identityId] = version + 1 - // 发送请求 - if let queryData = try? request.serializedData() { - quicClient.send(type: .policyRequest, data: queryData) - } - - Task { - // 启动冷却期 - try? await Task.sleep(for: .seconds(5)) - self.endCooldown(for: identityId) - } - } - - private func endCooldown(for key: UInt32) { - self.coolingDown.remove(key) - } - -}