From 227db9b7fd0dff8d5cc8a87e7adb01923460bd3a Mon Sep 17 00:00:00 2001 From: sqlerrorthing <148702857+sqlerrorthing@users.noreply.github.com> Date: Wed, 29 Oct 2025 04:52:48 +0800 Subject: [PATCH] right linking --- .gitmodules | 3 ++ rust/CMakeLists.txt | 19 ------- rust/Cargo.lock | 122 ++++++++++++++++++++++++++++++++++++++----- rust/Cargo.toml | 5 +- rust/build.rs | 22 +++++--- rust/libs/zapret | 1 + rust/src/libnfqws.rs | 9 ++++ rust/src/main.rs | 17 +++--- 8 files changed, 149 insertions(+), 49 deletions(-) create mode 100644 .gitmodules delete mode 100644 rust/CMakeLists.txt create mode 160000 rust/libs/zapret create mode 100644 rust/src/libnfqws.rs diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..faf5539 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "rust/libs/zapret"] + path = rust/libs/zapret + url = https://github.com/bol-van/zapret.git diff --git a/rust/CMakeLists.txt b/rust/CMakeLists.txt deleted file mode 100644 index 03334f4..0000000 --- a/rust/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -cmake_minimum_required(VERSION 3.10) -project(nfqws C) - -set(CMAKE_C_STANDARD 99) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Os -std=gnu99") - -file(GLOB SRC_FILES - "${CMAKE_CURRENT_SOURCE_DIR}/zapret/nfq/*.c" - "${CMAKE_CURRENT_SOURCE_DIR}/zapret/nfq/crypto/*.c" -) - -add_library(nfqws STATIC ${SRC_FILES}) - -target_link_libraries(nfqws - z - netfilter_queue - nfnetlink - mnl -) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 9f610c4..b971f98 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -88,6 +88,26 @@ 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" @@ -122,6 +142,15 @@ 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" @@ -139,6 +168,17 @@ 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" @@ -179,15 +219,6 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" -[[package]] -name = "cmake" -version = "0.1.54" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7caa3f9de89ddbe2c607f4101924c5abec803763ae9534e4f4d7d8f84aa81f0" -dependencies = [ - "cc", -] - [[package]] name = "colorchoice" version = "1.0.4" @@ -244,6 +275,12 @@ 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" @@ -263,7 +300,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -293,6 +330,12 @@ 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" @@ -351,6 +394,15 @@ 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" @@ -379,6 +431,16 @@ 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" @@ -407,6 +469,12 @@ 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" @@ -428,6 +496,16 @@ 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" @@ -488,6 +566,16 @@ 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" @@ -578,6 +666,12 @@ 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" @@ -588,7 +682,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -879,7 +973,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -1029,10 +1123,12 @@ name = "zaprett" version = "0.0.1" dependencies = [ "anyhow", + "bindgen", + "cc", "clap", - "cmake", "iptables", "libc", + "once_cell", "procfs", "regex", "rust-ini", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index c2d0713..72f1131 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -15,6 +15,9 @@ serde = { version = "1.0.228", features = ["derive"] } serde_json = "1.0.145" sysctl = "0.7.1" tokio = { version = "1.48.0", features = ["full"] } +once_cell = "1.21.3" [build-dependencies] -cmake = "0.1.49" +cc = "1.2.43" +bindgen = "0.72.1" +once_cell = "1.21.3" diff --git a/rust/build.rs b/rust/build.rs index 417ebe5..4df33a8 100644 --- a/rust/build.rs +++ b/rust/build.rs @@ -1,12 +1,18 @@ -use cmake::Config; +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(); + Path::new(&manifest_dir).join("libs/zapret/nfq") +}); fn main() { - let dst = Config::new(".").build_target("nfqws").build(); - let lib_path = dst.join("build"); - println!("cargo:rustc-link-search=native={}", lib_path.display()); + cc::Build::new() + .file(NFQ.join("nfqws.c")) + .include(&*NFQ) + .compile("libnfqws.a"); + println!("cargo:rustc-link-lib=static=nfqws"); - println!("cargo:rustc-link-lib=z"); - println!("cargo:rustc-link-lib=netfilter_queue"); - println!("cargo:rustc-link-lib=nfnetlink"); - println!("cargo:rustc-link-lib=mnl"); + println!("cargo:rerun-if-changed={}", NFQ.display()); } diff --git a/rust/libs/zapret b/rust/libs/zapret new file mode 160000 index 0000000..1408c38 --- /dev/null +++ b/rust/libs/zapret @@ -0,0 +1 @@ +Subproject commit 1408c38522193e6debdf7491a6cd7ee44a8fce05 diff --git a/rust/src/libnfqws.rs b/rust/src/libnfqws.rs new file mode 100644 index 0000000..32af99c --- /dev/null +++ b/rust/src/libnfqws.rs @@ -0,0 +1,9 @@ +// Raw FFI bindings to libnfqws.a + +use std::ffi::c_char; + +#[link(name = "nfqws", kind = "static")] +unsafe extern "C" { + #[link_name = "main"] + pub fn nfqws_main(argc: libc::c_int, argv: *const *const c_char) -> libc::c_int; +} diff --git a/rust/src/main.rs b/rust/src/main.rs index a114f17..ca3c27c 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,18 +1,19 @@ -use clap::builder::StringValueParser; +mod libnfqws; + use clap::{ArgAction, Parser, Subcommand, builder::BoolishValueParser}; use ini::Ini; use procfs::process::all_processes; use regex::Regex; use serde::{Deserialize, Serialize}; -use std::cmp::Ordering; use std::ffi::CString; use std::fs::File; use std::io::BufReader; use std::io::{Read, Write}; use std::os::raw::c_char; -use std::sync::atomic::AtomicBool; +use std::sync::atomic::{AtomicBool, Ordering}; use std::{fs, path::Path}; use sysctl::{CtlValue, Sysctl}; +use crate::libnfqws::nfqws_main; #[derive(Parser)] #[command(version)] @@ -67,10 +68,6 @@ struct Config { whitelist: Vec, blacklist: Vec, } -#[link(name = "nfqws", kind = "static")] -unsafe extern "C" { - fn nfqws_main(argc: libc::c_int, argv: *const *const c_char) -> libc::c_int; -} #[tokio::main] async fn main() { @@ -322,5 +319,9 @@ async fn run_nfqws(args_str: &str) { let argv: Vec<*const c_char> = c_args.iter().map(|arg| arg.as_ptr()).collect(); RUNNING.store(true, Ordering::SeqCst); - tokio::task::spawn_blocking(|| nfqws_main(argv.len() as libc::c_int, argv.as_ptr())); + // tokio::task::spawn_blocking(move || ); + + unsafe { + nfqws_main(argv.len() as libc::c_int, argv.as_ptr()); + } }