Skip to content

Commit

Permalink
components update
Browse files Browse the repository at this point in the history
- prepared tabs
- change home to title
- packetdump change pause logic for arp packets handling
  • Loading branch information
Chleba committed Apr 28, 2024
1 parent 0a568b0 commit b129398
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,7 +31,7 @@ pub struct App {

impl App {
pub fn new(tick_rate: f64, frame_rate: f64) -> Result<Self> {
let home = Home::new();
let title = Title::new();
let interfaces = Interfaces::default();
let wifiscan = WifiScan::default();
let wifi_interface = WifiInterface::default();
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
66 changes: 33 additions & 33 deletions src/components/packetdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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),
),
)
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/components/home.rs → src/components/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use crate::{
};

#[derive(Default)]
pub struct Home {
pub struct Tabs {
command_tx: Option<UnboundedSender<Action>>,
config: Config,
}

impl Home {
impl Tabs {
pub fn new() -> Self {
Self {
command_tx: None,
Expand All @@ -27,7 +27,7 @@ impl Home {
}
}

impl Component for Home {
impl Component for Tabs {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
self.command_tx = Some(tx);
Ok(())
Expand Down
56 changes: 56 additions & 0 deletions src/components/title.rs
Original file line number Diff line number Diff line change
@@ -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<UnboundedSender<Action>>,
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<Action>) -> 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<Option<Action>> {
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(())
}
}

0 comments on commit b129398

Please sign in to comment.