mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-04 05:00:05 +08:00
refactor(config): accumulate profile keys and logs during rendering
This commit is contained in:
@@ -79,17 +79,17 @@ pub struct RenderedConfig {
|
||||
pub discarded_keys: Vec<String>,
|
||||
}
|
||||
|
||||
async fn process_override_items<Fut: Future<Output = ScriptOutcome> + Send>(
|
||||
async fn apply_merge_and_script<Fut: Future<Output = ScriptOutcome> + Send>(
|
||||
mut config: Mapping,
|
||||
mut exists_keys: Vec<String>,
|
||||
mut result_map: HashMap<String, ResultLog>,
|
||||
profile_keys: &mut HashSet<String>,
|
||||
chain_logs: &mut HashMap<String, ResultLog>,
|
||||
(merge_item, script_item): (Arc<ChainItem>, Arc<ChainItem>),
|
||||
profile_name: &str,
|
||||
authoritative: &AuthoritativeFields,
|
||||
execute_script: &(impl Fn(String, serde_json::Value, String) -> Fut + Sync),
|
||||
) -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
|
||||
) -> Mapping {
|
||||
if let ChainType::Merge(merge) = &merge_item.data {
|
||||
exists_keys.extend(lowercase_config_keys(merge));
|
||||
profile_keys.extend(lowercase_config_keys(merge));
|
||||
let before = authoritative.current(&config);
|
||||
config = use_merge(merge.clone(), config);
|
||||
let notes: ResultLog = authoritative
|
||||
@@ -98,7 +98,7 @@ async fn process_override_items<Fut: Future<Output = ScriptOutcome> + Send>(
|
||||
.map(discarded_note)
|
||||
.collect();
|
||||
if !notes.is_empty() {
|
||||
result_map.entry(merge_item.uid.clone()).or_default().extend(notes);
|
||||
chain_logs.entry(merge_item.uid.clone()).or_default().extend(notes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ async fn process_override_items<Fut: Future<Output = ScriptOutcome> + Send>(
|
||||
let before = authoritative.current(&config);
|
||||
let (res_config, changed_keys, mut logs) =
|
||||
use_script(script.clone(), config, profile_name.into(), execute_script).await;
|
||||
exists_keys.extend(changed_keys);
|
||||
profile_keys.extend(changed_keys);
|
||||
config = res_config;
|
||||
logs.extend(
|
||||
authoritative
|
||||
@@ -114,13 +114,13 @@ async fn process_override_items<Fut: Future<Output = ScriptOutcome> + Send>(
|
||||
.into_iter()
|
||||
.map(discarded_note),
|
||||
);
|
||||
result_map.insert(script_item.uid.clone(), logs);
|
||||
chain_logs.insert(script_item.uid.clone(), logs);
|
||||
}
|
||||
|
||||
(config, exists_keys, result_map)
|
||||
config
|
||||
}
|
||||
|
||||
fn process_seq_items(
|
||||
fn apply_sequence_overrides(
|
||||
mut config: Mapping,
|
||||
rules_item: Arc<ChainItem>,
|
||||
proxies_item: Arc<ChainItem>,
|
||||
@@ -652,20 +652,14 @@ pub async fn render<Fut: Future<Output = ScriptOutcome> + Send>(
|
||||
#[cfg(target_os = "linux")]
|
||||
tproxy_enabled,
|
||||
} = settings;
|
||||
let config = profile.config;
|
||||
let merge_item = profile.merge_item;
|
||||
let script_item = profile.script_item;
|
||||
let rules_item = profile.rules_item;
|
||||
let proxies_item = profile.proxies_item;
|
||||
let groups_item = profile.groups_item;
|
||||
let global_merge = profile.global_merge;
|
||||
let global_script = profile.global_script;
|
||||
let profile_name = profile.profile_name;
|
||||
|
||||
let result_map = HashMap::new();
|
||||
|
||||
let config = process_seq_items(config, rules_item, proxies_item, groups_item);
|
||||
let exists_keys = lowercase_config_keys(&config).collect::<Vec<_>>();
|
||||
let config = apply_sequence_overrides(
|
||||
profile.config,
|
||||
profile.rules_item,
|
||||
profile.proxies_item,
|
||||
profile.groups_item,
|
||||
);
|
||||
let mut profile_keys = lowercase_config_keys(&config).collect();
|
||||
let mut chain_logs = HashMap::new();
|
||||
let gui_tun_keys = gui_tun_keys(&clash_config);
|
||||
|
||||
let config = merge_default_config(
|
||||
@@ -682,34 +676,28 @@ pub async fn render<Fut: Future<Output = ScriptOutcome> + Send>(
|
||||
|
||||
let config = apply_builtin_scripts(config, enable_builtin);
|
||||
let config = use_tun(config, enable_tun);
|
||||
let (config, dns_settings) = match dns_config {
|
||||
let (mut config, dns_settings) = match dns_config {
|
||||
Some(dns_config) => merge_dns_config(config, dns_config),
|
||||
None => (config, Mapping::new()),
|
||||
};
|
||||
|
||||
let authoritative = AuthoritativeFields::capture(&config, &gui_tun_keys, dns_settings);
|
||||
|
||||
let (config, exists_keys, result_map) = process_override_items(
|
||||
config,
|
||||
exists_keys,
|
||||
result_map,
|
||||
(global_merge, global_script),
|
||||
profile_name.as_str(),
|
||||
&authoritative,
|
||||
&execute_script,
|
||||
)
|
||||
.await;
|
||||
|
||||
let (config, exists_keys, result_map) = process_override_items(
|
||||
config,
|
||||
exists_keys,
|
||||
result_map,
|
||||
(merge_item, script_item),
|
||||
profile_name.as_str(),
|
||||
&authoritative,
|
||||
&execute_script,
|
||||
)
|
||||
.await;
|
||||
for overrides in [
|
||||
(profile.global_merge, profile.global_script),
|
||||
(profile.merge_item, profile.script_item),
|
||||
] {
|
||||
config = apply_merge_and_script(
|
||||
config,
|
||||
&mut profile_keys,
|
||||
&mut chain_logs,
|
||||
overrides,
|
||||
&profile.profile_name,
|
||||
&authoritative,
|
||||
&execute_script,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
let discarded_keys = authoritative.overridden(&authoritative.fingerprints, &authoritative.current(&config));
|
||||
let config = authoritative.enforce(config);
|
||||
@@ -721,8 +709,8 @@ pub async fn render<Fut: Future<Output = ScriptOutcome> + Send>(
|
||||
|
||||
RenderedConfig {
|
||||
config,
|
||||
exists_keys: exists_keys.into_iter().collect(),
|
||||
logs: result_map,
|
||||
exists_keys: profile_keys,
|
||||
logs: chain_logs,
|
||||
discarded_keys,
|
||||
}
|
||||
}
|
||||
@@ -1184,10 +1172,10 @@ mod authoritative_field_tests {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
AuthoritativeFields, ChainItem, ChainType, Mapping, ScriptOutcome, String, cleanup_proxy_groups,
|
||||
ensure_lan_bind_address, lowercase_config_keys, process_override_items,
|
||||
AuthoritativeFields, ChainItem, ChainType, Mapping, ScriptOutcome, String, apply_merge_and_script,
|
||||
cleanup_proxy_groups, ensure_lan_bind_address, lowercase_config_keys,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::future::{Ready, ready};
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -1236,10 +1224,12 @@ hosts: {script.example: "192.0.2.4"}"#;
|
||||
})
|
||||
};
|
||||
let profile_name = "test-profile";
|
||||
let (config, keys, logs) = process_override_items(
|
||||
let mut keys = HashSet::new();
|
||||
let mut logs = HashMap::new();
|
||||
let config = apply_merge_and_script(
|
||||
config,
|
||||
vec![],
|
||||
HashMap::new(),
|
||||
&mut keys,
|
||||
&mut logs,
|
||||
(
|
||||
merge_item("global-merge"),
|
||||
Arc::new(ChainItem {
|
||||
@@ -1252,10 +1242,10 @@ hosts: {script.example: "192.0.2.4"}"#;
|
||||
&apply_script_fixture,
|
||||
)
|
||||
.await;
|
||||
let (config, _, logs) = process_override_items(
|
||||
let config = apply_merge_and_script(
|
||||
config,
|
||||
keys,
|
||||
logs,
|
||||
&mut keys,
|
||||
&mut logs,
|
||||
(
|
||||
merge_item("profile-merge"),
|
||||
Arc::new(ChainItem {
|
||||
@@ -1298,7 +1288,7 @@ hosts: {script.example: "192.0.2.4"}"#;
|
||||
r"{global-merge-wins: other, global-script-wins: other, profile-merge-wins: other,
|
||||
profile-script-wins: other, nested: {winner: other}, dns: {enable: true}, tun: {enable: true}}",
|
||||
);
|
||||
let exists_keys = lowercase_config_keys(&config).collect();
|
||||
let mut exists_keys = lowercase_config_keys(&config).collect();
|
||||
config.insert("application-only".into(), true.into());
|
||||
|
||||
let global_merge = Arc::new(ChainItem {
|
||||
@@ -1331,20 +1321,21 @@ hosts: {script.example: "192.0.2.4"}"#;
|
||||
|
||||
let profile_name = "test-profile";
|
||||
let authoritative = AuthoritativeFields::capture(&config, &[], Mapping::new());
|
||||
let (config, exists_keys, result_map) = process_override_items(
|
||||
let mut logs = HashMap::new();
|
||||
let config = apply_merge_and_script(
|
||||
config,
|
||||
exists_keys,
|
||||
HashMap::new(),
|
||||
&mut exists_keys,
|
||||
&mut logs,
|
||||
(global_merge, global_script),
|
||||
profile_name,
|
||||
&authoritative,
|
||||
&apply_script_fixture,
|
||||
)
|
||||
.await;
|
||||
let (config, exists_keys, _) = process_override_items(
|
||||
let config = apply_merge_and_script(
|
||||
config,
|
||||
exists_keys,
|
||||
result_map,
|
||||
&mut exists_keys,
|
||||
&mut logs,
|
||||
(profile_merge, profile_script),
|
||||
profile_name,
|
||||
&authoritative,
|
||||
@@ -1364,7 +1355,7 @@ hosts: {script.example: "192.0.2.4"}"#;
|
||||
.and_then(serde_yaml_ng::Value::as_str),
|
||||
Some("profile-script")
|
||||
);
|
||||
assert!(!exists_keys.contains(&"application-only".into()));
|
||||
assert!(!exists_keys.contains("application-only"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user