From 027295d99560bed7206f17fd56936a73240339ad Mon Sep 17 00:00:00 2001 From: GyDi Date: Thu, 16 Feb 2023 23:52:55 +0800 Subject: [PATCH] feat: support to disable clash fields filter --- src-tauri/src/config/verge.rs | 5 +++++ src-tauri/src/enhance/field.rs | 6 +++++- src-tauri/src/enhance/merge.rs | 3 +-- src-tauri/src/enhance/mod.rs | 21 +++++++++++---------- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src-tauri/src/config/verge.rs b/src-tauri/src/config/verge.rs index 8db5cac..164a829 100644 --- a/src-tauri/src/config/verge.rs +++ b/src-tauri/src/config/verge.rs @@ -66,6 +66,9 @@ pub struct IVerge { /// 默认的延迟测试连接 pub default_latency_test: Option, + /// 支持关闭字段过滤,避免meta的新字段都被过滤掉,默认为真 + pub enable_clash_fields: Option, + /// 是否使用内部的脚本支持,默认为真 pub enable_builtin_enhanced: Option, @@ -120,6 +123,7 @@ impl IVerge { proxy_guard_duration: Some(30), auto_close_connection: Some(true), enable_builtin_enhanced: Some(true), + enable_clash_fields: Some(true), ..Self::default() } } @@ -163,6 +167,7 @@ impl IVerge { patch!(default_latency_test); patch!(enable_builtin_enhanced); patch!(proxy_layout_column); + patch!(enable_clash_fields); } /// 在初始化前尝试拿到单例端口的值 diff --git a/src-tauri/src/enhance/field.rs b/src-tauri/src/enhance/field.rs index e6f5752..893e521 100644 --- a/src-tauri/src/enhance/field.rs +++ b/src-tauri/src/enhance/field.rs @@ -68,7 +68,11 @@ pub fn use_valid_fields(mut valid: Vec) -> Vec { .collect() } -pub fn use_filter(config: Mapping, filter: &Vec) -> Mapping { +pub fn use_filter(config: Mapping, filter: &Vec, enable: bool) -> Mapping { + if !enable { + return config; + } + let mut ret = Mapping::new(); for (key, value) in config.into_iter() { diff --git a/src-tauri/src/enhance/merge.rs b/src-tauri/src/enhance/merge.rs index 7f6b89d..20342c9 100644 --- a/src-tauri/src/enhance/merge.rs +++ b/src-tauri/src/enhance/merge.rs @@ -1,7 +1,6 @@ use super::{use_filter, use_lowercase}; use serde_yaml::{self, Mapping, Sequence, Value}; -#[allow(unused)] const MERGE_FIELDS: [&str; 6] = [ "prepend-rules", "append-rules", @@ -20,7 +19,7 @@ pub fn use_merge(merge: Mapping, mut config: Mapping) -> Mapping { }); let merge_list = MERGE_FIELDS.iter().map(|s| s.to_string()); - let merge = use_filter(merge, &merge_list.collect()); + let merge = use_filter(merge, &merge_list.collect(), true); ["rules", "proxies", "proxy-groups"] .iter() diff --git a/src-tauri/src/enhance/mod.rs b/src-tauri/src/enhance/mod.rs index 8c27a8f..115fda7 100644 --- a/src-tauri/src/enhance/mod.rs +++ b/src-tauri/src/enhance/mod.rs @@ -23,13 +23,14 @@ pub fn enhance() -> (Mapping, Vec, HashMap) { // config.yaml 的配置 let clash_config = { Config::clash().latest().0.clone() }; - let (clash_core, tun_mode, enable_builtin) = { + let (clash_core, enable_tun, enable_builtin, enable_filter) = { let verge = Config::verge(); let verge = verge.latest(); ( verge.clash_core.clone(), - verge.enable_tun_mode.clone(), - verge.enable_builtin_enhanced.clone(), + verge.enable_tun_mode.clone().unwrap_or(false), + verge.enable_builtin_enhanced.clone().unwrap_or(true), + verge.enable_clash_fields.clone().unwrap_or(true), ) }; @@ -58,14 +59,14 @@ pub fn enhance() -> (Mapping, Vec, HashMap) { let mut exists_keys = use_keys(&config); // 保存出现过的keys let valid = use_valid_fields(valid); - config = use_filter(config, &valid); + config = use_filter(config, &valid, enable_filter); // 处理用户的profile chain.into_iter().for_each(|item| match item.data { ChainType::Merge(merge) => { exists_keys.extend(use_keys(&merge)); config = use_merge(merge, config.to_owned()); - config = use_filter(config.to_owned(), &valid); + config = use_filter(config.to_owned(), &valid, enable_filter); } ChainType::Script(script) => { let mut logs = vec![]; @@ -73,7 +74,7 @@ pub fn enhance() -> (Mapping, Vec, HashMap) { match use_script(script, config.to_owned()) { Ok((res_config, res_logs)) => { exists_keys.extend(use_keys(&res_config)); - config = use_filter(res_config, &valid); + config = use_filter(res_config, &valid, enable_filter); logs.extend(res_logs); } Err(err) => logs.push(("exception".into(), err.to_string())), @@ -91,7 +92,7 @@ pub fn enhance() -> (Mapping, Vec, HashMap) { let clash_fields = use_clash_fields(); // 内建脚本最后跑 - if enable_builtin.unwrap_or(true) { + if enable_builtin { ChainItem::builtin() .into_iter() .filter(|(s, _)| s.is_support(clash_core.as_ref())) @@ -102,7 +103,7 @@ pub fn enhance() -> (Mapping, Vec, HashMap) { match item.data { ChainType::Script(script) => match use_script(script, config.to_owned()) { Ok((res_config, _)) => { - config = use_filter(res_config, &clash_fields); + config = use_filter(res_config, &clash_fields, enable_filter); } Err(err) => { log::error!(target: "app", "builtin script error `{err}`"); @@ -113,8 +114,8 @@ pub fn enhance() -> (Mapping, Vec, HashMap) { }); } - config = use_filter(config, &clash_fields); - config = use_tun(config, tun_mode.unwrap_or(false)); + config = use_filter(config, &clash_fields, enable_filter); + config = use_tun(config, enable_tun); config = use_sort(config); let mut exists_set = HashSet::new();