Skip to content

Commit

Permalink
feat: adjust builtin script and support meta guard script
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi committed Nov 22, 2022
1 parent 4046f14 commit fe81687
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 21 deletions.
6 changes: 6 additions & 0 deletions src-tauri/src/enhance/builtin/meta_guard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function main(params) {
if (params.mode === "script") {
params.mode = "rule";
}
return params;
}
File renamed without changes.
45 changes: 39 additions & 6 deletions src-tauri/src/enhance/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
}
}
}
52 changes: 37 additions & 15 deletions src-tauri/src/enhance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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();

Expand All @@ -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));
Expand All @@ -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();
Expand Down

0 comments on commit fe81687

Please sign in to comment.