6 Commits

Author SHA1 Message Date
CherretGit
ca65e0c6f1 remove iptables crate, fix service_status 2025-11-03 21:16:14 +07:00
egor-white
234e9e5824 Merge pull request #7 from sqlerrorthing/refactor/rust/autostart
refactor: autostart
2025-11-03 16:28:23 +03:00
egor-white
aedf249712 Merge branch 'main' into refactor/rust/autostart 2025-11-03 16:28:13 +03:00
egor-white
526bf7ded2 Merge pull request #6 from sqlerrorthing/fix/right-library
fix: right library functions
2025-11-03 16:26:06 +03:00
sqlerrorthing
2c8721e42b right library api 2025-11-03 05:42:28 +08:00
sqlerrorthing
d59340a5c9 refactor autostart 2025-11-03 05:35:33 +08:00
8 changed files with 107 additions and 77 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"
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",

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
use tokio::fs::File;
use tokio::fs;
use crate::MODULE_PATH;
use tokio::fs;
use tokio::fs::File;
pub async fn set_autostart(autostart: bool) -> Result<(), anyhow::Error> {
let autostart_path = MODULE_PATH.join("autostart");

View File

@@ -59,8 +59,8 @@ impl Command {
}
}
Command::GetAutostart => get_autostart(),
Command::ModuleVersion => module_version(),
Command::BinaryVersion => bin_version(),
Command::ModuleVersion => println!("{}", module_version().await?),
Command::BinaryVersion => println!("{}", bin_version()),
}
Ok(())

View File

@@ -1,51 +1,88 @@
use std::error;
use std::process::Command;
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(
"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<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(
"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(())
}

View File

@@ -12,10 +12,14 @@ use std::ffi::CString;
use std::os::raw::c_char;
use std::path::Path;
use std::sync::LazyLock;
use anyhow::bail;
use tokio::fs::File;
use tokio::io::{copy, AsyncWriteExt};
use tokio::task::spawn_blocking;
pub static MODULE_PATH: LazyLock<&Path> =
LazyLock::new(|| Path::new("/data/adb/modules/zaprett"));
pub static MODULE_PATH: LazyLock<&Path> = LazyLock::new(|| Path::new("/data/adb/modules/zaprett"));
pub static ZAPRETT_DIR_PATH: LazyLock<&Path> =
LazyLock::new(|| Path::new("/storage/emulated/0/zaprett"));
@@ -28,17 +32,21 @@ pub static DEFAULT_START: &str = "
--filter-udp=443 --dpi-desync=fake --dpi-desync-repeats=6 $hostlist
";
fn module_version() {
if let Ok(prop) = Ini::load_from_file(MODULE_PATH.join("module.prop"))
&& let Some(props) = prop.section::<String>(None)
async fn module_version() -> anyhow::Result<String> {
let prop = spawn_blocking(|| Ini::load_from_file(MODULE_PATH.join("module.prop")))
.await??;
if let Some(props) = prop.section::<String>(None)
&& let Some(version) = props.get("version")
{
println!("{version}");
}
return Ok(version.into());
}
fn bin_version() {
println!("{}", env!("ZAPRET_VERSION"));
bail!("Failed to get version, prop not found")
}
fn bin_version() -> &'static str {
env!("ZAPRET_VERSION")
}
pub async fn merge_files(

View File

@@ -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<bool> {
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::<usize>().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::<i32>() {
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)
}