From 3e659d180d24b0a8f140ab5b6c37595a242c13f6 Mon Sep 17 00:00:00 2001 From: Janis Steiner Date: Sat, 1 Nov 2025 13:24:28 +0100 Subject: [PATCH] adding installer --- client/protector/src/settings/settings.rs | 6 +- installer/protector_installer/Cargo.lock | 7 ++ installer/protector_installer/Cargo.toml | 6 ++ installer/protector_installer/src/main.rs | 96 +++++++++++++++++++ .../target/.rustc_info.json | 1 + .../protector_installer/target/CACHEDIR.TAG | 3 + 6 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 installer/protector_installer/Cargo.lock create mode 100644 installer/protector_installer/Cargo.toml create mode 100644 installer/protector_installer/src/main.rs create mode 100644 installer/protector_installer/target/.rustc_info.json create mode 100644 installer/protector_installer/target/CACHEDIR.TAG diff --git a/client/protector/src/settings/settings.rs b/client/protector/src/settings/settings.rs index 33f822f..29ca23e 100644 --- a/client/protector/src/settings/settings.rs +++ b/client/protector/src/settings/settings.rs @@ -11,8 +11,8 @@ pub struct Settings { pub static SETTINGS: Lazy> = Lazy::new(|| { RwLock::new(Settings { - server_ip: String::new(), - server_port: 0, + server_ip: String::new(), + server_port: 0, client_secret: String::new(), server_secret: String::new(), }) @@ -50,7 +50,7 @@ pub fn load_settings(path: &str) -> bool { Err(_) => return false, // invalid port number } } - _ => continue, //return false, // unknown key + _ => continue,//ignore unknown key } } diff --git a/installer/protector_installer/Cargo.lock b/installer/protector_installer/Cargo.lock new file mode 100644 index 0000000..6c5e331 --- /dev/null +++ b/installer/protector_installer/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "protector_installer" +version = "0.1.0" diff --git a/installer/protector_installer/Cargo.toml b/installer/protector_installer/Cargo.toml new file mode 100644 index 0000000..afa00dd --- /dev/null +++ b/installer/protector_installer/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "protector_installer" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/installer/protector_installer/src/main.rs b/installer/protector_installer/src/main.rs new file mode 100644 index 0000000..5232be3 --- /dev/null +++ b/installer/protector_installer/src/main.rs @@ -0,0 +1,96 @@ +use std::fs::{self, File}; +use std::io::Write; +use std::path::Path; +use std::process::Command; + +fn main() { + // Predefined values + let app_path = "/etc/protector/app/protector"; + let service_name = "protector"; + let working_dir = "/etc/protector"; + let log_file = "/var/log/protector.log"; + + // Step 1: Create folder structure + let folders = ["/etc/protector/app", "/etc/protector/conf"]; + for folder in folders.iter() { + if !Path::new(folder).exists() { + match fs::create_dir_all(folder) { + Ok(_) => println!("Created folder: {}", folder), + Err(e) => eprintln!("Failed to create folder {}: {}", folder, e), + } + } + } + + // Step 2: Create empty log file if it doesn't exist + if !Path::new(log_file).exists() { + match File::create(log_file) { + Ok(_) => println!("Created log file: {}", log_file), + Err(e) => eprintln!("Failed to create log file {}: {}", log_file, e), + } + } + + // Step 3: Create systemd service content + let service_content = format!( + r#"[Unit] +Description={0} system-wide service +After=network.target + +[Service] +Type=simple +ExecStart={1} +Restart=on-failure +WorkingDirectory={2} +StandardOutput=append:{3} +StandardError=append:{3} + +[Install] +WantedBy=multi-user.target +"#, + service_name, app_path, working_dir, log_file + ); + + let service_path = format!("/etc/systemd/system/{}.service", service_name); + + // Step 4: Write the service file + if Path::new("/etc/systemd/system/").exists() { + match File::create(&service_path) { + Ok(mut file) => { + file.write_all(service_content.as_bytes()) + .expect("Failed to write service file"); + println!("System-wide service file created at {}", service_path); + + // Step 5: Reload systemd, enable, and start the service + let reload = Command::new("systemctl") + .arg("daemon-reload") + .status() + .expect("Failed to reload systemd"); + if reload.success() { + println!("systemd reloaded successfully."); + } + + let enable = Command::new("systemctl") + .arg("enable") + .arg(service_name) + .status() + .expect("Failed to enable service"); + if enable.success() { + println!("Service {} enabled to start on boot.", service_name); + } + + let start = Command::new("systemctl") + .arg("start") + .arg(service_name) + .status() + .expect("Failed to start service"); + if start.success() { + println!("Service {} started successfully.", service_name); + } else { + eprintln!("Failed to start service {}.", service_name); + } + } + Err(e) => eprintln!("Failed to create service file: {}", e), + } + } else { + println!("Directory /etc/systemd/system/ does not exist. Cannot install"); + } +} diff --git a/installer/protector_installer/target/.rustc_info.json b/installer/protector_installer/target/.rustc_info.json new file mode 100644 index 0000000..b984203 --- /dev/null +++ b/installer/protector_installer/target/.rustc_info.json @@ -0,0 +1 @@ +{"rustc_fingerprint":15569509355061161103,"outputs":{"17747080675513052775":{"success":true,"status":"","code":0,"stdout":"rustc 1.89.0 (29483883e 2025-08-04)\nbinary: rustc\ncommit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2\ncommit-date: 2025-08-04\nhost: x86_64-unknown-linux-gnu\nrelease: 1.89.0\nLLVM version: 20.1.7\n","stderr":""},"7971740275564407648":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/janis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""}},"successes":{}} \ No newline at end of file diff --git a/installer/protector_installer/target/CACHEDIR.TAG b/installer/protector_installer/target/CACHEDIR.TAG new file mode 100644 index 0000000..20d7c31 --- /dev/null +++ b/installer/protector_installer/target/CACHEDIR.TAG @@ -0,0 +1,3 @@ +Signature: 8a477f597d28d172789f06886806bc55 +# This file is a cache directory tag created by cargo. +# For information about cache directory tags see https://bford.info/cachedir/