Skip to content

Commit

Permalink
feat: add top downloader/uploader into a summary
Browse files Browse the repository at this point in the history
  • Loading branch information
Chleba committed Oct 10, 2024
1 parent 406d435 commit dac861c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "netscanner"
version = "0.5.4"
version = "0.6.0"
edition = "2021"
description = "Network Scanner"
license = "MIT"
Expand Down
79 changes: 70 additions & 9 deletions src/components/sniff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -39,13 +40,13 @@ pub struct IPTraffic {
pub struct Sniffer {
active_tab: TabsEnum,
action_tx: Option<UnboundedSender<Action>>,
active_interface: Option<NetworkInterface>,
list_state: ListState,
scrollbar_state: ScrollbarState,
traffic_ips: Vec<IPTraffic>,
scrollview_state: ScrollViewState,
udp_sum: f64,
tcp_sum: f64,
active_inft_ips: Vec<IpNetwork>,
}

impl Default for Sniffer {
Expand All @@ -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(),
}
}

Expand Down Expand Up @@ -228,7 +229,7 @@ impl Sniffer {
Rect {
x: area.x + 2,
y: area.y + 2,
width: area.width / 2,
width: area.width,
height: 1,
},
);
Expand All @@ -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,
},
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/scroll_traffic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit dac861c

Please sign in to comment.