Skip to content

Commit d216eef

Browse files
committed
handle rules with direction lifetimes
1 parent e96112b commit d216eef

File tree

3 files changed

+78
-16
lines changed

3 files changed

+78
-16
lines changed

oryx-tui/src/filter/direction.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ impl TrafficDirectionFilter {
8282
self.selected_direction.clear();
8383
}
8484

85+
pub fn is_ingress_loaded(&self) -> bool {
86+
self.applied_direction.contains(&TrafficDirection::Ingress)
87+
}
88+
89+
pub fn is_egress_loaded(&self) -> bool {
90+
self.applied_direction.contains(&TrafficDirection::Egress)
91+
}
92+
8593
pub fn render(&mut self, frame: &mut Frame, block: Rect, is_focused: bool) {
8694
let layout = Layout::default()
8795
.direction(Direction::Horizontal)

oryx-tui/src/handler.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ pub fn handle_key_events(
7575
if app.filter.focused_block == FocusedBlock::Apply {
7676
app.filter
7777
.update(sender.clone(), app.data_channel_sender.clone())?;
78+
if !app.filter.traffic_direction.is_ingress_loaded() {
79+
app.section.firewall.disable_ingress_rules();
80+
}
81+
82+
if !app.filter.traffic_direction.is_egress_loaded() {
83+
app.section.firewall.disable_egress_rules();
84+
}
7885

7986
app.active_popup = None;
8087
}
@@ -172,6 +179,16 @@ pub fn handle_key_events(
172179
}
173180
}
174181

182+
KeyCode::Char(' ') => {
183+
if app.section.focused_section == FocusedSection::Firewall {
184+
app.section.firewall.load_rule(
185+
sender.clone(),
186+
app.filter.traffic_direction.is_ingress_loaded(),
187+
app.filter.traffic_direction.is_egress_loaded(),
188+
)?;
189+
}
190+
}
191+
175192
KeyCode::Char('s') => {
176193
let app_packets = app.packets.lock().unwrap();
177194
if app_packets.is_empty() {

oryx-tui/src/section/firewall.rs

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,59 @@ impl Firewall {
341341
self.rules.retain(|r| r.name != rule.name);
342342
}
343343

344-
pub fn remove_ingress_rules(&mut self) {}
345-
pub fn remove_egress_rules(&mut self) {}
344+
pub fn disable_ingress_rules(&mut self) {
345+
self.rules.iter_mut().for_each(|rule| {
346+
if rule.enabled && rule.direction == TrafficDirection::Ingress {
347+
rule.enabled = false;
348+
}
349+
});
350+
}
351+
pub fn disable_egress_rules(&mut self) {
352+
self.rules.iter_mut().for_each(|rule| {
353+
if rule.enabled && rule.direction == TrafficDirection::Egress {
354+
rule.enabled = false;
355+
}
356+
});
357+
}
358+
359+
pub fn load_rule(
360+
&mut self,
361+
sender: kanal::Sender<crate::event::Event>,
362+
is_ingress_loaded: bool,
363+
is_egress_loaded: bool,
364+
) -> AppResult<()> {
365+
if let Some(index) = self.state.selected() {
366+
let rule = &mut self.rules[index];
367+
368+
match rule.direction {
369+
TrafficDirection::Ingress => {
370+
if is_ingress_loaded {
371+
rule.enabled = !rule.enabled;
372+
self.ingress_sender.send(rule.clone())?;
373+
} else {
374+
Notification::send(
375+
"Ingress is not loaded.",
376+
crate::notification::NotificationLevel::Warning,
377+
sender.clone(),
378+
)?;
379+
}
380+
}
381+
TrafficDirection::Egress => {
382+
if is_egress_loaded {
383+
rule.enabled = !rule.enabled;
384+
self.egress_sender.send(rule.clone())?;
385+
} else {
386+
Notification::send(
387+
"Egress is not loaded.",
388+
crate::notification::NotificationLevel::Warning,
389+
sender.clone(),
390+
)?;
391+
}
392+
}
393+
}
394+
}
395+
Ok(())
396+
}
346397

347398
pub fn handle_keys(
348399
&mut self,
@@ -430,20 +481,6 @@ impl Firewall {
430481
self.add_rule();
431482
}
432483

433-
KeyCode::Char(' ') => {
434-
if let Some(index) = self.state.selected() {
435-
let rule = &mut self.rules[index];
436-
rule.enabled = !rule.enabled;
437-
438-
match rule.direction {
439-
TrafficDirection::Ingress => {
440-
self.ingress_sender.send(rule.clone())?;
441-
}
442-
TrafficDirection::Egress => self.egress_sender.send(rule.clone())?,
443-
}
444-
}
445-
}
446-
447484
KeyCode::Char('e') => {
448485
if let Some(index) = self.state.selected() {
449486
let rule = self.rules[index].clone();

0 commit comments

Comments
 (0)