From 4c82a6cddbae70e542876c6e2b2ce316a29ff886 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:24:35 +0800 Subject: [PATCH] perf(enhance): marshal scripts by value and diff fields by fingerprint - scripts cross into boa via JsValue::from_json/to_json instead of the JSON text round-trip (config clone + json string + UTF-16 JsString + stringify + re-parse all disappear); size guards use an allocation-free approximate walk - merge documents and tun/dns patches move values instead of deep-cloning whole sub-trees per pass - authoritative-field snapshots hash dns/hosts/tun values instead of cloning them around every chain item (9 snapshots per enhance) - proxy-name cleanup sets build from &str in one conversion --- src-tauri/src/enhance/field.rs | 6 +- src-tauri/src/enhance/merge.rs | 6 +- src-tauri/src/enhance/mod.rs | 138 +++++++++++++++++++------------- src-tauri/src/enhance/script.rs | 127 ++++++++++++++++++----------- src-tauri/src/enhance/tun.rs | 12 +-- 5 files changed, 171 insertions(+), 118 deletions(-) diff --git a/src-tauri/src/enhance/field.rs b/src-tauri/src/enhance/field.rs index 038980f55..1b1567fed 100644 --- a/src-tauri/src/enhance/field.rs +++ b/src-tauri/src/enhance/field.rs @@ -24,12 +24,12 @@ fn lowercase_key(key: &str) -> Value { Value::from(key.as_str()) } -pub fn use_lowercase(config: &Mapping) -> Mapping { +pub fn use_lowercase(config: Mapping) -> Mapping { let mut lowercased = Mapping::new(); - for (key, value) in config.iter() { + for (key, value) in config { if let Some(key_str) = key.as_str() { - lowercased.insert(lowercase_key(key_str), value.clone()); + lowercased.insert(lowercase_key(key_str), value); } } lowercased diff --git a/src-tauri/src/enhance/merge.rs b/src-tauri/src/enhance/merge.rs index c1c00fc05..6c0ea375a 100644 --- a/src-tauri/src/enhance/merge.rs +++ b/src-tauri/src/enhance/merge.rs @@ -16,7 +16,7 @@ fn deep_merge(a: &mut Value, b: Value) { } } -pub fn use_merge(merge: &Mapping, mut config: Mapping) -> Mapping { +pub fn use_merge(merge: Mapping, mut config: Mapping) -> Mapping { for (key, value) in use_lowercase(merge) { match (config.get_mut(&key), value) { (Some(Value::Mapping(existing)), Value::Mapping(overlay)) if key.as_str() == Some("dns") => { @@ -79,7 +79,7 @@ fn test_merge() -> anyhow::Result<()> { let merge = serde_yaml_ng::from_str::(merge)?; let config = serde_yaml_ng::from_str::(config)?; - let result = use_merge(&merge, config); + let result = use_merge(merge.clone(), config); assert_eq!(result["dns"]["nameserver-policy"], merge["dns"]["nameserver-policy"]); assert_eq!(result["dns"]["fallback-filter"], merge["dns"]["fallback-filter"]); assert_eq!(result["hosts"], merge["hosts"]); @@ -91,7 +91,7 @@ fn test_merge() -> anyhow::Result<()> { let cleared = serde_yaml_ng::from_str::( "dns: {enable: false, nameserver-policy: {}, fallback-filter: {}, nameserver: []}\nhosts: {}", )?; - let result = use_merge(&cleared, result); + let result = use_merge(cleared.clone(), result); assert_eq!(result["dns"], cleared["dns"]); assert_eq!(result["hosts"], cleared["hosts"]); diff --git a/src-tauri/src/enhance/mod.rs b/src-tauri/src/enhance/mod.rs index e84e2c20c..ae18918ec 100644 --- a/src-tauri/src/enhance/mod.rs +++ b/src-tauri/src/enhance/mod.rs @@ -28,6 +28,7 @@ use parking_lot::Mutex; use serde_yaml_ng::{Mapping, Value}; use smartstring::alias::String; use std::collections::{HashMap, HashSet}; +use std::hash::{Hash as _, Hasher as _}; use tokio::fs; type ResultLog = Vec<(String, String)>; @@ -236,13 +237,13 @@ async fn process_override_items( mut result_map: HashMap, merge_item: ChainItem, script_item: ChainItem, - profile_name: &String, + profile_name: &str, authoritative: &AuthoritativeFields, ) -> (Mapping, Vec, HashMap) { if let ChainType::Merge(merge) = merge_item.data { exists_keys.extend(use_keys(&merge)); let before = authoritative.current(&config); - config = use_merge(&merge, config); + config = use_merge(merge, config); let notes: ResultLog = authoritative .overridden(&before, &authoritative.current(&config)) .into_iter() @@ -255,7 +256,7 @@ async fn process_override_items( if let ChainType::Script(script) = script_item.data { let before = authoritative.current(&config); - let (res_config, changed_keys, mut logs) = use_script(script, config, profile_name.clone()).await; + let (res_config, changed_keys, mut logs) = use_script(script, config, profile_name.into()).await; exists_keys.extend(changed_keys); config = res_config; logs.extend( @@ -320,15 +321,45 @@ struct AuthoritativeFields { tun: Mapping, dns: Mapping, hosts: Option, + fingerprints: FieldFingerprints, +} + +#[derive(Default)] +struct FieldFingerprints { + control_plane: Vec>, + tun: Vec>, + dns: Vec>, + hosts: Option, +} + +fn fingerprint(value: Option<&Value>) -> Option { + let value = value?; + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + value.hash(&mut hasher); + Some(hasher.finish()) } impl AuthoritativeFields { fn capture(config: &Mapping, gui_tun_keys: &[Value], mut dns_settings: Mapping) -> Self { + let control_plane = snapshot_control_plane(config); + let tun = snapshot_tun(config, gui_tun_keys); + let dns = take_mapping(&mut dns_settings, "dns"); + let hosts = dns_settings.remove("hosts"); + let fingerprints = FieldFingerprints { + control_plane: CONTROL_PLANE_KEYS + .iter() + .map(|&key| fingerprint(control_plane.get(Value::from(key)))) + .collect(), + tun: tun.iter().map(|(_, value)| fingerprint(Some(value))).collect(), + dns: dns.iter().map(|(_, value)| fingerprint(Some(value))).collect(), + hosts: fingerprint(hosts.as_ref()), + }; Self { - control_plane: snapshot_control_plane(config), - tun: snapshot_tun(config, gui_tun_keys), - dns: take_mapping(&mut dns_settings, "dns"), - hosts: dns_settings.remove("hosts"), + control_plane, + tun, + dns, + hosts, + fingerprints, } } @@ -346,43 +377,50 @@ impl AuthoritativeFields { config } - /// The owned values as `config` holds them now, for diffing one override. - fn current(&self, config: &Mapping) -> Self { - let tun_keys: Vec = self.tun.keys().cloned().collect(); + /// What `config` holds now, for diffing one override. + fn current(&self, config: &Mapping) -> FieldFingerprints { + let tun = config.get("tun").and_then(Value::as_mapping); let dns = config.get("dns").and_then(Value::as_mapping); - Self { - control_plane: snapshot_control_plane(config), - tun: snapshot_tun(config, &tun_keys), + FieldFingerprints { + control_plane: CONTROL_PLANE_KEYS + .iter() + .map(|&key| fingerprint(config.get(key))) + .collect(), + tun: self + .tun + .keys() + .map(|key| fingerprint(tun.and_then(|map| map.get(key)))) + .collect(), dns: self .dns .keys() - .filter_map(|key| Some((key.clone(), dns?.get(key)?.clone()))) + .map(|key| fingerprint(dns.and_then(|map| map.get(key)))) .collect(), - hosts: self.hosts.as_ref().and_then(|_| config.get("hosts")).cloned(), + hosts: self.hosts.as_ref().and_then(|_| fingerprint(config.get("hosts"))), } } /// Owned keys an override moved off the app value; `enforce` discards those writes. - fn overridden(&self, before: &Self, after: &Self) -> Vec { + fn overridden(&self, before: &FieldFingerprints, after: &FieldFingerprints) -> Vec { let mut keys = Vec::new(); - for &key in CONTROL_PLANE_KEYS { - if before.control_plane.get(key) != after.control_plane.get(key) - && after.control_plane.get(key) != self.control_plane.get(key) + for (index, &key) in CONTROL_PLANE_KEYS.iter().enumerate() { + if before.control_plane[index] != after.control_plane[index] + && after.control_plane[index] != self.fingerprints.control_plane[index] { keys.push(key.into()); } } - for key in self.tun.keys() { - if before.tun.get(key) != after.tun.get(key) && after.tun.get(key) != self.tun.get(key) { + for (index, key) in self.tun.keys().enumerate() { + if before.tun[index] != after.tun[index] && after.tun[index] != self.fingerprints.tun[index] { keys.push(format!("tun.{}", key.as_str().unwrap_or_default()).into()); } } - for key in self.dns.keys() { - if before.dns.get(key) != after.dns.get(key) && after.dns.get(key) != self.dns.get(key) { + for (index, key) in self.dns.keys().enumerate() { + if before.dns[index] != after.dns[index] && after.dns[index] != self.fingerprints.dns[index] { keys.push(format!("dns.{}", key.as_str().unwrap_or_default()).into()); } } - if self.hosts.is_some() && before.hosts != after.hosts && after.hosts != self.hosts { + if self.hosts.is_some() && before.hosts != after.hosts && after.hosts != self.fingerprints.hosts { keys.push("hosts".into()); } keys @@ -540,6 +578,7 @@ fn ensure_tun_default_mtu(mut config: Mapping) -> Mapping { config } +} /// Merges the app's clash config over the profile; switches are parameters to keep it pure. fn merge_default_config( @@ -553,16 +592,16 @@ fn merge_default_config( ) -> Mapping { for (key, value) in clash_config.into_iter() { if key.as_str() == Some("tun") { - let mut tun = config.get_mut("tun").map_or_else(Mapping::new, |val| { - val.as_mapping().cloned().unwrap_or_else(Mapping::new) - }); - let patch_tun = value.as_mapping().cloned().unwrap_or_else(Mapping::new); - for (key, value) in patch_tun.into_iter() { - if !value.is_null() { - tun.insert(key, value); + if let Value::Mapping(patch_tun) = value { + let mut tun = take_mapping(&mut config, "tun"); + for (key, value) in patch_tun { + if !value.is_null() { + tun.insert(key, value); + } } + config.insert(Value::from("tun"), Value::Mapping(tun)); + } } - config.insert("tun".into(), tun.into()); } else { if key.as_str() == Some("socks-port") && !socks_enabled { config.remove("socks-port"); @@ -649,11 +688,8 @@ fn cleanup_proxy_groups(mut config: Mapping) -> Mapping { .map_or_default(|seq| { seq.iter() .filter_map(|item| match item { - Value::Mapping(map) => map - .get("name") - .and_then(Value::as_str) - .map(|name| name.to_owned().into()), - Value::String(name) => Some(name.to_owned().into()), + Value::Mapping(map) => map.get("name").and_then(Value::as_str).map(Into::into), + Value::String(name) => Some(name.as_str().into()), _ => None, }) .collect::>() @@ -872,7 +908,7 @@ pub async fn enhance( result_map, global_merge, global_script, - &profile_name, + profile_name.as_str(), &authoritative, ) .await; @@ -883,12 +919,12 @@ pub async fn enhance( result_map, merge_item, script_item, - &profile_name, + profile_name.as_str(), &authoritative, ) .await; - notify_discarded_keys(authoritative.overridden(&authoritative, &authoritative.current(&config))); + notify_discarded_keys(authoritative.overridden(&authoritative.fingerprints, &authoritative.current(&config))); let config = authoritative.enforce(config); let config = ensure_tun_default_mtu(config); let config = ensure_lan_bind_address(config); @@ -1407,17 +1443,14 @@ mod tests { uid: uid.into(), data: ChainType::Merge(merge.clone()), }; - let profile_name = "test-profile".into(); + let profile_name = "test-profile"; let (config, keys, logs) = process_override_items( config, vec![], HashMap::new(), merge_item("global-merge"), - ChainItem { - uid: "global-script".into(), - data: ChainType::Script(script.into()), - }, - &profile_name, + ChainItem::to_script("global-script", script), + profile_name, &authoritative, ) .await; @@ -1426,11 +1459,8 @@ mod tests { keys, logs, merge_item("profile-merge"), - ChainItem { - uid: "profile-script".into(), - data: ChainType::Script(script.into()), - }, - &profile_name, + ChainItem::to_script("profile-script", script), + profile_name, &authoritative, ) .await; @@ -1506,7 +1536,7 @@ mod tests { ), }; - let profile_name = "test-profile".into(); + let profile_name = "test-profile"; let authoritative = AuthoritativeFields::capture(&config, &[], Mapping::new()); let (config, exists_keys, result_map) = process_override_items( config, @@ -1514,7 +1544,7 @@ mod tests { HashMap::new(), global_merge, global_script, - &profile_name, + profile_name, &authoritative, ) .await; @@ -1524,7 +1554,7 @@ mod tests { result_map, profile_merge, profile_script, - &profile_name, + profile_name, &authoritative, ) .await; diff --git a/src-tauri/src/enhance/script.rs b/src-tauri/src/enhance/script.rs index b69ee7494..da22bf415 100644 --- a/src-tauri/src/enhance/script.rs +++ b/src-tauri/src/enhance/script.rs @@ -1,7 +1,7 @@ use crate::{process::AsyncHandler, utils::tmpl}; use super::field::use_lowercase; -use anyhow::{Error, Result}; +use anyhow::Result; use boa_engine::{Context, JsString, JsValue, Source, js_string, native_function::NativeFunction, property::Attribute}; use clash_verge_logging::{Type, logging}; use parking_lot::Mutex; @@ -22,14 +22,26 @@ type EvalOutcome = (Result, Vec<(String, String)>) pub(super) async fn use_script(script: String, config: Mapping, name: String) -> ScriptResult { // The template is identity-plus-lowercasing; skip the Boa round-trip. if script == tmpl::ITEM_SCRIPT { - return (use_lowercase(&config), vec![], vec![]); + return (use_lowercase(config), vec![], vec![]); } - let json = match serde_json::to_string(&use_lowercase(&config)) { - Ok(json) if json.len() <= MAX_JSON_SIZE => json, - Ok(_) => { - let message = "Configuration size exceeds maximum allowed size"; - return (config, vec![], vec![("exception".into(), message.into())]); + let json = match serde_json::to_value(&config) { + Ok(mut json) => { + if let Some(map) = json.as_object_mut() { + let mut lowered = serde_json::Map::with_capacity(map.len()); + for (key, value) in std::mem::take(map) { + let mut key = key; + key.make_ascii_lowercase(); + lowered.insert(key, value); + } + *map = lowered; + } + if json_approx_size(&json) <= MAX_JSON_SIZE { + json + } else { + let message = "Configuration size exceeds maximum allowed size"; + return (config, vec![], vec![("exception".into(), message.into())]); + } } Err(err) => return (config, vec![], vec![("exception".into(), err.to_string().into())]), }; @@ -66,7 +78,7 @@ pub(super) async fn use_script(script: String, config: Mapping, name: String) -> } /// Console logs survive every failure path. -fn eval_script(script: &str, config_json: &str, name: &String) -> EvalOutcome { +fn eval_script(script: &str, config_json: &serde_json::Value, name: &String) -> EvalOutcome { let mut context = Context::default(); context @@ -131,12 +143,11 @@ fn eval_script(script: &str, config_json: &str, name: &String) -> EvalOutcome { });"#, )); - // Bind the JSON instead of embedding it in program source; Boa's parser never chews it. - if let Err(err) = context.register_global_property( - js_string!("__verge_config__"), - JsValue::from(JsString::from(config_json)), - Attribute::all(), - ) { + let config_value = match JsValue::from_json(config_json, &mut context) { + Ok(value) => value, + Err(err) => return bail(format!("failed to bind config for script: {err}")), + }; + if let Err(err) = context.register_global_property(js_string!("__verge_config__"), config_value, Attribute::all()) { return bail(format!("failed to bind config for script: {err}")); } @@ -149,31 +160,61 @@ fn eval_script(script: &str, config_json: &str, name: &String) -> EvalOutcome { let code = format!( r"try{{ {script}; - JSON.stringify(main(JSON.parse(globalThis.__verge_config__),'{safe_name}')||'') + main(globalThis.__verge_config__,'{safe_name}') }} catch(err) {{ `__error_flag__ ${{err.toString()}}` }}" ); let result = match context.eval(Source::from_bytes(code.as_str())) { - Ok(result) if result.is_string() => result, - _ => return bail("main function should return object".to_owned()), - }; - let result = match result.to_string(&mut context) { Ok(result) => result, - Err(e) => return bail(format!("Failed to convert JS result to string: {e}")), + Err(err) => { + logging!( + error, + Type::Config, + "Script execution error: {err}. Script name: {name}" + ); + return bail("Script execution failed".to_owned()); + } }; - let result = match result.to_std_string() { - Ok(result) => result, - Err(_) => return bail("Failed to convert JS string to std string".to_owned()), + // The JS wrapper funnels exceptions into a marker string; surface its text. + if result.is_string() { + let text = result + .to_string(&mut context) + .ok() + .and_then(|text| text.to_std_string().ok()) + .unwrap_or_default(); + if text.trim_start().starts_with("__error_flag__") { + logging!( + error, + Type::Config, + "Script execution error: {}. Script name: {name}", + text.trim_start()["__error_flag__".len()..].trim() + ); + return bail("Script execution failed".to_owned()); + } + } + if !result.is_object() { + return bail("main function should return object".to_owned()); + } + let result = match result.to_json(&mut context) { + Ok(Some(result)) => result, + Ok(None) => return bail("main function should return object".to_owned()), + Err(err) => { + logging!( + error, + Type::Config, + "Script execution error: {err}. Script name: {name}" + ); + return bail("Script execution failed".to_owned()); + } }; - - if result.len() > MAX_JSON_SIZE { + if json_approx_size(&result) > MAX_JSON_SIZE { return bail("Script result exceeds maximum allowed size".to_owned()); } - match parse_json_safely(&result) { - Ok(config) => (Ok(use_lowercase(&config)), outputs.lock().to_vec()), + match serde_json::from_value::(result) { + Ok(config) => (Ok(use_lowercase(config)), outputs.lock().to_vec()), Err(err) => { logging!( error, @@ -185,27 +226,17 @@ fn eval_script(script: &str, config_json: &str, name: &String) -> EvalOutcome { } } -fn parse_json_safely(json_str: &str) -> Result { - if json_str.len() > MAX_JSON_SIZE { - anyhow::bail!("JSON string too large"); - } - - let json_str = strip_outer_quotes(json_str); - Ok(serde_json::from_str::(json_str)?) -} - -// 安全地移除外层引号 -fn strip_outer_quotes(s: &str) -> &str { - let s = s.trim(); - - if s.len() < 2 { - return s; - } - - if (s.starts_with('"') && s.ends_with('"')) || (s.starts_with('\'') && s.ends_with('\'')) { - &s[1..s.len() - 1] - } else { - s +/// Serialized-size estimate without building the serialized string; a DoS guard, not an exact byte count. +fn json_approx_size(value: &serde_json::Value) -> usize { + match value { + serde_json::Value::String(s) => s.len() + 2, + serde_json::Value::Object(map) => map + .iter() + .map(|(key, value)| key.len() + 4 + json_approx_size(value)) + .sum(), + serde_json::Value::Array(items) => items.iter().map(json_approx_size).sum(), + serde_json::Value::Number(n) => n.to_string().len(), + serde_json::Value::Bool(_) | serde_json::Value::Null => 4, } } diff --git a/src-tauri/src/enhance/tun.rs b/src-tauri/src/enhance/tun.rs index e56810135..61362c9b5 100644 --- a/src-tauri/src/enhance/tun.rs +++ b/src-tauri/src/enhance/tun.rs @@ -8,18 +8,10 @@ macro_rules! revise { } pub fn use_tun(mut config: Mapping, enable: bool) -> Mapping { - let tun_key = Value::from("tun"); - let tun_val = config.get(&tun_key); - let mut tun_val = tun_val.map_or_else(Mapping::new, |val| { - val.as_mapping().cloned().unwrap_or_else(Mapping::new) - }); + let mut tun_val = super::take_mapping(&mut config, "tun"); if enable { - let dns_key = Value::from("dns"); - let dns_val = config.get(&dns_key); - let mut dns_val = dns_val.map_or_else(Mapping::new, |val| { - val.as_mapping().cloned().unwrap_or_else(Mapping::new) - }); + let mut dns_val = super::take_mapping(&mut config, "dns"); let ipv6_key = Value::from("ipv6"); let ipv6_val = config.get(&ipv6_key).and_then(|v| v.as_bool()).unwrap_or(false);