diff --git a/Cargo.lock b/Cargo.lock index fdcb0e8..7d4e70a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "aho-corasick" @@ -472,6 +472,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "indoc" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" + [[package]] name = "instability" version = "0.3.2" @@ -709,23 +715,23 @@ dependencies = [ [[package]] name = "ratatui" -version = "0.28.1" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdef7f9be5c0122f890d58bdf4d964349ba6a6161f705907526d891efabba57d" +checksum = "eabd94c2f37801c20583fc49dd5cd6b0ba68c716787c2dd6ed18571e1e63117b" dependencies = [ "bitflags", "cassowary", "compact_str", "crossterm", + "indoc", "instability", "itertools", "lru", "paste", "strum", - "strum_macros", "unicode-segmentation", "unicode-truncate", - "unicode-width", + "unicode-width 0.2.0", ] [[package]] @@ -949,9 +955,9 @@ dependencies = [ [[package]] name = "tui-big-text" -version = "0.6.0" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "399b145b30537641e56878b64bf648e04e1df996efb5d32cd7f19944152e3868" +checksum = "8b046cf880cb40db75567b60ca22dc38a4165ef490be6bfad5718b22bcb6aabf" dependencies = [ "derive_builder", "font8x8", @@ -961,12 +967,12 @@ dependencies = [ [[package]] name = "tui-input" -version = "0.10.1" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd137780d743c103a391e06fe952487f914b299a4fe2c3626677f6a6339a7c6b" +checksum = "ffde6d8fcffe86b617018ca9b2171d673b41def44ebf802de203d2f2c598d3de" dependencies = [ "ratatui", - "unicode-width", + "unicode-width 0.2.0", ] [[package]] @@ -989,7 +995,7 @@ checksum = "b3644627a5af5fa321c95b9b235a72fd24cd29c648c2c379431e6628655627bf" dependencies = [ "itertools", "unicode-segmentation", - "unicode-width", + "unicode-width 0.1.14", ] [[package]] @@ -998,6 +1004,12 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" +[[package]] +name = "unicode-width" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd" + [[package]] name = "utf8parse" version = "0.2.2" diff --git a/oryx-tui/Cargo.toml b/oryx-tui/Cargo.toml index e18c3e4..98ca497 100644 --- a/oryx-tui/Cargo.toml +++ b/oryx-tui/Cargo.toml @@ -10,9 +10,9 @@ edition.workspace = true [dependencies] crossterm = { version = "0.28", default-features = false } -ratatui = "0.28" -tui-big-text = "0.6" -tui-input = "0.10" +ratatui = "0.29" +tui-big-text = "0.7" +tui-input = "0.11" libc = "0.2" aya = "0.13" oryx-common = { path = "../oryx-common" } diff --git a/oryx-tui/src/app.rs b/oryx-tui/src/app.rs index c1c528b..2446f2f 100644 --- a/oryx-tui/src/app.rs +++ b/oryx-tui/src/app.rs @@ -10,7 +10,11 @@ use std::{ time::Duration, }; -use crate::{filter::Filter, help::Help}; +use crate::{ + filter::Filter, + help::Help, + packet::{direction::TrafficDirection, NetworkPacket}, +}; use crate::{filter::IoChannels, notification::Notification}; use crate::{packet::AppPacket, section::Section}; @@ -41,7 +45,7 @@ pub struct App { pub packets: Arc>>, pub notifications: Vec, pub section: Section, - pub data_channel_sender: kanal::Sender<[u8; RawPacket::LEN]>, + pub data_channel_sender: kanal::Sender<([u8; RawPacket::LEN], TrafficDirection)>, pub is_editing: bool, pub active_popup: Option, } @@ -54,7 +58,7 @@ impl Default for App { impl App { pub fn new() -> Self { - let packets = Arc::new(Mutex::new(Vec::with_capacity(AppPacket::LEN * 1024 * 1024))); + let packets = Arc::new(Mutex::new(Vec::with_capacity(RawPacket::LEN * 1024 * 1024))); let (sender, receiver) = kanal::unbounded(); @@ -62,8 +66,13 @@ impl App { thread::spawn({ let packets = packets.clone(); move || loop { - if let Ok(raw_packet) = receiver.recv() { - let app_packet = AppPacket::from(raw_packet); + if let Ok((raw_packet, direction)) = receiver.recv() { + let network_packet = NetworkPacket::from(raw_packet); + let app_packet = AppPacket { + packet: network_packet, + pid: None, + direction, + }; let mut packets = packets.lock().unwrap(); if packets.len() == packets.capacity() { packets.reserve(1024 * 1024); diff --git a/oryx-tui/src/ebpf/egress.rs b/oryx-tui/src/ebpf/egress.rs index 68e3008..a0b19cf 100644 --- a/oryx-tui/src/ebpf/egress.rs +++ b/oryx-tui/src/ebpf/egress.rs @@ -19,6 +19,7 @@ use crate::{ event::Event, filter::FilterChannelSignal, notification::{Notification, NotificationLevel}, + packet::direction::TrafficDirection, section::firewall::FirewallSignal, }; use mio::{unix::SourceFd, Events, Interest, Poll, Token}; @@ -31,7 +32,7 @@ use super::{ pub fn load_egress( iface: String, notification_sender: kanal::Sender, - data_sender: kanal::Sender<[u8; RawPacket::LEN]>, + data_sender: kanal::Sender<([u8; RawPacket::LEN], TrafficDirection)>, filter_channel_receiver: kanal::Receiver, firewall_egress_receiver: kanal::Receiver, terminate: Arc, @@ -221,7 +222,7 @@ pub fn load_egress( break; } let packet: [u8; RawPacket::LEN] = item.to_owned().try_into().unwrap(); - data_sender.send(packet).ok(); + data_sender.send((packet, TrafficDirection::Egress)).ok(); } } } diff --git a/oryx-tui/src/ebpf/ingress.rs b/oryx-tui/src/ebpf/ingress.rs index 826e258..2db027a 100644 --- a/oryx-tui/src/ebpf/ingress.rs +++ b/oryx-tui/src/ebpf/ingress.rs @@ -19,6 +19,7 @@ use crate::{ event::Event, filter::FilterChannelSignal, notification::{Notification, NotificationLevel}, + packet::direction::TrafficDirection, section::firewall::FirewallSignal, }; use mio::{unix::SourceFd, Events, Interest, Poll, Token}; @@ -31,7 +32,7 @@ use super::{ pub fn load_ingress( iface: String, notification_sender: kanal::Sender, - data_sender: kanal::Sender<[u8; RawPacket::LEN]>, + data_sender: kanal::Sender<([u8; RawPacket::LEN], TrafficDirection)>, filter_channel_receiver: kanal::Receiver, firewall_ingress_receiver: kanal::Receiver, terminate: Arc, @@ -225,7 +226,7 @@ pub fn load_ingress( break; } let packet: [u8; RawPacket::LEN] = item.to_owned().try_into().unwrap(); - data_sender.send(packet).ok(); + data_sender.send((packet, TrafficDirection::Ingress)).ok(); } } } diff --git a/oryx-tui/src/export.rs b/oryx-tui/src/export.rs index 78599ca..de5e4d0 100644 --- a/oryx-tui/src/export.rs +++ b/oryx-tui/src/export.rs @@ -8,7 +8,7 @@ use crate::{ app::AppResult, packet::{ network::{IpPacket, IpProto}, - AppPacket, + AppPacket, NetworkPacket, }, }; @@ -39,8 +39,8 @@ pub fn export(packets: &[AppPacket]) -> AppResult<()> { headers.0, headers.1, headers.2, headers.3, headers.4 )?; for packet in packets { - match packet { - AppPacket::Arp(p) => { + match packet.packet { + NetworkPacket::Arp(p) => { writeln!( file, "{:39} {:^11} {:39} {:^11} ARP", @@ -50,7 +50,7 @@ pub fn export(packets: &[AppPacket]) -> AppResult<()> { "-" )?; } - AppPacket::Ip(packet) => match packet { + NetworkPacket::Ip(packet) => match packet { IpPacket::V4(ipv4_packet) => match ipv4_packet.proto { IpProto::Tcp(p) => { writeln!( diff --git a/oryx-tui/src/filter.rs b/oryx-tui/src/filter.rs index a62a717..3c80600 100644 --- a/oryx-tui/src/filter.rs +++ b/oryx-tui/src/filter.rs @@ -5,7 +5,7 @@ mod network; mod transport; use crossterm::event::{KeyCode, KeyEvent}; -use direction::{TrafficDirection, TrafficDirectionFilter}; +use direction::TrafficDirectionFilter; use link::LinkFilter; use network::NetworkFilter; use oryx_common::{ @@ -30,6 +30,7 @@ use crate::{ ebpf::{egress::load_egress, ingress::load_ingress}, event::Event, interface::Interface, + packet::direction::TrafficDirection, section::firewall::FirewallSignal, }; @@ -145,7 +146,7 @@ impl Filter { pub fn start( &mut self, notification_sender: kanal::Sender, - data_sender: kanal::Sender<[u8; RawPacket::LEN]>, + data_sender: kanal::Sender<([u8; RawPacket::LEN], TrafficDirection)>, ) -> AppResult<()> { let iface = self.interface.selected_interface.name.clone(); diff --git a/oryx-tui/src/filter/direction.rs b/oryx-tui/src/filter/direction.rs index bda78e7..55bd363 100644 --- a/oryx-tui/src/filter/direction.rs +++ b/oryx-tui/src/filter/direction.rs @@ -1,9 +1,6 @@ -use std::{ - fmt::Display, - sync::{ - atomic::{AtomicBool, Ordering}, - Arc, - }, +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, }; use ratatui::{ @@ -12,7 +9,8 @@ use ratatui::{ widgets::{Block, BorderType, Borders, Row, Table, TableState}, Frame, }; -use serde::{Deserialize, Serialize}; + +use crate::packet::direction::TrafficDirection; #[derive(Debug)] pub struct TrafficDirectionFilter { @@ -23,21 +21,6 @@ pub struct TrafficDirectionFilter { pub terminate_egress: Arc, } -#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] -pub enum TrafficDirection { - Ingress, - Egress, -} - -impl Display for TrafficDirection { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - TrafficDirection::Ingress => write!(f, "Ingress"), - TrafficDirection::Egress => write!(f, "Egress"), - } - } -} - impl Default for TrafficDirectionFilter { fn default() -> Self { Self::new() @@ -121,7 +104,7 @@ impl TrafficDirectionFilter { ]; let table = Table::new(filters, widths) - .highlight_style(Style::new().bg(ratatui::style::Color::DarkGray)); + .row_highlight_style(Style::new().bg(ratatui::style::Color::DarkGray)); frame.render_widget( Block::new() diff --git a/oryx-tui/src/filter/link.rs b/oryx-tui/src/filter/link.rs index cfe09cd..955da4c 100644 --- a/oryx-tui/src/filter/link.rs +++ b/oryx-tui/src/filter/link.rs @@ -102,7 +102,7 @@ impl LinkFilter { ])]; let table = Table::new(link_filters, widths) - .highlight_style(Style::new().bg(ratatui::style::Color::DarkGray)); + .row_highlight_style(Style::new().bg(ratatui::style::Color::DarkGray)); frame.render_widget( Block::new() diff --git a/oryx-tui/src/filter/network.rs b/oryx-tui/src/filter/network.rs index 0bac490..ee2535b 100644 --- a/oryx-tui/src/filter/network.rs +++ b/oryx-tui/src/filter/network.rs @@ -134,7 +134,7 @@ impl NetworkFilter { ]; let network_filters_table = Table::new(network_filters, widths) - .highlight_style(Style::new().bg(ratatui::style::Color::DarkGray)); + .row_highlight_style(Style::new().bg(ratatui::style::Color::DarkGray)); frame.render_widget( Block::new() diff --git a/oryx-tui/src/filter/transport.rs b/oryx-tui/src/filter/transport.rs index 47fac48..cfc49d0 100644 --- a/oryx-tui/src/filter/transport.rs +++ b/oryx-tui/src/filter/transport.rs @@ -119,7 +119,7 @@ impl TransportFilter { ]; let table = Table::new(transport_filters, widths) - .highlight_style(Style::new().bg(ratatui::style::Color::DarkGray)); + .row_highlight_style(Style::new().bg(ratatui::style::Color::DarkGray)); frame.render_widget( Block::new() diff --git a/oryx-tui/src/handler.rs b/oryx-tui/src/handler.rs index 6a1a7be..dca3f05 100644 --- a/oryx-tui/src/handler.rs +++ b/oryx-tui/src/handler.rs @@ -21,10 +21,7 @@ pub fn handle_key_events( match key_event.code { KeyCode::Enter => { if app.filter.focused_block == FocusedBlock::Apply { - app.section.stats = Some(Stats::new( - app.packets.clone(), - app.filter.interface.selected_interface.clone(), - )); + app.section.stats = Some(Stats::new(app.packets.clone())); app.filter .start(event_sender.clone(), app.data_channel_sender.clone())?; diff --git a/oryx-tui/src/interface.rs b/oryx-tui/src/interface.rs index 808613c..50b6a09 100644 --- a/oryx-tui/src/interface.rs +++ b/oryx-tui/src/interface.rs @@ -209,7 +209,7 @@ impl Interface { .style(Style::new().bold()) .bottom_margin(1), ) - .highlight_style(Style::new().bg(ratatui::style::Color::DarkGray)) + .row_highlight_style(Style::new().bg(ratatui::style::Color::DarkGray)) .column_spacing(3); frame.render_widget( diff --git a/oryx-tui/src/packet.rs b/oryx-tui/src/packet.rs index 543469d..bb4f243 100644 --- a/oryx-tui/src/packet.rs +++ b/oryx-tui/src/packet.rs @@ -1,9 +1,11 @@ +pub mod direction; pub mod link; pub mod network; pub mod transport; use std::{fmt::Display, mem, net::Ipv4Addr}; +use direction::TrafficDirection; use link::{ArpPacket, ArpType, MacAddr}; use network::{IcmpPacket, IcmpType, IpPacket, IpProto, Ipv4Packet, Ipv6Packet}; use network_types::ip::IpHdr; @@ -11,16 +13,27 @@ use oryx_common::{ProtoHdr, RawPacket}; use transport::{TcpPacket, UdpPacket}; #[derive(Debug, Copy, Clone)] -pub enum AppPacket { +pub struct AppPacket { + pub packet: NetworkPacket, + pub pid: Option, + pub direction: TrafficDirection, +} + +impl AppPacket { + pub const LEN: usize = mem::size_of::(); +} + +#[derive(Debug, Copy, Clone)] +pub enum NetworkPacket { Ip(IpPacket), Arp(ArpPacket), } -impl AppPacket { +impl NetworkPacket { pub const LEN: usize = mem::size_of::(); } -impl Display for AppPacket { +impl Display for NetworkPacket { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { Self::Arp(packet) => write!(f, "{}", packet), @@ -29,7 +42,20 @@ impl Display for AppPacket { } } -impl From<[u8; RawPacket::LEN]> for AppPacket { +impl Display for AppPacket { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self.pid { + Some(pid) => { + write!(f, "{} {}", self.packet, pid) + } + None => { + write!(f, "{}", self.packet) + } + } + } +} + +impl From<[u8; RawPacket::LEN]> for NetworkPacket { fn from(value: [u8; RawPacket::LEN]) -> Self { let raw_packet = value.as_ptr() as *const RawPacket; match unsafe { &*raw_packet } { @@ -87,7 +113,7 @@ impl From<[u8; RawPacket::LEN]> for AppPacket { } }; - AppPacket::Ip(IpPacket::V4(Ipv4Packet { + NetworkPacket::Ip(IpPacket::V4(Ipv4Packet { src_ip, dst_ip, ihl: u8::from_be(ipv4_packet.ihl()), @@ -153,7 +179,7 @@ impl From<[u8; RawPacket::LEN]> for AppPacket { } }; - AppPacket::Ip(IpPacket::V6(Ipv6Packet { + NetworkPacket::Ip(IpPacket::V6(Ipv6Packet { traffic_class: ipv6_packet.priority(), flow_label: ipv6_packet.flow_label, payload_length: u16::from_be(ipv6_packet.payload_len), diff --git a/oryx-tui/src/packet/direction.rs b/oryx-tui/src/packet/direction.rs new file mode 100644 index 0000000..b6a4cf7 --- /dev/null +++ b/oryx-tui/src/packet/direction.rs @@ -0,0 +1,18 @@ +use std::fmt::Display; + +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] +pub enum TrafficDirection { + Ingress, + Egress, +} + +impl Display for TrafficDirection { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + TrafficDirection::Ingress => write!(f, "Ingress"), + TrafficDirection::Egress => write!(f, "Egress"), + } + } +} diff --git a/oryx-tui/src/section/alert/syn_flood.rs b/oryx-tui/src/section/alert/syn_flood.rs index c93dfc1..339f8ec 100644 --- a/oryx-tui/src/section/alert/syn_flood.rs +++ b/oryx-tui/src/section/alert/syn_flood.rs @@ -15,8 +15,9 @@ use ratatui::{ }; use crate::packet::{ + direction::TrafficDirection, network::{IpPacket, IpProto}, - AppPacket, + AppPacket, NetworkPacket, }; const WIN_SIZE: usize = 100_000; @@ -48,6 +49,11 @@ impl SynFlood { packets.clone() }; + let app_packets: Vec = app_packets + .into_iter() + .filter(|packet| packet.direction == TrafficDirection::Ingress) + .collect(); + let mut map = map.lock().unwrap(); map.clear(); @@ -59,8 +65,8 @@ impl SynFlood { app_packets[start_index..app_packets.len().saturating_sub(1)] .iter() - .for_each(|packet| { - if let AppPacket::Ip(ip_packet) = packet { + .for_each(|app_packet| { + if let NetworkPacket::Ip(ip_packet) = app_packet.packet { if let IpPacket::V4(ipv4_packet) = ip_packet { if let IpProto::Tcp(tcp_packet) = ipv4_packet.proto { if tcp_packet.syn == 1 { @@ -92,7 +98,7 @@ impl SynFlood { } }); - if (nb_syn_packets as f64 / WIN_SIZE as f64) > 0.45 { + if (nb_syn_packets as f64 / WIN_SIZE as f64) > 0.95 { detected.store(true, std::sync::atomic::Ordering::Relaxed); } else { detected.store(false, std::sync::atomic::Ordering::Relaxed); diff --git a/oryx-tui/src/section/firewall.rs b/oryx-tui/src/section/firewall.rs index df44284..c81d85b 100644 --- a/oryx-tui/src/section/firewall.rs +++ b/oryx-tui/src/section/firewall.rs @@ -15,7 +15,7 @@ use std::{fs, net::IpAddr, num::ParseIntError, os::unix::fs::chown, str::FromStr use tui_input::{backend::crossterm::EventHandler, Input}; use uuid; -use crate::{app::AppResult, filter::direction::TrafficDirection, notification::Notification}; +use crate::{app::AppResult, notification::Notification, packet::direction::TrafficDirection}; #[derive(Debug, Clone)] pub enum FirewallSignal { @@ -682,7 +682,7 @@ impl Firewall { let table = Table::new(rows, widths) .column_spacing(2) .flex(Flex::SpaceBetween) - .highlight_style(Style::default().bg(Color::DarkGray)) + .row_highlight_style(Style::default().bg(Color::DarkGray)) .header( Row::new(vec![ Line::from("Name").centered().blue(), diff --git a/oryx-tui/src/section/inspection.rs b/oryx-tui/src/section/inspection.rs index 9bbca58..5d0b482 100644 --- a/oryx-tui/src/section/inspection.rs +++ b/oryx-tui/src/section/inspection.rs @@ -20,7 +20,7 @@ use crate::{ notification::{Notification, NotificationLevel}, packet::{ network::{IpPacket, IpProto}, - AppPacket, + AppPacket, NetworkPacket, }, }; @@ -332,15 +332,15 @@ impl Inspection { let packets: Vec = if fuzzy.is_enabled() & !fuzzy.filter.value().is_empty() { packets_to_display .iter() - .map(|app_packet| match app_packet { - AppPacket::Arp(packet) => Row::new(vec![ + .map(|app_packet| match app_packet.packet { + NetworkPacket::Arp(packet) => Row::new(vec![ fuzzy::highlight(pattern, packet.src_mac.to_string()).blue(), Cell::from(Line::from("-").centered()).yellow(), fuzzy::highlight(pattern, packet.dst_mac.to_string()).blue(), Cell::from(Line::from("-").centered()).yellow(), fuzzy::highlight(pattern, "ARP".to_string()).cyan(), ]), - AppPacket::Ip(packet) => match packet { + NetworkPacket::Ip(packet) => match packet { IpPacket::V4(ipv4_packet) => match ipv4_packet.proto { IpProto::Tcp(p) => Row::new(vec![ fuzzy::highlight(pattern, ipv4_packet.src_ip.to_string()).blue(), @@ -393,8 +393,8 @@ impl Inspection { } else { packets_to_display .iter() - .map(|app_packet| match app_packet { - AppPacket::Arp(packet) => Row::new(vec![ + .map(|app_packet| match app_packet.packet { + NetworkPacket::Arp(packet) => Row::new(vec![ Span::from(packet.src_mac.to_string()) .into_centered_line() .blue(), @@ -405,7 +405,7 @@ impl Inspection { Span::from("-").into_centered_line().yellow(), Span::from("ARP".to_string()).into_centered_line().cyan(), ]), - AppPacket::Ip(packet) => match packet { + NetworkPacket::Ip(packet) => match packet { IpPacket::V4(ipv4_packet) => match ipv4_packet.proto { IpProto::Tcp(p) => Row::new(vec![ Span::from(ipv4_packet.src_ip.to_string()) @@ -527,7 +527,7 @@ impl Inspection { ) .column_spacing(2) .flex(Flex::SpaceBetween) - .highlight_style(Style::new().bg(ratatui::style::Color::DarkGray)) + .row_highlight_style(Style::new().bg(ratatui::style::Color::DarkGray)) .highlight_spacing(HighlightSpacing::Always) .block(Block::default().padding(Padding::uniform(1))); @@ -657,9 +657,9 @@ impl Inspection { .border_type(BorderType::Thick), block, ); - match packet { - AppPacket::Ip(ip_packet) => ip_packet.render(block, frame), - AppPacket::Arp(arp_packet) => arp_packet.render(block, frame), + match packet.packet { + NetworkPacket::Ip(ip_packet) => ip_packet.render(block, frame), + NetworkPacket::Arp(arp_packet) => arp_packet.render(block, frame), }; } } diff --git a/oryx-tui/src/section/stats.rs b/oryx-tui/src/section/stats.rs index 0f813d2..3934ab3 100644 --- a/oryx-tui/src/section/stats.rs +++ b/oryx-tui/src/section/stats.rs @@ -17,10 +17,10 @@ use ratatui::{ use crate::{ bandwidth::Bandwidth, dns::get_hostname, - interface::NetworkInterface, packet::{ + direction::TrafficDirection, network::{IpPacket, IpProto}, - AppPacket, + AppPacket, NetworkPacket, }, }; @@ -41,7 +41,7 @@ pub struct Stats { } impl Stats { - pub fn new(packets: Arc>>, selected_interface: NetworkInterface) -> Self { + pub fn new(packets: Arc>>) -> Self { let packet_stats: Arc> = Arc::new(Mutex::new(PacketStats::default())); thread::spawn({ @@ -51,24 +51,23 @@ impl Stats { loop { thread::sleep(Duration::from_millis(500)); - let packets = { packets.lock().unwrap().clone() }; + let app_packets = { packets.lock().unwrap().clone() }; - if packets.is_empty() { + if app_packets.is_empty() { continue; } let mut packet_stats = packet_stats.lock().unwrap(); - for packet in packets[last_index..].iter() { - match packet { - AppPacket::Arp(_) => { + + for app_packet in app_packets[last_index..].iter() { + match app_packet.packet { + NetworkPacket::Arp(_) => { packet_stats.link.arp += 1; } - AppPacket::Ip(packet) => match packet { + NetworkPacket::Ip(packet) => match packet { IpPacket::V4(ipv4_packet) => { packet_stats.network.ipv4 += 1; - if !ipv4_packet.dst_ip.is_private() - && !ipv4_packet.dst_ip.is_loopback() - { + if app_packet.direction == TrafficDirection::Egress { if let Some((_, counts)) = packet_stats .addresses .get_mut(&IpAddr::V4(ipv4_packet.dst_ip)) @@ -103,10 +102,7 @@ impl Stats { IpPacket::V6(ipv6_packet) => { packet_stats.network.ipv6 += 1; - if !selected_interface - .addresses - .contains(&IpAddr::V6(ipv6_packet.dst_ip)) - { + if app_packet.direction == TrafficDirection::Egress { if let Some((_, counts)) = packet_stats .addresses .get_mut(&IpAddr::V6(ipv6_packet.dst_ip)) @@ -144,7 +140,7 @@ impl Stats { packet_stats.total += 1; } - last_index = packets.len() - 1; + last_index = app_packets.len() - 1; } } });