Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Oct 14, 2024
1 parent fb11c17 commit 0cd0ede
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 37 deletions.
8 changes: 4 additions & 4 deletions oryx-tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
};

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

pub type AppResult<T> = std::result::Result<T, Box<dyn error::Error>>;
Expand Down Expand Up @@ -58,7 +58,7 @@ impl App {

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

let firewall_chans = IoChans::new();
let firewall_channels = IoChannels::new();
thread::spawn({
let packets = packets.clone();
move || loop {
Expand All @@ -76,11 +76,11 @@ impl App {
Self {
running: true,
help: Help::new(),
filter: Filter::new(firewall_chans.clone()),
filter: Filter::new(firewall_channels.clone()),
start_sniffing: false,
packets: packets.clone(),
notifications: Vec::new(),
section: Section::new(packets.clone(), firewall_chans.clone()),
section: Section::new(packets.clone(), firewall_channels.clone()),
data_channel_sender: sender,
is_editing: false,
active_popup: None,
Expand Down
65 changes: 41 additions & 24 deletions oryx-tui/src/ebpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use aya::{
programs::{tc, SchedClassifier, TcAttachType},
Ebpf, EbpfLoader,
};
use log::error;
use oryx_common::{protocols::Protocol, RawPacket, MAX_RULES_PORT};

use crate::{
Expand Down Expand Up @@ -150,7 +151,6 @@ fn update_ipv6_blocklist(
.insert(addr.to_bits(), blocked_ports, 0)
.unwrap();
} else {
//TODO:
unreachable!(); // list is full
}
} else {
Expand Down Expand Up @@ -199,6 +199,11 @@ fn update_ipv6_blocklist(
}
}

enum EbpfTrafficDirection {
Ingress = -1,
Egress = 1,
}

pub fn load_ingress(
iface: String,
notification_sender: kanal::Sender<Event>,
Expand All @@ -219,16 +224,19 @@ pub fn load_ingress(

unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &rlim) };

let traffic_direction = EbpfTrafficDirection::Ingress as i32;

#[cfg(debug_assertions)]
let mut bpf = match EbpfLoader::new()
.set_global("TRAFFIC_DIRECTION", &-1i32, true)
.set_global("TRAFFIC_DIRECTION", &traffic_direction, true)
.load(include_bytes_aligned!(
"../../target/bpfel-unknown-none/debug/oryx"
)) {
Ok(v) => v,
Err(e) => {
error!("Failed to load the ingress eBPF bytecode. {}", e);
Notification::send(
format!("Failed to load the ingress eBPF bytecode\n {}", e),
"Failed to load the ingress eBPF bytecode",
NotificationLevel::Error,
notification_sender,
)
Expand All @@ -239,14 +247,15 @@ pub fn load_ingress(

#[cfg(not(debug_assertions))]
let mut bpf = match EbpfLoader::new()
.set_global("TRAFFIC_DIRECTION", &-1i32, true)
.set_global("TRAFFIC_DIRECTION", &traffic_direction, true)
.load(include_bytes_aligned!(
"../../target/bpfel-unknown-none/release/oryx"
"../../target/bpfel-unknown-none/debug/oryx"
)) {
Ok(v) => v,
Err(e) => {
error!("Failed to load the ingress eBPF bytecode. {}", e);
Notification::send(
format!("Failed to load the ingress eBPF bytecode\n {}", e),
"Failed to load the ingress eBPF bytecode",
NotificationLevel::Error,
notification_sender,
)
Expand All @@ -261,11 +270,12 @@ pub fn load_ingress(
bpf.program_mut("oryx").unwrap().try_into().unwrap();

if let Err(e) = program.load() {
error!(
"Failed to load the ingress eBPF program to the kernel. {}",
e
);
Notification::send(
format!(
"Failed to load the ingress eBPF program to the kernel\n{}",
e
),
"Failed to load the ingress eBPF program to the kernel",
NotificationLevel::Error,
notification_sender,
)
Expand All @@ -274,11 +284,12 @@ pub fn load_ingress(
};

if let Err(e) = program.attach(&iface, TcAttachType::Ingress) {
error!(
"Failed to attach the ingress eBPF program to the interface. {}",
e
);
Notification::send(
format!(
"Failed to attach the ingress eBPF program to the interface\n{}",
e
),
"Failed to attach the ingress eBPF program to the interface",
NotificationLevel::Error,
notification_sender,
)
Expand Down Expand Up @@ -423,16 +434,19 @@ pub fn load_egress(

unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &rlim) };

let traffic_direction = EbpfTrafficDirection::Egress as i32;

#[cfg(debug_assertions)]
let mut bpf = match EbpfLoader::new()
.set_global("TRAFFIC_DIRECTION", &1i32, true)
.set_global("TRAFFIC_DIRECTION", &traffic_direction, true)
.load(include_bytes_aligned!(
"../../target/bpfel-unknown-none/debug/oryx"
)) {
Ok(v) => v,
Err(e) => {
error!("Fail to load the egress eBPF bytecode. {}", e);
Notification::send(
format!("Fail to load the egress eBPF bytecode\n {}", e),
"Fail to load the egress eBPF bytecode",
NotificationLevel::Error,
notification_sender,
)
Expand All @@ -443,14 +457,15 @@ pub fn load_egress(

#[cfg(not(debug_assertions))]
let mut bpf = match EbpfLoader::new()
.set_global("TRAFFIC_DIRECTION", &1i32, true)
.set_global("TRAFFIC_DIRECTION", &traffic_direction, true)
.load(include_bytes_aligned!(
"../../target/bpfel-unknown-none/release/oryx"
"../../target/bpfel-unknown-none/debug/oryx"
)) {
Ok(v) => v,
Err(e) => {
error!("Fail to load the egress eBPF bytecode. {}", e);
Notification::send(
format!("Failed to load the egress eBPF bytecode\n {}", e),
"Fail to load the egress eBPF bytecode",
NotificationLevel::Error,
notification_sender,
)
Expand All @@ -464,8 +479,9 @@ pub fn load_egress(
bpf.program_mut("oryx").unwrap().try_into().unwrap();

if let Err(e) = program.load() {
error!("Fail to load the egress eBPF program to the kernel. {}", e);
Notification::send(
format!("Fail to load the egress eBPF program to the kernel\n{}", e),
"Fail to load the egress eBPF program to the kernel",
NotificationLevel::Error,
notification_sender,
)
Expand All @@ -474,11 +490,12 @@ pub fn load_egress(
};

if let Err(e) = program.attach(&iface, TcAttachType::Egress) {
error!(
"Failed to attach the egress eBPF program to the interface.{}",
e
);
Notification::send(
format!(
"Failed to attach the egress eBPF program to the interface\n{}",
e
),
"Failed to attach the egress eBPF program to the interface",
NotificationLevel::Error,
notification_sender,
)
Expand Down
14 changes: 7 additions & 7 deletions oryx-tui/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct Channels<T> {
}

#[derive(Debug, Clone)]
pub struct IoChans<T> {
pub struct IoChannels<T> {
pub ingress: Channels<T>,
pub egress: Channels<T>,
}
Expand All @@ -59,7 +59,7 @@ impl<T> Channels<T> {
}
}

impl<T> IoChans<T> {
impl<T> IoChannels<T> {
pub fn new() -> Self {
Self {
ingress: Channels::new(),
Expand All @@ -74,7 +74,7 @@ impl<T> Default for Channels<T> {
}
}

impl<T> Default for IoChans<T> {
impl<T> Default for IoChannels<T> {
fn default() -> Self {
Self::new()
}
Expand All @@ -97,20 +97,20 @@ pub struct Filter {
pub transport: TransportFilter,
pub link: LinkFilter,
pub traffic_direction: TrafficDirectionFilter,
pub filter_chans: IoChans<FilterChannelSignal>,
pub firewall_chans: IoChans<FirewallSignal>,
pub filter_chans: IoChannels<FilterChannelSignal>,
pub firewall_chans: IoChannels<FirewallSignal>,
pub focused_block: FocusedBlock,
}

impl Filter {
pub fn new(firewall_chans: IoChans<FirewallSignal>) -> Self {
pub fn new(firewall_chans: IoChannels<FirewallSignal>) -> Self {
Self {
interface: Interface::new(),
network: NetworkFilter::new(),
transport: TransportFilter::new(),
link: LinkFilter::new(),
traffic_direction: TrafficDirectionFilter::new(),
filter_chans: IoChans::new(),
filter_chans: IoChannels::new(),
firewall_chans,
focused_block: FocusedBlock::Interface,
}
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 @@ -22,7 +22,7 @@ use stats::Stats;
use crate::{
app::{ActivePopup, AppResult},
event::Event,
filter::IoChans,
filter::IoChannels,
packet::AppPacket,
};

Expand All @@ -46,7 +46,7 @@ pub struct Section {
impl Section {
pub fn new(
packets: Arc<Mutex<Vec<AppPacket>>>,
firewall_chans: IoChans<FirewallSignal>,
firewall_chans: IoChannels<FirewallSignal>,
) -> Self {
Self {
focused_section: FocusedSection::Inspection,
Expand Down

0 comments on commit 0cd0ede

Please sign in to comment.