11 Commits

Author SHA1 Message Date
sqlerrorthing
0f5492a29a move zapret from libs/ 2025-10-29 07:50:10 +08:00
sqlerrorthing
a16796a699 recode 2025-10-29 07:46:08 +08:00
sqlerrorthing
19a8ccd384 generate bindings 2025-10-29 07:26:32 +08:00
sqlerrorthing
58d0fdbab3 add rerun 2025-10-29 07:00:49 +08:00
sqlerrorthing
3c0488f629 working! 2025-10-29 06:58:55 +08:00
sqlerrorthing
3e009479c8 idk rename main 2025-10-29 06:08:28 +08:00
sqlerrorthing
3b66cc8136 suppress fucking C warnings 2025-10-29 05:26:09 +08:00
sqlerrorthing
db724d8667 Merge remote-tracking branch 'upstream/rust' into fix/shitcode
# Conflicts:
#	rust/src/main.rs
2025-10-29 05:20:04 +08:00
sqlerrorthing
a1a2961628 fire and forget 2025-10-29 05:05:44 +08:00
sqlerrorthing
e91cb00eec spawn blocking 2025-10-29 05:00:27 +08:00
sqlerrorthing
227db9b7fd right linking 2025-10-29 04:52:48 +08:00
11 changed files with 290 additions and 107 deletions

9
.gitmodules vendored Normal file
View File

@@ -0,0 +1,9 @@
[submodule "rust/libs/zapret"]
path = rust/libs/zapret
url = https://github.com/bol-van/zapret.git
[submodule "rust/crates/libnfqws/libs/zapret"]
path = rust/crates/libnfqws/libs/zapret
url = https://github.com/bol-van/zapret.git
[submodule "rust/crates/libnfqws/zapret"]
path = rust/crates/libnfqws/zapret
url = https://github.com/bol-van/zapret.git

View File

@@ -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
)

180
rust/Cargo.lock generated
View File

