From b039db77f0073eb9081fe7b2f8136f2c0b5f4776 Mon Sep 17 00:00:00 2001 From: Integral Date: Mon, 25 Nov 2024 17:25:57 +0800 Subject: [PATCH] refactor: replace subprocess crate with std::process (#697) --- Cargo.lock | 11 ----------- Cargo.toml | 1 - src/scripts/mod.rs | 48 +++++++++++++++++++++++++++++++++------------- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c4689ee..32c761b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1519,7 +1519,6 @@ dependencies = [ "rlimit", "serde", "serde_derive", - "subprocess", "text_placeholder", "toml", "wait-timeout", @@ -1633,16 +1632,6 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" -[[package]] -name = "subprocess" -version = "0.2.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c2e86926081dda636c546d8c5e641661049d7562a68f5488be4a1f7f66f6086" -dependencies = [ - "libc", - "winapi", -] - [[package]] name = "syn" version = "2.0.69" diff --git a/Cargo.toml b/Cargo.toml index b2ca1ce..87f2717 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,6 @@ cidr-utils = "0.6.1" itertools = "0.13.0" hickory-resolver = { version = "0.24.0", features = ["dns-over-rustls"] } anyhow = "1.0.40" -subprocess = "0.2.6" text_placeholder = { version = "0.5", features = ["struct_context"] } once_cell = "1.20.2" diff --git a/src/scripts/mod.rs b/src/scripts/mod.rs index 21ea68c..8bc188d 100644 --- a/src/scripts/mod.rs +++ b/src/scripts/mod.rs @@ -79,15 +79,17 @@ use crate::input::ScriptsRequired; use anyhow::{anyhow, Result}; use log::debug; use serde_derive::{Deserialize, Serialize}; -use std::convert::TryInto; use std::fs::{self, File}; use std::io::{self, prelude::*}; use std::net::IpAddr; use std::path::PathBuf; +use std::process::{Command, Stdio}; use std::string::ToString; -use subprocess::{Exec, ExitStatus, Redirection}; use text_placeholder::Template; +#[cfg(unix)] +use std::os::unix::process::ExitStatusExt; + static DEFAULT: &str = r#"tags = ["core_approved", "RustScan", "default"] developer = [ "RustScan", "https://github.com/RustScan" ] ports_separator = "," @@ -271,21 +273,41 @@ impl Script { #[cfg(not(tarpaulin_include))] fn execute_script(script: &str) -> Result { debug!("\nScript arguments {}", script); - let process = Exec::shell(script) - .stdout(Redirection::Pipe) - .stderr(Redirection::Pipe); - match process.capture() { - Ok(c) => { - let es = match c.exit_status { - ExitStatus::Exited(c) => c.try_into().unwrap(), - ExitStatus::Signaled(c) => c.into(), - ExitStatus::Other(c) => c, - ExitStatus::Undetermined => -1, + + let (cmd, arg) = if cfg!(unix) { + ("sh", "-c") + } else { + ("cmd.exe", "/c") + }; + + match Command::new(cmd) + .args(&[arg, script]) + .stdin(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + { + Ok(output) => { + let status = output.status; + + let es = match status.code() { + Some(code) => code, + _ => { + #[cfg(unix)] + { + status.signal().unwrap() + } + + #[cfg(windows)] + { + return Err(anyhow!("Unknown exit status")); + } + } }; + if es != 0 { return Err(anyhow!("Exit code = {}", es)); } - Ok(c.stdout_str()) + Ok(String::from_utf8_lossy(&output.stdout).into_owned()) } Err(error) => { debug!("Command error {}", error.to_string());