Skip to content

Commit

Permalink
use RwLock instead of Mutex for packets
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Jan 15, 2025
1 parent 29c1b69 commit 470e083
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 31 deletions.
10 changes: 6 additions & 4 deletions oryx-tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ratatui::{
};
use std::{
error,
sync::{Arc, Mutex},
sync::{Arc, RwLock},
thread,
time::Duration,
};
Expand Down Expand Up @@ -43,7 +43,7 @@ pub struct App {
pub help: Help,
pub filter: Filter,
pub start_sniffing: bool,
pub packets: Arc<Mutex<Vec<AppPacket>>>,
pub packets: Arc<RwLock<Vec<AppPacket>>>,
pub notifications: Vec<Notification>,
pub section: Section,
pub data_channel_sender: kanal::Sender<([u8; RawPacket::LEN], TrafficDirection)>,
Expand All @@ -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();

Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions oryx-tui/src/filter/fuzzy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
sync::{Arc, Mutex},
sync::{Arc, Mutex, RwLock},
thread,
time::Duration,
};
Expand All @@ -24,7 +24,7 @@ pub struct Fuzzy {
}

impl Fuzzy {
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Arc<Mutex<Self>> {
pub fn new(packets: Arc<RwLock<Vec<AppPacket>>>) -> Arc<Mutex<Self>> {
let fuzzy = Arc::new(Mutex::new(Self::default()));

thread::spawn({
Expand All @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions oryx-tui/src/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -49,7 +49,7 @@ pub struct Section {

impl Section {
pub fn new(
packets: Arc<Mutex<Vec<AppPacket>>>,
packets: Arc<RwLock<Vec<AppPacket>>>,
firewall_chans: IoChannels<FirewallSignal>,
) -> Self {
Self {
Expand Down
4 changes: 2 additions & 2 deletions oryx-tui/src/section/alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +19,7 @@ pub struct Alert {
}

impl Alert {
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Self {
pub fn new(packets: Arc<RwLock<Vec<AppPacket>>>) -> Self {
Self {
syn_flood: SynFlood::new(packets),
flash_count: 1,
Expand Down
8 changes: 4 additions & 4 deletions oryx-tui/src/section/alert/syn_flood.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
collections::HashMap,
net::IpAddr,
sync::{atomic::AtomicBool, Arc, Mutex},
sync::{atomic::AtomicBool, Arc, Mutex, RwLock},
thread,
time::Duration,
};
Expand Down Expand Up @@ -29,7 +29,7 @@ pub struct SynFlood {
}

impl SynFlood {
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Self {
pub fn new(packets: Arc<RwLock<Vec<AppPacket>>>) -> Self {
let map: Arc<Mutex<HashMap<IpAddr, usize>>> = Arc::new(Mutex::new(HashMap::new()));

let detected = Arc::new(AtomicBool::new(false));
Expand All @@ -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()
};

Expand Down
18 changes: 9 additions & 9 deletions oryx-tui/src/section/inspection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Mutex, RwLock};

use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
Expand Down Expand Up @@ -26,7 +26,7 @@ use crate::{

#[derive(Debug)]
pub struct Inspection {
pub packets: Arc<Mutex<Vec<AppPacket>>>,
pub packets: Arc<RwLock<Vec<AppPacket>>>,
pub state: TableState,
pub fuzzy: Arc<Mutex<Fuzzy>>,
pub manuall_scroll: bool,
Expand All @@ -36,7 +36,7 @@ pub struct Inspection {
}

impl Inspection {
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Self {
pub fn new(packets: Arc<RwLock<Vec<AppPacket>>>) -> Self {
Self {
packets: packets.clone(),
state: TableState::default(),
Expand All @@ -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() {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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();

Expand Down Expand Up @@ -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()]
Expand Down
8 changes: 4 additions & 4 deletions oryx-tui/src/section/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
cmp,
ops::Range,
sync::{atomic::AtomicBool, Arc, Mutex},
sync::{atomic::AtomicBool, Arc, Mutex, RwLock},
thread,
time::Duration,
};
Expand Down Expand Up @@ -39,7 +39,7 @@ struct ListState {
#[derive(Debug)]
pub struct Metrics {
user_input: UserInput,
app_packets: Arc<Mutex<Vec<AppPacket>>>,
app_packets: Arc<RwLock<Vec<AppPacket>>>,
metrics: Vec<Arc<Mutex<PortCountMetric>>>,
terminate: Arc<AtomicBool>,
state: ListState,
Expand Down Expand Up @@ -96,7 +96,7 @@ pub struct PortCountMetric {
}

impl Metrics {
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Self {
pub fn new(packets: Arc<RwLock<Vec<AppPacket>>>) -> Self {
Self {
user_input: UserInput::default(),
app_packets: packets,
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions oryx-tui/src/section/stats.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
collections::HashMap,
net::IpAddr,
sync::{Arc, Mutex},
sync::{Arc, Mutex, RwLock},
thread,
time::Duration,
};
Expand Down Expand Up @@ -41,7 +41,7 @@ pub struct Stats {
}

impl Stats {
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Self {
pub fn new(packets: Arc<RwLock<Vec<AppPacket>>>) -> Self {
let packet_stats: Arc<Mutex<PacketStats>> = Arc::new(Mutex::new(PacketStats::default()));

thread::spawn({
Expand All @@ -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;
Expand Down

0 comments on commit 470e083

Please sign in to comment.