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)]
|
||||
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<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)
|
||||
|| !toPort.parse::<i32>().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 {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -71,9 +71,7 @@ fn runApp<B: Backend>(app: &mut AppState, terminal: &mut Terminal<B>) -> 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<B: Backend>(app: &mut AppState, terminal: &mut Terminal<B>) -> 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<B: Backend>(app: &mut AppState, terminal: &mut Terminal<B>) -> Result<
|
||||
CurrentScreen::Exit => match key.code {
|
||||
KeyCode::Enter => {
|
||||
app.save();
|
||||
app.writeToConfig();
|
||||
return Ok(true);
|
||||
}
|
||||
KeyCode::Esc | KeyCode::Char('m') => app.screen = CurrentScreen::Main,
|
||||
|
||||
Reference in New Issue
Block a user