fix Policy Runtime
This commit is contained in:
parent
e37b0f74e0
commit
a772b67205
@ -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:
|
||||||
|
|||||||
@ -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)
|
||||||
|
|||||||
@ -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)
|
||||||
|
|||||||
28
Tun/Punchnet/Policy/PolicyRuntime.swift
Normal file
28
Tun/Punchnet/Policy/PolicyRuntime.swift
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user