mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-02 19:50:21 +08:00
Plan provider destinations and assets in the crate. Adapt the plan to the Service wire format at the call site.
671 lines
27 KiB
Rust
671 lines
27 KiB
Rust
mod error;
|
|
|
|
pub use error::RuntimeBundleError;
|
|
|
|
use clash_verge_logging::{Type, logging};
|
|
use serde_yaml_ng::{Mapping, Value};
|
|
use sha2::{Digest as _, Sha256};
|
|
use std::collections::{BTreeMap, HashSet};
|
|
use std::path::{Component, Path, PathBuf, Prefix};
|
|
|
|
pub const GEO_ASSETS: &[&str] = &[
|
|
"Country.mmdb",
|
|
"ASN.mmdb",
|
|
"geoip.dat",
|
|
"geosite.dat",
|
|
"geoip.metadb",
|
|
"GeoSite.dat",
|
|
];
|
|
|
|
/// Provider identity used by the runtime config and core API.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct RemoteProviderRef {
|
|
pub section: &'static str,
|
|
pub name: String,
|
|
pub destination: String,
|
|
pub url: String,
|
|
}
|
|
|
|
pub struct RuntimeAsset {
|
|
pub source: String,
|
|
pub destination: String,
|
|
}
|
|
|
|
pub struct RuntimeBundlePlan {
|
|
pub yaml: String,
|
|
pub assets: Vec<RuntimeAsset>,
|
|
pub remote_providers: Vec<RemoteProviderRef>,
|
|
}
|
|
|
|
struct RuntimeParts {
|
|
config: Value,
|
|
assets: Vec<RuntimeAsset>,
|
|
remote_providers: Vec<RemoteProviderRef>,
|
|
}
|
|
|
|
type Result<T> = std::result::Result<T, RuntimeBundleError>;
|
|
|
|
pub async fn collect_runtime_bundle(config_file: &Path) -> Result<RuntimeBundlePlan> {
|
|
let yaml = tokio::fs::read_to_string(config_file)
|
|
.await
|
|
.map_err(|source| RuntimeBundleError::ReadConfig {
|
|
path: config_file.to_owned(),
|
|
source,
|
|
})?;
|
|
let config: Value = serde_yaml_ng::from_str(&yaml).map_err(|source| RuntimeBundleError::ParseConfig {
|
|
path: config_file.to_owned(),
|
|
source,
|
|
})?;
|
|
let config_root = config_file.parent().ok_or(RuntimeBundleError::MissingConfigDirectory)?;
|
|
let parts = collect_runtime_parts(config, config_root)?;
|
|
Ok(RuntimeBundlePlan {
|
|
yaml: serde_yaml_ng::to_string(&parts.config).map_err(RuntimeBundleError::SerializeConfig)?,
|
|
assets: parts.assets,
|
|
remote_providers: parts.remote_providers,
|
|
})
|
|
}
|
|
|
|
pub fn remote_providers_of(config: &Mapping, config_root: &Path) -> Result<Vec<RemoteProviderRef>> {
|
|
Ok(collect_runtime_parts(Value::Mapping(config.clone()), config_root)?.remote_providers)
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct ProviderPathOwners {
|
|
remote: BTreeMap<String, Vec<(&'static str, Value)>>,
|
|
local: bool,
|
|
}
|
|
|
|
pub fn resolve_provider_path_conflicts(config: &mut Mapping, config_root: &Path) -> Result<()> {
|
|
let config_root = std::fs::canonicalize(config_root)?;
|
|
let mut paths = BTreeMap::<String, ProviderPathOwners>::new();
|
|
let mut reserved: HashSet<String> = GEO_ASSETS.iter().map(|name| (*name).to_owned()).collect();
|
|
for section in ["proxy-providers", "rule-providers"] {
|
|
let Some(providers) = config.get(section).and_then(Value::as_mapping) else {
|
|
continue;
|
|
};
|
|
for (name, provider) in providers {
|
|
let Some(raw_path) = provider.get("path").and_then(Value::as_str) else {
|
|
continue;
|
|
};
|
|
let is_remote = provider.get("type").and_then(Value::as_str) == Some("http");
|
|
let destination = if is_remote {
|
|
provider_destination(&config_root, raw_path)
|
|
} else {
|
|
local_provider_source(&config_root, raw_path)
|
|
.and_then(|source| destination_below_root(&config_root, &source))
|
|
.or_else(|_| provider_destination(&config_root, raw_path))
|
|
};
|
|
let Ok(destination) = destination else {
|
|
continue;
|
|
};
|
|
reserved.insert(destination.clone());
|
|
let owners = paths.entry(destination).or_default();
|
|
match (is_remote, provider.get("url").and_then(Value::as_str)) {
|
|
(true, Some(url)) => owners
|
|
.remote
|
|
.entry(url.to_owned())
|
|
.or_default()
|
|
.push((section, name.clone())),
|
|
_ => owners.local = true,
|
|
}
|
|
}
|
|
}
|
|
|
|
for (destination, owners) in paths {
|
|
if owners.local || owners.remote.len() < 2 {
|
|
continue;
|
|
}
|
|
// Allocate every source in a conflict group, so declaration order cannot select a cache's owner.
|
|
for (url, providers) in owners.remote {
|
|
let replacement = allocate_provider_destination(&destination, &url, &mut reserved);
|
|
for (section, name) in providers {
|
|
config[section][&name]["path"] = Value::String(replacement.clone());
|
|
logging!(
|
|
warn,
|
|
Type::Config,
|
|
"provider {section}/{name:?} shared cache {destination:?} with another source; using {replacement:?}"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn allocate_provider_destination(destination: &str, url: &str, reserved: &mut HashSet<String>) -> String {
|
|
let mut digest = Sha256::new();
|
|
digest.update(destination.as_bytes());
|
|
digest.update([0]);
|
|
digest.update(url.as_bytes());
|
|
let digest = hex::encode(digest.finalize());
|
|
let path = Path::new(destination);
|
|
let extension = path
|
|
.extension()
|
|
.map_or_default(|ext| format!(".{}", ext.to_string_lossy()));
|
|
let mut suffix = String::new();
|
|
let mut sequence = 0;
|
|
loop {
|
|
let candidate = path
|
|
.with_file_name(format!("cvr-{digest}{suffix}{extension}"))
|
|
.to_string_lossy()
|
|
.replace('\\', "/");
|
|
if reserved.insert(candidate.clone()) {
|
|
return candidate;
|
|
}
|
|
sequence += 1;
|
|
suffix = format!("-{sequence}");
|
|
}
|
|
}
|
|
|
|
fn collect_runtime_parts(mut config: Value, config_root: &Path) -> Result<RuntimeParts> {
|
|
let config_root = std::fs::canonicalize(config_root)?;
|
|
let mut assets = Vec::new();
|
|
let mut destinations = HashSet::new();
|
|
let mut remote_providers = Vec::new();
|
|
|
|
collect_provider_assets(
|
|
&mut config,
|
|
"proxy-providers",
|
|
&config_root,
|
|
&mut destinations,
|
|
&mut assets,
|
|
&mut remote_providers,
|
|
)?;
|
|
collect_provider_assets(
|
|
&mut config,
|
|
"rule-providers",
|
|
&config_root,
|
|
&mut destinations,
|
|
&mut assets,
|
|
&mut remote_providers,
|
|
)?;
|
|
collect_ssh_private_keys(&mut config, &config_root, &mut destinations, &mut assets);
|
|
for filename in GEO_ASSETS {
|
|
let source = config_root.join(filename);
|
|
if source.is_file() && destinations.insert((*filename).to_string()) {
|
|
assets.push(RuntimeAsset {
|
|
source: std::fs::canonicalize(&source)?.to_string_lossy().into_owned(),
|
|
destination: (*filename).to_string(),
|
|
});
|
|
}
|
|
}
|
|
|
|
Ok(RuntimeParts {
|
|
config,
|
|
assets,
|
|
remote_providers,
|
|
})
|
|
}
|
|
|
|
/// Rewrites provider paths and records copied assets separately from core-downloaded providers.
|
|
/// Remote URLs let the service decide whether a core download cache remains reusable.
|
|
fn collect_provider_assets(
|
|
config: &mut Value,
|
|
section: &'static str,
|
|
config_root: &Path,
|
|
destinations: &mut HashSet<String>,
|
|
assets: &mut Vec<RuntimeAsset>,
|
|
remote_providers: &mut Vec<RemoteProviderRef>,
|
|
) -> Result<()> {
|
|
let Some(providers) = config
|
|
.as_mapping_mut()
|
|
.and_then(|mapping| mapping.get_mut(section))
|
|
.and_then(Value::as_mapping_mut)
|
|
else {
|
|
return Ok(());
|
|
};
|
|
|
|
for (name, provider) in providers.iter_mut() {
|
|
let Some(provider) = provider.as_mapping_mut() else {
|
|
continue;
|
|
};
|
|
let Some(raw_path) = provider.get("path").and_then(Value::as_str) else {
|
|
continue;
|
|
};
|
|
// Match mihomo's `type` classification and leave malformed-but-accepted providers untouched.
|
|
let is_remote = provider.get("type").and_then(Value::as_str) == Some("http");
|
|
let url = provider.get("url").and_then(Value::as_str).map(str::to_owned);
|
|
let destination = match (is_remote, url) {
|
|
(true, Some(url)) => {
|
|
let destination = provider_destination(config_root, raw_path)?;
|
|
// Reserve remote destinations too; copied and downloaded files must not collide.
|
|
if destinations.insert(destination.clone()) {
|
|
remote_providers.push(RemoteProviderRef {
|
|
section,
|
|
name: name.as_str().unwrap_or_default().to_owned(),
|
|
destination: destination.clone(),
|
|
url,
|
|
});
|
|
} else {
|
|
match remote_providers
|
|
.iter()
|
|
.find(|declared| declared.destination == destination)
|
|
{
|
|
Some(declared) if declared.url == url => {}
|
|
Some(_) => return Err(RuntimeBundleError::DifferentSources(destination)),
|
|
None => return Err(RuntimeBundleError::DuplicateDestination(destination)),
|
|
}
|
|
}
|
|
destination
|
|
}
|
|
(true, None) => {
|
|
logging!(
|
|
warn,
|
|
Type::Config,
|
|
"remote provider {name:?} declares no url; leaving its path to the core"
|
|
);
|
|
continue;
|
|
}
|
|
(false, _) => {
|
|
let Ok(source) = local_provider_source(config_root, raw_path) else {
|
|
logging!(
|
|
warn,
|
|
Type::Config,
|
|
"local provider {name:?} is unavailable at {raw_path:?}; leaving its path to the core"
|
|
);
|
|
continue;
|
|
};
|
|
let destination = destination_below_root(config_root, &source)?;
|
|
if destinations.insert(destination.clone()) {
|
|
assets.push(RuntimeAsset {
|
|
source: source.to_string_lossy().into_owned(),
|
|
destination: destination.clone(),
|
|
});
|
|
}
|
|
destination
|
|
}
|
|
};
|
|
provider.insert(Value::String("path".to_owned()), Value::String(destination));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Stages file-based ssh keys, which mihomo resolves against the core home directory.
|
|
fn collect_ssh_private_keys(
|
|
config: &mut Value,
|
|
config_root: &Path,
|
|
destinations: &mut HashSet<String>,
|
|
assets: &mut Vec<RuntimeAsset>,
|
|
) {
|
|
let Some(config) = config.as_mapping_mut() else {
|
|
return;
|
|
};
|
|
// mihomo parses `payload` for every provider type, eagerly as fallback proxies for file and http.
|
|
let mut proxy_lists = Vec::new();
|
|
for (key, value) in config.iter_mut() {
|
|
match key.as_str() {
|
|
Some("proxies") => proxy_lists.extend(value.as_sequence_mut()),
|
|
Some("proxy-providers") => proxy_lists.extend(
|
|
value
|
|
.as_mapping_mut()
|
|
.into_iter()
|
|
.flat_map(Mapping::values_mut)
|
|
.filter_map(|provider| provider.get_mut("payload").and_then(Value::as_sequence_mut)),
|
|
),
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
for proxy in proxy_lists.into_iter().flatten().filter_map(Value::as_mapping_mut) {
|
|
if proxy.get("type").and_then(Value::as_str) != Some("ssh") {
|
|
continue;
|
|
}
|
|
// mihomo reads any value containing "PRIVATE KEY" as inline key material.
|
|
let Some(raw_path) = proxy
|
|
.get("private-key")
|
|
.and_then(Value::as_str)
|
|
.filter(|key| !key.is_empty() && !key.contains("PRIVATE KEY") && is_local_path(key))
|
|
else {
|
|
continue;
|
|
};
|
|
let Some(source) = local_provider_source(config_root, raw_path)
|
|
.ok()
|
|
.filter(|source| source.is_file())
|
|
else {
|
|
let name = proxy.get("name").and_then(Value::as_str).unwrap_or_default();
|
|
logging!(
|
|
warn,
|
|
Type::Config,
|
|
"ssh proxy {name:?} private key is unavailable below the config root; leaving it to the core"
|
|
);
|
|
continue;
|
|
};
|
|
// A root-level alias keeps the user's path away from service name rules and mihomo's inline-key test.
|
|
let source = source.to_string_lossy().into_owned();
|
|
let destination = allocate_provider_destination("ssh-private-key", &source, destinations);
|
|
assets.push(RuntimeAsset {
|
|
source,
|
|
destination: destination.clone(),
|
|
});
|
|
proxy.insert(Value::String("private-key".to_owned()), Value::String(destination));
|
|
}
|
|
}
|
|
|
|
/// Resolving a UNC or device path would authenticate to its server, so those stay with the core.
|
|
fn is_local_path(raw_path: &str) -> bool {
|
|
match Path::new(raw_path).components().next() {
|
|
Some(Component::Prefix(prefix)) => matches!(prefix.kind(), Prefix::Disk(_) | Prefix::VerbatimDisk(_)),
|
|
_ => true,
|
|
}
|
|
}
|
|
|
|
fn local_provider_source(config_root: &Path, raw_path: &str) -> Result<PathBuf> {
|
|
let path = Path::new(raw_path);
|
|
let source = if path.is_absolute() {
|
|
path.to_path_buf()
|
|
} else {
|
|
config_root.join(path)
|
|
};
|
|
let canonical_source =
|
|
std::fs::canonicalize(&source).map_err(|error| RuntimeBundleError::LocalProviderUnavailable {
|
|
path: source,
|
|
source: error,
|
|
})?;
|
|
canonical_source
|
|
.strip_prefix(config_root)
|
|
.map_err(|_| RuntimeBundleError::LocalProviderOutsideRoot(canonical_source.clone()))?;
|
|
Ok(canonical_source)
|
|
}
|
|
|
|
fn provider_destination(config_root: &Path, raw_path: &str) -> Result<String> {
|
|
let path = Path::new(raw_path);
|
|
if path
|
|
.components()
|
|
.any(|component| matches!(component, Component::ParentDir))
|
|
{
|
|
return Err(RuntimeBundleError::DestinationOutsideRuntime);
|
|
}
|
|
if path.is_absolute() {
|
|
let normalized = canonicalize_with_missing_tail(path)?;
|
|
destination_below_root(config_root, &normalized)
|
|
} else {
|
|
normalized_destination(path)
|
|
}
|
|
}
|
|
|
|
fn canonicalize_with_missing_tail(path: &Path) -> Result<PathBuf> {
|
|
let mut ancestor = path;
|
|
let mut tail = Vec::new();
|
|
while !ancestor.exists() {
|
|
let name = ancestor
|
|
.file_name()
|
|
.ok_or_else(|| RuntimeBundleError::MissingAncestor(path.to_owned()))?;
|
|
tail.push(name.to_owned());
|
|
ancestor = ancestor
|
|
.parent()
|
|
.ok_or_else(|| RuntimeBundleError::MissingAncestor(path.to_owned()))?;
|
|
}
|
|
let mut normalized = std::fs::canonicalize(ancestor)?;
|
|
for component in tail.iter().rev() {
|
|
normalized.push(component);
|
|
}
|
|
Ok(normalized)
|
|
}
|
|
|
|
fn destination_below_root(config_root: &Path, path: &Path) -> Result<String> {
|
|
let relative = path
|
|
.strip_prefix(config_root)
|
|
.map_err(|_| RuntimeBundleError::ProviderOutsideRoot(path.to_owned()))?;
|
|
normalized_destination(relative)
|
|
}
|
|
|
|
fn normalized_destination(relative: &Path) -> Result<String> {
|
|
let mut destination = PathBuf::new();
|
|
for component in relative.components() {
|
|
match component {
|
|
Component::Normal(component) => destination.push(component),
|
|
Component::CurDir => {}
|
|
_ => return Err(RuntimeBundleError::LocalDestinationOutsideRuntime),
|
|
}
|
|
}
|
|
if destination.as_os_str().is_empty() {
|
|
return Err(RuntimeBundleError::EmptyDestination);
|
|
}
|
|
Ok(destination.to_string_lossy().replace('\\', "/"))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::{collect_runtime_bundle, remote_providers_of, resolve_provider_path_conflicts};
|
|
|
|
#[tokio::test]
|
|
async fn conflicting_sources_get_separate_paths_in_config_bundle_and_readback()
|
|
-> std::result::Result<(), Box<dyn std::error::Error>> {
|
|
let root = std::env::temp_dir().join(format!("clash-verge-provider-paths-{}", std::process::id()));
|
|
std::fs::create_dir_all(&root)?;
|
|
let mut config: serde_yaml_ng::Mapping = serde_yaml_ng::from_str(
|
|
"rule-providers:\n AppleDev:\n type: http\n url: https://one.example/AppleDev.yaml\n path: ./providers/rule/AppleDev.yaml\n mirror:\n type: http\n url: https://two.example/AppleDev.yaml\n path: providers/rule/AppleDev.yaml\n",
|
|
)?;
|
|
let original = config.clone();
|
|
let root = std::fs::canonicalize(root)?;
|
|
let mut reordered = config.clone();
|
|
reordered["rule-providers"] = serde_yaml_ng::from_str(&format!(
|
|
"mirror:\n type: http\n url: https://two.example/AppleDev.yaml\n path: {}\nAppleDev:\n type: http\n url: https://one.example/AppleDev.yaml\n path: providers/rule/AppleDev.yaml\n",
|
|
root.join("providers/rule/AppleDev.yaml").display(),
|
|
))?;
|
|
|
|
resolve_provider_path_conflicts(&mut config, &root)?;
|
|
let config_file = root.join("config.yaml");
|
|
std::fs::write(&config_file, serde_yaml_ng::to_string(&config)?)?;
|
|
let bundle = collect_runtime_bundle(&config_file).await?;
|
|
let paths = &config["rule-providers"];
|
|
assert_ne!(paths["AppleDev"]["path"], paths["mirror"]["path"]);
|
|
resolve_provider_path_conflicts(&mut reordered, &root)?;
|
|
assert_eq!(
|
|
config, reordered,
|
|
"path allocation must not depend on declaration order or path spelling"
|
|
);
|
|
let resolved = config.clone();
|
|
resolve_provider_path_conflicts(&mut config, &root)?;
|
|
assert_eq!(
|
|
config, resolved,
|
|
"resolving an already generated config must be idempotent"
|
|
);
|
|
|
|
assert_eq!(serde_yaml_ng::from_str::<serde_yaml_ng::Mapping>(&bundle.yaml)?, config);
|
|
let readback = remote_providers_of(&config, &root)?;
|
|
assert_eq!(bundle.remote_providers.len(), 2);
|
|
for declared in readback {
|
|
assert!(bundle.remote_providers.contains(&declared));
|
|
assert_eq!(config[declared.section][&declared.name]["path"], declared.destination);
|
|
assert_eq!(
|
|
config[declared.section][&declared.name]["url"],
|
|
original[declared.section][&declared.name]["url"]
|
|
);
|
|
}
|
|
std::fs::remove_dir_all(root)?;
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn allocated_provider_paths_do_not_claim_another_declared_path()
|
|
-> std::result::Result<(), Box<dyn std::error::Error>> {
|
|
let root = std::env::temp_dir();
|
|
let original: serde_yaml_ng::Mapping = serde_yaml_ng::from_str(
|
|
"proxy-providers:\n a:\n type: http\n url: https://one.example/p.yaml\n path: ./providers/shared.yaml\nrule-providers:\n b:\n type: http\n url: https://two.example/r.yaml\n path: ./providers/shared.yaml\n",
|
|
)?;
|
|
let mut config = original.clone();
|
|
resolve_provider_path_conflicts(&mut config, &root)?;
|
|
let allocated = config["proxy-providers"]["a"]["path"].clone();
|
|
let mut config = original;
|
|
config["rule-providers"]["reserved"] = serde_yaml_ng::from_str("type: file\npath: placeholder")?;
|
|
config["rule-providers"]["reserved"]["path"] = allocated.clone();
|
|
resolve_provider_path_conflicts(&mut config, &root)?;
|
|
assert_ne!(config["proxy-providers"]["a"]["path"], allocated);
|
|
assert_ne!(
|
|
config["proxy-providers"]["a"]["path"],
|
|
config["rule-providers"]["b"]["path"]
|
|
);
|
|
assert_eq!(config["rule-providers"]["reserved"]["path"], allocated);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn provider_path_resolution_leaves_shared_sources_and_local_files_unchanged()
|
|
-> std::result::Result<(), Box<dyn std::error::Error>> {
|
|
let mut config: serde_yaml_ng::Mapping = serde_yaml_ng::from_str(
|
|
"rule-providers:\n a:\n type: http\n url: https://one.example/r.yaml\n path: ./rules/shared.yaml\n b:\n type: http\n url: https://one.example/r.yaml\n path: ./rules/shared.yaml\n local:\n type: file\n path: ./rules/local.yaml\n remote:\n type: http\n url: https://two.example/r.yaml\n path: ./rules/local.yaml\n mirror:\n type: http\n url: https://three.example/r.yaml\n path: ./rules/local.yaml\n",
|
|
)?;
|
|
let original = config.clone();
|
|
resolve_provider_path_conflicts(&mut config, &std::env::temp_dir())?;
|
|
assert_eq!(config, original);
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn collects_only_local_providers_and_existing_geo_assets()
|
|
-> std::result::Result<(), Box<dyn std::error::Error>> {
|
|
let root = std::env::temp_dir().join(format!("clash-verge-runtime-bundle-{}", std::process::id()));
|
|
std::fs::create_dir_all(root.join("providers"))?;
|
|
std::fs::write(root.join("providers/local.yaml"), b"proxies: []\n")?;
|
|
std::fs::write(root.join("Country.mmdb"), b"geo")?;
|
|
let config = root.join("config.yaml");
|
|
let absolute_local = root.join("providers/local.yaml");
|
|
let absolute_remote = root.join("providers/remote.yaml");
|
|
std::fs::write(
|
|
&config,
|
|
format!(
|
|
"proxy-providers:\n local:\n type: file\n path: {}\n remote:\n type: http\n url: https://example.com/p.yaml\n path: {}\nrule-providers: {{}}\n",
|
|
absolute_local.display(),
|
|
absolute_remote.display()
|
|
),
|
|
)?;
|
|
|
|
let bundle = collect_runtime_bundle(&config).await?;
|
|
|
|
let bundled_config: serde_yaml_ng::Value = serde_yaml_ng::from_str(&bundle.yaml)?;
|
|
assert_eq!(
|
|
bundled_config["proxy-providers"]["local"]["path"].as_str(),
|
|
Some("providers/local.yaml")
|
|
);
|
|
assert_eq!(
|
|
bundled_config["proxy-providers"]["remote"]["path"].as_str(),
|
|
Some("providers/remote.yaml")
|
|
);
|
|
assert!(!bundle.yaml.contains(&root.to_string_lossy().into_owned()));
|
|
assert!(
|
|
bundle
|
|
.assets
|
|
.iter()
|
|
.any(|asset| asset.destination == "providers/local.yaml")
|
|
);
|
|
assert!(bundle.assets.iter().any(|asset| asset.destination == "Country.mmdb"));
|
|
assert!(!bundle.assets.iter().any(|asset| asset.destination.contains("remote")));
|
|
// Remote declarations preserve service-side cache reuse.
|
|
assert_eq!(
|
|
bundle
|
|
.remote_providers
|
|
.iter()
|
|
.map(|provider| (provider.destination.as_str(), provider.url.as_str()))
|
|
.collect::<Vec<_>>(),
|
|
[("providers/remote.yaml", "https://example.com/p.yaml")],
|
|
"the declaration must pair the rewritten destination with the url, not the original path"
|
|
);
|
|
assert!(
|
|
!bundle
|
|
.assets
|
|
.iter()
|
|
.any(|asset| asset.destination.contains("geoip.dat"))
|
|
);
|
|
std::fs::remove_dir_all(root)?;
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn two_names_for_one_remote_file_are_folded_rather_than_refused()
|
|
-> std::result::Result<(), Box<dyn std::error::Error>> {
|
|
// Identical repeats are one service-side file and are safe to fold.
|
|
let root = std::env::temp_dir().join(format!("clash-verge-bundle-dup-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&root);
|
|
std::fs::create_dir_all(&root)?;
|
|
let config = root.join("config.yaml");
|
|
std::fs::write(
|
|
&config,
|
|
"rule-providers:\n a:\n type: http\n url: https://one.example/x.yaml\n path: ./rules/x.yaml\n b:\n type: http\n url: https://one.example/x.yaml\n path: ./rules/x.yaml\n",
|
|
)?;
|
|
|
|
let bundle = collect_runtime_bundle(&config).await?;
|
|
|
|
assert_eq!(bundle.remote_providers.len(), 1, "one file, declared twice");
|
|
assert_eq!(bundle.remote_providers[0].destination, "rules/x.yaml");
|
|
std::fs::remove_dir_all(root)?;
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn two_sources_for_one_remote_file_are_refused() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|
let root = std::env::temp_dir().join(format!("clash-verge-bundle-conflict-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&root);
|
|
std::fs::create_dir_all(&root)?;
|
|
let config = root.join("config.yaml");
|
|
std::fs::write(
|
|
&config,
|
|
"rule-providers:\n a:\n type: http\n url: https://one.example/x.yaml\n path: ./rules/x.yaml\n b:\n type: http\n url: https://two.example/x.yaml\n path: ./rules/x.yaml\n",
|
|
)?;
|
|
|
|
let Err(error) = collect_runtime_bundle(&config).await else {
|
|
return Err("two sources cannot own one file".into());
|
|
};
|
|
|
|
assert!(error.to_string().contains("two different sources"), "{error}");
|
|
std::fs::remove_dir_all(root)?;
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn an_odd_provider_is_left_alone_instead_of_failing_the_whole_bundle()
|
|
-> std::result::Result<(), Box<dyn std::error::Error>> {
|
|
// Odd providers accepted by mihomo must not block the rest of the bundle.
|
|
let root = std::env::temp_dir().join(format!("clash-verge-bundle-odd-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&root);
|
|
std::fs::create_dir_all(&root)?;
|
|
let config = root.join("config.yaml");
|
|
std::fs::write(
|
|
&config,
|
|
"rule-providers:\n nourl:\n type: http\n path: ./rules/nourl.yaml\n missing:\n type: inline\n url: https://one.example/i.yaml\n path: ./rules/missing.yaml\n good:\n type: http\n url: https://one.example/g.yaml\n path: ./rules/g.yaml\n",
|
|
)?;
|
|
|
|
let bundle = collect_runtime_bundle(&config).await?;
|
|
|
|
assert_eq!(
|
|
bundle.remote_providers.len(),
|
|
1,
|
|
"only the provider that could be classified is declared"
|
|
);
|
|
assert_eq!(bundle.remote_providers[0].destination, "rules/g.yaml");
|
|
let reserialized: serde_yaml_ng::Value = serde_yaml_ng::from_str(&bundle.yaml)?;
|
|
assert_eq!(
|
|
reserialized["rule-providers"]["nourl"]["path"].as_str(),
|
|
Some("./rules/nourl.yaml"),
|
|
"an unclassifiable provider keeps the path the core was given"
|
|
);
|
|
std::fs::remove_dir_all(root)?;
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rejects_provider_paths_outside_the_config_root() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|
let root = std::env::temp_dir().join(format!("clash-verge-runtime-bundle-outside-{}", std::process::id()));
|
|
std::fs::create_dir_all(&root)?;
|
|
let config = root.join("config.yaml");
|
|
let outside = root
|
|
.parent()
|
|
.ok_or("test root has no parent")?
|
|
.join(format!("outside-{}.yaml", std::process::id()));
|
|
std::fs::write(
|
|
&config,
|
|
format!(
|
|
"proxy-providers:\n remote:\n type: http\n url: https://example.com/p.yaml\n path: {}\n",
|
|
outside.display()
|
|
),
|
|
)?;
|
|
|
|
let Err(error) = collect_runtime_bundle(&config).await else {
|
|
return Err("outside provider path must be rejected".into());
|
|
};
|
|
|
|
assert!(error.to_string().contains("outside the config root"));
|
|
std::fs::remove_dir_all(root)?;
|
|
Ok(())
|
|
}
|
|
}
|