@@ -10,9 +10,9 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "aho-corasick"
version = "1.1.3"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
dependencies = [
"memchr",
]
@@ -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"
@@ -359,9 +411,9 @@ checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
[[package]]
name = "js-sys"
version = "0.3.81"
version = "0.3.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec48937a97411dcb524a265206ccd4c90bb711fca92b2792c407f268825b9305"
checksum = "b011eec8cc36da2aab2d5cff675ec18454fad408585853910a202391cf9f8e65"
dependencies = [
"once_cell",
"wasm-bindgen",
@@ -379,6 +431,26 @@ 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 = "libnfqws"
version = "0.0.1"
dependencies = [
"bindgen",
"cc",
"glob",
"once_cell",
]
[[package]]
name = "linux-raw-sys"
version = "0.11.0"
@@ -387,11 +459,10 @@ checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039"
[[package]]
name = "lock_api"
version = "0.4.13"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765"
checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965"
dependencies = [
"autocfg",
"scopeguard",
]
@@ -407,6 +478,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 +505,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"
@@ -461,9 +548,9 @@ dependencies = [
[[package]]
name = "parking_lot"
version = "0.12.4"
version = "0.12.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70d58bf43669b5795d1576d0641cfb6fbb2057bf629506267a92807158584a13"
checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a"
dependencies = [
"lock_api",
"parking_lot_core",
@@ -488,6 +575,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 +675,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 +691,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -816,9 +919,9 @@ checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "wasm-bindgen"
version = "0.2.104"
version = "0.2.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1da10c01ae9f1ae40cbfac0bac3b1e724b320abfcf52229f80b547c0d250e2d"
checksum = "da95793dfc411fbbd93f5be7715b0578ec61fe87cb1a42b12eb625caa5c5ea60"
dependencies = [
"cfg-if",
"once_cell",
@@ -827,25 +930,11 @@ dependencies = [
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "671c9a5a66f49d8a47345ab942e2cb93c7d1d0339065d4f8139c486121b43b19"
dependencies = [
"bumpalo",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.104"
version = "0.2.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ca60477e4c59f5f2986c50191cd972e3a50d8a95603bc9434501cf156a9a119"
checksum = "04264334509e04a7bf8690f2384ef5265f05143a4bff3889ab7a3269adab59c2"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@@ -853,22 +942,22 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.104"
version = "0.2.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f07d2f20d4da7b26400c9f4a0511e6e0345b040694e8a75bd41d578fa4421d7"
checksum = "420bc339d9f322e562942d52e115d57e950d12d88983a14c79b86859ee6c7ebc"
dependencies = [
"bumpalo",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.104"
version = "0.2.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bad67dc8b2a1a6e5448428adec4c3e84c43e561d8c9ee8a9e5aabeb193ec41d1"
checksum = "76f218a38c84bcb33c25ec7059b07847d465ce0e0a76b995e134a45adcb6af76"
dependencies = [
"unicode-ident",
]
@@ -879,7 +968,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]]
@@ -1030,9 +1119,10 @@ version = "0.0.1"
dependencies = [
"anyhow",
"clap",
"cmake",
"iptables",
"libc",
"libnfqws",
"once_cell",
"procfs",
"regex",
"rust-ini",

View File

@@ -1,9 +1,13 @@
[package]
name = "zaprett"
[workspace.package]
version = "0.0.1"
edition = "2024"
repository = "https://github.com/egor-white/zaprett"
[dependencies]
[workspace]
members = ["crates/*"]
resolver = "3"
[workspace.dependencies]
anyhow = "1.0.100"
clap = { version = "4.5.50", features = ["derive"] }
iptables = "0.6.0"
@@ -15,6 +19,5 @@ 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"

View File

@@ -1,12 +0,0 @@
use cmake::Config;
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());
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");
}

View File

@@ -0,0 +1,11 @@
[package]
name = "libnfqws"
version.workspace = true
edition.workspace = true
repository.workspace = true
[build-dependencies]
cc = "1.2.43"
once_cell = "1.21.3"
glob = "0.3.3"
bindgen = "0.72.1"

View File

@@ -0,0 +1,60 @@
use once_cell::sync::Lazy;
use std::env;
use std::path::{Path, PathBuf};
macro_rules! rel_manifest_path {
($name:ident, $path:expr) => {
static $name: Lazy<PathBuf> = Lazy::new(|| {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
Path::new(&manifest_dir).join($path)
});
};
}
rel_manifest_path!(NFQ, "zapret/nfq");
rel_manifest_path!(NFQ_CRYPTO, "zapret/nfq/crypto");
fn main() {
cc::Build::new()
.files(glob::glob(&format!("{}/*.c", NFQ.display()))
.unwrap()
.filter_map(Result::ok))
.files(glob::glob(&format!("{}/*.c", NFQ_CRYPTO.display()))
.unwrap()
.filter_map(Result::ok))
.include(&*NFQ)
.include(&*NFQ_CRYPTO)
.flag("-w")
.define("main", "nfqws_main")
.compile("libnfqws.a");
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:rustc-link-lib=static=nfqws");
println!("cargo:rerun-if-changed={}", NFQ.display());
println!("cargo:rerun-if-changed={}", NFQ_CRYPTO.display());
let mut builder = bindgen::Builder::default();
for header in glob::glob(&format!("{}/*.h", NFQ.display()))
.unwrap()
.filter_map(Result::ok)
{
builder = builder.header(header.to_string_lossy());
}
builder = builder.clang_arg("-Dmain=nfqws_main");
let bindings = builder
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate libnfqws");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("libnfqws.rs"))
.expect("Couldn't write libnfqws");
}

View File

@@ -0,0 +1,2 @@
#![allow(warnings)]
include!(concat!(env!("OUT_DIR"), "/libnfqws.rs"));

View File

@@ -0,0 +1,20 @@
[package]
name = "zaprett"
version.workspace = true
edition.workspace = true
repository.workspace = true
[dependencies]
anyhow = { workspace = true }
clap = { workspace = true }
iptables = { workspace = true }
libc = { workspace = true }
procfs = { workspace = true }
regex = { workspace = true }
rust-ini = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
sysctl ={ workspace = true }
tokio = { workspace = true }
once_cell = { workspace = true }
libnfqws = { path = "../libnfqws" }

View File

@@ -1,3 +1,6 @@
// mod libnfqws;
use anyhow::bail;
use clap::{ArgAction, Parser, Subcommand, builder::BoolishValueParser};
use ini::Ini;
use procfs::process::all_processes;
@@ -8,9 +11,11 @@ use std::fs::File;
use std::io::BufReader;
use std::io::{Read, Write};
use std::os::raw::c_char;
use std::thread;
use std::sync::atomic::{AtomicBool, Ordering};
use std::{fs, path::Path};
use sysctl::{CtlValue, Sysctl};
use tokio::task;
use libnfqws::nfqws_main;
#[derive(Parser)]
#[command(version)]
@@ -39,7 +44,7 @@ enum Commands {
value_name = "boolean",
action = ArgAction::Set,
value_parser = BoolishValueParser::new()
)]
)]
autostart: bool,
},
@@ -65,10 +70,6 @@ struct Config {
whitelist: Vec<String>,
blacklist: Vec<String>,
}
#[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() {
@@ -83,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"),
None => run_nfqws("--version".to_string()).await.unwrap(),
}
tokio::signal::ctrl_c().await.unwrap();
}
@@ -128,7 +129,7 @@ fn start_service() {
config.active_lists,
"/data/adb/modules/zaprett/tmp/hostlist",
)
.unwrap();
.unwrap();
merge_files(config.active_ipsets, "/data/adb/modules/zaprett/tmp/ipset").unwrap();
let hosts = String::from("--hostlist=/data/adb/modules/zaprett/tmp/hostlist");
@@ -146,12 +147,12 @@ fn start_service() {
config.active_exclude_lists,
"/data/adb/modules/zaprett/tmp/hostlist-exclude",
)
.unwrap();
.unwrap();
merge_files(
config.active_exclude_ipsets,
"/data/adb/modules/zaprett/tmp/ipset-exclude",
)
.unwrap();
.unwrap();
let hosts =
String::from("--hostlist-exclude=/data/adb/modules/zaprett/tmp/hostlist-exclude");
@@ -263,20 +264,20 @@ fn setup_iptables_rules() {
"-j NFQUEUE --queue-num 200 --queue-bypass",
1,
)
.unwrap();
.unwrap();
ipt.insert(
"mangle",
"PREROUTING",
"-j NFQUEUE --queue-num 200 --queue-bypass",
1,
)
.unwrap();
.unwrap();
ipt.append(
"filter",
"FORWARD",
"-j NFQUEUE --queue-num 200 --queue-bypass",
)
.unwrap();
.unwrap();
}
fn clear_iptables_rules() {
let ipt = iptables::new(false).unwrap();
@@ -286,34 +287,51 @@ fn clear_iptables_rules() {
"POSTROUTING",
"-j NFQUEUE --queue-num 200 --queue-bypass",
)
.unwrap();
.unwrap();
ipt.delete(
"mangle",
"PREROUTING",
"-j NFQUEUE --queue-num 200 --queue-bypass",
)
.unwrap();
.unwrap();
ipt.delete(
"filter",
"FORWARD",
"-j NFQUEUE --queue-num 200 --queue-bypass",
)
.unwrap();
.unwrap();
}
fn run_nfqws(args_str: &str) {
let mut args: Vec<&str> = vec!["nfqws"];
async fn run_nfqws(args_str: String) -> anyhow::Result<()> {
static RUNNING: AtomicBool = AtomicBool::new(false);
if RUNNING.swap(true, Ordering::SeqCst) {
bail!("nfqws already started!");
}
let mut args = vec!["nfqws".to_string()];
if args_str.trim().is_empty() {
args.push("-v");
args.push("-v".to_string());
} else {
for token in args_str.trim().split_whitespace() {
args.push(token);
}
args.extend(args_str.trim().split_whitespace().map(String::from));
}
let c_args: Vec<CString> = args.iter().map(|&arg| CString::new(arg).unwrap()).collect();
let argv: Vec<*const c_char> = c_args.iter().map(|arg| arg.as_ptr()).collect();
thread::spawn(|| unsafe {
nfqws_main(argv.len() as libc::c_int, argv.as_ptr());
// fire-and-forget
let _ = task::spawn_blocking(move || {
let c_args: Vec<CString> = args
.into_iter()
.map(|arg| CString::new(arg).unwrap())
.collect();
let mut ptrs: Vec<*const c_char> = c_args.iter().map(|arg| arg.as_ptr()).collect();
unsafe {
nfqws_main(c_args.len() as libc::c_int, ptrs.as_mut_ptr() as *mut _);
}
RUNNING.store(false, Ordering::SeqCst);
});
Ok(())
}