diff --git a/src/app_manager.rs b/src/app_manager.rs index 922126c..7952175 100644 --- a/src/app_manager.rs +++ b/src/app_manager.rs @@ -101,58 +101,40 @@ pub fn save_apps(apps: &[App]) { fs::write(path, json).expect("Failed to write apps.json"); } -pub fn uninstall_app(apps: &mut Vec, id: &str) { - let id = { - let url = id.trim_end_matches(".git"); +pub fn uninstall_app(apps: &mut Vec, index: usize) { + if index >= apps.len() { + eprintln!("Invalid app index: {}", index); + return; + } - if url.starts_with("git@") { - // Format: git@github.com:usr/repo - url.split_once(':').map(|(_, path)| path.to_string()) - } else if url.starts_with("https://") || url.starts_with("http://") { - // Format: https://github.com/usr/repo - url.split_once("github.com/") - .map(|(_, path)| path.to_string()) + let app = &apps[index]; + let repo_dir = apps_dir().join(app.repo_folder_name()); + + if repo_dir.exists() { + if let Err(e) = fs::remove_dir_all(&repo_dir) { + eprintln!("Failed to remove directory {:?}: {}", repo_dir, e); } else { - None + println!("Removed app: {}", app.repo_folder_name()); } } - .unwrap(); - if let Some(index) = apps - .iter() - .position(|app| app.id().expect("Unable to obtain id") == id) - { - let app = &apps[index]; - let repo_dir = apps_dir().join(app.repo_folder_name()); + let user_systemd_dir = home_dir().join(".config/systemd/user"); - if repo_dir.exists() { - if let Err(e) = fs::remove_dir_all(&repo_dir) { - eprintln!("Failed to remove directory {:?}: {}", repo_dir, e); - } else { - println!("Removed app: {}", id); - } - } + let service_path = user_systemd_dir.join(format!( + "{}.service", + app.id_system().expect("Unable to obtain id") + )); - let user_systemd_dir = home_dir().join(".config/systemd/user"); + println!("Removing service file: {:?}", service_path); - let service_path = user_systemd_dir.join(format!( - "{}.service", - app.id_system().expect("Unable to obtain id") - )); + remove_file(service_path).unwrap_or_else(|e| { + eprintln!("Failed to remove service file: {}", e); + }); - println!("Removing service file: {:?}", service_path); + update_systemd(); - remove_file(service_path).unwrap_or_else(|e| { - eprintln!("Failed to remove service file: {}", e); - }); - - update_systemd(); - - apps.remove(index); - save_apps(apps); - } else { - eprintln!("App not found: {}", id); - } + apps.remove(index); + save_apps(apps); } pub fn install_app(apps: &mut Vec, app: App) { @@ -236,45 +218,50 @@ pub fn install_app(apps: &mut Vec, app: App) { save_apps(apps); } -pub fn toggle_app_state(id: &str, enable: bool) { - Command::new("systemctl") - .arg("--user") - .arg(if enable { "enable" } else { "disable" }) - .arg(id) - .status() - .expect("Failed to toggle app enable state"); +pub fn toggle_app_state(apps: &[App], index: usize, enable: bool) { + if let Some(app) = apps.get(index) { + let id = app.id_system().expect("Unable to obtain id"); + Command::new("systemctl") + .arg("--user") + .arg(if enable { "enable" } else { "disable" }) + .arg(format!("{}.service", id)) + .status() + .expect("Failed to toggle app state"); + } } -pub fn start_app(id: &str) { - Command::new("systemctl") - .arg("--user") - .arg("start") - .arg(id) - .status() - .expect("Failed to start app"); +pub fn start_app(apps: &[App], index: usize) { + if let Some(app) = apps.get(index) { + let id = app.id_system().expect("Unable to obtain id"); + Command::new("systemctl") + .arg("--user") + .arg("start") + .arg(format!("{}.service", id)) + .status() + .expect("Failed to start app"); + } } -pub fn stop_app(id: &str) { - Command::new("systemctl") - .arg("--user") - .arg("stop") - .arg(id) - .status() - .expect("Failed to stop app"); +pub fn stop_app(apps: &[App], index: usize) { + if let Some(app) = apps.get(index) { + let id = app.id_system().expect("Unable to obtain id"); + Command::new("systemctl") + .arg("--user") + .arg("stop") + .arg(format!("{}.service", id)) + .status() + .expect("Failed to stop app"); + } } -pub fn restart_app(id: &str) { - Command::new("systemctl") - .arg("restart") - .arg(id) - .status() - .expect("Failed to restart app"); -} - -pub fn get_app_by_id<'a>(apps: &'a [App], id: &str) -> Option<&'a App> { - apps.iter().find(|app| app.id() == Some(id.to_string())) -} - -pub fn get_app_by_id_mut<'a>(apps: &'a mut [App], id: &str) -> Option<&'a mut App> { - apps.iter_mut().find(|app| app.id() == Some(id.to_string())) +pub fn restart_app(apps: &[App], index: usize) { + if let Some(app) = apps.get(index) { + let id = app.id_system().expect("Unable to obtain id"); + Command::new("systemctl") + .arg("--user") + .arg("restart") + .arg(format!("{}.service", id)) + .status() + .expect("Failed to restart app"); + } } diff --git a/src/main.rs b/src/main.rs index 16f118f..39e0a92 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ pub mod app_manager; pub mod server_config; -use crate::app_manager::{get_app_by_id, get_app_by_id_mut, install_app, App}; use crate::server_config::ServerStats; use std::sync::Arc; use std::thread::spawn; @@ -11,20 +10,22 @@ use tungstenite::accept; #[derive(Debug, Clone, serde::Deserialize, serde::Serialize)] pub enum Method { - Install(App), - SetEnabled(String, bool), - Edit(String, App), - Uninstall(String), - Toggle(String, bool), - Start(String), - Stop(String), - Restart(String), + Install(app_manager::App), + Edit(usize, app_manager::App), + Uninstall(usize), + Toggle(usize, bool), + Start(usize), + Stop(usize), + Restart(usize), } fn main() { + println!("SentryX server starting..."); let apps = app_manager::init(); + println!("Loaded {} apps", apps.len()); - let server = TcpListener::bind("127.0.0.1:5273").unwrap(); + let server = TcpListener::bind("0.0.0.0:5273").unwrap(); + println!("Listening on 127.0.0.1:5273"); for stream in server.incoming() { let mut apps = apps.clone(); @@ -32,6 +33,7 @@ fn main() { let websocket = Arc::new(Mutex::new(accept(stream.unwrap()).unwrap())); let reader_socket = Arc::clone(&websocket); let config = Arc::new(server_config::ServerConfig::new()); + println!("Client connected, awaiting master key..."); { let master_key = reader_socket.lock().unwrap().read().unwrap(); @@ -39,15 +41,17 @@ fn main() { if !master_key.is_text() || master_key.to_text().unwrap() != config.master_key.clone() { + println!("Invalid master key from client"); websocket .lock() .unwrap() .send(tungstenite::Message::text("!Invalid master key")) .unwrap(); - return; } + println!("Client authenticated successfully"); + websocket .lock() .unwrap() @@ -67,52 +71,39 @@ fn main() { ) .unwrap(); + println!("Received command: {:?}", mth); + match mth { Method::Install(app) => { - install_app(&mut apps, app); - } - Method::SetEnabled(i, b) => { - let app = get_app_by_id_mut(&mut apps, &i).expect("App not found"); - if let Some(id) = app.id_system() { - app_manager::toggle_app_state(&format!("{}.service", id), b); - } - app_manager::save_apps(&apps); + println!("→ Installing app: {}", app.repo); + app_manager::install_app(&mut apps, app); } Method::Edit(i, app) => { - let existing_app = get_app_by_id_mut(&mut apps, &i).expect("App not found"); - *existing_app = app; - app_manager::save_apps(&apps); + println!("→ Editing app at index {}: {}", i, app.repo); + if let Some(existing_app) = apps.get_mut(i) { + *existing_app = app; + app_manager::save_apps(&apps); + } } - Method::Uninstall(id) => { - app_manager::uninstall_app(&mut apps, &id); + Method::Uninstall(i) => { + println!("→ Uninstalling app at index {}", i); + app_manager::uninstall_app(&mut apps, i); } - Method::Toggle(id, enable) => { - let id = get_app_by_id(&mut apps, &id) - .expect("App not found") - .id_system() - .unwrap(); - app_manager::toggle_app_state(&format!("{}.service", id), enable); + Method::Toggle(index, enable) => { + println!("→ Toggling app at index {} to {}", index, enable); + app_manager::toggle_app_state(&apps, index, enable); } - Method::Start(id) => { - let id = get_app_by_id(&mut apps, &id) - .expect("App not found") - .id_system() - .unwrap(); - app_manager::start_app(&format!("{}.service", id)); + Method::Start(index) => { + println!("→ Starting app at index {}", index); + app_manager::start_app(&apps, index); } - Method::Stop(id) => { - let id = get_app_by_id(&mut apps, &id) - .expect("App not found") - .id_system() - .unwrap(); - app_manager::stop_app(&format!("{}.service", id)); + Method::Stop(index) => { + println!("→ Stopping app at index {}", index); + app_manager::stop_app(&apps, index); } - Method::Restart(id) => { - let id = get_app_by_id(&mut apps, &id) - .expect("App not found") - .id_system() - .unwrap(); - app_manager::restart_app(&format!("{}.service", id)); + Method::Restart(index) => { + println!("→ Restarting app at index {}", index); + app_manager::restart_app(&apps, index); } } }); @@ -128,6 +119,7 @@ fn main() { networks.refresh(true); disks.refresh(true); + println!("→ Sending system stats update..."); websocket .lock() .unwrap() @@ -139,6 +131,7 @@ fn main() { std::thread::sleep(std::time::Duration::from_secs(2)); } + println!("Client disconnected, cleaning up..."); }); } } diff --git a/test.js b/test.js index c3ee8ae..fc88507 100644 --- a/test.js +++ b/test.js @@ -17,26 +17,26 @@ ws.onmessage = (msg) => console.log("Received:", msg.data); ws.onopen = () => { ws.send("my-key"); - ws.send( - JSON.stringify({ - Install: { - repo: "git@github.com:osui-rs/osui.git", - branch: "master", - install_command: "", - run_command: "cargo run", - } - }) - ); + // ws.send( + // JSON.stringify({ + // Install: { + // repo: "git@github.com:osui-rs/osui.git", + // branch: "master", + // install_command: "", + // run_command: "cargo run", + // } + // }) + // ); + + // ws.send( + // JSON.stringify({ + // SetEnabled: ["osui-rs/osui", true] + // }) + // ); ws.send( JSON.stringify({ - SetEnabled: ["osui-rs/osui", true] - }) - ); - - ws.send( - JSON.stringify({ - Start: "osui-rs/osui" + Start: 0 }) );