Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add excluded ips #339

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -38,6 +41,16 @@ pub struct Opt {
#[derivative(Default(value = "Verbosity::new(0, 0)"))]
pub verbosity: Verbosity<InfoLevel>,

#[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<Vec<Ipv4Addr>>,

#[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<Vec<SocketAddrV4>>,

#[command(flatten)]
pub render_opts: RenderOpts,
}
Expand Down
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
37 changes: 36 additions & 1 deletion src/network/utilization.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::collections::HashMap;
use std::{
collections::HashMap,
net::{Ipv4Addr, SocketAddrV4},
};

use crate::network::{Connection, Direction, Segment};

Expand Down Expand Up @@ -42,4 +45,36 @@ impl Utilization {
}
}
}
pub fn remove_ip(&mut self, ips: &Vec<Ipv4Addr>) {
// 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<SocketAddrV4>) {
// 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) */
}
}
}
}
}