diff --git a/src-tauri/src/enhance/builtin/meta_guard.js b/src-tauri/src/enhance/builtin/meta_guard.js new file mode 100644 index 0000000000000000000000000000000000000000..be4183bb2c65420213f6bf34915cfa91401816b6 --- /dev/null +++ b/src-tauri/src/enhance/builtin/meta_guard.js @@ -0,0 +1,6 @@ +function main(params) { + if (params.mode === "script") { + params.mode = "rule"; + } + return params; +} diff --git a/src-tauri/src/enhance/builtin/hy_alpn.js b/src-tauri/src/enhance/builtin/meta_hy_alpn.js similarity index 100% rename from src-tauri/src/enhance/builtin/hy_alpn.js rename to src-tauri/src/enhance/builtin/meta_hy_alpn.js diff --git a/src-tauri/src/enhance/chain.rs b/src-tauri/src/enhance/chain.rs index 1d65ba22a05f026a93359d2caa46c25004b76a9a..75c61fb8542e4e5c99413c4121cdcbf8335499c9 100644 --- a/src-tauri/src/enhance/chain.rs +++ b/src-tauri/src/enhance/chain.rs @@ -17,6 +17,13 @@ pub enum ChainType { Script(String), } +#[derive(Debug, Clone)] +pub enum ChainSupport { + Clash, + ClashMeta, + All, +} + impl From<&PrfItem> for Option<ChainItem> { fn from(item: &PrfItem) -> Self { let itype = item.itype.as_ref()?.as_str(); @@ -44,13 +51,39 @@ impl From<&PrfItem> for Option<ChainItem> { impl ChainItem { /// 内建支æŒä¸€äº›è„šæœ¬ - pub fn builtin() -> Vec<ChainItem> { + pub fn builtin() -> Vec<(ChainSupport, ChainItem)> { + // meta çš„ä¸€äº›å¤„ç† + let meta_guard = + ChainItem::to_script("verge_meta_guard", include_str!("./builtin/meta_guard.js")); + // meta 1.13.2 alpn string 转 数组 - let hy_alpn = ChainItem { - uid: "verge_hy_alpn".into(), - data: ChainType::Script(include_str!("./builtin/hy_alpn.js").into()), - }; + let hy_alpn = + ChainItem::to_script("verge_hy_alpn", include_str!("./builtin/meta_hy_alpn.js")); + + vec![ + (ChainSupport::ClashMeta, hy_alpn), + (ChainSupport::ClashMeta, meta_guard), + ] + } - vec![hy_alpn] + pub fn to_script<U: Into<String>, D: Into<String>>(uid: U, data: D) -> Self { + Self { + uid: uid.into(), + data: ChainType::Script(data.into()), + } + } +} + +impl ChainSupport { + pub fn is_support(&self, core: Option<&String>) -> bool { + match core { + Some(core) => match (self, core.as_str()) { + (ChainSupport::All, _) => true, + (ChainSupport::Clash, "clash") => true, + (ChainSupport::ClashMeta, "clash-meta") => true, + _ => false, + }, + None => true, + } } } diff --git a/src-tauri/src/enhance/mod.rs b/src-tauri/src/enhance/mod.rs index 4cf9231b486ad5729677b3b0feed2969435d42df..8c27a8f534b14dad32bccbfb8ee9497ed76bb32d 100644 --- a/src-tauri/src/enhance/mod.rs +++ b/src-tauri/src/enhance/mod.rs @@ -4,8 +4,9 @@ mod merge; mod script; mod tun; -use self::chain::*; pub(self) use self::field::*; + +use self::chain::*; use self::merge::*; use self::script::*; use self::tun::*; @@ -19,21 +20,21 @@ type ResultLog = Vec<(String, String)>; /// Enhance mode /// 返回最终é…ç½®ã€è¯¥é…置包å«çš„é”®ã€å’Œscript执行的结果 pub fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) { + // config.yaml çš„é…ç½® let clash_config = { Config::clash().latest().0.clone() }; - let (tun_mode, enable_builtin) = { + let (clash_core, tun_mode, enable_builtin) = { let verge = Config::verge(); let verge = verge.latest(); ( + verge.clash_core.clone(), verge.enable_tun_mode.clone(), verge.enable_builtin_enhanced.clone(), ) }; - let tun_mode = tun_mode.unwrap_or(false); - let enable_builtin = enable_builtin.unwrap_or(true); - - let (mut config, mut chain, valid) = { + // 从profiles里拿东西 + let (mut config, chain, valid) = { let profiles = Config::profiles(); let profiles = profiles.latest(); @@ -53,15 +54,13 @@ pub fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) { (current, chain, valid) }; - let mut result_map = HashMap::new(); - let mut exists_keys = use_keys(&config); + let mut result_map = HashMap::new(); // ä¿å˜è„šæœ¬æ—¥å¿— + let mut exists_keys = use_keys(&config); // ä¿å˜å‡ºçŽ°è¿‡çš„keys let valid = use_valid_fields(valid); + config = use_filter(config, &valid); - if enable_builtin { - chain.extend(ChainItem::builtin().into_iter()); - } - + // 处ç†ç”¨æˆ·çš„profile chain.into_iter().for_each(|item| match item.data { ChainType::Merge(merge) => { exists_keys.extend(use_keys(&merge)); @@ -84,15 +83,38 @@ pub fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) { } }); - config = use_filter(config, &valid); - + // åˆå¹¶é»˜è®¤çš„config for (key, value) in clash_config.into_iter() { config.insert(key, value); } let clash_fields = use_clash_fields(); + + // 内建脚本最åŽè·‘ + if enable_builtin.unwrap_or(true) { + ChainItem::builtin() + .into_iter() + .filter(|(s, _)| s.is_support(clash_core.as_ref())) + .map(|(_, c)| c) + .for_each(|item| { + log::debug!(target: "app", "run builtin script {}", item.uid); + + match item.data { + ChainType::Script(script) => match use_script(script, config.to_owned()) { + Ok((res_config, _)) => { + config = use_filter(res_config, &clash_fields); + } + Err(err) => { + log::error!(target: "app", "builtin script error `{err}`"); + } + }, + _ => {} + } + }); + } + config = use_filter(config, &clash_fields); - config = use_tun(config, tun_mode); + config = use_tun(config, tun_mode.unwrap_or(false)); config = use_sort(config); let mut exists_set = HashSet::new();