perf(runtime-bundle): read remote providers from borrowed configuration

This commit is contained in:
Tunglies
2026-12-01 22:58:35 +08:00
parent 5c05722f9a
commit bc5ed53a1b
+103 -58
View File
@@ -56,7 +56,26 @@ pub async fn collect_runtime_bundle(config_file: &Path) -> Result<RuntimeBundleP
}
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)
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<TriStr>,
assets: Option<&mut Vec<RuntimeAsset>>,
remote_providers: &mut Vec<RemoteProviderRef>,
) -> Result<Option<String>> {
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.