Skip to content

Commit e96112b

Browse files
committed
add direction
1 parent 567cfcb commit e96112b

File tree

1 file changed

+79
-22
lines changed

1 file changed

+79
-22
lines changed

oryx-tui/src/section/firewall.rs

Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::{net::IpAddr, num::ParseIntError, str::FromStr};
1111
use tui_input::{backend::crossterm::EventHandler, Input};
1212
use uuid;
1313

14-
use crate::{app::AppResult, notification::Notification};
14+
use crate::{app::AppResult, filter::direction::TrafficDirection, notification::Notification};
1515

1616
#[derive(Debug, Clone)]
1717
pub struct FirewallRule {
@@ -20,6 +20,7 @@ pub struct FirewallRule {
2020
pub enabled: bool,
2121
pub ip: IpAddr,
2222
pub port: BlockedPort,
23+
direction: TrafficDirection,
2324
}
2425

2526
#[derive(Debug, Clone, PartialEq)]
@@ -48,6 +49,7 @@ impl FromStr for BlockedPort {
4849
}
4950
}
5051

52+
// TODO: Add direction
5153
impl Display for FirewallRule {
5254
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5355
write!(f, "{} {}", self.ip, self.port)
@@ -58,14 +60,16 @@ pub enum FocusedInput {
5860
Name,
5961
Ip,
6062
Port,
63+
Direction,
6164
}
6265

6366
#[derive(Debug, Clone)]
6467
struct UserInput {
6568
id: Option<uuid::Uuid>,
66-
pub name: UserInputField,
67-
pub ip: UserInputField,
68-
pub port: UserInputField,
69+
name: UserInputField,
70+
ip: UserInputField,
71+
port: UserInputField,
72+
direction: TrafficDirection,
6973
focus_input: FocusedInput,
7074
}
7175

@@ -82,6 +86,7 @@ impl UserInput {
8286
name: UserInputField::default(),
8387
ip: UserInputField::default(),
8488
port: UserInputField::default(),
89+
direction: TrafficDirection::Ingress,
8590
focus_input: FocusedInput::Name,
8691
}
8792
}
@@ -137,7 +142,7 @@ impl UserInput {
137142
.direction(Direction::Horizontal)
138143
.constraints([
139144
Constraint::Fill(1),
140-
Constraint::Max(80),
145+
Constraint::Percentage(80),
141146
Constraint::Fill(1),
142147
])
143148
.flex(ratatui::layout::Flex::SpaceBetween)
@@ -172,8 +177,22 @@ impl UserInput {
172177
}
173178
})
174179
.fg(Color::Black),
180+
Cell::from(self.direction.to_string())
181+
.bg({
182+
if self.focus_input == FocusedInput::Direction {
183+
Color::Gray
184+
} else {
185+
Color::DarkGray
186+
}
187+
})
188+
.fg(Color::Black),
189+
]),
190+
Row::new(vec![
191+
Cell::new(""),
192+
Cell::new(""),
193+
Cell::new(""),
194+
Cell::new(""),
175195
]),
176-
Row::new(vec![Cell::new(""), Cell::new(""), Cell::new("")]),
177196
Row::new(vec![
178197
Cell::from({
179198
if let Some(error) = &self.name.error {
@@ -199,13 +218,15 @@ impl UserInput {
199218
}
200219
})
201220
.red(),
221+
Cell::new(""),
202222
]),
203223
];
204224

205225
let widths = [
206-
Constraint::Percentage(33),
207-
Constraint::Percentage(33),
208-
Constraint::Percentage(33),
226+
Constraint::Percentage(25),
227+
Constraint::Percentage(25),
228+
Constraint::Percentage(25),
229+
Constraint::Percentage(25),
209230
];
210231

211232
let table = Table::new(rows, widths)
@@ -214,6 +235,7 @@ impl UserInput {
214235
Line::from("Name").centered(),
215236
Line::from("IP").centered(),
216237
Line::from("Port").centered(),
238+
Line::from("Direction").centered(),
217239
])
218240
.style(Style::new().bold())
219241
.bottom_margin(1),
@@ -252,6 +274,7 @@ impl From<FirewallRule> for UserInput {
252274
field: Input::from(rule.port.to_string()),
253275
error: None,
254276
},
277+
direction: rule.direction,
255278
focus_input: FocusedInput::Name,
256279
}
257280
}
@@ -318,6 +341,9 @@ impl Firewall {
318341
self.rules.retain(|r| r.name != rule.name);
319342
}
320343

