From b1293987230e140ee0009023a2fee991a5007a91 Mon Sep 17 00:00:00 2001 From: chleba Date: Sun, 28 Apr 2024 02:15:43 +0200 Subject: [PATCH] components update - prepared tabs - change home to title - packetdump change pause logic for arp packets handling --- src/app.rs | 6 +-- src/components.rs | 2 +- src/components/packetdump.rs | 66 ++++++++++++++--------------- src/components/{home.rs => tabs.rs} | 6 +-- src/components/title.rs | 56 ++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 40 deletions(-) rename src/components/{home.rs => tabs.rs} (95%) create mode 100644 src/components/title.rs diff --git a/src/app.rs b/src/app.rs index 61431cd..52814c9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -7,7 +7,7 @@ use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; use crate::{ action::Action, components::{ - discovery::Discovery, home::Home, interfaces::Interfaces, packetdump::PacketDump, + discovery::Discovery, title::Title, interfaces::Interfaces, packetdump::PacketDump, wifi_chart::WifiChart, wifi_interface::WifiInterface, wifi_scan::WifiScan, Component, }, config::Config, @@ -31,7 +31,7 @@ pub struct App { impl App { pub fn new(tick_rate: f64, frame_rate: f64) -> Result { - let home = Home::new(); + let title = Title::new(); let interfaces = Interfaces::default(); let wifiscan = WifiScan::default(); let wifi_interface = WifiInterface::default(); @@ -47,7 +47,7 @@ impl App { tick_rate: 10.0, frame_rate, components: vec![ - Box::new(home), + Box::new(title), Box::new(interfaces), Box::new(wifiscan), Box::new(wifi_interface), diff --git a/src/components.rs b/src/components.rs index a36bd6c..422a9b5 100644 --- a/src/components.rs +++ b/src/components.rs @@ -10,7 +10,7 @@ use crate::{ }; pub mod discovery; -pub mod home; +pub mod title; pub mod interfaces; pub mod packetdump; pub mod wifi_chart; diff --git a/src/components/packetdump.rs b/src/components/packetdump.rs index 20c580f..5016ca8 100644 --- a/src/components/packetdump.rs +++ b/src/components/packetdump.rs @@ -372,11 +372,8 @@ impl PacketDump { } // Ok(_) => panic!("Unknown channel type"), // Err(e) => panic!("Error happened {}", e), }; + // while !paused.load(Ordering::Relaxed) { loop { - if paused.load(Ordering::Relaxed) { - break; - } - let mut buf: [u8; 1600] = [0u8; 1600]; let mut fake_ethernet_frame = MutableEthernetPacket::new(&mut buf[..]).unwrap(); @@ -748,27 +745,33 @@ impl PacketDump { } fn make_state_toast(&mut self) -> Paragraph<'static> { - let mut text = Span::styled("..running..", Style::default().bg(Color::Green).fg(Color::Black)); + let mut text = Span::styled( + "..running..", + Style::default().bg(Color::Green).fg(Color::Black), + ); if self.dump_paused.load(Ordering::Relaxed) { - text = Span::styled("..stopped..", Style::default().bg(Color::Red).fg(Color::White)); + text = Span::styled( + "..stopped..", + Style::default().bg(Color::Red).fg(Color::White), + ); } Paragraph::new(text).block( Block::default() .borders(Borders::ALL) .border_style(Style::default().fg(Color::Rgb(100, 100, 100))) .title( - ratatui::widgets::block::Title::from(Line::from(vec![ - Span::raw("|"), - Span::styled( - "d", - Style::default().add_modifier(Modifier::BOLD).fg(Color::Red), - ), - Span::styled("ump", Style::default().fg(Color::Yellow)), - Span::raw("|"), - ])) - .alignment(Alignment::Right) - .position(ratatui::widgets::block::Position::Bottom), - ) + ratatui::widgets::block::Title::from(Line::from(vec![ + Span::raw("|"), + Span::styled( + "d", + Style::default().add_modifier(Modifier::BOLD).fg(Color::Red), + ), + Span::styled("ump", Style::default().fg(Color::Yellow)), + Span::raw("|"), + ])) + .alignment(Alignment::Right) + .position(ratatui::widgets::block::Position::Bottom), + ), ) } @@ -916,16 +919,18 @@ impl Component for PacketDump { } } // -- packet recieved - if let Action::PacketDump(time, packet, packet_type) = action { - match packet_type { - PacketTypeEnum::Tcp => self.tcp_packets.push((time, packet.clone())), - PacketTypeEnum::Arp => self.arp_packets.push((time, packet.clone())), - PacketTypeEnum::Udp => self.udp_packets.push((time, packet.clone())), - PacketTypeEnum::Icmp => self.icmp_packets.push((time, packet.clone())), - PacketTypeEnum::Icmp6 => self.icmp6_packets.push((time, packet.clone())), - _ => {} + if !self.dump_paused.load(Ordering::Relaxed) { + if let Action::PacketDump(time, packet, packet_type) = action { + match packet_type { + PacketTypeEnum::Tcp => self.tcp_packets.push((time, packet.clone())), + PacketTypeEnum::Arp => self.arp_packets.push((time, packet.clone())), + PacketTypeEnum::Udp => self.udp_packets.push((time, packet.clone())), + PacketTypeEnum::Icmp => self.icmp_packets.push((time, packet.clone())), + PacketTypeEnum::Icmp6 => self.icmp6_packets.push((time, packet.clone())), + _ => {} + } + self.all_packets.push((time, packet.clone())); } - self.all_packets.push((time, packet.clone())); } Ok(None) @@ -948,12 +953,7 @@ impl Component for PacketDump { // -- STATE TOAST let toast = self.make_state_toast(); - let toast_react = Rect::new( - table_rect.width - 14, - table_rect.y + 1, - 13, - 3, - ); + let toast_react = Rect::new(table_rect.width - 14, table_rect.y + 1, 13, 3); f.render_widget(toast, toast_react); // -- SCROLLBAR diff --git a/src/components/home.rs b/src/components/tabs.rs similarity index 95% rename from src/components/home.rs rename to src/components/tabs.rs index 6a4fbab..a81f9fb 100644 --- a/src/components/home.rs +++ b/src/components/tabs.rs @@ -13,12 +13,12 @@ use crate::{ }; #[derive(Default)] -pub struct Home { +pub struct Tabs { command_tx: Option>, config: Config, } -impl Home { +impl Tabs { pub fn new() -> Self { Self { command_tx: None, @@ -27,7 +27,7 @@ impl Home { } } -impl Component for Home { +impl Component for Tabs { fn register_action_handler(&mut self, tx: UnboundedSender) -> Result<()> { self.command_tx = Some(tx); Ok(()) diff --git a/src/components/title.rs b/src/components/title.rs new file mode 100644 index 0000000..abc595c --- /dev/null +++ b/src/components/title.rs @@ -0,0 +1,56 @@ +use std::{collections::HashMap, time::Duration}; + +use color_eyre::eyre::Result; +use crossterm::event::{KeyCode, KeyEvent}; +use ratatui::{prelude::*, widgets::*}; +use serde::{Deserialize, Serialize}; +use tokio::sync::mpsc::UnboundedSender; + +use super::{Component, Frame}; +use crate::{ + action::Action, + config::{Config, KeyBindings}, +}; + +#[derive(Default)] +pub struct Title { + command_tx: Option>, + config: Config, +} + +impl Title { + pub fn new() -> Self { + Self { + command_tx: None, + config: Config::default(), + } + } +} + +impl Component for Title { + fn register_action_handler(&mut self, tx: UnboundedSender) -> Result<()> { + self.command_tx = Some(tx); + Ok(()) + } + + fn register_config_handler(&mut self, config: Config) -> Result<()> { + self.config = config; + Ok(()) + } + + fn update(&mut self, action: Action) -> Result> { + match action { + Action::Tick => {} + _ => {} + } + Ok(None) + } + + fn draw(&mut self, f: &mut Frame<'_>, area: Rect) -> Result<()> { + let rect = Rect::new(0, 0, f.size().width, 1); + let version: &str = env!("CARGO_PKG_VERSION"); + let title = format!(" Network Scanner (v{})", version); + f.render_widget(Paragraph::new(title), rect); + Ok(()) + } +}