Skip to content

Commit

Permalink
do not edit enabled rule
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Oct 7, 2024
1 parent a30f6fc commit 3faee02
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
23 changes: 16 additions & 7 deletions oryx-tui/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ pub fn handle_key_events(
app.filter.handle_key_events(key_event, true);
}
ActivePopup::NewFirewallRule => {
app.section.firewall.handle_keys(key_event)?;
app.section
.firewall
.handle_keys(key_event, sender.clone())?;
app.is_editing = false;
}
_ => {}
Expand All @@ -78,7 +80,12 @@ pub fn handle_key_events(
}
}
ActivePopup::NewFirewallRule => {
if app.section.firewall.handle_keys(key_event).is_ok() {
if app
.section
.firewall
.handle_keys(key_event, sender.clone())
.is_ok()
{
app.active_popup = None;
app.is_editing = false;
}
Expand All @@ -91,7 +98,9 @@ pub fn handle_key_events(
app.filter.handle_key_events(key_event, true);
}
ActivePopup::NewFirewallRule => {
app.section.firewall.handle_keys(key_event)?;
app.section
.firewall
.handle_keys(key_event, sender.clone())?;
}
_ => {}
},
Expand All @@ -106,7 +115,7 @@ pub fn handle_key_events(
_ => {}
}

app.section.handle_keys(key_event)?;
app.section.handle_keys(key_event, sender.clone())?;
return Ok(());
}

Expand Down Expand Up @@ -145,14 +154,14 @@ pub fn handle_key_events(
KeyCode::Char('/') => {
if app.section.focused_section == FocusedSection::Inspection {
app.is_editing = true;
app.section.handle_keys(key_event)?;
app.section.handle_keys(key_event, sender.clone())?;
}
}

KeyCode::Char('n') | KeyCode::Char('e') => {
if app.section.focused_section == FocusedSection::Firewall {
app.is_editing = true;
app.section.handle_keys(key_event)?;
app.section.handle_keys(key_event, sender)?;
app.active_popup = Some(ActivePopup::NewFirewallRule);
}
}
Expand Down Expand Up @@ -187,7 +196,7 @@ pub fn handle_key_events(
}
}
_ => {
app.section.handle_keys(key_event)?;
app.section.handle_keys(key_event, sender.clone())?;
}
}

Expand Down
2 changes: 1 addition & 1 deletion oryx-tui/src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Notification {
let notif = Notification {
message: message.to_string(),
level,
ttl: 500,
ttl: 75,
};

sender.send(Event::Notification(notif))?;
Expand Down
12 changes: 9 additions & 3 deletions oryx-tui/src/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use ratatui::{
};
use stats::Stats;

use crate::{app::AppResult, packet::AppPacket};
use crate::{app::AppResult, event::Event, packet::AppPacket};

#[derive(Debug, PartialEq)]
pub enum FocusedSection {
Expand Down Expand Up @@ -118,7 +118,11 @@ impl Section {
}
}

pub fn handle_keys(&mut self, key_event: KeyEvent) -> AppResult<()> {
pub fn handle_keys(
&mut self,
key_event: KeyEvent,
notification_sender: kanal::Sender<Event>,
) -> AppResult<()> {
match key_event.code {
KeyCode::Tab => match self.focused_section {
FocusedSection::Inspection => self.focused_section = FocusedSection::Stats,
Expand All @@ -136,7 +140,9 @@ impl Section {

_ => match self.focused_section {
FocusedSection::Inspection => self.inspection.handle_keys(key_event),
FocusedSection::Firewall => self.firewall.handle_keys(key_event)?,
FocusedSection::Firewall => self
.firewall
.handle_keys(key_event, notification_sender.clone())?,
_ => {}
},
}
Expand Down
30 changes: 17 additions & 13 deletions oryx-tui/src/section/firewall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{net::IpAddr, num::ParseIntError, str::FromStr};
use tui_input::{backend::crossterm::EventHandler, Input};
use uuid;

use crate::app::AppResult;
use crate::{app::AppResult, notification::Notification};

#[derive(Debug, Clone)]
pub struct FirewallRule {
Expand Down Expand Up @@ -41,9 +41,9 @@ impl FromStr for BlockedPort {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "*" {
return Ok(BlockedPort::All);
Ok(BlockedPort::All)
} else {
return Ok(BlockedPort::Single(u16::from_str(s)?));
Ok(BlockedPort::Single(u16::from_str(s)?))
}
}
}
Expand Down Expand Up @@ -283,7 +283,11 @@ impl Firewall {
self.rules.retain(|r| r.name != rule.name);
}

pub fn handle_keys(&mut self, key_event: KeyEvent) -> AppResult<()> {
pub fn handle_keys(
&mut self,
key_event: KeyEvent,
sender: kanal::Sender<crate::event::Event>,
) -> AppResult<()> {
if let Some(user_input) = &mut self.user_input {
match key_event.code {
KeyCode::Esc => {
Expand All @@ -296,14 +300,6 @@ impl Firewall {

if let Some(id) = user_input.id {
let rule = self.rules.iter_mut().find(|rule| rule.id == id).unwrap();

if rule.enabled {
// set disable notification on previous rule definition
rule.enabled = false;
self.ingress_sender.send(rule.clone())?;
}

// update rule with user input
rule.name = user_input.name.field.to_string();
rule.ip = IpAddr::from_str(user_input.ip.field.value()).unwrap();
rule.port =
Expand Down Expand Up @@ -360,7 +356,15 @@ impl Firewall {
KeyCode::Char('e') => {
if let Some(index) = self.state.selected() {
let rule = self.rules[index].clone();
self.user_input = Some(rule.into());
if rule.enabled {
Notification::send(
"Can not edit enabled rule",
crate::notification::NotificationLevel::Warning,
sender.clone(),
)?;
} else {
self.user_input = Some(rule.into());
}
}
}

Expand Down

0 comments on commit 3faee02

Please sign in to comment.