mirror of
https://github.com/egor-white/zaprett.git
synced 2026-03-22 00:18:13 +05:00
remove ${zaprettdir} and add ${lua_lib:id}
This commit is contained in:
@@ -6,8 +6,6 @@ pub mod path {
|
||||
LazyLock::new(|| Path::new("/data/adb/modules/zaprett"));
|
||||
pub static ZAPRETT_DIR_PATH: LazyLock<&Path> =
|
||||
LazyLock::new(|| Path::new("/storage/emulated/0/zaprett"));
|
||||
pub static ZAPRETT_LIBS_PATH: LazyLock<&Path> =
|
||||
LazyLock::new(|| Path::new("/storage/emulated/0/zaprett/files/strategies/nfqws2/libs"));
|
||||
}
|
||||
|
||||
// Only for testing
|
||||
@@ -20,6 +18,4 @@ pub mod path {
|
||||
LazyLock::new(|| Path::new("zaprett_module"));
|
||||
pub static ZAPRETT_DIR_PATH: LazyLock<&Path> =
|
||||
LazyLock::new(|| Path::new("zaprett_dir"));
|
||||
pub static ZAPRETT_LIBS_PATH: LazyLock<&Path> =
|
||||
LazyLock::new(|| Path::new("zaprett_dir/strategies/nfqws2/libs"));
|
||||
}
|
||||
@@ -16,7 +16,7 @@ use sysctl::{Ctl, CtlValue, Sysctl};
|
||||
use sysinfo::{Pid as SysPid, System};
|
||||
use tokio::fs;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use crate::path::path::{MODULE_PATH, ZAPRETT_DIR_PATH, ZAPRETT_LIBS_PATH};
|
||||
use crate::path::path::{MODULE_PATH, ZAPRETT_DIR_PATH};
|
||||
use crate::strategy::prepare_manifests;
|
||||
|
||||
pub async fn start_service() -> anyhow::Result<()> {
|
||||
@@ -71,14 +71,13 @@ pub async fn start_service() -> anyhow::Result<()> {
|
||||
let manifest = get_manifest(Path::new(strategy_path))?;
|
||||
Cow::Owned(fs::read_to_string(manifest.file()).await?)
|
||||
};
|
||||
let regex_hostlists = Regex::new(r"\$(?:hostlists|\{hostlists})")?;
|
||||
let regex_hostlists = Regex::new(r"\$\{hostlists\}")?;
|
||||
let regex_hostlist = Regex::new(r"\$\{hostlist:([^}]+)\}")?;
|
||||
let regex_hostlist_exclude = Regex::new(r"\$\{hostlist_exclude:([^}]+)\}")?;
|
||||
let regex_ipset = Regex::new(r"\$\{ipset:([^}]+)\}")?;
|
||||
let regex_ipset_exclude = Regex::new(r"\$\{ipset_exclude:([^}]+)\}")?;
|
||||
let regex_ipsets = Regex::new(r"\$(?:ipsets|\{ipsets})")?;
|
||||
let regex_zaprettdir = Regex::new(r"\$(?:zaprettdir|\{zaprettdir})")?;
|
||||
let regex_libsdir = Regex::new(r"\$(?:libsdir|\{libsdir})")?;
|
||||
let regex_ipsets = Regex::new(r"\$\{ipsets\}")?;
|
||||
let regex_libsdir = Regex::new(r"\$\{lua_lib:([^}]+)\}")?;
|
||||
let regex_bindir = Regex::new(r"\$\{bin:([^}]+)\}")?;
|
||||
let (hosts, ipsets) = config.list_type().merge(&config).await?;
|
||||
let hostlists: HashMap<String, Manifest> =
|
||||
@@ -105,29 +104,32 @@ pub async fn start_service() -> anyhow::Result<()> {
|
||||
.into_iter()
|
||||
.map(|m| (m.id().clone(), m))
|
||||
.collect();
|
||||
let lua_lib: HashMap<String, Manifest> =
|
||||
get_all_manifests(&ZAPRETT_DIR_PATH.join("manifests/libs"))
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|m| (m.id().clone(), m))
|
||||
.collect();
|
||||
let bins: HashMap<String, Manifest> =
|
||||
get_all_manifests(&ZAPRETT_DIR_PATH.join("manifests/bin"))
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|m| (m.id().clone(), m))
|
||||
.collect();
|
||||
let strat_modified = prepare_manifests(&start, ®ex_hostlist, &hostlists, &tmp_dir, "txt")?;
|
||||
let strat_modified = prepare_manifests(&strat_modified, ®ex_hostlist_exclude, &hostlists_exclude, &tmp_dir, "txt")?;
|
||||
let strat_modified = prepare_manifests(&strat_modified, ®ex_ipset, &ipset, &tmp_dir, "txt")?;
|
||||
let strat_modified = prepare_manifests(&strat_modified, ®ex_ipset_exclude, &ipset_exclude, &tmp_dir, "txt")?;
|
||||
let strat_modified = prepare_manifests(&strat_modified, ®ex_bindir, &bins, &tmp_dir, "bin")?;
|
||||
let strat_modified = prepare_manifests(&start, ®ex_hostlist, &hostlists, &tmp_dir)?;
|
||||
let strat_modified = prepare_manifests(&strat_modified, ®ex_hostlist_exclude, &hostlists_exclude, &tmp_dir)?;
|
||||
let strat_modified = prepare_manifests(&strat_modified, ®ex_ipset, &ipset, &tmp_dir)?;
|
||||
let strat_modified = prepare_manifests(&strat_modified, ®ex_ipset_exclude, &ipset_exclude, &tmp_dir)?;
|
||||
let strat_modified = prepare_manifests(&strat_modified, ®ex_libsdir, &lua_lib, &tmp_dir)?;
|
||||
let strat_modified = prepare_manifests(&strat_modified, ®ex_bindir, &bins, &tmp_dir)?;
|
||||
let strat_modified = regex_hostlists.replace_all(&strat_modified, &hosts);
|
||||
let strat_modified = regex_ipsets.replace_all(&strat_modified, &ipsets);
|
||||
let strat_modified =
|
||||
regex_zaprettdir.replace_all(&strat_modified, ZAPRETT_DIR_PATH.to_str().unwrap());
|
||||
let strat_modified =
|
||||
regex_libsdir.replace_all(&strat_modified, ZAPRETT_LIBS_PATH.to_str().unwrap());
|
||||
let strat_modified = strat_modified.into_owned();
|
||||
|
||||
let ctl = Ctl::new("net.netfilter.nf_conntrack_tcp_be_liberal")?;
|
||||
ctl.set_value(CtlValue::String("1".into()))?;
|
||||
|
||||
setup_iptables_rules().expect("setup iptables rules");
|
||||
setup_iptables_rules()?;
|
||||
|
||||
if config.service_type() == &ServiceType::Nfqws {
|
||||
daemonize_nfqws(&strat_modified).await;
|
||||
|
||||
@@ -3,7 +3,7 @@ use regex::Regex;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub fn prepare_manifests(input: &str, regex: &Regex, manifests: &HashMap<String, Manifest>, tmp_dir: &Path, ext: &str) -> anyhow::Result<String> {
|
||||
pub fn prepare_manifests(input: &str, regex: &Regex, manifests: &HashMap<String, Manifest>, tmp_dir: &Path) -> anyhow::Result<String> {
|
||||
let required: HashSet<String> = regex.captures_iter(input).map(|c| c[1].to_string()).collect();
|
||||
let mut paths: HashMap<String, PathBuf> = HashMap::new();
|
||||
for id in &required {
|
||||
@@ -12,7 +12,9 @@ pub fn prepare_manifests(input: &str, regex: &Regex, manifests: &HashMap<String,
|
||||
.ok_or_else(|| anyhow::anyhow!("Manifest not found: {}", id))?;
|
||||
let path = Path::new(manifest.file());
|
||||
let mut dst = tmp_dir.join(id);
|
||||
dst.set_extension(ext);
|
||||
if let Some(ext) = path.extension() {
|
||||
dst.set_extension(ext);
|
||||
}
|
||||
std::fs::copy(path, &dst)?;
|
||||
paths.insert(id.clone(), dst);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user