Skip to content

Commit

Permalink
refactor: add direction and pid to AppPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Nov 5, 2024
1 parent 15390c3 commit 570b1d1
Show file tree
Hide file tree
Showing 19 changed files with 150 additions and 100 deletions.
34 changes: 23 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions oryx-tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ edition.workspace = true

[dependencies]
crossterm = { version = "0.28", default-features = false }
ratatui = "0.28"
tui-big-text = "0.6"
tui-input = "0.10"
ratatui = "0.29"
tui-big-text = "0.7"
tui-input = "0.11"
libc = "0.2"
aya = "0.13"
oryx-common = { path = "../oryx-common" }
Expand Down
19 changes: 14 additions & 5 deletions oryx-tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ use std::{
time::Duration,
};

use crate::{filter::Filter, help::Help};
use crate::{
filter::Filter,
help::Help,
packet::{direction::TrafficDirection, NetworkPacket},
};
use crate::{filter::IoChannels, notification::Notification};
use crate::{packet::AppPacket, section::Section};

Expand Down Expand Up @@ -41,7 +45,7 @@ pub struct App {
pub packets: Arc<Mutex<Vec<AppPacket>>>,
pub notifications: Vec<Notification>,
pub section: Section,
pub data_channel_sender: kanal::Sender<[u8; RawPacket::LEN]>,
pub data_channel_sender: kanal::Sender<([u8; RawPacket::LEN], TrafficDirection)>,
pub is_editing: bool,
pub active_popup: Option<ActivePopup>,
}
Expand All @@ -54,16 +58,21 @@ impl Default for App {

impl App {
pub fn new() -> Self {
let packets = Arc::new(Mutex::new(Vec::with_capacity(AppPacket::LEN * 1024 * 1024)));
let packets = Arc::new(Mutex::new(Vec::with_capacity(RawPacket::LEN * 1024 * 1024)));

let (sender, receiver) = kanal::unbounded();

let firewall_channels = IoChannels::new();
thread::spawn({
let packets = packets.clone();
move || loop {
if let Ok(raw_packet) = receiver.recv() {
let app_packet = AppPacket::from(raw_packet);
if let Ok((raw_packet, direction)) = receiver.recv() {
let network_packet = NetworkPacket::from(raw_packet);
let app_packet = AppPacket {
packet: network_packet,
pid: None,
direction,
};
let mut packets = packets.lock().unwrap();
if packets.len() == packets.capacity() {
packets.reserve(1024 * 1024);
Expand Down
5 changes: 3 additions & 2 deletions oryx-tui/src/ebpf/egress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
event::Event,
filter::FilterChannelSignal,
notification::{Notification, NotificationLevel},
packet::direction::TrafficDirection,
section::firewall::FirewallSignal,
};
use mio::{unix::SourceFd, Events, Interest, Poll, Token};
Expand All @@ -31,7 +32,7 @@ use super::{
pub fn load_egress(
iface: String,
notification_sender: kanal::Sender<Event>,
data_sender: kanal::Sender<[u8; RawPacket::LEN]>,
data_sender: kanal::Sender<([u8; RawPacket::LEN], TrafficDirection)>,
filter_channel_receiver: kanal::Receiver<FilterChannelSignal>,
firewall_egress_receiver: kanal::Receiver<FirewallSignal>,
terminate: Arc<AtomicBool>,
Expand Down Expand Up @@ -221,7 +222,7 @@ pub fn load_egress(
break;
}
let packet: [u8; RawPacket::LEN] = item.to_owned().try_into().unwrap();
data_sender.send(packet).ok();
data_sender.send((packet, TrafficDirection::Egress)).ok();
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions oryx-tui/src/ebpf/ingress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
event::Event,
filter::FilterChannelSignal,
notification::{Notification, NotificationLevel},
packet::direction::TrafficDirection,
section::firewall::FirewallSignal,
};
use mio::{unix::SourceFd, Events, Interest, Poll, Token};
Expand All @@ -31,7 +32,7 @@ use super::{
pub fn load_ingress(
iface: String,
notification_sender: kanal::Sender<Event>,
data_sender: kanal::Sender<[u8; RawPacket::LEN]>,
data_sender: kanal::Sender<([u8; RawPacket::LEN], TrafficDirection)>,
filter_channel_receiver: kanal::Receiver<FilterChannelSignal>,
firewall_ingress_receiver: kanal::Receiver<FirewallSignal>,
terminate: Arc<AtomicBool>,
Expand Down Expand Up @@ -225,7 +226,7 @@ pub fn load_ingress(
break;
}
let packet: [u8; RawPacket::LEN] = item.to_owned().try_into().unwrap();
data_sender.send(packet).ok();
data_sender.send((packet, TrafficDirection::Ingress)).ok();
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions oryx-tui/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
app::AppResult,
packet::{
network::{IpPacket, IpProto},
AppPacket,
AppPacket, NetworkPacket,
},
};

Expand Down Expand Up @@ -39,8 +39,8 @@ pub fn export(packets: &[AppPacket]) -> AppResult<()> {
headers.0, headers.1, headers.2, headers.3, headers.4
)?;
for packet in packets {
match packet {
AppPacket::Arp(p) => {
match packet.packet {
NetworkPacket::Arp(p) => {
writeln!(
file,
"{:39} {:^11} {:39} {:^11} ARP",
Expand All @@ -50,7 +50,7 @@ pub fn export(packets: &[AppPacket]) -> AppResult<()> {
"-"
)?;
}
AppPacket::Ip(packet) => match packet {
NetworkPacket::Ip(packet) => match packet {
IpPacket::V4(ipv4_packet) => match ipv4_packet.proto {
IpProto::Tcp(p) => {
writeln!(
Expand Down
5 changes: 3 additions & 2 deletions oryx-tui/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod network;
mod transport;

use crossterm::event::{KeyCode, KeyEvent};
use direction::{TrafficDirection, TrafficDirectionFilter};
use direction::TrafficDirectionFilter;
use link::LinkFilter;
use network::NetworkFilter;
use oryx_common::{
Expand All @@ -30,6 +30,7 @@ use crate::{
ebpf::{egress::load_egress, ingress::load_ingress},
event::Event,
interface::Interface,
packet::direction::TrafficDirection,
section::firewall::FirewallSignal,
};

Expand Down Expand Up @@ -145,7 +146,7 @@ impl Filter {
pub fn start(
&mut self,
notification_sender: kanal::Sender<Event>,
data_sender: kanal::Sender<[u8; RawPacket::LEN]>,
data_sender: kanal::Sender<([u8; RawPacket::LEN], TrafficDirection)>,
) -> AppResult<()> {
let iface = self.interface.selected_interface.name.clone();

Expand Down
29 changes: 6 additions & 23 deletions oryx-tui/src/filter/direction.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{
fmt::Display,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};

use ratatui::{
Expand All @@ -12,7 +9,8 @@ use ratatui::{
widgets::{Block, BorderType, Borders, Row, Table, TableState},
Frame,
};
use serde::{Deserialize, Serialize};

use crate::packet::direction::TrafficDirection;

#[derive(Debug)]
pub struct TrafficDirectionFilter {
Expand All @@ -23,21 +21,6 @@ pub struct TrafficDirectionFilter {
pub terminate_egress: Arc<AtomicBool>,
}

#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
pub enum TrafficDirection {
Ingress,
Egress,
}

impl Display for TrafficDirection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TrafficDirection::Ingress => write!(f, "Ingress"),
TrafficDirection::Egress => write!(f, "Egress"),
}
}
}

impl Default for TrafficDirectionFilter {
fn default() -> Self {
Self::new()
Expand Down Expand Up @@ -121,7 +104,7 @@ impl TrafficDirectionFilter {
];

let table = Table::new(filters, widths)
.highlight_style(Style::new().bg(ratatui::style::Color::DarkGray));
.row_highlight_style(Style::new().bg(ratatui::style::Color::DarkGray));

frame.render_widget(
Block::new()
Expand Down
2 changes: 1 addition & 1 deletion oryx-tui/src/filter/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl LinkFilter {
])];

let table = Table::new(link_filters, widths)
.highlight_style(Style::new().bg(ratatui::style::Color::DarkGray));
.row_highlight_style(Style::new().bg(ratatui::style::Color::DarkGray));

frame.render_widget(
Block::new()
Expand Down
2 changes: 1 addition & 1 deletion oryx-tui/src/filter/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl NetworkFilter {
];

let network_filters_table = Table::new(network_filters, widths)
.highlight_style(Style::new().bg(ratatui::style::Color::DarkGray));
.row_highlight_style(Style::new().bg(ratatui::style::Color::DarkGray));

frame.render_widget(
Block::new()
Expand Down
2 changes: 1 addition & 1 deletion oryx-tui/src/filter/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl TransportFilter {
];

let table = Table::new(transport_filters, widths)
.highlight_style(Style::new().bg(ratatui::style::Color::DarkGray));
.row_highlight_style(Style::new().bg(ratatui::style::Color::DarkGray));

frame.render_widget(
Block::new()
Expand Down
5 changes: 1 addition & 4 deletions oryx-tui/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ pub fn handle_key_events(
match key_event.code {
KeyCode::Enter => {
if app.filter.focused_block == FocusedBlock::Apply {
app.section.stats = Some(Stats::new(
app.packets.clone(),
app.filter.interface.selected_interface.clone(),
));
app.section.stats = Some(Stats::new(app.packets.clone()));
app.filter
.start(event_sender.clone(), app.data_channel_sender.clone())?;

Expand Down
2 changes: 1 addition & 1 deletion oryx-tui/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl Interface {
.style(Style::new().bold())
.bottom_margin(1),
)
.highlight_style(Style::new().bg(ratatui::style::Color::DarkGray))
.row_highlight_style(Style::new().bg(ratatui::style::Color::DarkGray))
.column_spacing(3);

frame.render_widget(
Expand Down
Loading

0 comments on commit 570b1d1

Please sign in to comment.