mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-05 22:20:25 +08:00
* chore(clippy): scope crate lint policy Keep panic-on-error lints at application target roots and scope signal dependencies to Linux. * docs(agents): define Rust backend architecture boundaries Document which layer owns Rust capabilities, fallible inputs, background work, and crate validation. * refactor(draft): type async publication conflicts Return typed conflicts for async Draft edits. Release the lock and edit permit before converting the error, and exercise overlapping updates. * refactor(logging): use typed logger errors Expose crate-owned logger errors and preserve the underlying error sources. * refactor(subscription): extract response decoding Keep HTTP and persistence in the app. Decode response metadata and framing in the crate, and fall back when the server filename is invalid. * refactor(profile): extract profile models and index rules Move profile models and index rules into the crate, preserving serialization and returning a typed missing-profile error. * refactor(profile): separate subscription refresh and scheduling policy Separate download and scheduling policy from the subscription use case while retaining the existing write path. * refactor(subscription): decode deep links in the subscription crate * refactor(profile): extract selected node recovery rules * docs(agents): define dependencies between capability crates * refactor(task): move retry primitives into the task crate * refactor(backup): extract backup ZIP writing * refactor(subscription): keep profile writes outside proxy fallback * refactor(runtime-bundle): extract runtime bundle planning Plan provider destinations and assets in the crate. Adapt the plan to the Service wire format at the call site. * perf(runtime-bundle): share destination strings with tristr Share destination storage within bundle planning and preserve the Service wire strings and provider readback. * refactor(profile): extract script validation and execution Share the Boa validation and execution API while preserving the save-side timeout and diagnostics. * refactor(config): extract configuration renderer Render from inputs supplied by the app, reuse the script crate, and preserve the default template fast path. * refactor(proxy): extract proxy view and its use case Project the Mihomo snapshot into the existing IPC DTO and move use-case orchestration out of the command. * refactor(config): inject the profile script executor * refactor(config): centralize Mihomo YAML output * perf(runtime-bundle): read remote providers from borrowed configuration * chore(clippy): scope crate lint policy Keep panic-on-error lints at application target roots and scope signal dependencies to Linux. * docs(agents): define Rust backend architecture boundaries Document which layer owns Rust capabilities, fallible inputs, background work, and crate validation. * refactor(draft): type async publication conflicts Return typed conflicts for async Draft edits. Release the lock and edit permit before converting the error, and exercise overlapping updates. * refactor(logging): use typed logger errors Expose crate-owned logger errors and preserve the underlying error sources. * refactor(subscription): extract response decoding Keep HTTP and persistence in the app. Decode response metadata and framing in the crate, and fall back when the server filename is invalid. * refactor(profile): extract profile models and index rules Move profile models and index rules into the crate, preserving serialization and returning a typed missing-profile error. * refactor(profile): separate subscription refresh and scheduling policy Separate download and scheduling policy from the subscription use case while retaining the existing write path. * refactor(subscription): decode deep links in the subscription crate * refactor(profile): extract selected node recovery rules * docs(agents): define dependencies between capability crates * refactor(task): move retry primitives into the task crate * refactor(backup): extract backup ZIP writing * refactor(subscription): keep profile writes outside proxy fallback * fix(profile): preserve delays for future update timestamps * fix(subscription): preserve filenames from response headers and URL paths * fix(subscription): separate deep link parameters from nested URLs * refactor(profile): look up the refresh target once * refactor(subscription): pass the proxy directly to each download * refactor(profile): build companion options in one place * refactor(profile): remove companions from the deleted profile * style(profile): keep profile fields together * refactor(profile): name index operations after what they change * refactor(profile): call the core reload directly * refactor(backup): move archive validation to the restore change * refactor(config): name the runtime rendering step * refactor(config): remove the unused runtime patch API * refactor(config): accumulate profile keys and logs during rendering * refactor(config): load cached chain items directly * refactor(script): keep console state local to evaluation * refactor(proxy): read group order and scope in one pass * refactor(config): reuse sequence storage when applying profile edits * refactor(proxy): collect provider snapshots in display order * refactor(proxy): build the view from its input * refactor(runtime-bundle): share provider section order * fix(config): fill blank IPv6 fake IP ranges in TUN mode * fix(config): keep runtime field sorting stable * perf(config): reuse profile sequence storage during rendering * fix(script): check the evaluated profile entry point * fix(script): support runtime console methods during validation * perf(script): count integer digits without formatting JSON * perf(proxy): resolve unique members without building scope caches * fix(runtime-bundle): reject mixed owners of a provider path * perf(runtime-bundle): reuse the normalized destination buffer * fix(script): reject oversized names before escaping
304 lines
8.9 KiB
Rust
304 lines
8.9 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use serde_yaml_ng::{Mapping, Sequence, Value};
|
|
use std::collections::HashSet;
|
|
use tristr::TriStr;
|
|
|
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
|
pub struct SeqMap {
|
|
pub prepend: Sequence,
|
|
pub append: Sequence,
|
|
pub delete: Vec<String>,
|
|
}
|
|
|
|
fn proxy_name(proxy: &Value) -> Option<&str> {
|
|
match proxy {
|
|
Value::Mapping(proxy) => proxy.get("name").and_then(Value::as_str),
|
|
Value::String(name) => Some(name),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn is_selector_group(group_map: &Mapping) -> bool {
|
|
group_map.get("type").and_then(Value::as_str).is_some_and(|value| {
|
|
let value = value.to_ascii_lowercase();
|
|
value == "select" || value == "selector"
|
|
})
|
|
}
|
|
|
|
pub fn use_seq(seq: SeqMap, mut config: Mapping, field: &str) -> Mapping {
|
|
let SeqMap {
|
|
prepend,
|
|
append,
|
|
delete,
|
|
} = seq;
|
|
|
|
let added_proxy_names = if field == "proxies" {
|
|
prepend
|
|
.iter()
|
|
.chain(&append)
|
|
.filter_map(proxy_name)
|
|
.map(TriStr::from)
|
|
.collect::<Vec<_>>()
|
|
} else {
|
|
Vec::new()
|
|
};
|
|
|
|
let mut updated_items = match config.remove(field) {
|
|
Some(Value::Sequence(items)) => items,
|
|
_ => Sequence::new(),
|
|
};
|
|
if !delete.is_empty() {
|
|
updated_items.retain(|item| {
|
|
let name = match item {
|
|
Value::String(name) => Some(name),
|
|
Value::Mapping(mapping) => match mapping.get("name") {
|
|
Some(Value::String(name)) => Some(name),
|
|
_ => None,
|
|
},
|
|
_ => None,
|
|
};
|
|
name.is_none_or(|name| !delete.contains(name))
|
|
});
|
|
}
|
|
updated_items.splice(..0, prepend);
|
|
updated_items.extend(append);
|
|
config.insert(Value::String(field.into()), Value::Sequence(updated_items));
|
|
|
|
if field != "proxies" {
|
|
return config;
|
|
}
|
|
|
|
let Some(proxy_groups_value) = config.remove("proxy-groups") else {
|
|
return config;
|
|
};
|
|
|
|
let Value::Sequence(mut proxy_groups) = proxy_groups_value else {
|
|
config.insert(Value::String("proxy-groups".into()), proxy_groups_value);
|
|
return config;
|
|
};
|
|
|
|
let mut appended_to_selector = false;
|
|
for group in &mut proxy_groups {
|
|
let Value::Mapping(group_map) = group else {
|
|
continue;
|
|
};
|
|
let mut group_proxies = match group_map.remove("proxies") {
|
|
Some(Value::Sequence(mut proxies)) => {
|
|
if !delete.is_empty() {
|
|
proxies.retain(|member| !matches!(member, Value::String(name) if delete.contains(name)));
|
|
}
|
|
Some(proxies)
|
|
}
|
|
Some(value) => {
|
|
group_map.insert(Value::String("proxies".into()), value);
|
|
None
|
|
}
|
|
None => None,
|
|
};
|
|
|
|
if !appended_to_selector && !added_proxy_names.is_empty() && is_selector_group(group_map) {
|
|
let base_group_proxies = group_proxies.unwrap_or_else(Sequence::new);
|
|
let mut merged_proxies = Sequence::new();
|
|
let mut seen_proxy_names = HashSet::new();
|
|
for name in &added_proxy_names {
|
|
if seen_proxy_names.insert(name.clone()) {
|
|
merged_proxies.push(Value::String(name.as_str().to_owned()));
|
|
}
|
|
}
|
|
for value in base_group_proxies {
|
|
if let Value::String(name) = &value
|
|
&& !seen_proxy_names.insert(TriStr::from(name.as_str()))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
merged_proxies.push(value);
|
|
}
|
|
group_proxies = Some(merged_proxies);
|
|
appended_to_selector = true;
|
|
}
|
|
|
|
if let Some(group_proxies) = group_proxies {
|
|
group_map.insert(Value::String("proxies".into()), Value::Sequence(group_proxies));
|
|
}
|
|
}
|
|
config.insert(Value::String("proxy-groups".into()), Value::Sequence(proxy_groups));
|
|
|
|
config
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
#[allow(unused_imports)]
|
|
use serde_yaml_ng::Value;
|
|
|
|
#[test]
|
|
#[allow(clippy::expect_used)]
|
|
fn test_delete_proxy_and_references() {
|
|
let config_str = r#"
|
|
proxies:
|
|
- name: "proxy1"
|
|
type: "ss"
|
|
- name: "proxy2"
|
|
type: "vmess"
|
|
proxy-groups:
|
|
- name: "group1"
|
|
type: "select"
|
|
proxies:
|
|
- "proxy1"
|
|
- "proxy2"
|
|
- name: "group2"
|
|
type: "select"
|
|
proxies:
|
|
- "proxy1"
|
|
"#;
|
|
let mut config: Mapping = serde_yaml_ng::from_str(config_str).expect("Failed to parse test config YAML");
|
|
|
|
let seq = SeqMap {
|
|
prepend: Sequence::new(),
|
|
append: Sequence::new(),
|
|
delete: vec!["proxy1".to_string()],
|
|
};
|
|
|
|
config = use_seq(seq, config, "proxies");
|
|
|
|
// Check if proxy1 is removed from proxies
|
|
let proxies = config
|
|
.get("proxies")
|
|
.expect("proxies field should exist")
|
|
.as_sequence()
|
|
.expect("proxies should be a sequence");
|
|
assert_eq!(proxies.len(), 1);
|
|
assert_eq!(
|
|
proxies[0]
|
|
.as_mapping()
|
|
.expect("proxy should be a mapping")
|
|
.get("name")
|
|
.expect("proxy should have name")
|
|
.as_str()
|
|
.expect("name should be string"),
|
|
"proxy2"
|
|
);
|
|
|
|
// Check if proxy1 is removed from all groups
|
|
let groups = config
|
|
.get("proxy-groups")
|
|
.expect("proxy-groups field should exist")
|
|
.as_sequence()
|
|
.expect("proxy-groups should be a sequence");
|
|
let group1_proxies = groups[0]
|
|
.as_mapping()
|
|
.expect("group should be a mapping")
|
|
.get("proxies")
|
|
.expect("group should have proxies")
|
|
.as_sequence()
|
|
.expect("group proxies should be a sequence");
|
|
let group2_proxies = groups[1]
|
|
.as_mapping()
|
|
.expect("group should be a mapping")
|
|
.get("proxies")
|
|
.expect("group should have proxies")
|
|
.as_sequence()
|
|
.expect("group proxies should be a sequence");
|
|
|
|
assert_eq!(group1_proxies.len(), 1);
|
|
assert_eq!(
|
|
group1_proxies[0].as_str().expect("proxy name should be string"),
|
|
"proxy2"
|
|
);
|
|
assert_eq!(group2_proxies.len(), 0);
|
|
}
|
|
|
|
#[test]
|
|
#[allow(clippy::expect_used)]
|
|
fn test_add_new_proxies_to_first_selector_group() {
|
|
let config_str = r#"
|
|
proxies:
|
|
- name: "proxy1"
|
|
type: "ss"
|
|
proxy-groups:
|
|
- name: "group1"
|
|
type: "select"
|
|
proxies:
|
|
- "proxy1"
|
|
- name: "group2"
|
|
type: "select"
|
|
proxies:
|
|
- "proxy1"
|
|
"#;
|
|
let mut config: Mapping = serde_yaml_ng::from_str(config_str).expect("Failed to parse test config YAML");
|
|
|
|
let prepend: Sequence = serde_yaml_ng::from_str(
|
|
r#"
|
|
- name: "proxy3"
|
|
type: "ss"
|
|
"#,
|
|
)
|
|
.expect("Failed to parse prepend proxies");
|
|
|
|
let append: Sequence = serde_yaml_ng::from_str(
|
|
r#"
|
|
- name: "proxy4"
|
|
type: "vmess"
|
|
"#,
|
|
)
|
|
.expect("Failed to parse append proxies");
|
|
|
|
let seq = SeqMap {
|
|
prepend,
|
|
append,
|
|
delete: vec![],
|
|
};
|
|
|
|
config = use_seq(seq, config, "proxies");
|
|
|
|
let groups = config
|
|
.get("proxy-groups")
|
|
.expect("proxy-groups field should exist")
|
|
.as_sequence()
|
|
.expect("proxy-groups should be a sequence");
|
|
let group1_proxies = groups[0]
|
|
.as_mapping()
|
|
.expect("group should be a mapping")
|
|
.get("proxies")
|
|
.expect("group should have proxies")
|
|
.as_sequence()
|
|
.expect("group proxies should be a sequence");
|
|
let names: Vec<&str> = group1_proxies.iter().filter_map(Value::as_str).collect();
|
|
assert_eq!(names, vec!["proxy3", "proxy4", "proxy1"]);
|
|
|
|
let group2_proxies = groups[1]
|
|
.as_mapping()
|
|
.expect("group should be a mapping")
|
|
.get("proxies")
|
|
.expect("group should have proxies")
|
|
.as_sequence()
|
|
.expect("group proxies should be a sequence");
|
|
let names: Vec<&str> = group2_proxies.iter().filter_map(Value::as_str).collect();
|
|
assert_eq!(names, vec!["proxy1"]);
|
|
}
|
|
|
|
#[test]
|
|
#[allow(clippy::expect_used)]
|
|
fn test_preserve_non_sequence_proxy_groups() {
|
|
let config_str = r#"
|
|
proxies:
|
|
- name: "proxy1"
|
|
type: "ss"
|
|
proxy-groups: "invalid"
|
|
"#;
|
|
let mut config: Mapping = serde_yaml_ng::from_str(config_str).expect("Failed to parse test config YAML");
|
|
|
|
let seq = SeqMap {
|
|
prepend: Sequence::new(),
|
|
append: Sequence::new(),
|
|
delete: vec!["proxy1".to_string()],
|
|
};
|
|
|
|
config = use_seq(seq, config, "proxies");
|
|
|
|
assert_eq!(config.get("proxy-groups").and_then(Value::as_str), Some("invalid"));
|
|
}
|
|
}
|