From b334ffabac0b8115d232ab722ceea9e2b27b03cc Mon Sep 17 00:00:00 2001 From: Phani Pavan K Date: Fri, 19 Sep 2025 23:30:30 +0530 Subject: [PATCH] fix ip regex, add write to sh file --- src/app/entry.rs | 12 ++++++------ src/app/mod.rs | 13 +++++++++++++ src/main.rs | 4 +--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/app/entry.rs b/src/app/entry.rs index 5b59d4e..e596fcd 100644 --- a/src/app/entry.rs +++ b/src/app/entry.rs @@ -5,10 +5,10 @@ use std::fmt::Display; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Entry { - fromIP: String, - fromPort: String, - toIP: String, - toPort: String, + pub fromIP: String, + pub fromPort: String, + pub toIP: String, + pub toPort: String, } impl Display for Entry { @@ -28,12 +28,12 @@ impl Entry { fromPort: String, toPort: String, ) -> Result { - let ip = Regex::new("/^(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}$/").unwrap(); + let ip = Regex::new("^(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\\.(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\\.(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\\.(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])$").unwrap(); if !fromPort.parse::().is_ok_and(|a| a > 1 && a < 65535) || !toPort.parse::().is_ok_and(|a| a > 1 && a < 65535) { Err(EntryCreation::PortValidationError) - } else if !ip.is_match(&fromIP) || !ip.is_match(&toIP) { + } else if !ip.is_match(&fromIP.trim()) || !ip.is_match(&toIP.trim()) { Err(EntryCreation::IPValidationError) } else { Ok(Entry { diff --git a/src/app/mod.rs b/src/app/mod.rs index cb0f65c..31ccd94 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -86,6 +86,19 @@ impl AppState { println!("{resString}"); } + pub fn writeToConfig(&self) { + let mut outputString: String = String::new(); + outputString.push_str("#! /bin/bash\n\n"); + for ent in self.entries.iter() { + outputString.push_str(&format!( + "socat TCP-LISTEN:{},fork,reuseaddr,bind={} TCP:{}:{} &\n", + ent.fromPort, ent.fromIP, ent.toIP, ent.toPort + )) + } + outputString.push_str("\nwait"); + std::fs::write("./forward.sh", outputString); + } + pub fn save(&self) { let mut settings = Settings::new(&self.confDir); settings.entries = self.entries.clone(); diff --git a/src/main.rs b/src/main.rs index 7fa7d64..0f33605 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,9 +71,7 @@ fn runApp(app: &mut AppState, terminal: &mut Terminal) -> Result< KeyCode::Char('q') | KeyCode::F(10) | KeyCode::Esc => { app.screen = CurrentScreen::Exit } - KeyCode::Char('s') | KeyCode::F(2) => app.screen = CurrentScreen::Settings, - _ => {} }, CurrentScreen::Add => match (key.modifiers, key.code) { @@ -113,7 +111,6 @@ fn runApp(app: &mut AppState, terminal: &mut Terminal) -> Result< } (KeyModifiers::NONE, KeyCode::Tab) => app.nextField(), (KeyModifiers::SHIFT, KeyCode::Tab) => app.prevField(), - (_, KeyCode::Char(v)) => { if let Some(e) = &app.currentlyEditing { let mut isIP = false; @@ -152,6 +149,7 @@ fn runApp(app: &mut AppState, terminal: &mut Terminal) -> Result< CurrentScreen::Exit => match key.code { KeyCode::Enter => { app.save(); + app.writeToConfig(); return Ok(true); } KeyCode::Esc | KeyCode::Char('m') => app.screen = CurrentScreen::Main,