fix Policy Rules
This commit is contained in:
parent
9a56eea6cf
commit
f9f018630c
@ -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: [:])
|
||||
}
|
||||
|
||||
}
|
||||
26
Tun/Punchnet/Policy/PolicyRuleSnapshot.swift
Normal file
26
Tun/Punchnet/Policy/PolicyRuleSnapshot.swift
Normal file
@ -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: [:])
|
||||
}
|
||||
|
||||
}
|
||||
@ -18,15 +18,15 @@ actor PolicyRuleStore {
|
||||
|
||||
nonisolated private let alloctor = ByteBufferAllocator()
|
||||
|
||||
private let publisher: SnapshotPublisher<IdentitySnapshot>
|
||||
private var identityMap: [UInt32: IdentityRuleMap] = [:]
|
||||
private let publisher: SnapshotPublisher<PolicyRuleSnapshot>
|
||||
private var ruleMapByIdentity: [UInt32: IdentityRuleMap] = [:]
|
||||
|
||||
init(publisher: SnapshotPublisher<IdentitySnapshot>) {
|
||||
init(publisher: SnapshotPublisher<PolicyRuleSnapshot>) {
|
||||
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) {
|
||||
|
||||
@ -10,7 +10,7 @@ import Foundation
|
||||
actor PolicyService {
|
||||
// 处理权限控制
|
||||
private let policyRuleStore: PolicyRuleStore
|
||||
nonisolated private let snapshotPublisher: SnapshotPublisher<IdentitySnapshot>
|
||||
nonisolated private let snapshotPublisher: SnapshotPublisher<PolicyRuleSnapshot>
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
|
||||
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user