79 lines
2.6 KiB
Swift
79 lines
2.6 KiB
Swift
//
|
|
// PolicyService.swift
|
|
// punchnet
|
|
//
|
|
// Created by 安礼成 on 2026/5/19.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
actor PolicyService {
|
|
// 处理权限控制
|
|
private let policyRuleStore: PolicyRuleStore
|
|
nonisolated private let snapshotPublisher: SnapshotPublisher<PolicyRuleSnapshot>
|
|
|
|
nonisolated private let flowSessionTable = FlowSessionTable()
|
|
|
|
// 当前节点的identityId值
|
|
let identityId: UInt32
|
|
private let acl: SDLConfiguration.ACL
|
|
|
|
init(identityId: UInt32, acl: SDLConfiguration.ACL) {
|
|
self.identityId = identityId
|
|
self.acl = acl
|
|
// 权限控制
|
|
let snapshotPublisher = SnapshotPublisher(initial: PolicyRuleSnapshot.empty())
|
|
self.policyRuleStore = PolicyRuleStore(publisher: snapshotPublisher)
|
|
self.snapshotPublisher = snapshotPublisher
|
|
}
|
|
|
|
nonisolated func policyRuntime() -> PolicyRuntime {
|
|
return PolicyRuntime(policyRuleSnapshot: self.snapshotPublisher.current(), flowSessionTable: self.flowSessionTable, acl: self.acl)
|
|
}
|
|
|
|
nonisolated func recordOutboundFlow(ipPacket: IPPacketView) {
|
|
guard let flowSession = ipPacket.flowSession() else {
|
|
return
|
|
}
|
|
|
|
switch ipPacket.transportPacket {
|
|
case .tcp(_, _, let flags):
|
|
self.flowSessionTable.recordOutboundTCP(flowSession, flags: flags)
|
|
case .udp(_, let dstPort, _):
|
|
self.flowSessionTable.recordOutboundUDP(flowSession, isDNS: dstPort == 53)
|
|
default:
|
|
return
|
|
}
|
|
}
|
|
|
|
func makePolicyRequest(srcIdentityID: UInt32) async -> Data? {
|
|
return await self.policyRuleStore.makePolicyRequest(srcIdentityId: srcIdentityID, dstIdentityId: self.identityId)
|
|
}
|
|
|
|
func updatePolicy(superServiceProxy: SDLSuperServiceProxy) async {
|
|
let requests = await self.policyRuleStore.makeBatchPolicyRequests(dstIdentityID: self.identityId)
|
|
for request in requests {
|
|
await superServiceProxy.send(type: .policyRequest, data: request)
|
|
}
|
|
}
|
|
|
|
func applyPolicyResponse(_ policyResponse: SDLPolicyResponse) async {
|
|
guard policyResponse.dstIdentityID == self.identityId else {
|
|
SDLLogger.log("[PolicyService] ignore policy response, dstIdentityID mismatch: \(policyResponse.dstIdentityID), expected: \(self.identityId)", for: .debug)
|
|
return
|
|
}
|
|
|
|
await self.policyRuleStore.applyPolicyResponse(policyResponse)
|
|
}
|
|
|
|
func clear() async {
|
|
self.flowSessionTable.clear()
|
|
await self.policyRuleStore.clear()
|
|
}
|
|
|
|
deinit {
|
|
SDLLogger.log("[PolicyService] deinit")
|
|
}
|
|
|
|
}
|