diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 0dc5f2c..025b27a 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -327,15 +327,6 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "135b12329e5e3ce057a9f972339ea52bc954fe1e9358ef27f95e89716fbc5424" -[[package]] -name = "iptables" -version = "0.6.0" -source = "git+https://github.com/egor-white/rust-iptables-android.git?branch=add-android#15e43378308c766919e029ef315ee1681990c78f" -dependencies = [ - "lazy_static", - "regex", -] - [[package]] name = "is-terminal" version = "0.4.17" @@ -368,12 +359,6 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" -[[package]] -name = "lazy_static" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" - [[package]] name = "libc" version = "0.2.177" @@ -1126,7 +1111,6 @@ dependencies = [ "clap", "daemonize", "getset", - "iptables", "libc", "libnfqws", "log", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 007d15a..651709d 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -22,6 +22,5 @@ daemonize = "0.5.0" log = "0.4.28" pretty_env_logger = "0.5.0" nix = { version = "0.30.1", features = ["signal"] } -iptables = { git = "https://github.com/egor-white/rust-iptables-android.git", branch = "add-android" } getset = "0.1.6" sysinfo = "0.37.2" diff --git a/rust/crates/zaprett/Cargo.toml b/rust/crates/zaprett/Cargo.toml index 5e309df..d1859f2 100644 --- a/rust/crates/zaprett/Cargo.toml +++ b/rust/crates/zaprett/Cargo.toml @@ -19,6 +19,5 @@ daemonize = { workspace = true } pretty_env_logger = { workspace = true } log = { workspace = true } nix = { workspace = true, features = ["user"] } -iptables = { workspace = true } getset = { workspace = true } sysinfo = { workspace = true } diff --git a/rust/crates/zaprett/src/autostart.rs b/rust/crates/zaprett/src/autostart.rs index d56aa67..f337fc2 100644 --- a/rust/crates/zaprett/src/autostart.rs +++ b/rust/crates/zaprett/src/autostart.rs @@ -1,21 +1,20 @@ use crate::MODULE_PATH; -use std::path::PathBuf; -use std::sync::LazyLock; use tokio::fs; use tokio::fs::File; -static AUTOSTART: LazyLock = LazyLock::new(|| MODULE_PATH.join("autostart")); - pub async fn set_autostart(autostart: bool) -> Result<(), anyhow::Error> { + let autostart_path = MODULE_PATH.join("autostart"); + if autostart { - File::create(&*AUTOSTART).await?; + File::create(autostart_path).await?; } else { - fs::remove_file(&*AUTOSTART).await?; + fs::remove_file(autostart_path).await?; } Ok(()) } -pub fn get_autostart() -> bool { - AUTOSTART.exists() +pub fn get_autostart() { + let file = MODULE_PATH.join("autostart"); + println!("{}", file.exists()); } diff --git a/rust/crates/zaprett/src/iptables_rust.rs b/rust/crates/zaprett/src/iptables_rust.rs index 7eb9b09..b46be71 100644 --- a/rust/crates/zaprett/src/iptables_rust.rs +++ b/rust/crates/zaprett/src/iptables_rust.rs @@ -1,51 +1,88 @@ use std::error; +use std::process::Command; pub fn setup_iptables_rules() -> Result<(), Box> { - let ipt = iptables::new(false)?; + Command::new("iptables") + .arg("-t") + .arg("mangle") + .arg("-I") + .arg("POSTROUTING") + .arg("-j") + .arg("NFQUEUE") + .arg("--queue-num") + .arg("200") + .arg("--queue-bypass") + .status() + .expect("failed to add iptables rules"); - ipt.insert( - "mangle", - "POSTROUTING", - "-j NFQUEUE --queue-num 200 --queue-bypass", - 1, - )?; + Command::new("iptables") + .arg("-t") + .arg("mangle") + .arg("-I") + .arg("PREROUTING") + .arg("-j") + .arg("NFQUEUE") + .arg("--queue-num") + .arg("200") + .arg("--queue-bypass") + .status() + .expect("failed to add iptables rules"); - ipt.insert( - "mangle", - "PREROUTING", - "-j NFQUEUE --queue-num 200 --queue-bypass", - 1, - )?; - - ipt.append( - "filter", - "FORWARD", - "-j NFQUEUE --queue-num 200 --queue-bypass", - )?; + Command::new("iptables") + .arg("-t") + .arg("filter") + .arg("-A") + .arg("FORWARD") + .arg("-j") + .arg("NFQUEUE") + .arg("--queue-num") + .arg("200") + .arg("--queue-bypass") + .status() + .expect("failed to add iptables rules"); Ok(()) } pub fn clear_iptables_rules() -> Result<(), Box> { - let ipt = iptables::new(false)?; + Command::new("iptables") + .arg("-t") + .arg("mangle") + .arg("-D") + .arg("POSTROUTING") + .arg("-j") + .arg("NFQUEUE") + .arg("--queue-num") + .arg("200") + .arg("--queue-bypass") + .status() + .expect("failed to remove iptables rules"); - ipt.delete( - "mangle", - "POSTROUTING", - "-j NFQUEUE --queue-num 200 --queue-bypass", - )?; + Command::new("iptables") + .arg("-t") + .arg("mangle") + .arg("-D") + .arg("PREROUTING") + .arg("-j") + .arg("NFQUEUE") + .arg("--queue-num") + .arg("200") + .arg("--queue-bypass") + .status() + .expect("failed to remove iptables rules"); - ipt.delete( - "mangle", - "PREROUTING", - "-j NFQUEUE --queue-num 200 --queue-bypass", - )?; - - ipt.delete( - "filter", - "FORWARD", - "-j NFQUEUE --queue-num 200 --queue-bypass", - )?; + Command::new("iptables") + .arg("-t") + .arg("filter") + .arg("-D") + .arg("FORWARD") + .arg("-j") + .arg("NFQUEUE") + .arg("--queue-num") + .arg("200") + .arg("--queue-bypass") + .status() + .expect("failed to remove iptables rules"); Ok(()) } diff --git a/rust/crates/zaprett/src/service.rs b/rust/crates/zaprett/src/service.rs index 33329fd..fa377b2 100644 --- a/rust/crates/zaprett/src/service.rs +++ b/rust/crates/zaprett/src/service.rs @@ -13,6 +13,7 @@ use sysinfo::{Pid as SysPid, System}; use tokio::fs; use tokio::fs::File; use tokio::io::AsyncReadExt; +use std::path::Path; pub async fn start_service() -> anyhow::Result<()> { if !Uid::effective().is_root() { @@ -106,17 +107,19 @@ pub async fn service_status() -> anyhow::Result { bail!("Running not from root, exiting"); }; - let Ok(Some(pid)) = fs::read_to_string(MODULE_PATH.join("/tmp/pid.lock")) - .await - .map(|s| s.trim().parse::().ok()) - else { - bail!("failed to get pid"); + let pid_i32 = match fs::read_to_string(Path::new(*MODULE_PATH).join("tmp/pid.lock")).await { + Ok(s) => match s.trim().parse::() { + Ok(pid) => pid, + Err(_) => return Ok(false), + }, + Err(_) => return Ok(false), }; - - let is_zaprett = System::new_all() - .process(SysPid::from(pid)) - .map(|process| process.name() == "zaprett") - .unwrap_or(false); - - Ok(is_zaprett) + 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) }