fix Policy Runtime

This commit is contained in:
anlicheng 2026-05-21 12:03:17 +08:00
parent e37b0f74e0
commit a772b67205
5 changed files with 51 additions and 56 deletions

View File

@ -49,8 +49,7 @@ actor PacketInboundActor {
let processor = PacketInboundProcessor( let processor = PacketInboundProcessor(
networkAddress: self.networkAddress, networkAddress: self.networkAddress,
dataCipher: self.dataCipher, dataCipher: self.dataCipher,
policySnapshot: self.policyService.policySnapshot(), policyRuntime: self.policyService.policyRuntime()
flowSessionManager: self.policyService.flowSessionManager
) )
guard let plan = try? processor.makeProcessingPlan(data: data) else { guard let plan = try? processor.makeProcessingPlan(data: data) else {
@ -72,7 +71,7 @@ actor PacketInboundActor {
SDLLogger.log("[PacketInboundActor] hole identity: \(identityID), allow, data count: \(packetData.count)", for: .trace) SDLLogger.log("[PacketInboundActor] hole identity: \(identityID), allow, data count: \(packetData.count)", for: .trace)
case .requestPolicy(let srcIdentityID): case .requestPolicy(let srcIdentityID):
SDLLogger.log("[PacketInboundActor] not found identity: \(srcIdentityID) ruleMap", for: .debug) SDLLogger.log("[PacketInboundActor] not found identity: \(srcIdentityID) ruleMap", for: .debug)
if let queryData = await self.policyService.identifyStore.makePolicyRequest(srcIdentityId: srcIdentityID, dstIdentityId: self.identityId) { if let queryData = await self.policyService.makePolicyRequest(srcIdentityID: srcIdentityID) {
await self.superServiceProxy.send(type: .policyRequest, data: queryData) await self.superServiceProxy.send(type: .policyRequest, data: queryData)
} }
case .none: case .none:

View File

@ -24,17 +24,14 @@ final class PacketInboundProcessor {
private let networkAddress: SDLConfiguration.NetworkAddress private let networkAddress: SDLConfiguration.NetworkAddress
private let dataCipher: CCDataCipher? private let dataCipher: CCDataCipher?
private let policySnapshot: PolicySnapshot private let policyRuntime: PolicyRuntime
private let flowSessionManager: SDLFlowSessionManager
init(networkAddress: SDLConfiguration.NetworkAddress, init(networkAddress: SDLConfiguration.NetworkAddress,
dataCipher: CCDataCipher?, dataCipher: CCDataCipher?,
policySnapshot: PolicySnapshot, policyRuntime: PolicyRuntime) {
flowSessionManager: SDLFlowSessionManager) {
self.networkAddress = networkAddress self.networkAddress = networkAddress
self.dataCipher = dataCipher self.dataCipher = dataCipher
self.policySnapshot = policySnapshot self.policyRuntime = policyRuntime
self.flowSessionManager = flowSessionManager
} }
func makeProcessingPlan(data: SDLData) throws -> ProcessingPlan? { func makeProcessingPlan(data: SDLData) throws -> ProcessingPlan? {
@ -96,17 +93,7 @@ final class PacketInboundProcessor {
return .init(inboundBytes: inboundBytes, action: .none) return .init(inboundBytes: inboundBytes, action: .none)
} }
if let reverseFlowSession = ipPacket.flowSession()?.reverse(), if self.policyRuntime.allowsInbound(srcIdentityID: identityID, ipPacket: ipPacket) {
self.flowSessionManager.hasSession(reverseFlowSession) {
self.flowSessionManager.updateSession(reverseFlowSession)
return .init(
inboundBytes: inboundBytes,
action: .writeToTun(packetData: ipPacket.data, identityID: identityID)
)
}
//
if self.policySnapshot.allows(srcIdentityID: identityID, ipPacket: ipPacket) {
return .init( return .init(
inboundBytes: inboundBytes, inboundBytes: inboundBytes,
action: .writeToTun(packetData: ipPacket.data, identityID: identityID) action: .writeToTun(packetData: ipPacket.data, identityID: identityID)

View File

@ -121,8 +121,8 @@ actor PacketOutboundActor {
let router = PacketOutboundRouter(networkAddress: self.networkAddress, exitNode: self.exitNode) let router = PacketOutboundRouter(networkAddress: self.networkAddress, exitNode: self.exitNode)
let decision = router.route(packet: packet) let decision = router.route(packet: packet)
if decision.shouldTrackFlow, let flowSession = packet.flowSession() { if decision.shouldTrackFlow {
self.policyService.flowSessionManager.updateSession(flowSession) self.policyService.recordOutboundFlow(ipPacket: packet)
} }
await self.handleTunRouteDecision(decision) await self.handleTunRouteDecision(decision)

View File

@ -0,0 +1,28 @@
//
// PolicyRuntime.swift
// Tun
//
// Created by Codex on 2026/5/21.
//
import Foundation
struct PolicyRuntime: @unchecked Sendable {
private let policySnapshot: PolicySnapshot
private let flowSessionManager: SDLFlowSessionManager
init(policySnapshot: PolicySnapshot, flowSessionManager: SDLFlowSessionManager) {
self.policySnapshot = policySnapshot
self.flowSessionManager = flowSessionManager
}
func allowsInbound(srcIdentityID: UInt32, ipPacket: IPPacket) -> Bool {
if let reverseFlowSession = ipPacket.flowSession()?.reverse(),
self.flowSessionManager.hasSession(reverseFlowSession) {
self.flowSessionManager.updateSession(reverseFlowSession)
return true
}
return self.policySnapshot.allows(srcIdentityID: srcIdentityID, ipPacket: ipPacket)
}
}

View File

@ -9,11 +9,11 @@ import Foundation
actor PolicyService { actor PolicyService {
// //
let identifyStore: IdentityStore private let identifyStore: IdentityStore
nonisolated private let snapshotPublisher: SnapshotPublisher<IdentitySnapshot> nonisolated private let snapshotPublisher: SnapshotPublisher<IdentitySnapshot>
// Flow : 180 // Flow : 180
nonisolated let flowSessionManager = SDLFlowSessionManager(sessionTimeout: 180) nonisolated private let flowSessionManager = SDLFlowSessionManager(sessionTimeout: 180)
// identityId // identityId
let identityId: UInt32 let identityId: UInt32
@ -26,40 +26,21 @@ actor PolicyService {
self.snapshotPublisher = snapshotPublisher self.snapshotPublisher = snapshotPublisher
} }
func checkPolicy(srcIdentityID: UInt32, ipPacket: IPPacket) -> Bool { nonisolated func policyRuntime() -> PolicyRuntime {
// let policySnapshot = PolicySnapshot(identitySnapshot: self.snapshotPublisher.current())
if let reverseFlowSession = ipPacket.flowSession()?.reverse(), return PolicyRuntime(policySnapshot: policySnapshot, flowSessionManager: self.flowSessionManager)
self.flowSessionManager.hasSession(reverseFlowSession) {
self.flowSessionManager.updateSession(reverseFlowSession)
return true
}
//
let identitySnapshot = self.snapshotPublisher.current()
let ruleMap = identitySnapshot.lookup(srcIdentityID)
//
let proto = ipPacket.header.proto
// 访
switch ipPacket.transportPacket {
case .tcp(let tcpPacket):
if let ruleMap, ruleMap.isAllow(proto: proto, port: tcpPacket.header.dstPort) {
return true
}
case .udp(let udpPacket):
if let ruleMap, ruleMap.isAllow(proto: proto, port: udpPacket.dstPort) {
return true
}
case .icmp(_):
return true
default:
return false
}
return false
} }
nonisolated func policySnapshot() -> PolicySnapshot { nonisolated func recordOutboundFlow(ipPacket: IPPacket) {
return PolicySnapshot(identitySnapshot: self.snapshotPublisher.current()) 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 { func updatePolicy(superServiceProxy: SDLSuperServiceProxy) async {