From 4f6a80a8ee24b9c08522a5e1f71f1af25385d74c Mon Sep 17 00:00:00 2001 From: CherretGit Date: Sat, 1 Nov 2025 19:56:08 +0700 Subject: [PATCH] fix service status --- rust/Cargo.lock | 184 +++++++++++++++++++++++- rust/Cargo.toml | 1 + rust/crates/zaprett/Cargo.toml | 3 +- rust/crates/zaprett/src/cli/commands.rs | 12 +- rust/crates/zaprett/src/lib.rs | 32 ++--- rust/crates/zaprett/src/service.rs | 31 +++- 6 files changed, 229 insertions(+), 34 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index d585ae4..1d8fa28 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -387,7 +387,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" dependencies = [ "cfg-if", - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -460,6 +460,34 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "ntapi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" +dependencies = [ + "winapi", +] + +[[package]] +name = "objc2-core-foundation" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536" +dependencies = [ + "bitflags", +] + +[[package]] +name = "objc2-io-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33fafba39597d6dc1fb709123dfa8289d39406734be322956a69f0931c73bb15" +dependencies = [ + "libc", + "objc2-core-foundation", +] + [[package]] name = "once_cell" version = "1.21.3" @@ -502,7 +530,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -751,6 +779,20 @@ dependencies = [ "walkdir", ] +[[package]] +name = "sysinfo" +version = "0.37.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16607d5caffd1c07ce073528f9ed972d88db15dd44023fa57142963be3feb11f" +dependencies = [ + "libc", + "memchr", + "ntapi", + "objc2-core-foundation", + "objc2-io-kit", + "windows", +] + [[package]] name = "termcolor" version = "1.4.1" @@ -845,6 +887,22 @@ version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.11" @@ -854,12 +912,120 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.61.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9babd3a767a4c1aef6900409f85f5d53ce2544ccdfaa86dad48c91782c6d6893" +dependencies = [ + "windows-collections", + "windows-core", + "windows-future", + "windows-link 0.1.3", + "windows-numerics", +] + +[[package]] +name = "windows-collections" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3beeceb5e5cfd9eb1d76b381630e82c4241ccd0d27f1a39ed41b2760b255c5e8" +dependencies = [ + "windows-core", +] + +[[package]] +name = "windows-core" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link 0.1.3", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-future" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc6a41e98427b19fe4b73c550f060b59fa592d7d686537eebf9385621bfbad8e" +dependencies = [ + "windows-core", + "windows-link 0.1.3", + "windows-threading", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-link" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-numerics" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1" +dependencies = [ + "windows-core", + "windows-link 0.1.3", +] + +[[package]] +name = "windows-result" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6" +dependencies = [ + "windows-link 0.1.3", +] + +[[package]] +name = "windows-strings" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57" +dependencies = [ + "windows-link 0.1.3", +] + [[package]] name = "windows-sys" version = "0.60.2" @@ -875,7 +1041,7 @@ version = "0.61.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -884,7 +1050,7 @@ version = "0.53.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" dependencies = [ - "windows-link", + "windows-link 0.2.1", "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", @@ -895,6 +1061,15 @@ dependencies = [ "windows_x86_64_msvc", ] +[[package]] +name = "windows-threading" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b66463ad2e0ea3bbf808b7f1d371311c80e115c0b71d60efc142cafbcfb057a6" +dependencies = [ + "windows-link 0.1.3", +] + [[package]] name = "windows_aarch64_gnullvm" version = "0.53.1" @@ -962,5 +1137,6 @@ dependencies = [ "serde", "serde_json", "sysctl", + "sysinfo", "tokio", ] diff --git a/rust/Cargo.toml b/rust/Cargo.toml index edafc77..21c6a3b 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -24,3 +24,4 @@ pretty_env_logger = "0.5.0" nix = { version = "0.30.1", features = ["signal"] } iptables = { git = "https://github.com/sqlerrorthing/rust-iptables.git", branch = "feat/add-android" } getset = "0.1.6" +sysinfo = "0.37.2" diff --git a/rust/crates/zaprett/Cargo.toml b/rust/crates/zaprett/Cargo.toml index 30483b7..5e309df 100644 --- a/rust/crates/zaprett/Cargo.toml +++ b/rust/crates/zaprett/Cargo.toml @@ -20,4 +20,5 @@ pretty_env_logger = { workspace = true } log = { workspace = true } nix = { workspace = true, features = ["user"] } iptables = { workspace = true } -getset = { workspace = true } \ No newline at end of file +getset = { workspace = true } +sysinfo = { workspace = true } diff --git a/rust/crates/zaprett/src/cli/commands.rs b/rust/crates/zaprett/src/cli/commands.rs index f42a8e4..ed97180 100644 --- a/rust/crates/zaprett/src/cli/commands.rs +++ b/rust/crates/zaprett/src/cli/commands.rs @@ -1,7 +1,7 @@ +use crate::service::{restart_service, service_status, start_service, stop_service}; use crate::{bin_version, get_autostart, module_version, set_autostart}; use clap::Subcommand; use log::error; -use crate::service::{restart_service, service_status, start_service, stop_service}; #[derive(Subcommand)] pub enum Command { @@ -40,23 +40,23 @@ impl Command { Command::Start => return start_service().await, Command::Stop => { let _ = stop_service().await; - }, + } Command::Restart => return restart_service().await, Command::Status => { println!( "zaprett is {}", - if service_status().await { + if service_status().await? { "working" } else { "stopped" } ); - }, + } Command::SetAutostart { autostart } => { if let Err(err) = set_autostart(autostart).await { error!("Failed to set auto start: {err}") } - }, + } Command::GetAutostart => get_autostart(), Command::ModuleVersion => module_version(), Command::BinaryVersion => bin_version(), @@ -64,4 +64,4 @@ impl Command { Ok(()) } -} \ No newline at end of file +} diff --git a/rust/crates/zaprett/src/lib.rs b/rust/crates/zaprett/src/lib.rs index bb11f3a..96f0e38 100644 --- a/rust/crates/zaprett/src/lib.rs +++ b/rust/crates/zaprett/src/lib.rs @@ -1,20 +1,20 @@ pub mod cli; pub mod config; +mod daemon; pub mod iptables_rust; mod service; -mod daemon; +use anyhow::bail; +use ini::Ini; +use libnfqws::nfqws_main; use std::error; use std::ffi::CString; use std::os::raw::c_char; use std::path::Path; use std::sync::LazyLock; -use anyhow::bail; -use ini::Ini; -use tokio::{fs, task}; use tokio::fs::File; -use tokio::io::{copy, AsyncWriteExt}; -use libnfqws::nfqws_main; +use tokio::io::{AsyncWriteExt, copy}; +use tokio::{fs, task}; pub static MODULE_PATH: LazyLock<&Path> = LazyLock::new(|| Path::new("/data/adb/modules/zaprett")); pub static ZAPRETT_DIR_PATH: LazyLock<&Path> = @@ -72,15 +72,13 @@ pub async fn merge_files( .await .map_err(|e| format!("Failed to open {}: {}", input_path.display(), e))?; - copy(&mut input_file, &mut output_file) - .await - .map_err(|e| { - format!( - "Failed to write contents of {}: {}", - input_path.display(), - e - ) - })?; + copy(&mut input_file, &mut output_file).await.map_err(|e| { + format!( + "Failed to write contents of {}: {}", + input_path.display(), + e + ) + })?; } output_file.flush().await?; @@ -88,7 +86,7 @@ pub async fn merge_files( } async fn run_nfqws(args_str: &str) -> anyhow::Result<()> { - if service::service_status().await { + if service::service_status().await? { bail!("nfqws already started!"); } @@ -112,7 +110,7 @@ async fn run_nfqws(args_str: &str) -> anyhow::Result<()> { nfqws_main(c_args.len() as libc::c_int, ptrs.as_mut_ptr() as *mut _); } }) - .await?; + .await?; Ok(()) } diff --git a/rust/crates/zaprett/src/service.rs b/rust/crates/zaprett/src/service.rs index 13678f1..0600054 100644 --- a/rust/crates/zaprett/src/service.rs +++ b/rust/crates/zaprett/src/service.rs @@ -8,7 +8,9 @@ use nix::sys::signal::{Signal, kill}; use nix::unistd::{Pid, Uid}; use regex::Regex; use std::borrow::Cow; +use std::path::Path; use sysctl::Sysctl; +use sysinfo::{Pid as SysPid, System}; use tokio::fs; use tokio::fs::File; use tokio::io::AsyncReadExt; @@ -82,16 +84,33 @@ pub async fn stop_service() -> anyhow::Result<()> { } pub async fn restart_service() -> anyhow::Result<()> { + if !Uid::effective().is_root() { + bail!("Running not from root, exiting"); + }; stop_service().await?; start_service().await?; info!("zaprett service restarted!"); Ok(()) } -pub async fn service_status() -> bool { - fs::read_to_string(MODULE_PATH.join("tmp/pid.lock")) - .await - .ok() - .and_then(|pid_str| pid_str.trim().parse::().ok()) - .is_some() +pub async fn service_status() -> anyhow::Result { + if !Uid::effective().is_root() { + bail!("Running not from root, exiting"); + }; + + let pid_i32 = match fs::read_to_string(Path::new(*MODULE_PATH).join("tmp/pid.lock")).await { + Ok(s) => match s.trim().parse::() { + Ok(pid) => pid, + Err(_) => return Ok(false), + }, + Err(_) => return Ok(false), + }; + let pid = SysPid::from(pid_i32 as usize); + let system = System::new_all(); + if let Some(process) = system.process(pid) { + if process.name() == "zaprett" { + return Ok(true); + } + } + Ok(false) }