adding interface name to TUI

This commit is contained in:
Marco Cadetg
2025-06-30 14:31:43 +02:00
parent 0eee869a2b
commit 2ebabdd438
3 changed files with 29 additions and 8 deletions

View File

@@ -93,6 +93,9 @@ pub struct App {
/// Loading state
is_loading: Arc<AtomicBool>,
/// Current network interface name
current_interface: Arc<RwLock<Option<String>>>,
}
impl App {
@@ -111,6 +114,7 @@ impl App {
service_lookup: Arc::new(service_lookup),
stats: Arc::new(AppStats::default()),
is_loading: Arc::new(AtomicBool::new(true)),
current_interface: Arc::new(RwLock::new(None)),
})
}
@@ -177,11 +181,14 @@ impl App {
let should_stop = Arc::clone(&self.should_stop);
let stats = Arc::clone(&self.stats);
let current_interface = Arc::clone(&self.current_interface);
thread::spawn(move || {
match setup_packet_capture(capture_config) {
Ok(capture) => {
info!("Packet capture started successfully");
Ok((capture, device_name)) => {
// Store the actual interface name being used
*current_interface.write().unwrap() = Some(device_name.clone());
info!("Packet capture started successfully on interface: {}", device_name);
let mut reader = PacketReader::new(capture);
let mut packets_read = 0u64;
let mut last_log = Instant::now();
@@ -525,6 +532,11 @@ impl App {
self.is_loading.load(Ordering::Relaxed)
}
/// Get the current network interface name
pub fn get_current_interface(&self) -> Option<String> {
self.current_interface.read().unwrap().clone()
}
/// Stop all threads gracefully
pub fn stop(&self) {
info!("Stopping application");

View File

@@ -132,7 +132,7 @@ fn find_best_device() -> Result<Device> {
}
/// Setup packet capture with the given configuration
pub fn setup_packet_capture(config: CaptureConfig) -> Result<Capture<Active>> {
pub fn setup_packet_capture(config: CaptureConfig) -> Result<(Capture<Active>, String)> {
// Find the capture device
let device = find_capture_device(&config.interface)?;
@@ -142,6 +142,8 @@ pub fn setup_packet_capture(config: CaptureConfig) -> Result<Capture<Active>> {
device.desc.as_deref().unwrap_or("no description")
);
let device_name = device.name.clone();
// Create capture handle
let mut cap = Capture::from_device(device)?
.promisc(config.promiscuous)
@@ -161,7 +163,7 @@ pub fn setup_packet_capture(config: CaptureConfig) -> Result<Capture<Active>> {
// Note: We're not setting non-blocking mode as we're using timeout instead
Ok(cap)
Ok((cap, device_name))
}
/// Find a capture device by name or return the default

View File

@@ -82,7 +82,7 @@ pub fn draw(
draw_tabs(f, ui_state, chunks[0]);
match ui_state.selected_tab {
0 => draw_overview(f, ui_state, connections, stats, chunks[1])?,
0 => draw_overview(f, ui_state, connections, stats, app, chunks[1])?,
1 => draw_connection_details(f, ui_state, connections, chunks[1])?,
2 => draw_help(f, chunks[1])?,
_ => {}
@@ -124,6 +124,7 @@ fn draw_overview(
ui_state: &UIState,
connections: &[Connection],
stats: &AppStats,
app: &App,
area: Rect,
) -> Result<()> {
let chunks = Layout::default()
@@ -132,7 +133,7 @@ fn draw_overview(
.split(area);
draw_connections_list(f, ui_state, connections, chunks[0]);
draw_stats_panel(f, connections, stats, chunks[1])?;
draw_stats_panel(f, connections, stats, app, chunks[1])?;
Ok(())
}
@@ -235,13 +236,14 @@ fn draw_stats_panel(
f: &mut Frame,
connections: &[Connection],
stats: &AppStats,
app: &App,
area: Rect,
) -> Result<()> {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(8), // Connection stats
Constraint::Min(0), // Traffic stats
Constraint::Length(10), // Connection stats (increased for interface line)
Constraint::Min(0), // Traffic stats
])
.split(area);
@@ -255,7 +257,12 @@ fn draw_stats_panel(
.filter(|c| c.protocol == Protocol::UDP)
.count();
let interface_name = app.get_current_interface()
.unwrap_or_else(|| "Unknown".to_string());
let conn_stats_text: Vec<Line> = vec![
Line::from(format!("Interface: {}", interface_name)),
Line::from(""),
Line::from(format!("TCP Connections: {}", tcp_count)),
Line::from(format!("UDP Connections: {}", udp_count)),
Line::from(format!("Total Connections: {}", connections.len())),