diff --git a/src/cli.rs b/src/cli.rs index 644a500ad..d70a43a64 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,7 @@ -use std::{net::Ipv4Addr, path::PathBuf}; +use std::{ + net::{Ipv4Addr, SocketAddrV4}, + path::PathBuf, +}; use clap::{Args, Parser}; use clap_verbosity_flag::{InfoLevel, Verbosity}; @@ -38,6 +41,16 @@ pub struct Opt { #[derivative(Default(value = "Verbosity::new(0, 0)"))] pub verbosity: Verbosity, + #[arg(short, long)] + /// exclude ip addres with <-e x.x.x.x> + /// exclude multiple ip addresses with <-e x.x.x.x -e y.y.y.y> + pub excluded_ipv4: Option>, + + #[arg(long)] + /// exclude ip addres with <-e x.x.x.x:zzzz> + /// exclude multiple ip addresses and port with <-e x.x.x.x:zzzz -e y.y.y.y:zzzz> + pub excluded_ipv4_port: Option>, + #[command(flatten)] pub render_opts: RenderOpts, } diff --git a/src/main.rs b/src/main.rs index 8f765f490..5fb0e8c57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -138,7 +138,15 @@ where move || { while running.load(Ordering::Acquire) { let render_start_time = Instant::now(); - let utilization = { network_utilization.lock().unwrap().clone_and_reset() }; + let mut utilization = { network_utilization.lock().unwrap().clone_and_reset() }; + match opts.excluded_ipv4 { + Some(ref ex) => utilization.remove_ip(ex), + None => {} + }; + match opts.excluded_ipv4_port { + Some(ref ex) => utilization.remove_ip_port(ex), + None => {} + }; let OpenSockets { sockets_to_procs } = get_open_sockets(); let mut ip_to_host = IpTable::new(); if let Some(dns_client) = dns_client.as_mut() { diff --git a/src/network/utilization.rs b/src/network/utilization.rs index c44356b6e..940c33833 100644 --- a/src/network/utilization.rs +++ b/src/network/utilization.rs @@ -1,4 +1,7 @@ -use std::collections::HashMap; +use std::{ + collections::HashMap, + net::{Ipv4Addr, SocketAddrV4}, +}; use crate::network::{Connection, Direction, Segment}; @@ -42,4 +45,36 @@ impl Utilization { } } } + pub fn remove_ip(&mut self, ips: &Vec) { + // might be possible to refactor this part better + // i still don't understand the whole borrow/own system very well yet + let placeholder = self.connections.clone(); + for util in placeholder { + match util.0.remote_socket.ip { + std::net::IpAddr::V4(ip) => { + if ips.contains(&ip) { + self.connections.remove_entry(&util.0); + } + } + std::net::IpAddr::V6(..) => { /* nothing here yet (maybe implement it for ipV6 too) */ + } + } + } + } + pub fn remove_ip_port(&mut self, ips: &Vec) { + // might be possible to refactor this part better + // i still don't understand the whole borrow/own system very well yet + let placeholder = self.connections.clone(); + for util in placeholder { + match util.0.remote_socket.ip { + std::net::IpAddr::V4(ip) => { + if ips.contains(&SocketAddrV4::new(ip, util.0.remote_socket.port)) { + self.connections.remove_entry(&util.0); + } + } + std::net::IpAddr::V6(..) => { /* nothing here yet (maybe implement it for ipV6 too) */ + } + } + } + } }