From e91cb00eeced954f9c963c8f594684a3fc1f57fe Mon Sep 17 00:00:00 2001 From: sqlerrorthing <148702857+sqlerrorthing@users.noreply.github.com> Date: Wed, 29 Oct 2025 05:00:27 +0800 Subject: [PATCH] spawn blocking --- rust/Cargo.lock | 104 ----------------------------------------------- rust/Cargo.toml | 1 - rust/build.rs | 2 +- rust/src/main.rs | 32 +++++++++------ 4 files changed, 21 insertions(+), 118 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index b971f98..8a01133 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -88,26 +88,6 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" -[[package]] -name = "bindgen" -version = "0.72.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "993776b509cfb49c750f11b8f07a46fa23e0a1386ffc01fb1e7d343efc387895" -dependencies = [ - "bitflags", - "cexpr", - "clang-sys", - "itertools", - "log", - "prettyplease", - "proc-macro2", - "quote", - "regex", - "rustc-hash", - "shlex", - "syn", -] - [[package]] name = "bitflags" version = "2.10.0" @@ -142,15 +122,6 @@ dependencies = [ "shlex", ] -[[package]] -name = "cexpr" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" -dependencies = [ - "nom", -] - [[package]] name = "cfg-if" version = "1.0.4" @@ -168,17 +139,6 @@ dependencies = [ "windows-link", ] -[[package]] -name = "clang-sys" -version = "1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" -dependencies = [ - "glob", - "libc", - "libloading", -] - [[package]] name = "clap" version = "4.5.50" @@ -275,12 +235,6 @@ dependencies = [ "const-random", ] -[[package]] -name = "either" -version = "1.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" - [[package]] name = "enum-as-inner" version = "0.6.1" @@ -330,12 +284,6 @@ dependencies = [ "wasi", ] -[[package]] -name = "glob" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280" - [[package]] name = "hashbrown" version = "0.14.5" @@ -394,15 +342,6 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" -[[package]] -name = "itertools" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" -dependencies = [ - "either", -] - [[package]] name = "itoa" version = "1.0.15" @@ -431,16 +370,6 @@ version = "0.2.177" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" -[[package]] -name = "libloading" -version = "0.8.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" -dependencies = [ - "cfg-if", - "windows-link", -] - [[package]] name = "linux-raw-sys" version = "0.11.0" @@ -469,12 +398,6 @@ version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" -[[package]] -name = "minimal-lexical" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" - [[package]] name = "miniz_oxide" version = "0.8.9" @@ -496,16 +419,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "nom" -version = "7.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" -dependencies = [ - "memchr", - "minimal-lexical", -] - [[package]] name = "num-traits" version = "0.2.19" @@ -566,16 +479,6 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" -[[package]] -name = "prettyplease" -version = "0.2.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" -dependencies = [ - "proc-macro2", - "syn", -] - [[package]] name = "proc-macro2" version = "1.0.103" @@ -666,12 +569,6 @@ dependencies = [ "ordered-multimap", ] -[[package]] -name = "rustc-hash" -version = "2.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" - [[package]] name = "rustix" version = "1.1.2" @@ -1123,7 +1020,6 @@ name = "zaprett" version = "0.0.1" dependencies = [ "anyhow", - "bindgen", "cc", "clap", "iptables", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 72f1131..1ef3da4 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -19,5 +19,4 @@ once_cell = "1.21.3" [build-dependencies] cc = "1.2.43" -bindgen = "0.72.1" once_cell = "1.21.3" diff --git a/rust/build.rs b/rust/build.rs index 4df33a8..d092a3c 100644 --- a/rust/build.rs +++ b/rust/build.rs @@ -1,6 +1,6 @@ +use once_cell::sync::Lazy; use std::env; use std::path::{Path, PathBuf}; -use once_cell::sync::Lazy; static NFQ: Lazy = Lazy::new(|| { let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); diff --git a/rust/src/main.rs b/rust/src/main.rs index ca3c27c..3ae925e 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,5 +1,7 @@ mod libnfqws; +use crate::libnfqws::nfqws_main; +use anyhow::bail; use clap::{ArgAction, Parser, Subcommand, builder::BoolishValueParser}; use ini::Ini; use procfs::process::all_processes; @@ -13,7 +15,7 @@ use std::os::raw::c_char; use std::sync::atomic::{AtomicBool, Ordering}; use std::{fs, path::Path}; use sysctl::{CtlValue, Sysctl}; -use crate::libnfqws::nfqws_main; +use tokio::task; #[derive(Parser)] #[command(version)] @@ -82,7 +84,7 @@ async fn main() { Some(Commands::ModuleVer) => module_version(), Some(Commands::BinVer) => todo!(), //bin_version(), //None => println!("zaprett installed. Join us: t.me/zaprett_module"), - None => run_nfqws("-v").await, + None => run_nfqws("-v").await.unwrap(), } tokio::signal::ctrl_c().await.unwrap(); } @@ -299,14 +301,15 @@ fn clear_iptables_rules() { ) .unwrap(); } -async fn run_nfqws(args_str: &str) { + +async fn run_nfqws(args_str: &str) -> anyhow::Result<()> { static RUNNING: AtomicBool = AtomicBool::new(false); - if RUNNING.load(Ordering::SeqCst) { - panic!("Thread with nfqws already started!"); + if RUNNING.swap(true, Ordering::SeqCst) { + bail!("nfqws already started!"); } - let mut args: Vec<&str> = vec!["nfqws"]; + let mut args = vec!["nfqws"]; if args_str.trim().is_empty() { args.push("-v"); @@ -316,12 +319,17 @@ async fn run_nfqws(args_str: &str) { } } let c_args: Vec = args.iter().map(|&arg| CString::new(arg).unwrap()).collect(); - let argv: Vec<*const c_char> = c_args.iter().map(|arg| arg.as_ptr()).collect(); - RUNNING.store(true, Ordering::SeqCst); - // tokio::task::spawn_blocking(move || ); + let _ = task::spawn_blocking(move || unsafe { + nfqws_main( + c_args.len() as libc::c_int, + c_args + .iter() + .map(|arg| arg.as_ptr()) + .collect::>() + .as_ptr(), + ); + }); - unsafe { - nfqws_main(argv.len() as libc::c_int, argv.as_ptr()); - } + Ok(()) }