fix proto

This commit is contained in:
anlicheng 2026-02-06 12:22:15 +08:00
parent 2e6e1e5b3f
commit 1554f3fe0b
3 changed files with 24 additions and 13 deletions

View File

@ -21,20 +21,11 @@ struct IPHeader {
let destination: UInt32
var source_ip: String {
return intToIp(source)
return SDLUtil.int32ToIp(source)
}
var destination_ip: String {
return intToIp(destination)
}
private func intToIp(_ num: UInt32) -> String {
let ip0 = (UInt8) (num >> 24 & 0xFF)
let ip1 = (UInt8) (num >> 16 & 0xFF)
let ip2 = (UInt8) (num >> 8 & 0xFF)
let ip3 = (UInt8) (num & 0xFF)
return "\(ip0).\(ip1).\(ip2).\(ip3)"
return SDLUtil.int32ToIp(destination)
}
public var description: String {
@ -83,4 +74,15 @@ struct IPPacket {
func getPayload() -> Data {
return data.subdata(in: 20..<data.count)
}
// ip
func getDstPort() -> UInt16? {
guard case .ipv4 = IPVersion(rawValue: self.header.version), self.data.count >= 24 else {
return nil
}
// ipv4(srcPort:16, dstPort:16, ...)
return UInt16(bytes: (self.data[22], self.data[23]))
}
}

View File

@ -498,10 +498,19 @@ actor SDLContextActor {
//
let identitySnapshot = self.snapshotPublisher.current()
if let ruleMap = identitySnapshot.lookup(data.identityID) {
if ruleMap.isAllow(proto: 2, port: 3) {
let proto = ipPacket.header.proto
switch TransportProtocol(rawValue: proto) {
case .udp, .tcp:
if let dstPort = ipPacket.getDstPort(), ruleMap.isAllow(proto: proto, port: dstPort) {
let packet = NEPacket(data: ipPacket.data, protocolFamily: 2)
self.provider.packetFlow.writePacketObjects([packet])
}
case .icmp:
let packet = NEPacket(data: ipPacket.data, protocolFamily: 2)
self.provider.packetFlow.writePacketObjects([packet])
default:
()
}
} else {
//
if let sessionToken = self.sessionToken {