mirror of
https://github.com/egor-white/zaprett.git
synced 2025-12-11 13:59:43 +05:00
move paths to constants
This commit is contained in:
@@ -3,19 +3,21 @@
|
||||
use anyhow::bail;
|
||||
use clap::{ArgAction, Parser, Subcommand, builder::BoolishValueParser};
|
||||
use ini::Ini;
|
||||
use libnfqws::nfqws_main;
|
||||
use procfs::process::all_processes;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::ffi::CString;
|
||||
use std::fmt::format;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::io::{Read, Write};
|
||||
use std::os::raw::c_char;
|
||||
use std::sync::LazyLock;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::{fs, path::Path};
|
||||
use sysctl::{CtlValue, Sysctl};
|
||||
use tokio::task;
|
||||
use libnfqws::nfqws_main;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version)]
|
||||
@@ -71,6 +73,10 @@ struct Config {
|
||||
blacklist: Vec<String>,
|
||||
}
|
||||
|
||||
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"));
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let cli = Cli::parse();
|
||||
@@ -83,22 +89,22 @@ async fn main() {
|
||||
Some(Commands::GetAutostart) => get_autostart(),
|
||||
Some(Commands::ModuleVer) => module_version(),
|
||||
Some(Commands::BinVer) => todo!(), //bin_version(),
|
||||
//None => println!("zaprett installed. Join us: t.me/zaprett_module"),
|
||||
None => run_nfqws("--version".to_string()).await.unwrap(),
|
||||
None => println!("zaprett installed. Join us: t.me/zaprett_module"),
|
||||
}
|
||||
tokio::signal::ctrl_c().await.unwrap();
|
||||
// tokio::signal::ctrl_c().await.unwrap();
|
||||
}
|
||||
|
||||
fn start_service() {
|
||||
println!("Starting zaprett service...");
|
||||
|
||||
let tmp_dir = Path::new("/data/adb/modules/zaprett/tmp");
|
||||
let tmp_dir = MODULE_PATH.join("tmp");
|
||||
if tmp_dir.exists() {
|
||||
fs::remove_dir_all(&tmp_dir).unwrap()
|
||||
}
|
||||
|
||||
let reader =
|
||||
BufReader::new(File::open("/sdcard/zaprett/config.json").expect("cannot open config.json"));
|
||||
let reader = BufReader::new(
|
||||
File::open(ZAPRETT_DIR_PATH.join("config.json")).expect("cannot open config.json"),
|
||||
);
|
||||
let config: Config = serde_json::from_reader(reader).expect("invalid json");
|
||||
|
||||
let list_type: &String = &config.list_type;
|
||||
@@ -121,49 +127,63 @@ fn start_service() {
|
||||
let regex_ipsets = Regex::new(r"\$ipset").unwrap();
|
||||
let regex_zaprettdir = Regex::new(r"\$\{?zaprettdir\}?").unwrap();
|
||||
|
||||
let zaprett_dir = String::from("/sdcard/zaprett");
|
||||
let mut strat_modified = String::new();
|
||||
|
||||
if list_type.eq("whitelist") {
|
||||
merge_files(
|
||||
config.active_lists,
|
||||
"/data/adb/modules/zaprett/tmp/hostlist",
|
||||
MODULE_PATH.join("tmp/hostlist").as_path(),
|
||||
)
|
||||
.unwrap();
|
||||
merge_files(config.active_ipsets, "/data/adb/modules/zaprett/tmp/ipset").unwrap();
|
||||
.unwrap();
|
||||
merge_files(
|
||||
config.active_ipsets,
|
||||
MODULE_PATH.join("tmp/ipset").as_path(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let hosts = String::from("--hostlist=/data/adb/modules/zaprett/tmp/hostlist");
|
||||
let ipsets = String::from("--ipset=/data/adb/modules/zaprett/tmp/ipset");
|
||||
let hosts = String::from(format!(
|
||||
"--hostlist={}/tmp/hostlist",
|
||||
MODULE_PATH.to_str().unwrap()
|
||||
));
|
||||
let ipsets = String::from(format!(
|
||||
"--ipset={}tmp/ipset",
|
||||
MODULE_PATH.to_str().unwrap()
|
||||
));
|
||||
|
||||
strat_modified = regex_hostlist.replace_all(&strat, &hosts).into_owned();
|
||||
strat_modified = regex_ipsets
|
||||
.replace_all(&strat_modified, &ipsets)
|
||||
.into_owned();
|
||||
strat_modified = regex_zaprettdir
|
||||
.replace_all(&strat_modified, &zaprett_dir)
|
||||
.replace_all(&strat_modified, &*ZAPRETT_DIR_PATH.to_str().unwrap())
|
||||
.into_owned();
|
||||
} else if list_type.eq("blacklist") {
|
||||
merge_files(
|
||||
config.active_exclude_lists,
|
||||
"/data/adb/modules/zaprett/tmp/hostlist-exclude",
|
||||
MODULE_PATH.join("tmp/hostlist-exclude").as_path(),
|
||||
)
|
||||
.unwrap();
|
||||
.unwrap();
|
||||
merge_files(
|
||||
config.active_exclude_ipsets,
|
||||
"/data/adb/modules/zaprett/tmp/ipset-exclude",
|
||||
MODULE_PATH.join("tmp/ipset-exclude").as_path(),
|
||||
)
|
||||
.unwrap();
|
||||
.unwrap();
|
||||
|
||||
let hosts =
|
||||
String::from("--hostlist-exclude=/data/adb/modules/zaprett/tmp/hostlist-exclude");
|
||||
let ipsets = String::from("--ipset-exclude=/data/adb/modules/zaprett/tmp/ipset-exclude");
|
||||
let hosts = String::from(format!(
|
||||
"--hostlist-exclude={}/tmp/hostlist-exclude",
|
||||
MODULE_PATH.to_str().unwrap()
|
||||
));
|
||||
let ipsets = String::from(format!(
|
||||
"--ipset-exclude={}/tmp/ipset-exclude",
|
||||
MODULE_PATH.to_str().unwrap()
|
||||
));
|
||||
|
||||
strat_modified = regex_hostlist.replace_all(&strat, &hosts).into_owned();
|
||||
strat_modified = regex_ipsets
|
||||
.replace_all(&strat_modified, &ipsets)
|
||||
.into_owned();
|
||||
strat_modified = regex_zaprettdir
|
||||
.replace_all(&strat_modified, &zaprett_dir)
|
||||
.replace_all(&strat_modified, &*ZAPRETT_DIR_PATH.to_str().unwrap())
|
||||
.into_owned();
|
||||
} else {
|
||||
panic!("no list-type called {}", &list_type)
|
||||
@@ -173,7 +193,7 @@ fn start_service() {
|
||||
ctl.set_value(CtlValue::Int(1)).unwrap();
|
||||
|
||||
setup_iptables_rules();
|
||||
//run_nfqws(&strat_modified);
|
||||
run_nfqws(&strat_modified);
|
||||
todo!();
|
||||
println!("zaprett service started!");
|
||||
}
|
||||
@@ -188,15 +208,15 @@ fn restart_service() {
|
||||
}
|
||||
fn set_autostart(autostart: &bool) {
|
||||
if *autostart {
|
||||
if let Err(e) = std::fs::File::create("/data/adb/modules/zaprett/autostart") {
|
||||
if let Err(e) = std::fs::File::create(MODULE_PATH.join("autostart")) {
|
||||
eprintln!("autostart: cannot create flag file: {e}");
|
||||
}
|
||||
} else {
|
||||
fs::remove_file("/data/adb/modules/zaprett/autostart").unwrap()
|
||||
fs::remove_file(MODULE_PATH.join("autostart")).unwrap()
|
||||
}
|
||||
}
|
||||
fn get_autostart() {
|
||||
let file = Path::new("/data/adb/modules/zaprett/autostart");
|
||||
let file = MODULE_PATH.join("autostart");
|
||||
println!("{}", file.exists());
|
||||
}
|
||||
fn service_status() {
|
||||
@@ -212,7 +232,7 @@ fn service_status() {
|
||||
}
|
||||
|
||||
fn module_version() {
|
||||
if let Ok(prop) = Ini::load_from_file("/data/adb/modules/zaprett/module.prop") {
|
||||
if let Ok(prop) = Ini::load_from_file(MODULE_PATH.join("module.prop")) {
|
||||
if let Some(props) = prop.section::<String>(None) {
|
||||
if let Some(v) = props.get("version") {
|
||||
println!("{}", v);
|
||||
@@ -238,7 +258,7 @@ fn bin_version() {
|
||||
}
|
||||
fn merge_files(
|
||||
input_paths: Vec<String>,
|
||||
output_path: &str,
|
||||
output_path: &Path,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut combined_content = String::new();
|
||||
|
||||
@@ -264,20 +284,20 @@ fn setup_iptables_rules() {
|
||||
"-j NFQUEUE --queue-num 200 --queue-bypass",
|
||||
1,
|
||||
)
|
||||
.unwrap();
|
||||
.unwrap();
|
||||
ipt.insert(
|
||||
"mangle",
|
||||
"PREROUTING",
|
||||
"-j NFQUEUE --queue-num 200 --queue-bypass",
|
||||
1,
|
||||
)
|
||||
.unwrap();
|
||||
.unwrap();
|
||||
ipt.append(
|
||||
"filter",
|
||||
"FORWARD",
|
||||
"-j NFQUEUE --queue-num 200 --queue-bypass",
|
||||
)
|
||||
.unwrap();
|
||||
.unwrap();
|
||||
}
|
||||
fn clear_iptables_rules() {
|
||||
let ipt = iptables::new(false).unwrap();
|
||||
@@ -287,22 +307,22 @@ fn clear_iptables_rules() {
|
||||
"POSTROUTING",
|
||||
"-j NFQUEUE --queue-num 200 --queue-bypass",
|
||||
)
|
||||
.unwrap();
|
||||
.unwrap();
|
||||
ipt.delete(
|
||||
"mangle",
|
||||
"PREROUTING",
|
||||
"-j NFQUEUE --queue-num 200 --queue-bypass",
|
||||
)
|
||||
.unwrap();
|
||||
.unwrap();
|
||||
ipt.delete(
|
||||
"filter",
|
||||
"FORWARD",
|
||||
"-j NFQUEUE --queue-num 200 --queue-bypass",
|
||||
)
|
||||
.unwrap();
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn run_nfqws(args_str: String) -> anyhow::Result<()> {
|
||||
async fn run_nfqws(args_str: &String) -> anyhow::Result<()> {
|
||||
static RUNNING: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
if RUNNING.swap(true, Ordering::SeqCst) {
|
||||
|
||||
Reference in New Issue
Block a user