This commit is contained in:
sqlerrorthing
2025-11-01 03:39:22 +08:00
parent bf76e162ee
commit 515c946f13
6 changed files with 77 additions and 71 deletions

9
.gitmodules vendored
View File

@@ -1,12 +1,3 @@
[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"] [submodule "rust/crates/libnfqws/zapret"]
path = rust/crates/libnfqws/zapret path = rust/crates/libnfqws/zapret
url = https://github.com/bol-van/zapret.git url = https://github.com/bol-van/zapret.git
[submodule "rust/crates/iptables"]
path = rust/crates/iptables
url = https://github.com/yaa110/rust-iptables

16
rust/Cargo.lock generated
View File

@@ -315,6 +315,15 @@ version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "135b12329e5e3ce057a9f972339ea52bc954fe1e9358ef27f95e89716fbc5424" checksum = "135b12329e5e3ce057a9f972339ea52bc954fe1e9358ef27f95e89716fbc5424"
[[package]]
name = "iptables"
version = "0.6.0"
source = "git+https://github.com/sqlerrorthing/rust-iptables.git?branch=feat%2Fadd-android#c56ec40f30d61becc66024061d2461851acf5d19"
dependencies = [
"lazy_static",
"regex",
]
[[package]] [[package]]
name = "is-terminal" name = "is-terminal"
version = "0.4.17" version = "0.4.17"
@@ -347,6 +356,12 @@ version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.177" version = "0.2.177"
@@ -901,6 +916,7 @@ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"daemonize", "daemonize",
"iptables",
"libc", "libc",
"libnfqws", "libnfqws",
"log", "log",

View File

@@ -22,3 +22,4 @@ daemonize = "0.5.0"
log = "0.4.28" log = "0.4.28"
pretty_env_logger = "0.5.0" 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" }

View File

@@ -19,3 +19,4 @@ daemonize = { workspace = true }
pretty_env_logger = { workspace = true } pretty_env_logger = { workspace = true }
log = { workspace = true } log = { workspace = true }
nix = { workspace = true, features = ["user"] } nix = { workspace = true, features = ["user"] }
iptables = { workspace = true }

View File

@@ -1,3 +1,4 @@
use std::error;
use anyhow::bail; use anyhow::bail;
use clap::{ArgAction, Parser, Subcommand, builder::BoolishValueParser}; use clap::{ArgAction, Parser, Subcommand, builder::BoolishValueParser};
use daemonize::Daemonize; use daemonize::Daemonize;
@@ -151,8 +152,8 @@ async fn start_service() -> anyhow::Result<()> {
let tmp_dir = MODULE_PATH.join("/tmp"); let tmp_dir = MODULE_PATH.join("/tmp");
if tmp_dir.exists() { if tmp_dir.exists() {
fs::remove_dir_all(&tmp_dir).unwrap(); fs::remove_dir_all(&tmp_dir)?;
fs::create_dir_all(&tmp_dir).unwrap(); fs::create_dir_all(&tmp_dir)?;
} }
let reader = BufReader::new( let reader = BufReader::new(
@@ -245,7 +246,8 @@ async fn start_service() -> anyhow::Result<()> {
let ctl = sysctl::Ctl::new("net.netfilter.nf_conntrack_tcp_be_liberal")?; let ctl = sysctl::Ctl::new("net.netfilter.nf_conntrack_tcp_be_liberal")?;
ctl.set_value(sysctl::CtlValue::String("1".into()))?; ctl.set_value(sysctl::CtlValue::String("1".into()))?;
setup_iptables_rules(); setup_iptables_rules().expect("setup iptables rules");
daemonize_nfqws(&strat_modified).await; daemonize_nfqws(&strat_modified).await;
info!("zaprett service started!"); info!("zaprett service started!");
Ok(()) Ok(())
@@ -256,7 +258,7 @@ async fn stop_service() -> anyhow::Result<()> {
bail!("Running not from root, exiting"); bail!("Running not from root, exiting");
}; };
clear_iptables_rules(); clear_iptables_rules().expect("clear iptables rules");
let pid_str = fs::read_to_string(MODULE_PATH.join("tmp/pid.lock").as_path())?; let pid_str = fs::read_to_string(MODULE_PATH.join("tmp/pid.lock").as_path())?;
let pid = pid_str.trim().parse::<i32>()?; let pid = pid_str.trim().parse::<i32>()?;
@@ -289,7 +291,7 @@ async fn restart_service() {
fn set_autostart(autostart: &bool) { fn set_autostart(autostart: &bool) {
if *autostart { if *autostart {
if let Err(e) = File::create(MODULE_PATH.join("autostart")) { if let Err(e) = File::create(MODULE_PATH.join("autostart")) {
eprintln!("autostart: cannot create flag file: {e}"); error!("Autostart: cannot create flag file: {e}");
} }
} else { } else {
fs::remove_file(MODULE_PATH.join("autostart")).unwrap() fs::remove_file(MODULE_PATH.join("autostart")).unwrap()
@@ -302,15 +304,10 @@ fn get_autostart() {
} }
fn service_status() -> bool { fn service_status() -> bool {
let Ok(pid_str) = fs::read_to_string(MODULE_PATH.join("tmp/pid.lock")) else { fs::read_to_string(MODULE_PATH.join("tmp/pid.lock"))
return false; .ok()
}; .and_then(|pid_str| pid_str.trim().parse::<i32>().ok())
.is_some()
let Ok(_) = pid_str.trim().parse::<i32>() else {
return false;
};
true
/*match all_processes() { /*match all_processes() {
Ok(iter) => iter Ok(iter) => iter
.filter_map(|rp| rp.ok()) .filter_map(|rp| rp.ok())
@@ -332,10 +329,11 @@ fn module_version() {
fn bin_version() { fn bin_version() {
println!("{}", env!("ZAPRET_VERSION")); println!("{}", env!("ZAPRET_VERSION"));
} }
fn merge_files( fn merge_files(
input_paths: Vec<String>, input_paths: Vec<String>,
output_path: &Path, output_path: &Path,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn error::Error>> {
let mut combined_content = String::new(); let mut combined_content = String::new();
for path_str in input_paths { for path_str in input_paths {
@@ -352,54 +350,54 @@ fn merge_files(
Ok(()) Ok(())
} }
fn setup_iptables_rules() { fn setup_iptables_rules() -> Result<(), Box<dyn error::Error>> {
todo!(); let ipt = iptables::new(false)?;
// let ipt = iptables::new(false).unwrap();
// ipt.insert(
// ipt.insert( "mangle",
// "mangle", "POSTROUTING",
// "POSTROUTING", "-j NFQUEUE --queue-num 200 --queue-bypass",
// "-j NFQUEUE --queue-num 200 --queue-bypass", 1,
// 1, )?;
// )
// .unwrap(); ipt.insert(
// ipt.insert( "mangle",
// "mangle", "PREROUTING",
// "PREROUTING", "-j NFQUEUE --queue-num 200 --queue-bypass",
// "-j NFQUEUE --queue-num 200 --queue-bypass", 1,
// 1, )?;
// )
// .unwrap(); ipt.append(
// ipt.append( "filter",
// "filter", "FORWARD",
// "FORWARD", "-j NFQUEUE --queue-num 200 --queue-bypass",
// "-j NFQUEUE --queue-num 200 --queue-bypass", )?;
// )
// .unwrap(); Ok(())
} }
fn clear_iptables_rules() { fn clear_iptables_rules() -> Result<(), Box<dyn error::Error>> {
todo!(); let ipt = iptables::new(false)?;
// let ipt = iptables::new(false).unwrap();
// ipt.delete(
// ipt.delete( "mangle",
// "mangle", "POSTROUTING",
// "POSTROUTING", "-j NFQUEUE --queue-num 200 --queue-bypass",
// "-j NFQUEUE --queue-num 200 --queue-bypass", )?;
// )
// .unwrap(); ipt.delete(
// ipt.delete( "mangle",
// "mangle", "PREROUTING",
// "PREROUTING", "-j NFQUEUE --queue-num 200 --queue-bypass",
// "-j NFQUEUE --queue-num 200 --queue-bypass", )?;
// )
// .unwrap(); ipt.delete(
// ipt.delete( "filter",
// "filter", "FORWARD",
// "FORWARD", "-j NFQUEUE --queue-num 200 --queue-bypass",
// "-j NFQUEUE --queue-num 200 --queue-bypass", )?;
// )
// .unwrap(); Ok(())
} }
async fn run_nfqws(args_str: &str) -> anyhow::Result<()> { async fn run_nfqws(args_str: &str) -> anyhow::Result<()> {