84 lines
3.2 KiB
Swift
84 lines
3.2 KiB
Swift
//
|
|
// PacketInboundActor.swift
|
|
// Tun
|
|
//
|
|
// Created by Codex on 2026/5/20.
|
|
//
|
|
|
|
import Foundation
|
|
import NetworkExtension
|
|
|
|
actor PacketInboundActor {
|
|
private let provider: NEPacketTunnelProvider
|
|
private let policyService: PolicyService
|
|
private let packetOutboundActor: PacketOutboundActor
|
|
private let arpResolver: ArpResolver
|
|
private let superServiceProxy: SDLSuperServiceProxy
|
|
private let flowTracer: SDLFlowTracer
|
|
|
|
private var networkAddress: SDLConfiguration.NetworkAddress
|
|
private var identityId: UInt32
|
|
private var dataCipher: CCDataCipher?
|
|
|
|
init(provider: NEPacketTunnelProvider,
|
|
config: SDLConfiguration,
|
|
dataCipher: CCDataCipher?,
|
|
policyService: PolicyService,
|
|
packetOutboundActor: PacketOutboundActor,
|
|
arpResolver: ArpResolver,
|
|
superServiceProxy: SDLSuperServiceProxy,
|
|
flowTracer: SDLFlowTracer) {
|
|
self.provider = provider
|
|
self.networkAddress = config.networkAddress
|
|
self.identityId = config.identityId
|
|
self.dataCipher = dataCipher
|
|
self.policyService = policyService
|
|
self.packetOutboundActor = packetOutboundActor
|
|
self.arpResolver = arpResolver
|
|
self.superServiceProxy = superServiceProxy
|
|
self.flowTracer = flowTracer
|
|
}
|
|
|
|
func updateRuntime(config: SDLConfiguration, dataCipher: CCDataCipher?) {
|
|
self.networkAddress = config.networkAddress
|
|
self.identityId = config.identityId
|
|
self.dataCipher = dataCipher
|
|
}
|
|
|
|
func handleData(_ data: SDLData) async {
|
|
let processor = PacketInboundProcessor(
|
|
networkAddress: self.networkAddress,
|
|
dataCipher: self.dataCipher,
|
|
policyRuntime: self.policyService.policyRuntime()
|
|
)
|
|
|
|
guard let plan = try? processor.makeProcessingPlan(data: data) else {
|
|
return
|
|
}
|
|
|
|
self.flowTracer.inc(num: plan.inboundBytes, type: .inbound)
|
|
|
|
switch plan.action {
|
|
case .sendARPReply(let dstMac, let responseData):
|
|
SDLLogger.log("[PacketInboundActor] get arp request packet")
|
|
await self.packetOutboundActor.routeLayerPacket(dstMac: dstMac, type: .arp, data: responseData)
|
|
case .appendARP(let ip, let mac):
|
|
SDLLogger.log("[PacketInboundActor] get arp response packet")
|
|
await self.arpResolver.append(ip: ip, mac: mac)
|
|
case .writeToTun(let packetData, let identityID):
|
|
let packet = NEPacket(data: packetData, protocolFamily: 2)
|
|
self.provider.packetFlow.writePacketObjects([packet])
|
|
SDLLogger.log("[PacketInboundActor] hole identity: \(identityID), allow, data count: \(packetData.count)", for: .trace)
|
|
case .requestPolicy(let context):
|
|
SDLLogger.log("[PacketInboundActor] policy miss, \(context.logDescription)", for: .debug)
|
|
if let queryData = await self.policyService.makePolicyRequest(srcIdentityID: context.srcIdentityID) {
|
|
await self.superServiceProxy.send(type: .policyRequest, data: queryData)
|
|
}
|
|
case .dropByPolicy(let context):
|
|
SDLLogger.log("[PacketInboundActor] policy denied, \(context.logDescription)", for: .trace)
|
|
case .none:
|
|
()
|
|
}
|
|
}
|
|
}
|