From dac861c9587875eb00aeef1168f8371ff2bef086 Mon Sep 17 00:00:00 2001 From: Chleba Date: Thu, 10 Oct 2024 18:26:44 +0200 Subject: [PATCH] feat: add top downloader/uploader into a summary --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/components/sniff.rs | 79 +++++++++++++++++++++++++++++++---- src/widgets/scroll_traffic.rs | 2 +- 4 files changed, 73 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7699bf5..ba7a62d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1005,7 +1005,7 @@ dependencies = [ [[package]] name = "netscanner" -version = "0.5.4" +version = "0.6.0" dependencies = [ "better-panic", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 9d8f3fd..707dea3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "netscanner" -version = "0.5.4" +version = "0.6.0" edition = "2021" description = "Network Scanner" license = "MIT" diff --git a/src/components/sniff.rs b/src/components/sniff.rs index 76feaa5..73ddc49 100644 --- a/src/components/sniff.rs +++ b/src/components/sniff.rs @@ -2,6 +2,7 @@ use color_eyre::eyre::Result; use color_eyre::owo_colors::OwoColorize; use dns_lookup::{lookup_addr, lookup_host}; +use ipnetwork::IpNetwork; use pnet::{ datalink::NetworkInterface, packet::{ @@ -39,13 +40,13 @@ pub struct IPTraffic { pub struct Sniffer { active_tab: TabsEnum, action_tx: Option>, - active_interface: Option, list_state: ListState, scrollbar_state: ScrollbarState, traffic_ips: Vec, scrollview_state: ScrollViewState, udp_sum: f64, tcp_sum: f64, + active_inft_ips: Vec, } impl Default for Sniffer { @@ -59,13 +60,13 @@ impl Sniffer { Self { active_tab: TabsEnum::Discovery, action_tx: None, - active_interface: None, list_state: ListState::default().with_selected(Some(0)), scrollbar_state: ScrollbarState::new(0), traffic_ips: Vec::new(), scrollview_state: ScrollViewState::new(), udp_sum: 0.0, tcp_sum: 0.0, + active_inft_ips: Vec::new(), } } @@ -228,7 +229,7 @@ impl Sniffer { Rect { x: area.x + 2, y: area.y + 2, - width: area.width / 2, + width: area.width, height: 1, }, ); @@ -240,19 +241,79 @@ impl Sniffer { f.render_widget( total_upload, Rect { - x: area.x + (area.width / 2) + 2, - y: area.y + 2, - width: area.width / 2, + x: area.x + 2, + y: area.y + 3, + width: area.width, height: 1, }, ); - let top_uploader = Line::from(vec!["Top uploader:".into()]); + let a_intfs = self.active_inft_ips.clone(); + let tu = self + .traffic_ips + .iter() + .filter(|item| { + let t_ip = item.ip.to_string(); + for i_ip in a_intfs.clone() { + if i_ip.ip().to_string() == t_ip { + return false; + } + } + true + }) + .max_by_key(|t| t.upload as u64); + + let mut tu_ip = String::from(""); + let mut tu_name = String::from(""); + if tu.is_some() { + tu_ip = tu.unwrap().ip.to_string(); + tu_name = format!(" ({})", tu.unwrap().hostname); + } + let top_uploader = Line::from(vec![ + "Top uploader: ".into(), + tu_ip.blue(), + tu_name.magenta(), + ]); f.render_widget( top_uploader, Rect { x: area.x + 2, - y: area.y + 4, + y: area.y + 5, + width: area.width, + height: 1, + }, + ); + + let td = self + .traffic_ips + .iter() + .filter(|item| { + let t_ip = item.ip.to_string(); + for i_ip in a_intfs.clone() { + if i_ip.ip().to_string() == t_ip { + return false; + } + } + true + }) + .max_by_key(|t| t.download as u64); + + let mut td_ip = String::from(""); + let mut td_name = String::from(""); + if td.is_some() { + td_ip = td.unwrap().ip.to_string(); + td_name = format!(" ({})", tu.unwrap().hostname); + } + let top_downloader = Line::from(vec![ + "Top downloader: ".into(), + td_ip.blue(), + td_name.magenta(), + ]); + f.render_widget( + top_downloader, + Rect { + x: area.x + 2, + y: area.y + 6, width: area.width, height: 1, }, @@ -291,7 +352,7 @@ impl Component for Sniffer { } if let Action::ActiveInterface(ref interface) = action { - self.active_interface = Some(interface.clone()); + self.active_inft_ips = interface.ips.clone(); } if let Action::PacketDump(time, packet, packet_type) = action { diff --git a/src/widgets/scroll_traffic.rs b/src/widgets/scroll_traffic.rs index 97dda2b..01e2441 100644 --- a/src/widgets/scroll_traffic.rs +++ b/src/widgets/scroll_traffic.rs @@ -31,7 +31,7 @@ impl StatefulWidget for TrafficScroll { .border_style(Style::default().fg(Color::Rgb(100, 100, 100))) .title_style(Style::default().fg(Color::Blue)) .title(Line::from(vec![ - format!("|{}|", item.ip).blue(), + format!("{}", item.ip).blue(), format!(" ({})", item.hostname.clone()).magenta(), ])); scrollview.render_widget(b, b_rect);