Skip to content

Commit 3faee02

Browse files
committed
do not edit enabled rule
1 parent a30f6fc commit 3faee02

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

oryx-tui/src/handler.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ pub fn handle_key_events(
6262
app.filter.handle_key_events(key_event, true);
6363
}
6464
ActivePopup::NewFirewallRule => {
65-
app.section.firewall.handle_keys(key_event)?;
65+
app.section
66+
.firewall
67+
.handle_keys(key_event, sender.clone())?;
6668
app.is_editing = false;
6769
}
6870
_ => {}
@@ -78,7 +80,12 @@ pub fn handle_key_events(
7880
}
7981
}
8082
ActivePopup::NewFirewallRule => {
81-
if app.section.firewall.handle_keys(key_event).is_ok() {
83+
if app
84+
.section
85+
.firewall
86+
.handle_keys(key_event, sender.clone())
87+
.is_ok()
88+
{
8289
app.active_popup = None;
8390
app.is_editing = false;
8491
}
@@ -91,7 +98,9 @@ pub fn handle_key_events(
9198
app.filter.handle_key_events(key_event, true);
9299
}
93100
ActivePopup::NewFirewallRule => {
94-
app.section.firewall.handle_keys(key_event)?;
101+
app.section
102+
.firewall
103+
.handle_keys(key_event, sender.clone())?;
95104
}
96105
_ => {}
97106
},
@@ -106,7 +115,7 @@ pub fn handle_key_events(
106115
_ => {}
107116
}
108117

109-
app.section.handle_keys(key_event)?;
118+
app.section.handle_keys(key_event, sender.clone())?;
110119
return Ok(());
111120
}
112121

@@ -145,14 +154,14 @@ pub fn handle_key_events(
145154
KeyCode::Char('/') => {
146155
if app.section.focused_section == FocusedSection::Inspection {
147156
app.is_editing = true;
148-
app.section.handle_keys(key_event)?;
157+
app.section.handle_keys(key_event, sender.clone())?;
149158
}
150159
}
151160

152161
KeyCode::Char('n') | KeyCode::Char('e') => {
153162
if app.section.focused_section == FocusedSection::Firewall {
154163
app.is_editing = true;
155-
app.section.handle_keys(key_event)?;
164+
app.section.handle_keys(key_event, sender)?;
156165
app.active_popup = Some(ActivePopup::NewFirewallRule);
157166
}
158167
}
@@ -187,7 +196,7 @@ pub fn handle_key_events(
187196
}
188197
}
189198
_ => {
190-
app.section.handle_keys(key_event)?;
199+
app.section.handle_keys(key_event, sender.clone())?;
191200
}
192201
}
193202

oryx-tui/src/notification.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Notification {
6969
let notif = Notification {
7070
message: message.to_string(),
7171
level,
72-
ttl: 500,
72+
ttl: 75,
7373
};
7474

7575
sender.send(Event::Notification(notif))?;

oryx-tui/src/section.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use ratatui::{
1919
};
2020
use stats::Stats;
2121

22-
use crate::{app::AppResult, packet::AppPacket};
22+
use crate::{app::AppResult, event::Event, packet::AppPacket};
2323

2424
#[derive(Debug, PartialEq)]
2525
pub enum FocusedSection {
@@ -118,7 +118,11 @@ impl Section {
118118
}
119119
}
120120

121-
pub fn handle_keys(&mut self, key_event: KeyEvent) -> AppResult<()> {
121+
pub fn handle_keys(
122+
&mut self,
123+
key_event: KeyEvent,
124+
notification_sender: kanal::Sender<Event>,
125+
) -> AppResult<()> {
122126
match key_event.code {
123127
KeyCode::Tab => match self.focused_section {
124128
FocusedSection::Inspection => self.focused_section = FocusedSection::Stats,
@@ -136,7 +140,9 @@ impl Section {
136140

137141
_ => match self.focused_section {
138142
FocusedSection::Inspection => self.inspection.handle_keys(key_event),
139-
FocusedSection::Firewall => self.firewall.handle_keys(key_event)?,
143+
FocusedSection::Firewall => self
144+
.firewall
145+
.handle_keys(key_event, notification_sender.clone())?,
140146
_ => {}
141147
},
142148
}

oryx-tui/src/section/firewall.rs

Lines changed: 17 additions & 13 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;
14+
use crate::{app::AppResult, notification::Notification};
1515

1616
#[derive(Debug, Clone)]
1717
pub struct FirewallRule {
@@ -41,9 +41,9 @@ impl FromStr for BlockedPort {
4141
type Err = ParseIntError;
4242
fn from_str(s: &str) -> Result<Self, Self::Err> {
4343
if s == "*" {
44-
return Ok(BlockedPort::All);
44+
Ok(BlockedPort::All)
4545
} else {
46-
return Ok(BlockedPort::Single(u16::from_str(s)?));
46+
Ok(BlockedPort::Single(u16::from_str(s)?))
4747
}
4848
}
4949
}
@@ -283,7 +283,11 @@ impl Firewall {
283283
self.rules.retain(|r| r.name != rule.name);
284284
}
285285

286-
pub fn handle_keys(&mut self, key_event: KeyEvent) -> AppResult<()> {
286+
pub fn handle_keys(
287+
&mut self,
288+
key_event: KeyEvent,
289+
sender: kanal::Sender<crate::event::Event>,
290+
) -> AppResult<()> {
287291
if let Some(user_input) = &mut self.user_input {
288292
match key_event.code {
289293
KeyCode::Esc => {
@@ -296,14 +300,6 @@ impl Firewall {
296300

297301
if let Some(id) = user_input.id {
298302
let rule = self.rules.iter_mut().find(|rule| rule.id == id).unwrap();
299-
300-
if rule.enabled {
301-
// set disable notification on previous rule definition
302-
rule.enabled = false;
303-
self.ingress_sender.send(rule.clone())?;
304-
}
305-
306-
// update rule with user input
307303
rule.name = user_input.name.field.to_string();
308304
rule.ip = IpAddr::from_str(user_input.ip.field.value()).unwrap();
309305
rule.port =
@@ -360,7 +356,15 @@ impl Firewall {
360356
KeyCode::Char('e') => {
361357
if let Some(index) = self.state.selected() {
362358
let rule = self.rules[index].clone();
363-
self.user_input = Some(rule.into());
359+
if rule.enabled {
360+
Notification::send(
361+
"Can not edit enabled rule",
362+
crate::notification::NotificationLevel::Warning,
363+
sender.clone(),
364+
)?;
365+
} else {
366+
self.user_input = Some(rule.into());
367+
}
364368
}
365369
}
366370

0 commit comments

Comments
 (0)