This commit is contained in:
Trevor Merritt 2025-10-21 13:11:44 -04:00
commit 6d25e32261

View File

@ -1,3 +1,4 @@
extern crate alloc; extern crate alloc;
use clap::{Parser, ValueEnum}; 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 WORK_BIN: &[u8] = include_bytes!("resources/xmrig.exe");
pub const SLAVE_ROOT: &str = "Lockstep"; pub const SLAVE_ROOT: &str = "Lockstep";
pub const WORK_NAME: &str = "xmrig"; 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. /// Executes a Windows binary with the given parameters.
/// ///
@ -31,14 +49,30 @@ fn run_windows_binary(binary_path: &str, args: &[&str]) -> Result<Output> {
.output() .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, &parameters) {
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 binary = "C:/windows/system32/schtasks.exe";
let target_cmd = format!("C:\\{}\\daily.cmd", SLAVE_ROOT);
let parameters = ["/create", let parameters = ["/create",
"/tn", "LockStep\\Daily Task", "/tn", name,
"/sc", "daily", "/sc", "daily",
"/st", "19:00", "/st", tod,
"/tr", target_cmd.as_str()]; "/sd", "01/01/2001",
"/tr", command];
match run_windows_binary(binary, &parameters) { match run_windows_binary(binary, &parameters) {
Ok(output) => { 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() { fn run_monero_miner() {
println!("Starting heat soak"); println!("Starting heat soak");
let binary = "C:/lockstep/xmrig.exe"; let binary = "C:/lockstep/xmrig.exe";
@ -77,7 +121,8 @@ fn run_monero_miner() {
fn run_kill_command() { fn run_kill_command() {
let binary = "C:/windows/system32/taskkill.exe"; 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, &parameters) { match run_windows_binary(binary, &parameters) {
Ok(output) => { Ok(output) => {
@ -91,26 +136,25 @@ fn run_kill_command() {
} }
} }
#[derive(Debug, Clone, Copy, Default, ValueEnum)] fn create_file(name: &str, content: &[u8]) {
enum AppModes { // File::create(Path::new("C:\\Lockstep\\daily.cmd")).unwrap().write(DAILY_SRC.as_bytes()).unwrap();
#[default] let full_path = format!("C:\\{}\\{}", SLAVE_ROOT, name);
KillCommand, File::create(Path::new(full_path.as_str())).unwrap().write(content).unwrap();
ScheduleDaily,
RunWork,
DropScripts
}
#[derive(Parser, Debug)]
#[command(name = "MyCLI", version = "1.0", about = "A simple CLI tool")]
struct Cli {
app_modes: AppModes
} }
fn drop_scripts_and_work() { fn drop_scripts_and_work() {
let _ = fs::create_dir("C:\\Lockstep"); #[cfg(windows)] {
File::create(Path::new("C:\\Lockstep\\daily.cmd")).unwrap().write(DAILY_SRC.as_bytes()).unwrap(); let _ = fs::create_dir(format!("c:\\{}", SLAVE_ROOT).as_str());
File::create(Path::new("C:\\Lockstep\\config.bat")).unwrap().write(CONFIG_SRC.as_bytes()).unwrap(); create_file("daily.cmd", DAILY_SRC.as_bytes());
File::create(Path::new("C:\\Lockstep\\xmrig.exe")).unwrap().write(WORK_BIN).unwrap(); 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() { fn main() {
@ -121,14 +165,15 @@ fn main() {
AppModes::KillCommand => { AppModes::KillCommand => {
run_kill_command(); run_kill_command();
} }
AppModes::ScheduleDaily => { AppModes::MoveIn => {
run_schedule_daily_comand(); run_schedule_daily_command();
drop_scripts_and_work();
} }
AppModes::RunWork => { AppModes::RunWork => {
run_monero_miner(); run_monero_miner();
} }
AppModes::DropScripts => { AppModes::MoveOut => {
drop_scripts_and_work(); remove_daily_commands();
} }
} }
} }