remove iptables crate, fix service_status

This commit is contained in:
CherretGit
2025-11-03 21:16:14 +07:00
parent 234e9e5824
commit ca65e0c6f1
6 changed files with 95 additions and 74 deletions

16
rust/Cargo.lock generated
View File

@@ -327,15 +327,6 @@ 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/egor-white/rust-iptables-android.git?branch=add-android#15e43378308c766919e029ef315ee1681990c78f"
dependencies = [
"lazy_static",
"regex",
]
[[package]] [[package]]
name = "is-terminal" name = "is-terminal"
version = "0.4.17" version = "0.4.17"
@@ -368,12 +359,6 @@ 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"
@@ -1126,7 +1111,6 @@ dependencies = [
"clap", "clap",
"daemonize", "daemonize",
"getset", "getset",
"iptables",
"libc", "libc",
"libnfqws", "libnfqws",
"log", "log",

View File

@@ -22,6 +22,5 @@ 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/egor-white/rust-iptables-android.git", branch = "add-android" }
getset = "0.1.6" getset = "0.1.6"
sysinfo = "0.37.2" sysinfo = "0.37.2"

View File

@@ -19,6 +19,5 @@ 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 }
getset = { workspace = true } getset = { workspace = true }
sysinfo = { workspace = true } sysinfo = { workspace = true }

View File

@@ -1,21 +1,20 @@
use crate::MODULE_PATH; use crate::MODULE_PATH;
use std::path::PathBuf;
use std::sync::LazyLock;
use tokio::fs; use tokio::fs;
use tokio::fs::File; use tokio::fs::File;
static AUTOSTART: LazyLock<PathBuf> = LazyLock::new(|| MODULE_PATH.join("autostart"));
pub async fn set_autostart(autostart: bool) -> Result<(), anyhow::Error> { pub async fn set_autostart(autostart: bool) -> Result<(), anyhow::Error> {
let autostart_path = MODULE_PATH.join("autostart");
if autostart { if autostart {
File::create(&*AUTOSTART).await?; File::create(autostart_path).await?;
} else { } else {
fs::remove_file(&*AUTOSTART).await?; fs::remove_file(autostart_path).await?;
} }
Ok(()) Ok(())
} }
pub fn get_autostart() -> bool { pub fn get_autostart() {
AUTOSTART.exists() let file = MODULE_PATH.join("autostart");
println!("{}", file.exists());
} }

View File

@@ -1,51 +1,88 @@
use std::error; use std::error;
use std::process::Command;
pub fn setup_iptables_rules() -> Result<(), Box<dyn error::Error>> { pub fn setup_iptables_rules() -> Result<(), Box<dyn error::Error>> {
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( Command::new("iptables")
"mangle", .arg("-t")
"POSTROUTING", .arg("mangle")
"-j NFQUEUE --queue-num 200 --queue-bypass", .arg("-I")
1, .arg("PREROUTING")
)?; .arg("-j")
.arg("NFQUEUE")
.arg("--queue-num")
.arg("200")
.arg("--queue-bypass")
.status()
.expect("failed to add iptables rules");
ipt.insert( Command::new("iptables")
"mangle", .arg("-t")
"PREROUTING", .arg("filter")
"-j NFQUEUE --queue-num 200 --queue-bypass", .arg("-A")
1, .arg("FORWARD")
)?; .arg("-j")
.arg("NFQUEUE")
ipt.append( .arg("--queue-num")
"filter", .arg("200")
"FORWARD", .arg("--queue-bypass")
"-j NFQUEUE --queue-num 200 --queue-bypass", .status()
)?; .expect("failed to add iptables rules");
Ok(()) Ok(())
} }
pub fn clear_iptables_rules() -> Result<(), Box<dyn error::Error>> { pub fn clear_iptables_rules() -> Result<(), Box<dyn error::Error>> {
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( Command::new("iptables")
"mangle", .arg("-t")
"POSTROUTING", .arg("mangle")
"-j NFQUEUE --queue-num 200 --queue-bypass", .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( Command::new("iptables")
"mangle", .arg("-t")
"PREROUTING", .arg("filter")
"-j NFQUEUE --queue-num 200 --queue-bypass", .arg("-D")
)?; .arg("FORWARD")
.arg("-j")
ipt.delete( .arg("NFQUEUE")
"filter", .arg("--queue-num")
"FORWARD", .arg("200")
"-j NFQUEUE --queue-num 200 --queue-bypass", .arg("--queue-bypass")
)?; .status()
.expect("failed to remove iptables rules");
Ok(()) Ok(())
} }

View File

@@ -13,6 +13,7 @@ use sysinfo::{Pid as SysPid, System};
use tokio::fs; use tokio::fs;
use tokio::fs::File; use tokio::fs::File;
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
use std::path::Path;
pub async fn start_service() -> anyhow::Result<()> { pub async fn start_service() -> anyhow::Result<()> {
if !Uid::effective().is_root() { if !Uid::effective().is_root() {
@@ -106,17 +107,19 @@ pub async fn service_status() -> anyhow::Result<bool> {
bail!("Running not from root, exiting"); bail!("Running not from root, exiting");
}; };
let Ok(Some(pid)) = fs::read_to_string(MODULE_PATH.join("/tmp/pid.lock")) let pid_i32 = match fs::read_to_string(Path::new(*MODULE_PATH).join("tmp/pid.lock")).await {
.await Ok(s) => match s.trim().parse::<i32>() {
.map(|s| s.trim().parse::<usize>().ok()) Ok(pid) => pid,
else { Err(_) => return Ok(false),
bail!("failed to get pid"); },
Err(_) => return Ok(false),
}; };
let pid = SysPid::from(pid_i32 as usize);
let is_zaprett = System::new_all() let system = System::new_all();
.process(SysPid::from(pid)) if let Some(process) = system.process(pid) {
.map(|process| process.name() == "zaprett") if process.name() == "zaprett" {
.unwrap_or(false); return Ok(true);
}
Ok(is_zaprett) }
Ok(false)
} }