mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
feat: reimplement enhanced mode
This commit is contained in:
97
src-tauri/src/config/field.rs
Normal file
97
src-tauri/src/config/field.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
use serde_yaml::{Mapping, Value};
|
||||
|
||||
pub const HANDLE_FIELDS: [&str; 9] = [
|
||||
"port",
|
||||
"socks-port",
|
||||
"mixed-port",
|
||||
"mode",
|
||||
"ipv6",
|
||||
"log-level",
|
||||
"allow-lan",
|
||||
"external-controller",
|
||||
"secret",
|
||||
];
|
||||
|
||||
pub const DEFAULT_FIELDS: [&str; 5] = [
|
||||
"proxies",
|
||||
"proxy-groups",
|
||||
"rules",
|
||||
"proxy-providers",
|
||||
"rule-providers",
|
||||
];
|
||||
|
||||
pub const OTHERS_FIELDS: [&str; 20] = [
|
||||
"tun",
|
||||
"dns",
|
||||
"ebpf",
|
||||
"hosts",
|
||||
"script",
|
||||
"profile",
|
||||
"payload",
|
||||
"auto-redir",
|
||||
"experimental",
|
||||
"interface-name",
|
||||
"routing-mark",
|
||||
"redir-port",
|
||||
"tproxy-port",
|
||||
"iptables",
|
||||
"external-ui",
|
||||
"bind-address",
|
||||
"authentication",
|
||||
"sniffer", // meta
|
||||
"geodata-mode", // meta
|
||||
"tcp-concurrent", // meta
|
||||
];
|
||||
|
||||
pub fn use_clash_fields() -> Vec<String> {
|
||||
DEFAULT_FIELDS
|
||||
.into_iter()
|
||||
.chain(HANDLE_FIELDS)
|
||||
.chain(OTHERS_FIELDS)
|
||||
.map(|s| s.to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn use_valid_fields(mut valid: Vec<String>) -> Vec<String> {
|
||||
let others = Vec::from(OTHERS_FIELDS);
|
||||
|
||||
valid.iter_mut().for_each(|s| s.make_ascii_lowercase());
|
||||
valid
|
||||
.into_iter()
|
||||
.filter(|s| others.contains(&s.as_str()))
|
||||
.chain(DEFAULT_FIELDS.iter().map(|s| s.to_string()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn use_filter(config: Mapping, filter: Vec<String>) -> Mapping {
|
||||
let mut ret = Mapping::new();
|
||||
|
||||
for (key, value) in config.into_iter() {
|
||||
key.as_str().map(|key_str| {
|
||||
// change to lowercase
|
||||
let mut key_str = String::from(key_str);
|
||||
key_str.make_ascii_lowercase();
|
||||
|
||||
if filter.contains(&key_str) {
|
||||
ret.insert(Value::from(key_str), value);
|
||||
}
|
||||
});
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
pub fn use_sort(config: Mapping) -> Mapping {
|
||||
let mut ret = Mapping::new();
|
||||
|
||||
HANDLE_FIELDS
|
||||
.into_iter()
|
||||
.chain(OTHERS_FIELDS)
|
||||
.chain(DEFAULT_FIELDS)
|
||||
.for_each(|key| {
|
||||
let key = Value::from(key);
|
||||
config.get(&key).map(|value| {
|
||||
ret.insert(key, value.clone());
|
||||
});
|
||||
});
|
||||
ret
|
||||
}
|
||||
99
src-tauri/src/config/merge.rs
Normal file
99
src-tauri/src/config/merge.rs
Normal file
@@ -0,0 +1,99 @@
|
||||
use super::{use_filter, use_valid_fields};
|
||||
use serde_yaml::{self, Mapping, Sequence, Value};
|
||||
|
||||
#[allow(unused)]
|
||||
const MERGE_FIELDS: [&str; 6] = [
|
||||
"prepend-rules",
|
||||
"append-rules",
|
||||
"prepend-proxies",
|
||||
"append-proxies",
|
||||
"prepend-proxy-groups",
|
||||
"append-proxy-groups",
|
||||
];
|
||||
|
||||
pub fn use_merge(merge: Mapping, mut config: Mapping, valid: Vec<String>) -> Mapping {
|
||||
let valid_list = use_valid_fields(valid);
|
||||
let merge_valid = use_filter(merge.clone(), valid_list);
|
||||
|
||||
// 直接覆盖原字段
|
||||
merge_valid.into_iter().for_each(|(key, value)| {
|
||||
config.insert(key, value);
|
||||
});
|
||||
|
||||
let merge_list = MERGE_FIELDS.iter().map(|s| s.to_string());
|
||||
let merge = use_filter(merge, merge_list.collect());
|
||||
|
||||
["rules", "proxies", "proxy-groups"]
|
||||
.iter()
|
||||
.for_each(|key_str| {
|
||||
let key_val = Value::from(key_str.to_string());
|
||||
|
||||
let mut list = Sequence::default();
|
||||
list = config.get(&key_val).map_or(list.clone(), |val| {
|
||||
val.as_sequence().map_or(list, |v| v.clone())
|
||||
});
|
||||
|
||||
let pre_key = Value::from(format!("prepend-{key_str}"));
|
||||
let post_key = Value::from(format!("append-{key_str}"));
|
||||
|
||||
if let Some(pre_val) = merge.get(&pre_key) {
|
||||
if pre_val.is_sequence() {
|
||||
let mut pre_val = pre_val.as_sequence().unwrap().clone();
|
||||
pre_val.extend(list);
|
||||
list = pre_val;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(post_val) = merge.get(&post_key) {
|
||||
if post_val.is_sequence() {
|
||||
list.extend(post_val.as_sequence().unwrap().clone());
|
||||
}
|
||||
}
|
||||
|
||||
config.insert(key_val, Value::from(list));
|
||||
});
|
||||
|
||||
config
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_merge() -> anyhow::Result<()> {
|
||||
let merge = r"
|
||||
prepend-rules:
|
||||
- prepend
|
||||
- 1123123
|
||||
append-rules:
|
||||
- append
|
||||
prepend-proxies:
|
||||
- 9999
|
||||
append-proxies:
|
||||
- 1111
|
||||
rules:
|
||||
- replace
|
||||
proxy-groups:
|
||||
- 123781923810
|
||||
tun:
|
||||
enable: true
|
||||
dns:
|
||||
enable: true
|
||||
";
|
||||
|
||||
let config = r"
|
||||
rules:
|
||||
- aaaaa
|
||||
script1: test
|
||||
";
|
||||
|
||||
let merge = serde_yaml::from_str::<Mapping>(merge)?;
|
||||
let config = serde_yaml::from_str::<Mapping>(config)?;
|
||||
|
||||
let result = serde_yaml::to_string(&use_merge(
|
||||
merge,
|
||||
config,
|
||||
vec!["tun"].iter().map(|s| s.to_string()).collect(),
|
||||
))?;
|
||||
|
||||
println!("{result}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
57
src-tauri/src/config/mod.rs
Normal file
57
src-tauri/src/config/mod.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
mod field;
|
||||
mod merge;
|
||||
mod script;
|
||||
|
||||
pub(self) use self::field::*;
|
||||
use self::merge::*;
|
||||
use self::script::*;
|
||||
use crate::core::PrfData;
|
||||
use serde_yaml::Mapping;
|
||||
use std::collections::HashMap;
|
||||
|
||||
type ResultLog = Vec<(String, String)>;
|
||||
|
||||
pub fn runtime_config(
|
||||
clash_config: Mapping,
|
||||
profile_config: Mapping,
|
||||
profile_enhanced: Vec<PrfData>,
|
||||
valid: Vec<String>,
|
||||
// tun_enable: bool,
|
||||
) -> (Mapping, HashMap<String, ResultLog>) {
|
||||
let mut config = profile_config;
|
||||
|
||||
let mut result_map = HashMap::new();
|
||||
|
||||
profile_enhanced.into_iter().for_each(|data| {
|
||||
if data.merge.is_some() {
|
||||
config = use_merge(data.merge.unwrap(), config.to_owned(), valid.clone());
|
||||
} else if data.script.is_some() {
|
||||
let mut logs = vec![];
|
||||
|
||||
match use_script(data.script.unwrap(), config.to_owned(), valid.clone()) {
|
||||
Ok((res_config, res_logs)) => {
|
||||
config = res_config;
|
||||
logs.extend(res_logs);
|
||||
}
|
||||
Err(err) => {
|
||||
logs.push(("error".into(), err.to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(uid) = data.item.uid {
|
||||
result_map.insert(uid, logs);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
config = use_filter(config, use_valid_fields(valid));
|
||||
|
||||
for (key, value) in clash_config.into_iter() {
|
||||
config.insert(key, value);
|
||||
}
|
||||
|
||||
config = use_filter(config, use_clash_fields());
|
||||
config = use_sort(config);
|
||||
|
||||
(config, result_map)
|
||||
}
|
||||
96
src-tauri/src/config/script.rs
Normal file
96
src-tauri/src/config/script.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
use super::{use_filter, use_valid_fields};
|
||||
use anyhow::Result;
|
||||
use serde_yaml::{self, Mapping};
|
||||
|
||||
pub fn use_script(
|
||||
script: String,
|
||||
config: Mapping,
|
||||
valid: Vec<String>,
|
||||
) -> Result<(Mapping, Vec<(String, String)>)> {
|
||||
use rquickjs::{Context, Func, Runtime};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
let runtime = Runtime::new().unwrap();
|
||||
let context = Context::full(&runtime).unwrap();
|
||||
let outputs = Arc::new(Mutex::new(vec![]));
|
||||
|
||||
let copy_outputs = outputs.clone();
|
||||
let result = context.with(|ctx| -> Result<Mapping> {
|
||||
ctx.globals().set(
|
||||
"__verge_log__",
|
||||
Func::from(move |level: String, data: String| {
|
||||
let mut out = copy_outputs.lock().unwrap();
|
||||
out.push((level, data));
|
||||
}),
|
||||
)?;
|
||||
|
||||
ctx.eval(
|
||||
r#"var console = Object.freeze({
|
||||
log(data){__verge_log__("log",JSON.stringify(data))},
|
||||
info(data){__verge_log__("info",JSON.stringify(data))},
|
||||
error(data){__verge_log__("error",JSON.stringify(data))},
|
||||
debug(data){__verge_log__("debug",JSON.stringify(data))},
|
||||
});"#,
|
||||
)?;
|
||||
|
||||
let config_str = serde_json::to_string(&config)?;
|
||||
|
||||
let code = format!("\n{script}\n;\nJSON.stringify(main({config_str})||'')");
|
||||
let result: String = ctx.eval(code.as_str())?;
|
||||
if result == "\"\"" {
|
||||
anyhow::bail!("main function should return object");
|
||||
}
|
||||
Ok(serde_json::from_str::<Mapping>(result.as_str())?)
|
||||
});
|
||||
|
||||
let mut out = outputs.lock().unwrap();
|
||||
match result {
|
||||
Ok(config) => {
|
||||
let valid = use_valid_fields(valid);
|
||||
let config = use_filter(config, valid);
|
||||
Ok((config, out.to_vec()))
|
||||
}
|
||||
Err(err) => {
|
||||
out.push(("error".into(), err.to_string()));
|
||||
Ok((config, out.to_vec()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_script() {
|
||||
let script = r#"
|
||||
function main(config) {
|
||||
if (Array.isArray(config.rules)) {
|
||||
config.rules = [...config.rules, "add"];
|
||||
}
|
||||
console.log(config);
|
||||
config.proxies = ["111"];
|
||||
return config;
|
||||
}
|
||||
"#;
|
||||
|
||||
let config = r#"
|
||||
rules:
|
||||
- 111
|
||||
- 222
|
||||
tun:
|
||||
enable: false
|
||||
dns:
|
||||
enable: false
|
||||
"#;
|
||||
|
||||
let config = serde_yaml::from_str(config).unwrap();
|
||||
let (config, results) = use_script(
|
||||
script.into(),
|
||||
config,
|
||||
vec!["tun"].iter().map(|s| s.to_string()).collect(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let config_str = serde_yaml::to_string(&config).unwrap();
|
||||
|
||||
println!("{config_str}");
|
||||
|
||||
dbg!(results);
|
||||
}
|
||||
Reference in New Issue
Block a user