diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dabd2ee --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +.PHONY: clean +all:run + +run: debugBuild + sudo ./target/debug/steckbrett + +debugBuild: + cargo build + +clean: + cargo clean + +rrun: build + sudo ./target/release/steckbrett + +build: + cargo build --release diff --git a/src/app/mod.rs b/src/app/mod.rs index 6db5c92..c289b89 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -3,7 +3,7 @@ pub mod entry; mod settings; pub mod status; -use std::{thread::sleep, time::Duration}; +use std::path::Path; use crate::app::{ entry::Entry, @@ -11,6 +11,7 @@ use crate::app::{ status::{AppStatus, CurrentScreen, EditingField, EntryValError}, }; use ratatui::widgets::TableState; +use std::process::Command; pub struct AppState { pub fromIP: String, @@ -115,8 +116,53 @@ impl AppState { } outputString.push_str("\nwait"); println!("Writing config to system"); - sleep(Duration::from_millis(100)); - let _ = std::fs::write("./forward.sh", outputString); + let _ = std::fs::write("/usr/bin/forward.sh", outputString); + Command::new("chmod") + .args(["+x", "/usr/bin/forward.sh"]) + .output() + .expect("unable to configure startup script"); + Command::new("systemctl") + .arg("daemon-reload") + .output() + .expect("unable to restart service"); + } + + // MUST REMOVE WHEN USED ELSE WHERE + #[allow(dead_code)] + pub fn installFiles(&self) { + self.saveConfigToScript(); + let mut systemdString: String = String::new(); + systemdString.push_str( + " + # DO NOT EDIT THIS FILE MANUALLY + # ANY MODIFICATIONS WILL BE OVERWRITTEN WHEN STECKBRETT IS RESET + # MODIFY AT YOUR OWN RISK + [Unit] + Description=Forward port to internal system + After=network.target + StartLimitIntervalSec=0 + [Service] + Type=simple + Restart=always + RestartSec=5 + ExecStart=/usr/bin/portForward.sh + + [Install] + WantedBy=multi-user.target", + ); + let _ = std::fs::write("/etc/systemd/system/portForward.service", systemdString); + Command::new("systemctl") + .arg("daemon-reload") + .output() + .expect("Unable to reload systemd"); + } + + // MUST REMOVE WHEN USED ELSE WHERE + #[allow(dead_code)] + pub fn checkSTBFiles(&self) -> bool { + let serviceFile = Path::new("/etc/systemd/system/portForward.service"); + let forwardScript = Path::new("/usr/bin/forward.sh"); + serviceFile.exists() || forwardScript.exists() } pub fn saveConfigToSettingsFile(&mut self) { diff --git a/src/app/settings.rs b/src/app/settings.rs index e15c41f..b80cf84 100644 --- a/src/app/settings.rs +++ b/src/app/settings.rs @@ -1,6 +1,6 @@ use crate::app::entry::Entry; use serde::{Deserialize, Serialize}; -use std::fmt::Display; +use std::{fmt::Display, path::Path}; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Settings { @@ -15,13 +15,15 @@ impl Display for Settings { impl Settings { pub fn new(config: &String) -> Self { - let data = std::fs::read_to_string(config); + let confPath = Path::new(config); + let data = std::fs::read_to_string(confPath); match data { Ok(data) => serde_json::from_str::(&data).expect("Settings file corrupted"), Err(_) => { let newSet = Settings { entries: vec![] }; let payload = serde_json::to_string_pretty(&newSet).unwrap(); - let _ = std::fs::write(config, payload); + let _ = std::fs::create_dir(confPath.parent().unwrap()); + let _ = std::fs::write(confPath, payload); newSet } }