Skip to content

Commit

Permalink
handle rules with direction lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Oct 8, 2024
1 parent e96112b commit d216eef
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 deletions.
8 changes: 8 additions & 0 deletions oryx-tui/src/filter/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ impl TrafficDirectionFilter {
self.selected_direction.clear();
}

pub fn is_ingress_loaded(&self) -> bool {
self.applied_direction.contains(&TrafficDirection::Ingress)
}

pub fn is_egress_loaded(&self) -> bool {
self.applied_direction.contains(&TrafficDirection::Egress)
}

pub fn render(&mut self, frame: &mut Frame, block: Rect, is_focused: bool) {
let layout = Layout::default()
.direction(Direction::Horizontal)
Expand Down
17 changes: 17 additions & 0 deletions oryx-tui/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ pub fn handle_key_events(
if app.filter.focused_block == FocusedBlock::Apply {
app.filter
.update(sender.clone(), app.data_channel_sender.clone())?;
if !app.filter.traffic_direction.is_ingress_loaded() {
app.section.firewall.disable_ingress_rules();
}

if !app.filter.traffic_direction.is_egress_loaded() {
app.section.firewall.disable_egress_rules();
}

app.active_popup = None;
}
Expand Down Expand Up @@ -172,6 +179,16 @@ pub fn handle_key_events(
}
}

KeyCode::Char(' ') => {
if app.section.focused_section == FocusedSection::Firewall {
app.section.firewall.load_rule(
sender.clone(),
app.filter.traffic_direction.is_ingress_loaded(),
app.filter.traffic_direction.is_egress_loaded(),
)?;
}
}

KeyCode::Char('s') => {
let app_packets = app.packets.lock().unwrap();
if app_packets.is_empty() {
Expand Down
69 changes: 53 additions & 16 deletions oryx-tui/src/section/firewall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,59 @@ impl Firewall {
self.rules.retain(|r| r.name != rule.name);
}

pub fn remove_ingress_rules(&mut self) {}
pub fn remove_egress_rules(&mut self) {}
pub fn disable_ingress_rules(&mut self) {
self.rules.iter_mut().for_each(|rule| {
if rule.enabled && rule.direction == TrafficDirection::Ingress {
rule.enabled = false;
}
});
}
pub fn disable_egress_rules(&mut self) {
self.rules.iter_mut().for_each(|rule| {
if rule.enabled && rule.direction == TrafficDirection::Egress {
rule.enabled = false;
}
});
}

pub fn load_rule(
&mut self,
sender: kanal::Sender<crate::event::Event>,
is_ingress_loaded: bool,
is_egress_loaded: bool,
) -> AppResult<()> {
if let Some(index) = self.state.selected() {
let rule = &mut self.rules[index];

match rule.direction {
TrafficDirection::Ingress => {
if is_ingress_loaded {
rule.enabled = !rule.enabled;
self.ingress_sender.send(rule.clone())?;
} else {
Notification::send(
"Ingress is not loaded.",
crate::notification::NotificationLevel::Warning,
sender.clone(),
)?;
}
}
TrafficDirection::Egress => {
if is_egress_loaded {
rule.enabled = !rule.enabled;
self.egress_sender.send(rule.clone())?;
} else {
Notification::send(
"Egress is not loaded.",
crate::notification::NotificationLevel::Warning,
sender.clone(),
)?;
}
}
}
}
Ok(())
}

pub fn handle_keys(
&mut self,
Expand Down Expand Up @@ -430,20 +481,6 @@ impl Firewall {
self.add_rule();
}

KeyCode::Char(' ') => {
if let Some(index) = self.state.selected() {
let rule = &mut self.rules[index];
rule.enabled = !rule.enabled;

match rule.direction {
TrafficDirection::Ingress => {
self.ingress_sender.send(rule.clone())?;
}
TrafficDirection::Egress => self.egress_sender.send(rule.clone())?,
}
}
}

KeyCode::Char('e') => {
if let Some(index) = self.state.selected() {
let rule = self.rules[index].clone();
Expand Down

0 comments on commit d216eef

Please sign in to comment.