mirror of
https://github.com/domcyrus/rustnet.git
synced 2026-02-15 19:29:46 -06:00
refactor: Pass monitor to platform connection fns, fix build
This commit is contained in:
@@ -250,6 +250,10 @@ impl I18n {
|
||||
"help_toggle_location".to_string(),
|
||||
"Toggle IP location display".to_string(),
|
||||
);
|
||||
self.translations.insert(
|
||||
"help_toggle_dns".to_string(),
|
||||
"Toggle DNS resolution".to_string(),
|
||||
);
|
||||
self.translations.insert(
|
||||
"help_toggle_help".to_string(),
|
||||
"Toggle help screen".to_string(),
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use log::{debug, error, info, warn};
|
||||
use pnet_datalink;
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use std::process::Command;
|
||||
|
||||
use super::{Connection, ConnectionState, NetworkMonitor, Process, Protocol};
|
||||
|
||||
impl NetworkMonitor {
|
||||
/// Get connections using platform-specific methods
|
||||
pub(super) fn get_platform_connections(&self, connections: &mut Vec<Connection>) -> Result<()> {
|
||||
// Debug output
|
||||
debug!("Attempting to get connections using platform-specific methods");
|
||||
/// Get platform-specific connections for Linux
|
||||
pub fn get_platform_connections(
|
||||
monitor: &NetworkMonitor,
|
||||
connections: &mut Vec<Connection>,
|
||||
) -> Result<()> {
|
||||
// Debug output
|
||||
debug!("Attempting to get connections using platform-specific methods");
|
||||
|
||||
// Use ss command to get TCP connections
|
||||
info!("Running ss command to get TCP connections...");
|
||||
let ss_result = self.get_connections_from_ss(connections);
|
||||
let ss_result = monitor.get_connections_from_ss(connections);
|
||||
if let Err(e) = &ss_result {
|
||||
error!("Error running ss command: {}", e);
|
||||
} else {
|
||||
@@ -22,7 +25,7 @@ impl NetworkMonitor {
|
||||
|
||||
// Use netstat to get UDP connections
|
||||
info!("Running netstat command to get UDP connections...");
|
||||
let netstat_result = self.get_connections_from_netstat(connections);
|
||||
let netstat_result = monitor.get_connections_from_netstat(connections);
|
||||
if let Err(e) = &netstat_result {
|
||||
error!("Error running netstat command: {}", e);
|
||||
} else {
|
||||
@@ -38,7 +41,7 @@ impl NetworkMonitor {
|
||||
// If we didn't get any connections from commands, try using pcap
|
||||
if connections.is_empty() {
|
||||
warn!("No connections found from commands, trying packet capture...");
|
||||
self.get_connections_from_pcap(connections)?;
|
||||
monitor.get_connections_from_pcap(connections)?;
|
||||
debug!(
|
||||
"Found {} connections from packet capture",
|
||||
connections.len()
|
||||
@@ -46,8 +49,14 @@ impl NetworkMonitor {
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
// Note: get_linux_process_for_connection, get_process_by_pid,
|
||||
// get_connections_from_ss, get_connections_from_netstat, get_connections_from_pcap
|
||||
// remain methods on NetworkMonitor as they are called via `monitor.method_name()`
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Methods below remain part of NetworkMonitor impl
|
||||
impl NetworkMonitor {
|
||||
/// Get Linux-specific process for a connection
|
||||
pub(super) fn get_linux_process_for_connection(
|
||||
&self,
|
||||
|
||||
@@ -64,13 +64,9 @@ pub fn get_interface_addresses(interface: &str) -> Result<Vec<IpAddr>> {
|
||||
|
||||
/// Get platform-specific connections for macOS
|
||||
pub fn get_platform_connections(
|
||||
monitor: &NetworkMonitor,
|
||||
connections: &mut Vec<Connection>,
|
||||
interface: &Option<String>,
|
||||
) -> Result<()> {
|
||||
// Create a temporary NetworkMonitor to use its methods
|
||||
// We only need this to access network-related methods
|
||||
let monitor = NetworkMonitor::new(interface.clone())?;
|
||||
|
||||
// Try different commands to maximize connection detection
|
||||
// First try netstat - more reliable on macOS than lsof in some cases
|
||||
monitor.get_connections_from_netstat(connections)?;
|
||||
@@ -85,7 +81,7 @@ pub fn get_platform_connections(
|
||||
);
|
||||
|
||||
// Filter by interface if specified
|
||||
if let Some(iface) = interface {
|
||||
if let Some(iface) = &monitor.interface {
|
||||
debug!("Filtering connections for interface: {}", iface);
|
||||
let connection_count_before = connections.len();
|
||||
|
||||
|
||||
@@ -562,17 +562,17 @@ impl NetworkMonitor {
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
// Use Linux-specific implementation
|
||||
linux::get_platform_connections(connections, &self.interface)?;
|
||||
linux::get_platform_connections(self, connections)?;
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
// Use macOS-specific implementation
|
||||
macos::get_platform_connections(connections, &self.interface)?;
|
||||
macos::get_platform_connections(self, connections)?;
|
||||
}
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
// Use Windows-specific implementation
|
||||
windows::get_platform_connections(connections, &self.interface)?;
|
||||
windows::get_platform_connections(self, connections)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -4,15 +4,19 @@ use std::process::Command;
|
||||
|
||||
use super::{Connection, ConnectionState, NetworkMonitor, Process, Protocol};
|
||||
|
||||
/// Get platform-specific connections for Windows
|
||||
pub fn get_platform_connections(
|
||||
monitor: &NetworkMonitor,
|
||||
connections: &mut Vec<Connection>,
|
||||
) -> Result<()> {
|
||||
// Use netstat on Windows for both TCP and UDP
|
||||
monitor.get_connections_from_netstat(connections)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Methods below remain part of NetworkMonitor impl
|
||||
impl NetworkMonitor {
|
||||
/// Get connections using platform-specific methods
|
||||
pub(super) fn get_platform_connections(&self, connections: &mut Vec<Connection>) -> Result<()> {
|
||||
// Use netstat on Windows for both TCP and UDP
|
||||
self.get_connections_from_netstat(connections)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get platform-specific process for a connection
|
||||
pub(super) fn get_platform_process_for_connection(
|
||||
&self,
|
||||
|
||||
Reference in New Issue
Block a user