mirror of
https://github.com/egor-white/zaprett.git
synced 2025-12-10 05:19:42 +05:00
Compare commits
6 Commits
fc5cc814d6
...
ca65e0c6f1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca65e0c6f1 | ||
|
|
234e9e5824 | ||
|
|
aedf249712 | ||
|
|
526bf7ded2 | ||
|
|
2c8721e42b | ||
|
|
d59340a5c9 |
16
rust/Cargo.lock
generated
16
rust/Cargo.lock
generated
@@ -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",
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|||||||
@@ -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 }
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use tokio::fs::File;
|
|
||||||
use tokio::fs;
|
|
||||||
use crate::MODULE_PATH;
|
use crate::MODULE_PATH;
|
||||||
|
use tokio::fs;
|
||||||
|
use tokio::fs::File;
|
||||||
|
|
||||||
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");
|
let autostart_path = MODULE_PATH.join("autostart");
|
||||||
|
|||||||
@@ -59,8 +59,8 @@ impl Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Command::GetAutostart => get_autostart(),
|
Command::GetAutostart => get_autostart(),
|
||||||
Command::ModuleVersion => module_version(),
|
Command::ModuleVersion => println!("{}", module_version().await?),
|
||||||
Command::BinaryVersion => bin_version(),
|
Command::BinaryVersion => println!("{}", bin_version()),
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -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(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,10 +12,14 @@ use std::ffi::CString;
|
|||||||
use std::os::raw::c_char;
|
use std::os::raw::c_char;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::LazyLock;
|
use std::sync::LazyLock;
|
||||||
|
use anyhow::bail;
|
||||||
use tokio::fs::File;
|
use tokio::fs::File;
|
||||||
use tokio::io::{copy, AsyncWriteExt};
|
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> =
|
pub static ZAPRETT_DIR_PATH: LazyLock<&Path> =
|
||||||
LazyLock::new(|| Path::new("/storage/emulated/0/zaprett"));
|
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
|
--filter-udp=443 --dpi-desync=fake --dpi-desync-repeats=6 $hostlist
|
||||||
";
|
";
|
||||||
|
|
||||||
fn module_version() {
|
async fn module_version() -> anyhow::Result<String> {
|
||||||
if let Ok(prop) = Ini::load_from_file(MODULE_PATH.join("module.prop"))
|
let prop = spawn_blocking(|| Ini::load_from_file(MODULE_PATH.join("module.prop")))
|
||||||
&& let Some(props) = prop.section::<String>(None)
|
.await??;
|
||||||
|
|
||||||
|
if let Some(props) = prop.section::<String>(None)
|
||||||
&& let Some(version) = props.get("version")
|
&& let Some(version) = props.get("version")
|
||||||
{
|
{
|
||||||
println!("{version}");
|
return Ok(version.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bail!("Failed to get version, prop not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bin_version() {
|
fn bin_version() -> &'static str {
|
||||||
println!("{}", env!("ZAPRET_VERSION"));
|
env!("ZAPRET_VERSION")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn merge_files(
|
pub async fn merge_files(
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user