diff --git a/oryx-tui/src/app.rs b/oryx-tui/src/app.rs index a6883bb..edc3005 100644 --- a/oryx-tui/src/app.rs +++ b/oryx-tui/src/app.rs @@ -5,7 +5,7 @@ use ratatui::{ }; use std::{ error, - sync::{Arc, Mutex}, + sync::{Arc, RwLock}, thread, time::Duration, }; @@ -43,7 +43,7 @@ pub struct App { pub help: Help, pub filter: Filter, pub start_sniffing: bool, - pub packets: Arc>>, + pub packets: Arc>>, pub notifications: Vec, pub section: Section, pub data_channel_sender: kanal::Sender<([u8; RawPacket::LEN], TrafficDirection)>, @@ -59,7 +59,9 @@ impl Default for App { impl App { pub fn new() -> Self { - let packets = Arc::new(Mutex::new(Vec::with_capacity(RawPacket::LEN * 1024 * 1024))); + let packets = Arc::new(RwLock::new(Vec::with_capacity( + RawPacket::LEN * 1024 * 1024, + ))); let (sender, receiver) = kanal::unbounded(); @@ -70,7 +72,7 @@ impl App { move || loop { if let Ok((raw_packet, direction)) = receiver.recv() { let network_packet = NetworkPacket::from(raw_packet); - let mut packets = packets.lock().unwrap(); + let mut packets = packets.write().unwrap(); if packets.len() == packets.capacity() { packets.reserve(1024 * 1024); } diff --git a/oryx-tui/src/filter/fuzzy.rs b/oryx-tui/src/filter/fuzzy.rs index 5b45564..685cbcd 100644 --- a/oryx-tui/src/filter/fuzzy.rs +++ b/oryx-tui/src/filter/fuzzy.rs @@ -1,5 +1,5 @@ use std::{ - sync::{Arc, Mutex}, + sync::{Arc, Mutex, RwLock}, thread, time::Duration, }; @@ -24,7 +24,7 @@ pub struct Fuzzy { } impl Fuzzy { - pub fn new(packets: Arc>>) -> Arc> { + pub fn new(packets: Arc>>) -> Arc> { let fuzzy = Arc::new(Mutex::new(Self::default())); thread::spawn({ @@ -38,7 +38,7 @@ impl Fuzzy { let mut fuzzy = fuzzy.lock().unwrap(); if fuzzy.is_enabled() && !fuzzy.filter.value().is_empty() { - let packets = packets.lock().unwrap(); + let packets = packets.read().unwrap(); let current_pattern = fuzzy.filter.value().to_owned(); if current_pattern != pattern { fuzzy.find(packets.as_slice()); diff --git a/oryx-tui/src/section.rs b/oryx-tui/src/section.rs index ea23120..7f07426 100644 --- a/oryx-tui/src/section.rs +++ b/oryx-tui/src/section.rs @@ -4,7 +4,7 @@ pub mod inspection; pub mod metrics; pub mod stats; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, RwLock}; use alert::Alert; use crossterm::event::{KeyCode, KeyEvent}; @@ -49,7 +49,7 @@ pub struct Section { impl Section { pub fn new( - packets: Arc>>, + packets: Arc>>, firewall_chans: IoChannels, ) -> Self { Self { diff --git a/oryx-tui/src/section/alert.rs b/oryx-tui/src/section/alert.rs index 7263f57..b5681a7 100644 --- a/oryx-tui/src/section/alert.rs +++ b/oryx-tui/src/section/alert.rs @@ -6,7 +6,7 @@ use ratatui::{ text::{Span, Text}, Frame, }; -use std::sync::{atomic::Ordering, Arc, Mutex}; +use std::sync::{atomic::Ordering, Arc, RwLock}; use syn_flood::SynFlood; use crate::packet::AppPacket; @@ -19,7 +19,7 @@ pub struct Alert { } impl Alert { - pub fn new(packets: Arc>>) -> Self { + pub fn new(packets: Arc>>) -> Self { Self { syn_flood: SynFlood::new(packets), flash_count: 1, diff --git a/oryx-tui/src/section/alert/syn_flood.rs b/oryx-tui/src/section/alert/syn_flood.rs index 13b5813..b627fd9 100644 --- a/oryx-tui/src/section/alert/syn_flood.rs +++ b/oryx-tui/src/section/alert/syn_flood.rs @@ -1,7 +1,7 @@ use std::{ collections::HashMap, net::IpAddr, - sync::{atomic::AtomicBool, Arc, Mutex}, + sync::{atomic::AtomicBool, Arc, Mutex, RwLock}, thread, time::Duration, }; @@ -29,7 +29,7 @@ pub struct SynFlood { } impl SynFlood { - pub fn new(packets: Arc>>) -> Self { + pub fn new(packets: Arc>>) -> Self { let map: Arc>> = Arc::new(Mutex::new(HashMap::new())); let detected = Arc::new(AtomicBool::new(false)); @@ -40,14 +40,14 @@ impl SynFlood { let detected = detected.clone(); move || loop { let start_index = { - let packets = packets.lock().unwrap(); + let packets = packets.read().unwrap(); packets.len().saturating_sub(1) }; thread::sleep(Duration::from_secs(5)); let app_packets = { - let packets = packets.lock().unwrap(); + let packets = packets.read().unwrap(); packets.clone() }; diff --git a/oryx-tui/src/section/inspection.rs b/oryx-tui/src/section/inspection.rs index 2e49182..7cf9829 100644 --- a/oryx-tui/src/section/inspection.rs +++ b/oryx-tui/src/section/inspection.rs @@ -1,4 +1,4 @@ -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, RwLock}; use crossterm::event::{KeyCode, KeyEvent}; use ratatui::{ @@ -26,7 +26,7 @@ use crate::{ #[derive(Debug)] pub struct Inspection { - pub packets: Arc>>, + pub packets: Arc>>, pub state: TableState, pub fuzzy: Arc>, pub manuall_scroll: bool, @@ -36,7 +36,7 @@ pub struct Inspection { } impl Inspection { - pub fn new(packets: Arc>>) -> Self { + pub fn new(packets: Arc>>) -> Self { Self { packets: packets.clone(), state: TableState::default(), @@ -49,7 +49,7 @@ impl Inspection { } pub fn can_show_popup(&mut self) -> bool { - let packets = self.packets.lock().unwrap(); + let packets = self.packets.read().unwrap(); let fuzzy = self.fuzzy.lock().unwrap(); if fuzzy.is_enabled() { @@ -134,7 +134,7 @@ impl Inspection { } KeyCode::Char('s') => { - let app_packets = self.packets.lock().unwrap(); + let app_packets = self.packets.read().unwrap(); if app_packets.is_empty() { Notification::send( "There is no packets".to_string(), @@ -168,7 +168,7 @@ impl Inspection { } pub fn scroll_up(&mut self) { - let app_packets = self.packets.lock().unwrap(); + let app_packets = self.packets.read().unwrap(); if !self.manuall_scroll { self.manuall_scroll = true; // Record the last position. Usefull for selecting the packets to display @@ -193,7 +193,7 @@ impl Inspection { } pub fn scroll_down(&mut self) { - let app_packets = self.packets.lock().unwrap(); + let app_packets = self.packets.read().unwrap(); if !self.manuall_scroll { self.manuall_scroll = true; @@ -220,7 +220,7 @@ impl Inspection { } pub fn render(&mut self, frame: &mut Frame, block: Rect) { - let app_packets = self.packets.lock().unwrap(); + let app_packets = self.packets.read().unwrap(); let mut fuzzy = self.fuzzy.lock().unwrap(); let fuzzy_packets = fuzzy.clone().packets.clone(); @@ -638,7 +638,7 @@ impl Inspection { .split(layout[1])[1]; let fuzzy = self.fuzzy.lock().unwrap(); - let packets = self.packets.lock().unwrap(); + let packets = self.packets.read().unwrap(); let app_packet = if fuzzy.is_enabled() { fuzzy.packets[self.packet_index.unwrap()] diff --git a/oryx-tui/src/section/metrics.rs b/oryx-tui/src/section/metrics.rs index 638ed69..41fa775 100644 --- a/oryx-tui/src/section/metrics.rs +++ b/oryx-tui/src/section/metrics.rs @@ -1,7 +1,7 @@ use std::{ cmp, ops::Range, - sync::{atomic::AtomicBool, Arc, Mutex}, + sync::{atomic::AtomicBool, Arc, Mutex, RwLock}, thread, time::Duration, }; @@ -39,7 +39,7 @@ struct ListState { #[derive(Debug)] pub struct Metrics { user_input: UserInput, - app_packets: Arc>>, + app_packets: Arc>>, metrics: Vec>>, terminate: Arc, state: ListState, @@ -96,7 +96,7 @@ pub struct PortCountMetric { } impl Metrics { - pub fn new(packets: Arc>>) -> Self { + pub fn new(packets: Arc>>) -> Self { Self { user_input: UserInput::default(), app_packets: packets, @@ -311,7 +311,7 @@ impl Metrics { 'main: loop { thread::sleep(Duration::from_millis(100)); - let app_packets = { packets.lock().unwrap().clone() }; + let app_packets = { packets.read().unwrap().clone() }; if app_packets.is_empty() { continue; diff --git a/oryx-tui/src/section/stats.rs b/oryx-tui/src/section/stats.rs index b266b30..b1e8148 100644 --- a/oryx-tui/src/section/stats.rs +++ b/oryx-tui/src/section/stats.rs @@ -1,7 +1,7 @@ use std::{ collections::HashMap, net::IpAddr, - sync::{Arc, Mutex}, + sync::{Arc, Mutex, RwLock}, thread, time::Duration, }; @@ -41,7 +41,7 @@ pub struct Stats { } impl Stats { - pub fn new(packets: Arc>>) -> Self { + pub fn new(packets: Arc>>) -> Self { let packet_stats: Arc> = Arc::new(Mutex::new(PacketStats::default())); thread::spawn({ @@ -51,7 +51,7 @@ impl Stats { loop { thread::sleep(Duration::from_millis(500)); - let app_packets = { packets.lock().unwrap().clone() }; + let app_packets = { packets.read().unwrap().clone() }; if app_packets.is_empty() { continue;