punchnet-macos/Tun/Punchnet/Policy/PolicyService.swift
2026-05-21 12:03:17 +08:00

72 lines
2.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// PolicyService.swift
// punchnet
//
// Created by on 2026/5/19.
//
import Foundation
actor PolicyService {
//
private let identifyStore: IdentityStore
nonisolated private let snapshotPublisher: SnapshotPublisher<IdentitySnapshot>
// Flow : 180
nonisolated private let flowSessionManager = SDLFlowSessionManager(sessionTimeout: 180)
// identityId
let identityId: UInt32
init(identityId: UInt32) {
self.identityId = identityId
//
let snapshotPublisher = SnapshotPublisher(initial: IdentitySnapshot.empty())
self.identifyStore = IdentityStore(publisher: snapshotPublisher)
self.snapshotPublisher = snapshotPublisher
}
nonisolated func policyRuntime() -> PolicyRuntime {
let policySnapshot = PolicySnapshot(identitySnapshot: self.snapshotPublisher.current())
return PolicyRuntime(policySnapshot: policySnapshot, flowSessionManager: self.flowSessionManager)
}
nonisolated func recordOutboundFlow(ipPacket: IPPacket) {
guard let flowSession = ipPacket.flowSession() else {
return
}
self.flowSessionManager.updateSession(flowSession)
}
func makePolicyRequest(srcIdentityID: UInt32) async -> Data? {
return await self.identifyStore.makePolicyRequest(srcIdentityId: srcIdentityID, dstIdentityId: self.identityId)
}
func updatePolicy(superServiceProxy: SDLSuperServiceProxy) async {
let requests = await self.identifyStore.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.identifyStore.applyPolicyResponse(policyResponse)
}
func clear() async {
self.flowSessionManager.clear()
await self.identifyStore.clear()
}
deinit {
SDLLogger.log("[PolicyService] deinit")
}
}