mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-28 08:20:04 +08:00
fix: preserve custom TUN fields and sync service provider caches
Limit GUI authority to saved TUN fields while preserving custom overrides. Sync provider caches after service startup, reloads and updates. Publish copies atomically with session, runtime and cache metadata checks.
This commit is contained in:
@@ -301,7 +301,6 @@ const CONTROL_PLANE_KEYS: &[&str] = &[
|
||||
"redir-port",
|
||||
#[cfg(target_os = "linux")]
|
||||
"tproxy-port",
|
||||
"tun",
|
||||
"mode",
|
||||
"allow-lan",
|
||||
"log-level",
|
||||
@@ -317,20 +316,23 @@ const CONTROL_PLANE_KEYS: &[&str] = &[
|
||||
/// override. As four separate calls that contract lived only in comments.
|
||||
struct AuthoritativeFields {
|
||||
control_plane: Mapping,
|
||||
tun: Mapping,
|
||||
/// Only tracked when the DNS page owns it; otherwise overrides may set `dns.ipv6` freely.
|
||||
dns_ipv6: Option<Value>,
|
||||
}
|
||||
|
||||
impl AuthoritativeFields {
|
||||
fn capture(config: &Mapping, enable_dns_settings: bool) -> Self {
|
||||
fn capture(config: &Mapping, gui_tun_keys: &[Value], enable_dns_settings: bool) -> Self {
|
||||
Self {
|
||||
control_plane: snapshot_control_plane(config),
|
||||
tun: snapshot_tun(config, gui_tun_keys),
|
||||
dns_ipv6: enable_dns_settings.then(|| snapshot_dns_ipv6(config)).flatten(),
|
||||
}
|
||||
}
|
||||
|
||||
fn enforce(self, config: Mapping) -> Mapping {
|
||||
let config = enforce_control_plane(config, self.control_plane);
|
||||
let config = enforce_tun(config, self.tun);
|
||||
enforce_dns_ipv6(config, self.dns_ipv6)
|
||||
}
|
||||
}
|
||||
@@ -359,6 +361,46 @@ fn enforce_control_plane(mut config: Mapping, snapshot: Mapping) -> Mapping {
|
||||
config
|
||||
}
|
||||
|
||||
/// Only saved GUI fields and the verge switch override profile settings.
|
||||
fn gui_tun_keys(clash_config: &Mapping) -> Vec<Value> {
|
||||
let mut keys = vec![Value::from("enable")];
|
||||
if let Some(Value::Mapping(tun)) = clash_config.get("tun") {
|
||||
keys.extend(
|
||||
constants::tun::GUI_KEYS
|
||||
.iter()
|
||||
.filter(|key| tun.contains_key(**key))
|
||||
.map(|key| Value::from(*key)),
|
||||
);
|
||||
}
|
||||
keys
|
||||
}
|
||||
|
||||
fn snapshot_tun(config: &Mapping, gui_tun_keys: &[Value]) -> Mapping {
|
||||
let mut snapshot = Mapping::new();
|
||||
if let Some(Value::Mapping(tun)) = config.get("tun") {
|
||||
for key in gui_tun_keys {
|
||||
if let Some(value) = tun.get(key) {
|
||||
snapshot.insert(key.clone(), value.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
snapshot
|
||||
}
|
||||
|
||||
fn enforce_tun(mut config: Mapping, snapshot: Mapping) -> Mapping {
|
||||
if snapshot.is_empty() {
|
||||
return config;
|
||||
}
|
||||
let mut tun = config
|
||||
.get("tun")
|
||||
.and_then(Value::as_mapping)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
tun.extend(snapshot);
|
||||
config.insert(Value::from("tun"), Value::Mapping(tun));
|
||||
config
|
||||
}
|
||||
|
||||
/// DNS 页权威的嵌套开关;只在 `enable_dns_settings` 时快照。
|
||||
fn snapshot_dns_ipv6(config: &Mapping) -> Option<Value> {
|
||||
config.get("dns")?.get("ipv6").cloned()
|
||||
@@ -742,6 +784,7 @@ pub async fn enhance(
|
||||
|
||||
let config = process_seq_items(config, rules_item, proxies_item, groups_item);
|
||||
let exists_keys = use_keys(&config).collect::<Vec<_>>();
|
||||
let gui_tun_keys = gui_tun_keys(&clash_config);
|
||||
|
||||
let config = merge_default_config(
|
||||
config,
|
||||
@@ -759,7 +802,7 @@ pub async fn enhance(
|
||||
let config = use_tun(config, enable_tun);
|
||||
let config = apply_dns_settings(config, enable_dns_settings).await;
|
||||
|
||||
let authoritative = AuthoritativeFields::capture(&config, enable_dns_settings);
|
||||
let authoritative = AuthoritativeFields::capture(&config, &gui_tun_keys, enable_dns_settings);
|
||||
|
||||
let (config, exists_keys, result_map) = process_global_items(
|
||||
config,
|
||||
@@ -1180,7 +1223,7 @@ mod authoritative_field_tests {
|
||||
#[test]
|
||||
fn an_override_cannot_change_a_field_the_app_owns() {
|
||||
let derived = config_with(&[("mode", Value::from("rule")), ("secret", Value::from("ours"))]);
|
||||
let authoritative = AuthoritativeFields::capture(&derived, false);
|
||||
let authoritative = AuthoritativeFields::capture(&derived, &[], false);
|
||||
|
||||
let overridden = config_with(&[("mode", Value::from("global")), ("secret", Value::from("theirs"))]);
|
||||
let result = authoritative.enforce(overridden);
|
||||
@@ -1193,7 +1236,7 @@ mod authoritative_field_tests {
|
||||
fn an_override_cannot_introduce_a_field_the_app_left_out() {
|
||||
// The app decided not to expose the external controller; a profile must not re-add it.
|
||||
let derived = Mapping::new();
|
||||
let authoritative = AuthoritativeFields::capture(&derived, false);
|
||||
let authoritative = AuthoritativeFields::capture(&derived, &[], false);
|
||||
|
||||
let overridden = config_with(&[("external-controller", Value::from("0.0.0.0:9090"))]);
|
||||
let result = authoritative.enforce(overridden);
|
||||
@@ -1204,7 +1247,7 @@ mod authoritative_field_tests {
|
||||
#[test]
|
||||
fn fields_the_app_does_not_own_survive_an_override() {
|
||||
let derived = config_with(&[("mode", Value::from("rule"))]);
|
||||
let authoritative = AuthoritativeFields::capture(&derived, false);
|
||||
let authoritative = AuthoritativeFields::capture(&derived, &[], false);
|
||||
|
||||
let overridden = config_with(&[("mode", Value::from("global")), ("profile-key", Value::from(1))]);
|
||||
let result = authoritative.enforce(overridden);
|
||||
@@ -1216,7 +1259,7 @@ mod authoritative_field_tests {
|
||||
fn dns_ipv6_is_only_reclaimed_when_the_dns_page_owns_it() {
|
||||
let derived = config_with(&[("dns", dns_with_ipv6(true))]);
|
||||
|
||||
let owned = AuthoritativeFields::capture(&derived, true);
|
||||
let owned = AuthoritativeFields::capture(&derived, &[], true);
|
||||
let restored = owned.enforce(config_with(&[("dns", dns_with_ipv6(false))]));
|
||||
assert_eq!(
|
||||
restored.get(Value::from("dns")).and_then(|dns| dns.get("ipv6")),
|
||||
@@ -1224,7 +1267,7 @@ mod authoritative_field_tests {
|
||||
"with the DNS page on, the app's value wins"
|
||||
);
|
||||
|
||||
let unowned = AuthoritativeFields::capture(&derived, false);
|
||||
let unowned = AuthoritativeFields::capture(&derived, &[], false);
|
||||
let left_alone = unowned.enforce(config_with(&[("dns", dns_with_ipv6(false))]));
|
||||
assert_eq!(
|
||||
left_alone.get(Value::from("dns")).and_then(|dns| dns.get("ipv6")),
|
||||
@@ -1236,7 +1279,7 @@ mod authoritative_field_tests {
|
||||
#[test]
|
||||
fn restoring_dns_ipv6_never_invents_a_dns_block() {
|
||||
let derived = config_with(&[("dns", dns_with_ipv6(true))]);
|
||||
let authoritative = AuthoritativeFields::capture(&derived, true);
|
||||
let authoritative = AuthoritativeFields::capture(&derived, &[], true);
|
||||
|
||||
// An override removed DNS entirely; reinstating just `ipv6` would be a half-config.
|
||||
let result = authoritative.enforce(Mapping::new());
|
||||
|
||||
Reference in New Issue
Block a user