From 4168a87983e4f16e0cd8eb510444788e1a0bc0fd Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Tue, 1 Dec 2026 22:58:35 +0800 Subject: [PATCH] perf(runtime-bundle): read remote providers from borrowed configuration --- crates/clash-verge-runtime-bundle/src/lib.rs | 161 ++++++++++++------- 1 file changed, 103 insertions(+), 58 deletions(-) diff --git a/crates/clash-verge-runtime-bundle/src/lib.rs b/crates/clash-verge-runtime-bundle/src/lib.rs index 24902c6e4..0e904d7f3 100644 --- a/crates/clash-verge-runtime-bundle/src/lib.rs +++ b/crates/clash-verge-runtime-bundle/src/lib.rs @@ -56,7 +56,26 @@ pub async fn collect_runtime_bundle(config_file: &Path) -> Result Result> { - Ok(collect_runtime_parts(Value::Mapping(config.clone()), config_root)?.remote_providers) + let config_root = std::fs::canonicalize(config_root)?; + let mut destinations = HashSet::new(); + let mut remote_providers = Vec::new(); + 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 { + plan_provider( + section, + name, + provider, + &config_root, + &mut destinations, + None, + &mut remote_providers, + )?; + } + } + Ok(remote_providers) } #[derive(Default)] @@ -206,70 +225,96 @@ fn collect_provider_assets( }; 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. - let reserved = TriStr::from(destination.as_str()); - if destinations.insert(reserved.clone()) { - remote_providers.push(RemoteProviderRef { - section, - name: name.as_str().unwrap_or_default().to_owned(), - destination: reserved, - 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)), - } + if let Some(destination) = plan_provider( + section, + name, + provider, + config_root, + destinations, + Some(assets), + remote_providers, + )? && let Some(provider) = provider.as_mapping_mut() + { + provider.insert(Value::String("path".to_owned()), Value::String(destination)); + } + } + Ok(()) +} + +fn plan_provider( + section: &'static str, + name: &Value, + provider: &Value, + config_root: &Path, + destinations: &mut HashSet, + assets: Option<&mut Vec>, + remote_providers: &mut Vec, +) -> Result> { + let Some(raw_path) = provider + .as_mapping() + .and_then(|provider| provider.get("path")) + .and_then(Value::as_str) + else { + return Ok(None); + }; + // 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); + 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. + let reserved = TriStr::from(destination.as_str()); + if destinations.insert(reserved.clone()) { + remote_providers.push(RemoteProviderRef { + section, + name: name.as_str().unwrap_or_default().to_owned(), + destination: reserved, + url: url.to_owned(), + }); + } 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) => { + destination + } + (true, None) => { + logging!( + warn, + Type::Config, + "remote provider {name:?} declares no url; leaving its path to the core" + ); + return Ok(None); + } + (false, _) => { + let Ok(source) = local_provider_source(config_root, raw_path) else { logging!( warn, Type::Config, - "remote provider {name:?} declares no url; leaving its path to the core" + "local provider {name:?} is unavailable at {raw_path:?}; leaving its path to the core" ); - continue; + return Ok(None); + }; + let destination = destination_below_root(config_root, &source)?; + let reserved = TriStr::from(destination.as_str()); + if destinations.insert(reserved.clone()) + && let Some(assets) = assets + { + assets.push(RuntimeAsset { + source: source.to_string_lossy().into_owned(), + destination: reserved, + }); } - (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)?; - let reserved = TriStr::from(destination.as_str()); - if destinations.insert(reserved.clone()) { - assets.push(RuntimeAsset { - source: source.to_string_lossy().into_owned(), - destination: reserved, - }); - } - destination - } - }; - provider.insert(Value::String("path".to_owned()), Value::String(destination)); - } - Ok(()) + destination + } + }; + Ok(Some(destination)) } /// Stages file-based ssh keys, which mihomo resolves against the core home directory.