37 lines
983 B
Swift
37 lines
983 B
Swift
//
|
|
// PolicySnapshot.swift
|
|
// Tun
|
|
//
|
|
// Created by Codex on 2026/5/20.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
final class PolicySnapshot: Snapshot {
|
|
private let policyRuleSnapshot: PolicyRuleSnapshot
|
|
|
|
init(policyRuleSnapshot: PolicyRuleSnapshot) {
|
|
self.policyRuleSnapshot = policyRuleSnapshot
|
|
}
|
|
|
|
func allows(srcIdentityID: UInt32, ipPacket: IPPacket) -> Bool {
|
|
let ruleMap = self.policyRuleSnapshot.lookup(srcIdentityID)
|
|
let proto = ipPacket.header.proto
|
|
|
|
switch ipPacket.transportPacket {
|
|
case .tcp(let tcpPacket):
|
|
return ruleMap?.isAllow(proto: proto, port: tcpPacket.header.dstPort) ?? false
|
|
case .udp(let udpPacket):
|
|
return ruleMap?.isAllow(proto: proto, port: udpPacket.dstPort) ?? false
|
|
case .icmp:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
static func empty() -> PolicySnapshot {
|
|
return PolicySnapshot(policyRuleSnapshot: .empty())
|
|
}
|
|
}
|