fix service status

This commit is contained in:
CherretGit
2025-11-01 19:56:08 +07:00
parent 8caa7ff696
commit 4f6a80a8ee
6 changed files with 229 additions and 34 deletions

184
rust/Cargo.lock generated
View File

@@ -387,7 +387,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"windows-link", "windows-link 0.2.1",
] ]
[[package]] [[package]]
@@ -460,6 +460,34 @@ dependencies = [
"minimal-lexical", "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]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.21.3" version = "1.21.3"
@@ -502,7 +530,7 @@ dependencies = [
"libc", "libc",
"redox_syscall", "redox_syscall",
"smallvec", "smallvec",
"windows-link", "windows-link 0.2.1",
] ]
[[package]] [[package]]
@@ -751,6 +779,20 @@ dependencies = [
"walkdir", "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]] [[package]]
name = "termcolor" name = "termcolor"
version = "1.4.1" 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" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" 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]] [[package]]
name = "winapi-util" name = "winapi-util"
version = "0.1.11" version = "0.1.11"
@@ -854,12 +912,120 @@ dependencies = [
"windows-sys 0.61.2", "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]] [[package]]
name = "windows-link" name = "windows-link"
version = "0.2.1" version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" 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]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.60.2" version = "0.60.2"
@@ -875,7 +1041,7 @@ version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [ dependencies = [
"windows-link", "windows-link 0.2.1",
] ]
[[package]] [[package]]
@@ -884,7 +1050,7 @@ version = "0.53.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3"
dependencies = [ dependencies = [
"windows-link", "windows-link 0.2.1",
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm",
"windows_aarch64_msvc", "windows_aarch64_msvc",
"windows_i686_gnu", "windows_i686_gnu",
@@ -895,6 +1061,15 @@ dependencies = [
"windows_x86_64_msvc", "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]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.53.1" version = "0.53.1"
@@ -962,5 +1137,6 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"sysctl", "sysctl",
"sysinfo",
"tokio", "tokio",
] ]

View File

@@ -24,3 +24,4 @@ pretty_env_logger = "0.5.0"
nix = { version = "0.30.1", features = ["signal"] } nix = { version = "0.30.1", features = ["signal"] }
iptables = { git = "https://github.com/sqlerrorthing/rust-iptables.git", branch = "feat/add-android" } iptables = { git = "https://github.com/sqlerrorthing/rust-iptables.git", branch = "feat/add-android" }
getset = "0.1.6" getset = "0.1.6"
sysinfo = "0.37.2"

View File

@@ -21,3 +21,4 @@ log = { workspace = true }
nix = { workspace = true, features = ["user"] } nix = { workspace = true, features = ["user"] }
iptables = { workspace = true } iptables = { workspace = true }
getset = { workspace = true } getset = { workspace = true }
sysinfo = { workspace = true }

View File

@@ -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 crate::{bin_version, get_autostart, module_version, set_autostart};
use clap::Subcommand; use clap::Subcommand;
use log::error; use log::error;
use crate::service::{restart_service, service_status, start_service, stop_service};
#[derive(Subcommand)] #[derive(Subcommand)]
pub enum Command { pub enum Command {
@@ -40,23 +40,23 @@ impl Command {
Command::Start => return start_service().await, Command::Start => return start_service().await,
Command::Stop => { Command::Stop => {
let _ = stop_service().await; let _ = stop_service().await;
}, }
Command::Restart => return restart_service().await, Command::Restart => return restart_service().await,
Command::Status => { Command::Status => {
println!( println!(
"zaprett is {}", "zaprett is {}",
if service_status().await { if service_status().await? {
"working" "working"
} else { } else {
"stopped" "stopped"
} }
); );
}, }
Command::SetAutostart { autostart } => { Command::SetAutostart { autostart } => {
if let Err(err) = set_autostart(autostart).await { if let Err(err) = set_autostart(autostart).await {
error!("Failed to set auto start: {err}") error!("Failed to set auto start: {err}")
} }
}, }
Command::GetAutostart => get_autostart(), Command::GetAutostart => get_autostart(),
Command::ModuleVersion => module_version(), Command::ModuleVersion => module_version(),
Command::BinaryVersion => bin_version(), Command::BinaryVersion => bin_version(),

View File

@@ -1,20 +1,20 @@
pub mod cli; pub mod cli;
pub mod config; pub mod config;
mod daemon;
pub mod iptables_rust; pub mod iptables_rust;
mod service; mod service;
mod daemon;
use anyhow::bail;
use ini::Ini;
use libnfqws::nfqws_main;
use std::error; use std::error;
use std::ffi::CString; use std::ffi::CString;
use std::os::raw::c_char; use std::os::raw::c_char;
use std::path::Path; use std::path::Path;
use std::sync::LazyLock; use std::sync::LazyLock;
use anyhow::bail;
use ini::Ini;
use tokio::{fs, task};
use tokio::fs::File; use tokio::fs::File;
use tokio::io::{copy, AsyncWriteExt}; use tokio::io::{AsyncWriteExt, copy};
use libnfqws::nfqws_main; use tokio::{fs, task};
pub static MODULE_PATH: LazyLock<&Path> = LazyLock::new(|| Path::new("/data/adb/modules/zaprett")); pub static MODULE_PATH: LazyLock<&Path> = LazyLock::new(|| Path::new("/data/adb/modules/zaprett"));
pub static ZAPRETT_DIR_PATH: LazyLock<&Path> = pub static ZAPRETT_DIR_PATH: LazyLock<&Path> =
@@ -72,9 +72,7 @@ pub async fn merge_files(
.await .await
.map_err(|e| format!("Failed to open {}: {}", input_path.display(), e))?; .map_err(|e| format!("Failed to open {}: {}", input_path.display(), e))?;
copy(&mut input_file, &mut output_file) copy(&mut input_file, &mut output_file).await.map_err(|e| {
.await
.map_err(|e| {
format!( format!(
"Failed to write contents of {}: {}", "Failed to write contents of {}: {}",
input_path.display(), input_path.display(),
@@ -88,7 +86,7 @@ pub async fn merge_files(
} }
async fn run_nfqws(args_str: &str) -> anyhow::Result<()> { async fn run_nfqws(args_str: &str) -> anyhow::Result<()> {
if service::service_status().await { if service::service_status().await? {
bail!("nfqws already started!"); bail!("nfqws already started!");
} }

View File

@@ -8,7 +8,9 @@ use nix::sys::signal::{Signal, kill};
use nix::unistd::{Pid, Uid}; use nix::unistd::{Pid, Uid};
use regex::Regex; use regex::Regex;
use std::borrow::Cow; use std::borrow::Cow;
use std::path::Path;
use sysctl::Sysctl; use sysctl::Sysctl;
use sysinfo::{Pid as SysPid, System};
use tokio::fs; use tokio::fs;
use tokio::fs::File; use tokio::fs::File;
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
@@ -82,16 +84,33 @@ pub async fn stop_service() -> anyhow::Result<()> {
} }
pub async fn restart_service() -> anyhow::Result<()> { pub async fn restart_service() -> anyhow::Result<()> {
if !Uid::effective().is_root() {
bail!("Running not from root, exiting");
};
stop_service().await?; stop_service().await?;
start_service().await?; start_service().await?;
info!("zaprett service restarted!"); info!("zaprett service restarted!");
Ok(()) Ok(())
} }
pub async fn service_status() -> bool { pub async fn service_status() -> anyhow::Result<bool> {
fs::read_to_string(MODULE_PATH.join("tmp/pid.lock")) if !Uid::effective().is_root() {
.await bail!("Running not from root, exiting");
.ok() };
.and_then(|pid_str| pid_str.trim().parse::<i32>().ok())
.is_some() let pid_i32 = match fs::read_to_string(Path::new(*MODULE_PATH).join("tmp/pid.lock")).await {
Ok(s) => match s.trim().parse::<i32>() {
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)
} }