Skip to content

Commit

Permalink
uuupgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
adgaultier committed Sep 29, 2024
1 parent 830c92f commit 87d6c39
Show file tree
Hide file tree
Showing 23 changed files with 582 additions and 472 deletions.
7 changes: 4 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Description
# Components Description

## phases :
## phases
- Startup
- Sniffing

Expand All @@ -17,7 +17,8 @@
- alert
- firewall


## notification
---
# Rendering
## startup (phase)
- help (popup)
Expand Down
128 changes: 46 additions & 82 deletions oryx-tui/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use oryx_common::RawPacket;
use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
widgets::TableState,
Frame,
};
use ratatui::widgets::TableState;
use std::{
error,
sync::{Arc, Mutex},
Expand All @@ -14,54 +10,63 @@ use std::{
use crate::{
ebpf::Ebpf,
event::Event,
filters::{
direction::TrafficDirection, filter::Filter, fuzzy::Fuzzy, start_menu::StartMenuBlock,
update_menu::UpdateFilterMenuBlock,
},
filters::{direction::TrafficDirection, filter::Filter, fuzzy::Fuzzy},
phase::Phase,
startup::Startup,
update::UpdateBlockEnum,
};

use crate::help::Help;
use crate::interface::Interface;
use crate::notification::Notification;
use crate::packets::packet::AppPacket;

use crate::stats::Stats;
use crate::{alerts::alert::Alert, firewall::Firewall};
use crate::{bandwidth::Bandwidth, mode::Mode};
use crate::alerts::alert::Alert;
use crate::bandwidth::Bandwidth;

use crate::sections::{firewall::Firewall, section::Section, stats::Stats};
pub type AppResult<T> = std::result::Result<T, Box<dyn error::Error>>;

pub const TICK_RATE: u64 = 40;

#[derive(Debug, Clone, PartialEq)]
pub enum FocusedBlock {
StartMenuBlock(StartMenuBlock),
UpdateFilterMenuBlock(UpdateFilterMenuBlock),
Help,
Main(Mode),
}
// #[derive(Debug, Clone, PartialEq)]
// pub enum FocusedBlock {
// StartMenuBlock(StartMenuBlock),
// UpdateFilterMenuBlock(UpdateFilterMenuBlock),
// Help,
// Main(Section),
// }

#[derive(Debug)]
pub struct DataEventHandler {
pub sender: kanal::Sender<[u8; RawPacket::LEN]>,
pub handler: thread::JoinHandle<()>,
}

#[derive(Debug, PartialEq)]
pub enum Mode {
Normal,
Insert,
}

#[derive(Debug)]
pub struct App {
pub running: bool,
pub help: Help,
pub focused_block: FocusedBlock,
// used in setup to know which block to fall into after discarding help
pub previous_focused_block: FocusedBlock,
// pub focused_block: FocusedBlock,
// // used in setup to know which block to fall into after discarding help
// pub previous_focused_block: FocusedBlock,
pub startup: Startup,
pub filter_update: UpdateBlockEnum,
pub interface: Interface,
pub filter: Filter,
pub start_sniffing: bool,
pub phase: Phase,
pub packets: Arc<Mutex<Vec<AppPacket>>>,
pub packets_table_state: TableState,
pub fuzzy: Arc<Mutex<Fuzzy>>,
pub notifications: Vec<Notification>,
pub manuall_scroll: bool,
pub mode: Mode,
pub section: Section,
pub stats: Arc<Mutex<Stats>>,
pub packet_end_index: usize,
pub packet_window_size: usize,
Expand All @@ -72,7 +77,8 @@ pub struct App {
pub packet_index: Option<usize>,
pub alert: Alert,
pub firewall: Firewall,
pub is_editing: bool,
pub mode: Mode,
pub notification_sender: Option<kanal::Sender<Event>>,
}

impl Default for App {
Expand Down Expand Up @@ -102,17 +108,19 @@ impl App {
Self {
running: true,
help: Help::new(),
focused_block: FocusedBlock::StartMenuBlock(StartMenuBlock::Interface),
previous_focused_block: FocusedBlock::StartMenuBlock(StartMenuBlock::Interface),
startup: Startup::new(),
// focused_block: FocusedBlock::StartMenuBlock(StartMenuBlock::Interface),
// previous_focused_block: FocusedBlock::StartMenuBlock(StartMenuBlock::Interface),
interface: Interface::default(),
filter: Filter::new(),
start_sniffing: false,
phase: Phase::new(),
filter_update: UpdateBlockEnum::NetworkFilter,
packets: packets.clone(),
packets_table_state: TableState::default(),
fuzzy: Fuzzy::new(packets.clone()),
notifications: Vec::new(),
manuall_scroll: false,
mode: Mode::Packet,
section: Section::Packet,
stats,
packet_end_index: 0,
packet_window_size: 0,
Expand All @@ -123,79 +131,35 @@ impl App {
packet_index: None,
alert: Alert::new(packets.clone()),
firewall: Firewall::new(),
is_editing: false,
mode: Mode::Normal,
notification_sender: None,
}
}

pub fn load_ingress(&self, sender: &kanal::Sender<Event>) {
pub fn set_sender(&mut self, sender: kanal::Sender<Event>) {
self.notification_sender = Some(sender);
}
pub fn load_ingress(&self) {
{
Ebpf::load_ingress(
self.interface.selected_interface.name.clone(),
sender.clone(),
self.notification_sender.clone().unwrap(),
self.data_channel_sender.clone(),
self.filter.ingress_channel.receiver.clone(),
self.filter.traffic_direction.terminate_ingress.clone(),
);
}
}
pub fn load_egress(&self, sender: &kanal::Sender<Event>) {
pub fn load_egress(&self) {
{
Ebpf::load_egress(
self.interface.selected_interface.name.clone(),
sender.clone(),
self.notification_sender.clone().unwrap(),
self.data_channel_sender.clone(),
self.filter.egress_channel.receiver.clone(),
self.filter.traffic_direction.terminate_egress.clone(),
);
}
}
pub fn render(&mut self, frame: &mut Frame) {
// Setup
match self.focused_block.clone() {
FocusedBlock::StartMenuBlock(b) => b.render(frame, self),
FocusedBlock::Main(mode) => self.render_main_section(frame, &mode),
_ => {
match self.previous_focused_block.clone() {
FocusedBlock::StartMenuBlock(b) => b.render(frame, self),
FocusedBlock::Main(mode) => self.render_main_section(frame, &mode),
_ => {}
}
match self.focused_block {
FocusedBlock::UpdateFilterMenuBlock(b) => b.render(frame, self),
FocusedBlock::Help => self.help.render(frame),
_ => {}
}
}
}
}

fn render_main_section(&mut self, frame: &mut Frame, mode: &Mode) {
// Build layout
let (settings_block, mode_block) = {
let chunks: std::rc::Rc<[Rect]> = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(8), Constraint::Fill(1)])
.split(frame.area());
(chunks[0], chunks[1])
};
let (filter_block, interface_block) = {
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.margin(1)
.split(settings_block);
(chunks[0], chunks[1])
};

// Render settings
// Interface
self.interface.render_on_sniffing(frame, interface_block);
// Filters
self.filter.render_on_sniffing(frame, filter_block);

// Render mode section
mode.render(frame, mode_block, self);
}

pub fn detach_ebpf(&mut self) {
self.filter
Expand Down
16 changes: 6 additions & 10 deletions oryx-tui/src/filters/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use ratatui::{
Frame,
};

use crate::{app::FocusedBlock, MenuComponent, Scrollable};

use super::{start_menu::StartMenuBlock, update_menu::UpdateFilterMenuBlock};
use crate::{traits::MenuComponent, traits::Scrollable};

#[derive(Debug)]
pub struct TrafficDirectionFilter {
Expand Down Expand Up @@ -100,7 +98,7 @@ impl TrafficDirectionFilter {
self.selected_direction.clear();
}

pub fn render(&mut self, frame: &mut Frame, block: Rect, focused_block: &FocusedBlock) {
pub fn render(&mut self, frame: &mut Frame, block: Rect, is_focused: bool) {
let layout = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Expand Down Expand Up @@ -146,12 +144,10 @@ impl TrafficDirectionFilter {
.title_style(Style::default().bold().fg(Color::Green))
.title_alignment(Alignment::Center)
.borders(Borders::LEFT)
.border_type(match *focused_block {
FocusedBlock::StartMenuBlock(StartMenuBlock::TrafficDirection)
| FocusedBlock::UpdateFilterMenuBlock(
UpdateFilterMenuBlock::TrafficDirection,
) => BorderType::Thick,
_ => BorderType::default(),
.border_type(if is_focused {
BorderType::Thick
} else {
BorderType::default()
})
.border_style(Style::default().fg(Color::Green)),
area,
Expand Down
46 changes: 2 additions & 44 deletions oryx-tui/src/filters/filter.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use oryx_common::protocols::{
Protocol, NB_LINK_PROTOCOL, NB_NETWORK_PROTOCOL, NB_TRANSPORT_PROTOCOL,
};
use oryx_common::protocols::Protocol;
use ratatui::{
layout::{Alignment, Constraint, Direction, Flex, Layout, Rect},
layout::{Alignment, Constraint, Rect},
style::{Style, Stylize},
text::{Line, Span},
widgets::{Block, BorderType, Borders, Padding, Row, Table},
Frame,
};

use crate::app::FocusedBlock;

use super::{
direction::TrafficDirectionFilter, link::LinkFilter, network::NetworkFilter,
transport::TransportFilter,
Expand Down Expand Up @@ -63,44 +59,6 @@ impl Filter {
}
}

pub fn render_on_setup(
&mut self,
frame: &mut Frame,
block: Rect,
focused_block: &FocusedBlock,
) {
let (
transport_filter_block,
network_filter_block,
link_filter_block,
traffic_direction_block,
) = {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(NB_TRANSPORT_PROTOCOL + 4),
Constraint::Length(NB_NETWORK_PROTOCOL + 4),
Constraint::Length(NB_LINK_PROTOCOL + 4),
Constraint::Length(6),
])
.margin(1)
.flex(Flex::SpaceAround)
.split(block);
(chunks[0], chunks[1], chunks[2], chunks[3])
};

self.network
.render(frame, network_filter_block, focused_block);

self.transport
.render(frame, transport_filter_block, focused_block);

self.link.render(frame, link_filter_block, focused_block);

self.traffic_direction
.render(frame, traffic_direction_block, focused_block);
}

pub fn render_on_sniffing(&mut self, frame: &mut Frame, block: Rect) {
let widths = [Constraint::Length(10), Constraint::Fill(1)];
let filters = {
Expand Down
16 changes: 6 additions & 10 deletions oryx-tui/src/filters/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use ratatui::{
Frame,
};

use crate::{app::FocusedBlock, MenuComponent, Scrollable};

use super::{start_menu::StartMenuBlock, update_menu::UpdateFilterMenuBlock};
use crate::{traits::MenuComponent, traits::Scrollable};

#[derive(Debug)]
pub struct LinkFilter {
Expand Down Expand Up @@ -84,7 +82,7 @@ impl LinkFilter {
self.selected_protocols.clear();
}

pub fn render(&mut self, frame: &mut Frame, block: Rect, focused_block: &FocusedBlock) {
pub fn render(&mut self, frame: &mut Frame, block: Rect, is_focused: bool) {
let layout = Layout::default()
.direction(Direction::Horizontal)
.constraints(
Expand Down Expand Up @@ -121,12 +119,10 @@ impl LinkFilter {
.title_style(Style::default().bold().fg(Color::Green))
.title_alignment(Alignment::Center)
.borders(Borders::LEFT)
.border_type(match *focused_block {
FocusedBlock::StartMenuBlock(StartMenuBlock::LinkFilter)
| FocusedBlock::UpdateFilterMenuBlock(UpdateFilterMenuBlock::LinkFilter) => {
BorderType::Thick
}
_ => BorderType::default(),
.border_type(if is_focused {
BorderType::Thick
} else {
BorderType::default()
})
.border_style(Style::default().fg(Color::Green)),
area,
Expand Down
Loading

0 comments on commit 87d6c39

Please sign in to comment.