mirror of
https://github.com/domcyrus/rustnet.git
synced 2026-02-26 10:34:46 -06:00
feat: Filter localhost traffic by default
This commit is contained in:
@@ -87,7 +87,8 @@ impl App {
|
||||
pub fn start_capture(&mut self) -> Result<()> {
|
||||
// Create network monitor
|
||||
let interface = self.config.interface.clone();
|
||||
let mut monitor = NetworkMonitor::new(interface)?;
|
||||
let filter_localhost = self.config.filter_localhost;
|
||||
let mut monitor = NetworkMonitor::new(interface, filter_localhost)?;
|
||||
|
||||
// Disable process information collection by default for better performance
|
||||
monitor.set_collect_process_info(false);
|
||||
|
||||
@@ -15,6 +15,8 @@ pub struct Config {
|
||||
pub refresh_interval: u64,
|
||||
/// Show IP locations (requires MaxMind DB)
|
||||
pub show_locations: bool,
|
||||
/// Filter out localhost (loopback) traffic
|
||||
pub filter_localhost: bool,
|
||||
/// Custom configuration file path
|
||||
pub config_path: Option<PathBuf>,
|
||||
}
|
||||
@@ -27,6 +29,7 @@ impl Default for Config {
|
||||
geoip_db_path: None,
|
||||
refresh_interval: 1000,
|
||||
show_locations: true,
|
||||
filter_localhost: true,
|
||||
config_path: None,
|
||||
}
|
||||
}
|
||||
@@ -82,6 +85,13 @@ impl Config {
|
||||
config.show_locations = false;
|
||||
}
|
||||
}
|
||||
"filter_localhost" => {
|
||||
if value == "true" {
|
||||
config.filter_localhost = true;
|
||||
} else if value == "false" {
|
||||
config.filter_localhost = false;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// Ignore unknown keys
|
||||
}
|
||||
|
||||
@@ -156,12 +156,13 @@ pub struct NetworkMonitor {
|
||||
connections: HashMap<String, Connection>,
|
||||
// geo_db: Option<maxminddb::Reader<Vec<u8>>>, // Field removed as unused (dependent on get_ip_location)
|
||||
collect_process_info: bool,
|
||||
filter_localhost: bool,
|
||||
last_packet_check: Instant,
|
||||
}
|
||||
|
||||
impl NetworkMonitor {
|
||||
/// Create a new network monitor
|
||||
pub fn new(interface: Option<String>) -> Result<Self> {
|
||||
pub fn new(interface: Option<String>, filter_localhost: bool) -> Result<Self> {
|
||||
let mut capture = if let Some(iface) = &interface {
|
||||
// Open capture on specific interface
|
||||
let device = Device::list()?
|
||||
@@ -219,6 +220,7 @@ impl NetworkMonitor {
|
||||
connections: HashMap::new(),
|
||||
// geo_db, // Field removed
|
||||
collect_process_info: false,
|
||||
filter_localhost,
|
||||
// Initialize last_packet_check to a time in the past
|
||||
// to ensure the first call to process_packets runs.
|
||||
last_packet_check: Instant::now() - Duration::from_millis(200),
|
||||
@@ -271,6 +273,13 @@ impl NetworkMonitor {
|
||||
// Sort connections by last activity
|
||||
connections.sort_by(|a, b| b.last_activity.cmp(&a.last_activity));
|
||||
|
||||
// Filter localhost connections if the flag is set
|
||||
if self.filter_localhost {
|
||||
connections.retain(|conn| {
|
||||
!(conn.local_addr.ip().is_loopback() && conn.remote_addr.ip().is_loopback())
|
||||
});
|
||||
}
|
||||
|
||||
Ok(connections)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user