diff --git a/Tun/Punchnet/Policy/IdentitySnapshot.swift b/Tun/Punchnet/Policy/IdentitySnapshot.swift deleted file mode 100644 index e39735b..0000000 --- a/Tun/Punchnet/Policy/IdentitySnapshot.swift +++ /dev/null @@ -1,26 +0,0 @@ -// -// IdentitySnapshot.swift -// punchnet -// -// Created by 安礼成 on 2026/2/5. -// -import Foundation - -final class IdentitySnapshot: Snapshot { - typealias IdentityID = UInt32 - - private let identityMap: [IdentityID: IdentityRuleMap] - - init(identityMap: [IdentityID : IdentityRuleMap]) { - self.identityMap = identityMap - } - - func lookup(_ id: IdentityID) -> IdentityRuleMap? { - return self.identityMap[id] - } - - static func empty() -> IdentitySnapshot { - return IdentitySnapshot(identityMap: [:]) - } - -} diff --git a/Tun/Punchnet/Policy/PolicyRuleSnapshot.swift b/Tun/Punchnet/Policy/PolicyRuleSnapshot.swift new file mode 100644 index 0000000..22fc0b2 --- /dev/null +++ b/Tun/Punchnet/Policy/PolicyRuleSnapshot.swift @@ -0,0 +1,26 @@ +// +// PolicyRuleSnapshot.swift +// punchnet +// +// Created by 安礼成 on 2026/2/5. +// +import Foundation + +final class PolicyRuleSnapshot: Snapshot { + typealias IdentityID = UInt32 + + private let ruleMapByIdentity: [IdentityID: IdentityRuleMap] + + init(ruleMapByIdentity: [IdentityID : IdentityRuleMap]) { + self.ruleMapByIdentity = ruleMapByIdentity + } + + func lookup(_ id: IdentityID) -> IdentityRuleMap? { + return self.ruleMapByIdentity[id] + } + + static func empty() -> PolicyRuleSnapshot { + return PolicyRuleSnapshot(ruleMapByIdentity: [:]) + } + +} diff --git a/Tun/Punchnet/Policy/PolicyRuleStore.swift b/Tun/Punchnet/Policy/PolicyRuleStore.swift index a0a09f4..493d65a 100644 --- a/Tun/Punchnet/Policy/PolicyRuleStore.swift +++ b/Tun/Punchnet/Policy/PolicyRuleStore.swift @@ -18,15 +18,15 @@ actor PolicyRuleStore { nonisolated private let alloctor = ByteBufferAllocator() - private let publisher: SnapshotPublisher - private var identityMap: [UInt32: IdentityRuleMap] = [:] + private let publisher: SnapshotPublisher + private var ruleMapByIdentity: [UInt32: IdentityRuleMap] = [:] - init(publisher: SnapshotPublisher) { + init(publisher: SnapshotPublisher) { self.publisher = publisher } func makeBatchPolicyRequests(dstIdentityID: UInt32) -> [Data] { - return self.identityMap.keys.compactMap { identityId in + return self.ruleMapByIdentity.keys.compactMap { identityId in var policyRequest = SDLPolicyRequest() policyRequest.srcIdentityID = identityId policyRequest.dstIdentityID = dstIdentityID @@ -60,7 +60,7 @@ actor PolicyRuleStore { let id = policyResponse.srcIdentityID let version = policyResponse.version - guard self.identityMap[id] == nil || ((self.identityMap[id]?.version ?? 0) < version) else { + guard self.ruleMapByIdentity[id] == nil || ((self.ruleMapByIdentity[id]?.version ?? 0) < version) else { return } @@ -74,7 +74,7 @@ actor PolicyRuleStore { } ruleMap[proto, default: [:]][port] = true } - self.identityMap[id] = IdentityRuleMap(version: version, ruleMap: ruleMap) + self.ruleMapByIdentity[id] = IdentityRuleMap(version: version, ruleMap: ruleMap) // 发布新的快照信息 let snapshot = compileSnapshot() @@ -84,12 +84,12 @@ actor PolicyRuleStore { func clear() { self.coolingDown.removeAll() self.versions.removeAll() - self.identityMap.removeAll() - self.publisher.publish(IdentitySnapshot.empty()) + self.ruleMapByIdentity.removeAll() + self.publisher.publish(PolicyRuleSnapshot.empty()) } - private func compileSnapshot() -> IdentitySnapshot { - return IdentitySnapshot(identityMap: identityMap) + private func compileSnapshot() -> PolicyRuleSnapshot { + return PolicyRuleSnapshot(ruleMapByIdentity: ruleMapByIdentity) } private func endCooldown(for key: UInt32) { diff --git a/Tun/Punchnet/Policy/PolicyService.swift b/Tun/Punchnet/Policy/PolicyService.swift index ddd63b1..07c3b18 100644 --- a/Tun/Punchnet/Policy/PolicyService.swift +++ b/Tun/Punchnet/Policy/PolicyService.swift @@ -10,7 +10,7 @@ import Foundation actor PolicyService { // 处理权限控制 private let policyRuleStore: PolicyRuleStore - nonisolated private let snapshotPublisher: SnapshotPublisher + nonisolated private let snapshotPublisher: SnapshotPublisher // Flow流会话管理, 过期时间为: 180秒 nonisolated private let flowSessionManager = SDLFlowSessionManager(sessionTimeout: 180) @@ -21,13 +21,13 @@ actor PolicyService { init(identityId: UInt32) { self.identityId = identityId // 权限控制 - let snapshotPublisher = SnapshotPublisher(initial: IdentitySnapshot.empty()) + let snapshotPublisher = SnapshotPublisher(initial: PolicyRuleSnapshot.empty()) self.policyRuleStore = PolicyRuleStore(publisher: snapshotPublisher) self.snapshotPublisher = snapshotPublisher } nonisolated func policyRuntime() -> PolicyRuntime { - let policySnapshot = PolicySnapshot(identitySnapshot: self.snapshotPublisher.current()) + let policySnapshot = PolicySnapshot(policyRuleSnapshot: self.snapshotPublisher.current()) return PolicyRuntime(policySnapshot: policySnapshot, flowSessionManager: self.flowSessionManager) } diff --git a/Tun/Punchnet/Policy/PolicySnapshot.swift b/Tun/Punchnet/Policy/PolicySnapshot.swift index b97ffa7..48ec8a4 100644 --- a/Tun/Punchnet/Policy/PolicySnapshot.swift +++ b/Tun/Punchnet/Policy/PolicySnapshot.swift @@ -8,14 +8,14 @@ import Foundation final class PolicySnapshot: Snapshot { - private let identitySnapshot: IdentitySnapshot + private let policyRuleSnapshot: PolicyRuleSnapshot - init(identitySnapshot: IdentitySnapshot) { - self.identitySnapshot = identitySnapshot + init(policyRuleSnapshot: PolicyRuleSnapshot) { + self.policyRuleSnapshot = policyRuleSnapshot } func allows(srcIdentityID: UInt32, ipPacket: IPPacket) -> Bool { - let ruleMap = self.identitySnapshot.lookup(srcIdentityID) + let ruleMap = self.policyRuleSnapshot.lookup(srcIdentityID) let proto = ipPacket.header.proto switch ipPacket.transportPacket { @@ -31,6 +31,6 @@ final class PolicySnapshot: Snapshot { } static func empty() -> PolicySnapshot { - return PolicySnapshot(identitySnapshot: .empty()) + return PolicySnapshot(policyRuleSnapshot: .empty()) } }