344+
pub fn remove_ingress_rules(&mut self) {}
345+
pub fn remove_egress_rules(&mut self) {}
346+
321347
pub fn handle_keys(
322348
&mut self,
323349
key_event: KeyEvent,
@@ -350,12 +376,14 @@ impl Firewall {
350376
rule.ip = IpAddr::from_str(user_input.ip.field.value()).unwrap();
351377
rule.port =
352378
BlockedPort::from_str(user_input.port.field.value()).unwrap();
379+
rule.direction = user_input.direction;
353380
} else {
354381
let rule = FirewallRule {
355382
id: uuid::Uuid::new_v4(),
356383
name: user_input.name.field.to_string(),
357384
ip: IpAddr::from_str(user_input.ip.field.value()).unwrap(),
358385
port: BlockedPort::from_str(user_input.port.field.value()).unwrap(),
386+
direction: user_input.direction,
359387
enabled: false,
360388
};
361389
self.rules.push(rule);
@@ -369,7 +397,8 @@ impl Firewall {
369397
match user_input.focus_input {
370398
FocusedInput::Name => user_input.focus_input = FocusedInput::Ip,
371399
FocusedInput::Ip => user_input.focus_input = FocusedInput::Port,
372-
FocusedInput::Port => user_input.focus_input = FocusedInput::Name,
400+
FocusedInput::Port => user_input.focus_input = FocusedInput::Direction,
401+
FocusedInput::Direction => user_input.focus_input = FocusedInput::Name,
373402
}
374403
}
375404
}
@@ -384,6 +413,15 @@ impl Firewall {
384413
FocusedInput::Port => {
385414
user_input.port.field.handle_event(&Event::Key(key_event));
386415
}
416+
FocusedInput::Direction => match key_event.code {
417+
KeyCode::Char('j') | KeyCode::Down => {
418+
user_input.direction = TrafficDirection::Ingress;
419+
}
420+
KeyCode::Char('k') | KeyCode::Up => {
421+
user_input.direction = TrafficDirection::Egress;
422+
}
423+
_ => {}
424+
},
387425
},
388426
}
389427
} else {
@@ -394,9 +432,15 @@ impl Firewall {
394432

395433
KeyCode::Char(' ') => {
396434
if let Some(index) = self.state.selected() {
397-
self.rules[index].enabled = !self.rules[index].enabled;
398-
self.ingress_sender.send(self.rules[index].clone())?;
399-
self.egress_sender.send(self.rules[index].clone())?
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+
}
400444
}
401445
}
402446

@@ -417,9 +461,16 @@ impl Firewall {
417461

418462
KeyCode::Char('d') => {
419463
if let Some(index) = self.state.selected() {
420-
self.rules[index].enabled = false;
421-
self.ingress_sender.send(self.rules[index].clone())?;
422-
self.egress_sender.send(self.rules[index].clone())?;
464+
let rule = &mut self.rules[index];
465+
466+
rule.enabled = false;
467+
match rule.direction {
468+
TrafficDirection::Ingress => {
469+
self.ingress_sender.send(rule.clone())?;
470+
}
471+
TrafficDirection::Egress => self.egress_sender.send(rule.clone())?,
472+
}
473+
423474
self.rules.remove(index);
424475
}
425476
}
@@ -483,16 +534,22 @@ impl Firewall {
483534
Constraint::Max(20),
484535
Constraint::Length(10),
485536
Constraint::Length(14),
537+
Constraint::Length(14),
486538
];
487539

488540
let rows = self.rules.iter().map(|rule| {
489541
Row::new(vec![
490542
Line::from(rule.name.clone()).centered().bold(),
491-
Line::from(rule.ip.to_string()).centered().centered().bold(),
492-
Line::from(rule.port.to_string())
493-
.centered()
494-
.centered()
495-
.bold(),
543+
Line::from(rule.ip.to_string()).centered().bold(),
544+
Line::from(rule.port.to_string()).centered().bold(),
545+
Line::from({
546+
match rule.direction {
547+
TrafficDirection::Ingress => String::from("Ingress 󰁅 "),
548+
TrafficDirection::Egress => String::from("Egress  "),
549+
}
550+
})
551+
.centered()
552+
.bold(),
496553
Line::from({
497554
if rule.enabled {
498555
"Enabled".to_string()
@@ -501,7 +558,6 @@ impl Firewall {
501558
}
502559
})
503560
.centered()
504-
.centered()
505561
.bold(),
506562
])
507563
});
@@ -519,6 +575,7 @@ impl Firewall {
519575
Line::from("Name").centered().blue(),
520576
Line::from("IP").centered().blue(),
521577
Line::from("Port").centered().blue(),
578+
Line::from("Direction").centered().blue(),
522579
Line::from("Status").centered().blue(),
523580
])
524581
.style(Style::new().bold())

0 commit comments

Comments
 (0)