Skip to content

Commit

Permalink
add key handler for saving rules
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Oct 10, 2024
1 parent 571e5a6 commit 69e9e1f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 36 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ sudo oryx

`e`: Edit a firewall rule.

`s`: Save firewall rules to `~/oryx/firewall.json`

`Enter`: Create or Save a firewall rule.

## ⚖️ License
Expand Down
25 changes: 0 additions & 25 deletions oryx-tui/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use std::{thread, time::Duration};
use crate::{
app::{ActivePopup, App, AppResult},
event::Event,
export::export,
filter::FocusedBlock,
notification::{Notification, NotificationLevel},
section::FocusedSection,
};
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
Expand Down Expand Up @@ -190,29 +188,6 @@ pub fn handle_key_events(
}
}

KeyCode::Char('s') => {
let app_packets = app.packets.lock().unwrap();
if app_packets.is_empty() {
Notification::send(
"There is no packets".to_string(),
NotificationLevel::Info,
event_sender,
)?;
} else {
match export(&app_packets) {
Ok(_) => {
Notification::send(
"Packets exported to ~/oryx/capture file".to_string(),
NotificationLevel::Info,
event_sender,
)?;
}
Err(e) => {
Notification::send(e.to_string(), NotificationLevel::Error, event_sender)?;
}
}
}
}
_ => {
app.section.handle_keys(key_event, event_sender.clone())?;
}
Expand Down
4 changes: 4 additions & 0 deletions oryx-tui/src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ impl Help {
(Cell::from("## Firewall").bold().yellow(), ""),
(Cell::from("n").bold(), "Add new firewall rule"),
(Cell::from("e").bold(), "Edit a firewall rule"),
(
Cell::from("s").bold(),
"Save firewall rules to ~/oryx/firewall.json ",
),
(Cell::from("Space").bold(), "Toggle firewall rule status"),
(Cell::from("Enter").bold(), "Create or Save a firewall rule"),
],
Expand Down
10 changes: 9 additions & 1 deletion oryx-tui/src/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ impl Section {
Span::from("i").bold(),
Span::from(" Infos").bold(),
Span::from(" | ").bold(),
Span::from("s").bold(),
Span::from(" Save").bold(),
Span::from(" | ").bold(),
Span::from("f").bold(),
Span::from(" Filters").bold(),
Span::from(" | ").bold(),
Expand All @@ -176,6 +179,9 @@ impl Section {
Span::from("e").bold(),
Span::from(" Edit").bold(),
Span::from(" | ").bold(),
Span::from("s").bold(),
Span::from(" Save").bold(),
Span::from(" | ").bold(),
Span::from("󱁐 ").bold(),
Span::from(" Toggle").bold(),
Span::from(" | ").bold(),
Expand Down Expand Up @@ -275,7 +281,9 @@ impl Section {
},

_ => match self.focused_section {
FocusedSection::Inspection => self.inspection.handle_keys(key_event),
FocusedSection::Inspection => self
.inspection
.handle_keys(key_event, notification_sender.clone())?,
FocusedSection::Firewall => self
.firewall
.handle_keys(key_event, notification_sender.clone())?,
Expand Down
35 changes: 26 additions & 9 deletions oryx-tui/src/section/firewall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,15 @@ impl Firewall {
if !self.rules.is_empty() {
info!("Saving Firewall Rules");

let json = serde_json::to_string(&self.rules).unwrap();
let json = serde_json::to_string(&self.rules)?;

let uid = unsafe { libc::geteuid() };
let user_uid = unsafe { libc::geteuid() };

let oryx_export_dir = dirs::home_dir().unwrap().join("oryx");

if !oryx_export_dir.exists() {
fs::create_dir(&oryx_export_dir)?;
chown(&oryx_export_dir, Some(uid), Some(uid))?;
chown(&oryx_export_dir, Some(user_uid), Some(user_uid))?;
}

let oryx_export_file = oryx_export_dir.join("firewall.json");
Expand All @@ -349,24 +349,23 @@ impl Firewall {
}

fn load_saved_rules() -> AppResult<Vec<FirewallRule>> {
info!("Loading Firewall Rules");
let oryx_export_file = dirs::home_dir().unwrap().join("oryx").join("firewall.json");
if oryx_export_file.exists() {
info!("Found previously saved Firewall Rules");
info!("Loading Firewall Rules");

let json_string = fs::read_to_string(oryx_export_file)?;

let mut parsed_rules: Vec<FirewallRule> = serde_json::from_str(&json_string)?;

// as we don't know if ingress/egress programs are loaded we have to disable all rules
for rule in &mut parsed_rules {
rule.enabled = false
}
parsed_rules
.iter_mut()
.for_each(|rule| rule.enabled = false);

info!("Firewall Rules loaded");
Ok(parsed_rules)
} else {
info!("No saved Firewall Rules found");
info!("Firewall Rules file not found");
Ok(Vec::new())
}
}
Expand Down Expand Up @@ -556,6 +555,24 @@ impl Firewall {
self.add_rule();
}

KeyCode::Char('s') => match self.save_rules() {
Ok(_) => {
Notification::send(
"Firewall rules saved to ~/oryx/firewall.json file",
crate::notification::NotificationLevel::Info,
sender.clone(),
)?;
}
Err(e) => {
Notification::send(
"Error while saving firewall rules.",
crate::notification::NotificationLevel::Error,
sender.clone(),
)?;
error!("Error while saving firewall rules. {}", e);
}
},

KeyCode::Char('e') => {
if let Some(index) = self.state.selected() {
let rule = self.rules[index].clone();
Expand Down
38 changes: 37 additions & 1 deletion oryx-tui/src/section/inspection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use ratatui::{
use tui_input::backend::crossterm::EventHandler;

use crate::{
app::AppResult,
export,
filter::fuzzy::{self, Fuzzy},
notification::{Notification, NotificationLevel},
packet::{
network::{IpPacket, IpProto},
AppPacket,
Expand Down Expand Up @@ -56,7 +59,11 @@ impl Inspection {
}
}

pub fn handle_keys(&mut self, key_event: KeyEvent) {
pub fn handle_keys(
&mut self,
key_event: KeyEvent,
event_sender: kanal::Sender<crate::event::Event>,
) -> AppResult<()> {
let fuzzy_is_enabled = { self.fuzzy.lock().unwrap().is_enabled() };

if fuzzy_is_enabled {
Expand Down Expand Up @@ -126,9 +133,38 @@ impl Inspection {
self.scroll_up();
}

KeyCode::Char('s') => {
let app_packets = self.packets.lock().unwrap();
if app_packets.is_empty() {
Notification::send(
"There is no packets".to_string(),
NotificationLevel::Info,
event_sender,
)?;
} else {
match export::export(&app_packets) {
Ok(_) => {
Notification::send(
"Packets exported to ~/oryx/capture file".to_string(),
NotificationLevel::Info,
event_sender,
)?;
}
Err(e) => {
Notification::send(
e.to_string(),
NotificationLevel::Error,
event_sender,
)?;
}
}
}
}

_ => {}
}
}
Ok(())
}

pub fn scroll_up(&mut self) {
Expand Down

0 comments on commit 69e9e1f

Please sign in to comment.