fix nfqws2 strategy, add id to manifest

This commit is contained in:
CherretGit
2026-03-12 18:33:14 +07:00
parent 229f624c55
commit eac364a9e1
4 changed files with 31 additions and 27 deletions

View File

@@ -50,6 +50,7 @@ pub struct Config {
#[getset(get = "pub")]
pub struct Manifest {
schema: i32,
id: String,
name: String,
version: String,
author: String,

View File

@@ -75,8 +75,13 @@ pub async fn merge_files(
}
pub fn read_manifest(path: &Path) -> anyhow::Result<Manifest> {
let content = fs::read_to_string(path)?;
Ok(serde_json::from_str(&content)?)
let content = fs::read_to_string(path).with_context(|| {
format!("Failed to read manifest: {}", path.display())
})?;
let manifest = serde_json::from_str(&content).with_context(|| {
format!("Failed to parse mnanifest: {}", path.display())
})?;
Ok(manifest)
}
pub fn check_dependencies(manifest: &Manifest) -> anyhow::Result<()> {
@@ -107,7 +112,10 @@ pub fn get_manifest(path: &Path) -> anyhow::Result<Manifest> {
pub fn get_all_manifests(path: &Path) -> anyhow::Result<Vec<Manifest>> {
path.read_dir()?.map(
|manifest_path| {
get_manifest(&manifest_path?.path())
let manifest_path = manifest_path?;
get_manifest(&manifest_path.path()).with_context(|| {
format!("Failed to get manifest: {}", manifest_path.path().display())
})
}
).collect()
}

View File

@@ -56,18 +56,17 @@ pub async fn start_service() -> anyhow::Result<()> {
}
let config: Config = serde_json::from_str(&config_contents)?;
let strategy = get_manifest(Path::new(config.strategy())).ok();
let strategy = match config.service_type() {
ServiceType::Nfqws => get_manifest(Path::new(config.strategy()))?,
ServiceType::Nfqws2 => get_manifest(Path::new(config.strategy_nfqws2()))?
};
let default_strategy = match config.service_type() {
ServiceType::Nfqws => DEFAULT_STRATEGY_NFQWS,
ServiceType::Nfqws2 => DEFAULT_STRATEGY_NFQWS2
};
let start: Cow<str> = if let Some(manifest) = strategy {
fs::read_to_string(manifest.file())
.await
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(default_strategy))
} else {
Cow::Borrowed(default_strategy)
let start = match fs::read_to_string(strategy.file()).await {
Ok(s) => Cow::Owned(s),
Err(_) => Cow::Borrowed(default_strategy)
};
let regex_hostlists = Regex::new(r"\$(?:hostlists|\{hostlists})")?;
let regex_hostlist = Regex::new(r"\$\{hostlist:([^}]+)\}")?;
@@ -82,25 +81,25 @@ pub async fn start_service() -> anyhow::Result<()> {
get_all_manifests(&ZAPRETT_DIR_PATH.join("manifests/lists/include"))
.unwrap_or_default()
.into_iter()
.map(|m| (m.name().clone(), m))
.map(|m| (m.id().clone(), m))
.collect();
let hostlists_exclude: HashMap<String, Manifest> =
get_all_manifests(&ZAPRETT_DIR_PATH.join("manifests/lists/exclude"))
.unwrap_or_default()
.into_iter()
.map(|m| (m.name().clone(), m))
.map(|m| (m.id().clone(), m))
.collect();
let ipset: HashMap<String, Manifest> =
get_all_manifests(&ZAPRETT_DIR_PATH.join("manifests/ipset/include"))
.unwrap_or_default()
.into_iter()
.map(|m| (m.name().clone(), m))
.map(|m| (m.id().clone(), m))
.collect();
let ipset_exclude: HashMap<String, Manifest> =
get_all_manifests(&ZAPRETT_DIR_PATH.join("manifests/ipset/exclude"))
.unwrap_or_default()
.into_iter()
.map(|m| (m.name().clone(), m))
.map(|m| (m.id().clone(), m))
.collect();
let strat_modified = prepare_manifests(&start, &regex_hostlist, &hostlists, &tmp_dir)?;
let strat_modified = prepare_manifests(&strat_modified, &regex_hostlist_exclude, &hostlists_exclude, &tmp_dir)?;

View File

@@ -1,23 +1,19 @@
use crate::config::Manifest;
use regex::Regex;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use anyhow::bail;
use regex::Regex;
use crate::config::Manifest;
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();
for name in &required {
if !manifests.contains_key(name) {
bail!("Manifest not found: {}", name)
}
}
let mut paths: HashMap<String, PathBuf> = HashMap::new();
for name in &required {
let manifest = &manifests[name];
for id in &required {
let manifest = manifests
.get(id)
.ok_or_else(|| anyhow::anyhow!("Manifest not found: {}", id))?;
let path = Path::new(manifest.file());
let dst = tmp_dir.join(format!("{}.txt", name));
let dst = tmp_dir.join(format!("{}.txt", id));
std::fs::copy(path, &dst)?;
paths.insert(name.clone(), dst);
paths.insert(id.clone(), dst);
}
let result = regex.replace_all(input, |caps: &regex::Captures| {
paths[&caps[1]].to_string_lossy().into_owned()