mirror of
https://github.com/DaLaw2/NetGuardia.git
synced 2026-08-24 14:10:28 +09:00
fix: Fix compile error
This commit is contained in:
parent
846751acd6
commit
39202f181a
@ -44,28 +44,28 @@ unsafe fn parse_ipv4_packet(start: usize, end: usize, target: *mut Event) -> Res
|
||||
core::ptr::write(ipv4_data_ptr as *mut IpProto, ipv4.proto);
|
||||
core::ptr::copy_nonoverlapping(
|
||||
ipv4.src_addr.as_ptr(),
|
||||
ipv4_data_ptr.add(core::mem::offset_of!(IPv4Event, source_ip)),
|
||||
ipv4_data_ptr.add(core::mem::offset_of!(IPv4Event, src_ip)),
|
||||
4,
|
||||
);
|
||||
core::ptr::copy_nonoverlapping(
|
||||
ipv4.dst_addr.as_ptr(),
|
||||
ipv4_data_ptr.add(core::mem::offset_of!(IPv4Event, destination_ip)),
|
||||
ipv4_data_ptr.add(core::mem::offset_of!(IPv4Event, dst_ip)),
|
||||
4,
|
||||
);
|
||||
core::ptr::write(
|
||||
ipv4_data_ptr.add(core::mem::offset_of!(IPv4Event, source_port)) as *mut u16,
|
||||
ipv4_data_ptr.add(core::mem::offset_of!(IPv4Event, src_port)) as *mut u16,
|
||||
source_port,
|
||||
);
|
||||
core::ptr::write(
|
||||
ipv4_data_ptr.add(core::mem::offset_of!(IPv4Event, destination_port)) as *mut u16,
|
||||
ipv4_data_ptr.add(core::mem::offset_of!(IPv4Event, dst_port)) as *mut u16,
|
||||
destination_port,
|
||||
);
|
||||
core::ptr::write(
|
||||
ipv4_data_ptr.add(core::mem::offset_of!(IPv4Event, len)) as *mut u32,
|
||||
ipv4_data_ptr.add(core::mem::offset_of!(IPv4Event, packet_length)) as *mut u32,
|
||||
(end - start) as u32,
|
||||
);
|
||||
core::ptr::write(
|
||||
ipv4_data_ptr.add(core::mem::offset_of!(IPv4Event, timestamp)) as *mut u64,
|
||||
ipv4_data_ptr.add(core::mem::offset_of!(IPv4Event, timestamp_us)) as *mut u64,
|
||||
bpf_ktime_get_ns(),
|
||||
);
|
||||
|
||||
@ -95,28 +95,28 @@ unsafe fn parse_ipv6_packet(start: usize, end: usize, target: *mut Event) -> Res
|
||||
core::ptr::write(ipv6_data_ptr as *mut IpProto, ipv6.next_hdr);
|
||||
core::ptr::copy_nonoverlapping(
|
||||
ipv6.src_addr.as_ptr(),
|
||||
ipv6_data_ptr.add(core::mem::offset_of!(IPv6Event, source_ip)),
|
||||
ipv6_data_ptr.add(core::mem::offset_of!(IPv6Event, src_ip)),
|
||||
16,
|
||||
);
|
||||
core::ptr::copy_nonoverlapping(
|
||||
ipv6.dst_addr.as_ptr(),
|
||||
ipv6_data_ptr.add(core::mem::offset_of!(IPv6Event, destination_ip)),
|
||||
ipv6_data_ptr.add(core::mem::offset_of!(IPv6Event, dst_ip)),
|
||||
16,
|
||||
);
|
||||
core::ptr::write(
|
||||
ipv6_data_ptr.add(core::mem::offset_of!(IPv6Event, source_port)) as *mut u16,
|
||||
ipv6_data_ptr.add(core::mem::offset_of!(IPv6Event, src_port)) as *mut u16,
|
||||
source_port,
|
||||
);
|
||||
core::ptr::write(
|
||||
ipv6_data_ptr.add(core::mem::offset_of!(IPv6Event, destination_port)) as *mut u16,
|
||||
ipv6_data_ptr.add(core::mem::offset_of!(IPv6Event, dst_port)) as *mut u16,
|
||||
destination_port,
|
||||
);
|
||||
core::ptr::write(
|
||||
ipv6_data_ptr.add(core::mem::offset_of!(IPv6Event, len)) as *mut u32,
|
||||
ipv6_data_ptr.add(core::mem::offset_of!(IPv6Event, packet_length)) as *mut u32,
|
||||
(end - start) as u32,
|
||||
);
|
||||
core::ptr::write(
|
||||
ipv6_data_ptr.add(core::mem::offset_of!(IPv6Event, timestamp)) as *mut u64,
|
||||
ipv6_data_ptr.add(core::mem::offset_of!(IPv6Event, timestamp_us)) as *mut u64,
|
||||
bpf_ktime_get_ns(),
|
||||
);
|
||||
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||
use network_types::ip::IpProto;
|
||||
|
||||
use crate::model::ip_address::{AddrPortV4, AddrPortV6};
|
||||
@ -10,6 +9,13 @@ pub enum Event {
|
||||
IPv6(IPv6Event),
|
||||
}
|
||||
|
||||
#[repr(C, align(8))]
|
||||
#[derive(Clone)]
|
||||
pub enum RawIp {
|
||||
V4(u32),
|
||||
V6(u128),
|
||||
}
|
||||
|
||||
impl Event {
|
||||
pub fn timestamp_us(&self) -> u64 {
|
||||
match self {
|
||||
@ -67,25 +73,17 @@ impl Event {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn src_ip(&self) -> IpAddr {
|
||||
pub fn src_ip(&self) -> RawIp {
|
||||
match self {
|
||||
Event::IPv4(e) => {
|
||||
IpAddr::V4(Ipv4Addr::from(e.src_ip))
|
||||
}
|
||||
Event::IPv6(e) => {
|
||||
IpAddr::V6(Ipv6Addr::from(e.src_ip))
|
||||
}
|
||||
Event::IPv4(e) => RawIp::V4(e.src_ip),
|
||||
Event::IPv6(e) => RawIp::V6(e.src_ip),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dst_ip(&self) -> IpAddr {
|
||||
pub fn dst_ip(&self) -> RawIp {
|
||||
match self {
|
||||
Event::IPv4(e) => {
|
||||
IpAddr::V4(Ipv4Addr::from(e.dst_ip))
|
||||
}
|
||||
Event::IPv6(e) => {
|
||||
IpAddr::V6(Ipv6Addr::from(e.dst_ip))
|
||||
}
|
||||
Event::IPv4(e) => RawIp::V4(e.dst_ip),
|
||||
Event::IPv6(e) => RawIp::V6(e.dst_ip),
|
||||
}
|
||||
}
|
||||
|
||||
@ -170,6 +168,7 @@ impl IPv6Event {
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C, align(8))]
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct TcpFlags {
|
||||
pub fin: bool,
|
||||
|
||||
@ -60,14 +60,14 @@ pub fn ipv6_update_stats(event: &IPv6Event) {
|
||||
unsafe fn ipv4_update_flow_stats(map: &LruHashMap<AddrPortV4, FlowStats>, key: &AddrPortV4, event: &IPv4Event) {
|
||||
unsafe {
|
||||
if let Some(status) = map.get_ptr_mut(key) {
|
||||
(*status).bytes += event.len as u64;
|
||||
(*status).bytes += event.packet_length as u64;
|
||||
(*status).packets += 1;
|
||||
(*status).last_seen = event.timestamp;
|
||||
(*status).last_seen = event.timestamp_us;
|
||||
} else {
|
||||
let new_stats = FlowStats {
|
||||
bytes: event.len as u64,
|
||||
bytes: event.packet_length as u64,
|
||||
packets: 1,
|
||||
last_seen: event.timestamp,
|
||||
last_seen: event.timestamp_us,
|
||||
};
|
||||
let _ = map.insert(key, &new_stats, 0);
|
||||
}
|
||||
@ -78,14 +78,14 @@ unsafe fn ipv4_update_flow_stats(map: &LruHashMap<AddrPortV4, FlowStats>, key: &
|
||||
unsafe fn ipv6_update_flow_status(map: &LruHashMap<AddrPortV6, FlowStats>, key: &AddrPortV6, event: &IPv6Event) {
|
||||
unsafe {
|
||||
if let Some(status) = map.get_ptr_mut(key) {
|
||||
(*status).bytes += event.len as u64;
|
||||
(*status).bytes += event.packet_length as u64;
|
||||
(*status).packets += 1;
|
||||
(*status).last_seen = event.timestamp;
|
||||
(*status).last_seen = event.timestamp_us;
|
||||
} else {
|
||||
let new_stats = FlowStats {
|
||||
bytes: event.len as u64,
|
||||
bytes: event.packet_length as u64,
|
||||
packets: 1,
|
||||
last_seen: event.timestamp,
|
||||
last_seen: event.timestamp_us,
|
||||
};
|
||||
let _ = map.insert(key, &new_stats, 0);
|
||||
}
|
||||
|
||||
@ -23,13 +23,13 @@ static IPV6_DST_BLACKLIST: HashMap<IPv6, [Port; MAX_RULES_PORT]> = HashMap::with
|
||||
|
||||
pub fn ipv4_is_whitelisted(event: &IPv4Event) -> bool {
|
||||
unsafe {
|
||||
if let Some(ports) = IPV4_SRC_WHITELIST.get(&event.source_ip) {
|
||||
if is_port_exist(ports, event.source_port) {
|
||||
if let Some(ports) = IPV4_SRC_WHITELIST.get(&event.src_ip) {
|
||||
if is_port_exist(ports, event.src_port) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if let Some(ports) = IPV4_DST_WHITELIST.get(&event.destination_ip) {
|
||||
if is_port_exist(ports, event.destination_port) {
|
||||
if let Some(ports) = IPV4_DST_WHITELIST.get(&event.dst_ip) {
|
||||
if is_port_exist(ports, event.dst_port) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -39,13 +39,13 @@ pub fn ipv4_is_whitelisted(event: &IPv4Event) -> bool {
|
||||
|
||||
pub fn ipv6_is_whitelisted(event: &IPv6Event) -> bool {
|
||||
unsafe {
|
||||
if let Some(ports) = IPV6_SRC_WHITELIST.get(&event.source_ip) {
|
||||
if is_port_exist(ports, event.source_port) {
|
||||
if let Some(ports) = IPV6_SRC_WHITELIST.get(&event.src_ip) {
|
||||
if is_port_exist(ports, event.src_port) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if let Some(ports) = IPV6_DST_WHITELIST.get(&event.destination_ip) {
|
||||
if is_port_exist(ports, event.destination_port) {
|
||||
if let Some(ports) = IPV6_DST_WHITELIST.get(&event.dst_ip) {
|
||||
if is_port_exist(ports, event.dst_port) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -55,13 +55,13 @@ pub fn ipv6_is_whitelisted(event: &IPv6Event) -> bool {
|
||||
|
||||
pub fn ipv4_is_blacklisted(event: &IPv4Event) -> bool {
|
||||
unsafe {
|
||||
if let Some(ports) = IPV4_SRC_BLACKLIST.get(&event.source_ip) {
|
||||
if is_port_exist(ports, event.source_port) {
|
||||
if let Some(ports) = IPV4_SRC_BLACKLIST.get(&event.src_ip) {
|
||||
if is_port_exist(ports, event.src_port) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if let Some(ports) = IPV4_DST_BLACKLIST.get(&event.destination_ip) {
|
||||
if is_port_exist(ports, event.destination_port) {
|
||||
if let Some(ports) = IPV4_DST_BLACKLIST.get(&event.dst_ip) {
|
||||
if is_port_exist(ports, event.dst_port) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -71,13 +71,13 @@ pub fn ipv4_is_blacklisted(event: &IPv4Event) -> bool {
|
||||
|
||||
pub fn ipv6_is_blacklisted(event: &IPv6Event) -> bool {
|
||||
unsafe {
|
||||
if let Some(ports) = IPV6_SRC_BLACKLIST.get(&event.source_ip) {
|
||||
if is_port_exist(ports, event.source_port) {
|
||||
if let Some(ports) = IPV6_SRC_BLACKLIST.get(&event.src_ip) {
|
||||
if is_port_exist(ports, event.src_port) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if let Some(ports) = IPV6_DST_BLACKLIST.get(&event.destination_ip) {
|
||||
if is_port_exist(ports, event.destination_port) {
|
||||
if let Some(ports) = IPV6_DST_BLACKLIST.get(&event.dst_ip) {
|
||||
if is_port_exist(ports, event.dst_port) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,14 +60,14 @@ pub fn ipv6_update_stats(event: &IPv6Event) {
|
||||
unsafe fn ipv4_update_flow_stats(map: &LruHashMap<AddrPortV4, FlowStats>, key: &AddrPortV4, event: &IPv4Event) {
|
||||
unsafe {
|
||||
if let Some(status) = map.get_ptr_mut(key) {
|
||||
(*status).bytes += event.len as u64;
|
||||
(*status).bytes += event.packet_length as u64;
|
||||
(*status).packets += 1;
|
||||
(*status).last_seen = event.timestamp;
|
||||
(*status).last_seen = event.timestamp_us;
|
||||
} else {
|
||||
let new_stats = FlowStats {
|
||||
bytes: event.len as u64,
|
||||
bytes: event.packet_length as u64,
|
||||
packets: 1,
|
||||
last_seen: event.timestamp,
|
||||
last_seen: event.timestamp_us,
|
||||
};
|
||||
let _ = map.insert(key, &new_stats, 0);
|
||||
}
|
||||
@ -78,14 +78,14 @@ unsafe fn ipv4_update_flow_stats(map: &LruHashMap<AddrPortV4, FlowStats>, key: &
|
||||
unsafe fn ipv6_update_flow_status(map: &LruHashMap<AddrPortV6, FlowStats>, key: &AddrPortV6, event: &IPv6Event) {
|
||||
unsafe {
|
||||
if let Some(status) = map.get_ptr_mut(key) {
|
||||
(*status).bytes += event.len as u64;
|
||||
(*status).bytes += event.packet_length as u64;
|
||||
(*status).packets += 1;
|
||||
(*status).last_seen = event.timestamp;
|
||||
(*status).last_seen = event.timestamp_us;
|
||||
} else {
|
||||
let new_stats = FlowStats {
|
||||
bytes: event.len as u64,
|
||||
bytes: event.packet_length as u64,
|
||||
packets: 1,
|
||||
last_seen: event.timestamp,
|
||||
last_seen: event.timestamp_us,
|
||||
};
|
||||
let _ = map.insert(key, &new_stats, 0);
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ use cargo_metadata::{Artifact, CompilerMessage, Message, Metadata, MetadataComma
|
||||
fn main() {
|
||||
build_ingress_ebpf();
|
||||
build_egress_ebpf();
|
||||
build_frontend();
|
||||
// build_frontend();
|
||||
}
|
||||
|
||||
fn build_ingress_ebpf() {
|
||||
|
||||
@ -39,11 +39,11 @@ impl GeoIpService {
|
||||
pub async fn lookup(&self, ip: IpAddr) -> Result<Option<GeoLocation>, MaxMindDbError> {
|
||||
if ip_address::is_private_ip(&ip) {
|
||||
return Ok(Some(GeoLocation {
|
||||
country: Some("Private Network".into()),
|
||||
country_code: Some("PRIVATE".into()),
|
||||
country: Some("Local IP".into()),
|
||||
country_code: Some("Local".into()),
|
||||
city: None,
|
||||
latitude: Some(0.0),
|
||||
longitude: Some(0.0),
|
||||
latitude: None,
|
||||
longitude: None,
|
||||
timezone: None,
|
||||
}));
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user