fix ip regex, add write to sh file
This commit is contained in:
@@ -5,10 +5,10 @@ use std::fmt::Display;
|
|||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct Entry {
|
pub struct Entry {
|
||||||
fromIP: String,
|
pub fromIP: String,
|
||||||
fromPort: String,
|
pub fromPort: String,
|
||||||
toIP: String,
|
pub toIP: String,
|
||||||
toPort: String,
|
pub toPort: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Entry {
|
impl Display for Entry {
|
||||||
@@ -28,12 +28,12 @@ impl Entry {
|
|||||||
fromPort: String,
|
fromPort: String,
|
||||||
toPort: String,
|
toPort: String,
|
||||||
) -> Result<Self, EntryCreation> {
|
) -> Result<Self, EntryCreation> {
|
||||||
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::<i32>().is_ok_and(|a| a > 1 && a < 65535)
|
if !fromPort.parse::<i32>().is_ok_and(|a| a > 1 && a < 65535)
|
||||||
|| !toPort.parse::<i32>().is_ok_and(|a| a > 1 && a < 65535)
|
|| !toPort.parse::<i32>().is_ok_and(|a| a > 1 && a < 65535)
|
||||||
{
|
{
|
||||||
Err(EntryCreation::PortValidationError)
|
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)
|
Err(EntryCreation::IPValidationError)
|
||||||
} else {
|
} else {
|
||||||
Ok(Entry {
|
Ok(Entry {
|
||||||
|
|||||||
@@ -86,6 +86,19 @@ impl AppState {
|
|||||||
println!("{resString}");
|
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) {
|
pub fn save(&self) {
|
||||||
let mut settings = Settings::new(&self.confDir);
|
let mut settings = Settings::new(&self.confDir);
|
||||||
settings.entries = self.entries.clone();
|
settings.entries = self.entries.clone();
|
||||||
|
|||||||
@@ -71,9 +71,7 @@ fn runApp<B: Backend>(app: &mut AppState, terminal: &mut Terminal<B>) -> Result<
|
|||||||
KeyCode::Char('q') | KeyCode::F(10) | KeyCode::Esc => {
|
KeyCode::Char('q') | KeyCode::F(10) | KeyCode::Esc => {
|
||||||
app.screen = CurrentScreen::Exit
|
app.screen = CurrentScreen::Exit
|
||||||
}
|
}
|
||||||
|
|
||||||
KeyCode::Char('s') | KeyCode::F(2) => app.screen = CurrentScreen::Settings,
|
KeyCode::Char('s') | KeyCode::F(2) => app.screen = CurrentScreen::Settings,
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
CurrentScreen::Add => match (key.modifiers, key.code) {
|
CurrentScreen::Add => match (key.modifiers, key.code) {
|
||||||
@@ -113,7 +111,6 @@ fn runApp<B: Backend>(app: &mut AppState, terminal: &mut Terminal<B>) -> Result<
|
|||||||
}
|
}
|
||||||
(KeyModifiers::NONE, KeyCode::Tab) => app.nextField(),
|
(KeyModifiers::NONE, KeyCode::Tab) => app.nextField(),
|
||||||
(KeyModifiers::SHIFT, KeyCode::Tab) => app.prevField(),
|
(KeyModifiers::SHIFT, KeyCode::Tab) => app.prevField(),
|
||||||
|
|
||||||
(_, KeyCode::Char(v)) => {
|
(_, KeyCode::Char(v)) => {
|
||||||
if let Some(e) = &app.currentlyEditing {
|
if let Some(e) = &app.currentlyEditing {
|
||||||
let mut isIP = false;
|
let mut isIP = false;
|
||||||
@@ -152,6 +149,7 @@ fn runApp<B: Backend>(app: &mut AppState, terminal: &mut Terminal<B>) -> Result<
|
|||||||
CurrentScreen::Exit => match key.code {
|
CurrentScreen::Exit => match key.code {
|
||||||
KeyCode::Enter => {
|
KeyCode::Enter => {
|
||||||
app.save();
|
app.save();
|
||||||
|
app.writeToConfig();
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
}
|
}
|
||||||
KeyCode::Esc | KeyCode::Char('m') => app.screen = CurrentScreen::Main,
|
KeyCode::Esc | KeyCode::Char('m') => app.screen = CurrentScreen::Main,
|
||||||
|
|||||||
Reference in New Issue
Block a user