mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 00:35:38 +08:00
添加链式代理下规则适配
This commit is contained in:
@@ -37,9 +37,8 @@ pub async fn get_runtime_logs() -> CmdResult<HashMap<String, Vec<(String, String
|
||||
Ok(Config::runtime().await.latest_ref().chain_logs.clone())
|
||||
}
|
||||
|
||||
/// 读取运行时链式代理配置
|
||||
#[tauri::command]
|
||||
pub async fn get_runtime_proxy_chain_config() -> CmdResult<String> {
|
||||
pub async fn get_runtime_proxy_chain_config(proxy_chain_exit_node: String) -> CmdResult<String> {
|
||||
let runtime = Config::runtime().await;
|
||||
let runtime = runtime.latest_ref();
|
||||
|
||||
@@ -50,51 +49,36 @@ pub async fn get_runtime_proxy_chain_config() -> CmdResult<String> {
|
||||
.ok_or(anyhow::anyhow!("failed to parse config to yaml file"))
|
||||
)?;
|
||||
|
||||
if let (
|
||||
Some(serde_yaml_ng::Value::Sequence(proxies)),
|
||||
Some(serde_yaml_ng::Value::Sequence(proxy_groups)),
|
||||
) = (config.get("proxies"), config.get("proxy-groups"))
|
||||
{
|
||||
let mut proxy_name = None;
|
||||
if let Some(serde_yaml_ng::Value::Sequence(proxies)) = config.get("proxies") {
|
||||
let mut proxy_name = Some(Some(proxy_chain_exit_node.as_str()));
|
||||
let mut proxies_chain = Vec::new();
|
||||
|
||||
let proxy_chain_groups = proxy_groups
|
||||
.iter()
|
||||
.filter_map(
|
||||
|proxy_group| match proxy_group.get("name").and_then(|n| n.as_str()) {
|
||||
Some("proxy_chain") => {
|
||||
if let Some(serde_yaml_ng::Value::Sequence(ps)) = proxy_group.get("proxies")
|
||||
&& let Some(x) = ps.first()
|
||||
{
|
||||
proxy_name = Some(x); //插入出口节点名字
|
||||
}
|
||||
Some(proxy_group.to_owned())
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
)
|
||||
.collect::<Vec<serde_yaml_ng::Value>>();
|
||||
|
||||
while let Some(proxy) = proxies.iter().find(|proxy| {
|
||||
if let serde_yaml_ng::Value::Mapping(proxy_map) = proxy {
|
||||
proxy_map.get("name") == proxy_name && proxy_map.get("dialer-proxy").is_some()
|
||||
proxy_map.get("name").map(|x| x.as_str()) == proxy_name
|
||||
&& proxy_map.get("dialer-proxy").is_some()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}) {
|
||||
proxies_chain.push(proxy.to_owned());
|
||||
proxy_name = proxy.get("dialer-proxy");
|
||||
proxy_name = proxy.get("dialer-proxy").map(|x| x.as_str());
|
||||
}
|
||||
|
||||
if let Some(entry_proxy) = proxies.iter().find(|proxy| proxy.get("name") == proxy_name) {
|
||||
if let Some(entry_proxy) = proxies
|
||||
.iter()
|
||||
.find(|proxy| proxy.get("name").map(|x| x.as_str()) == proxy_name)
|
||||
&& !proxies_chain.is_empty()
|
||||
{
|
||||
// 添加第一个节点
|
||||
proxies_chain.push(entry_proxy.to_owned());
|
||||
}
|
||||
|
||||
proxies_chain.reverse();
|
||||
|
||||
let mut config: HashMap<String, Vec<serde_yaml_ng::Value>> = HashMap::new();
|
||||
|
||||
config.insert("proxies".to_string(), proxies_chain);
|
||||
config.insert("proxy-groups".to_string(), proxy_chain_groups);
|
||||
|
||||
wrap_err!(serde_yaml_ng::to_string(&config).context("YAML generation failed"))
|
||||
} else {
|
||||
|
||||
@@ -92,22 +92,6 @@ impl IRuntime {
|
||||
/// 传入none 为删除
|
||||
pub fn update_proxy_chain_config(&mut self, proxy_chain_config: Option<Value>) {
|
||||
if let Some(config) = self.config.as_mut() {
|
||||
// 获取 默认第一的代理组的名字
|
||||
let proxy_group_name =
|
||||
if let Some(Value::Sequence(proxy_groups)) = config.get("proxy-groups") {
|
||||
if let Some(Value::Mapping(proxy_group)) = proxy_groups.first() {
|
||||
if let Some(Value::String(proxy_group_name)) = proxy_group.get("name") {
|
||||
proxy_group_name.to_string()
|
||||
} else {
|
||||
"".to_string()
|
||||
}
|
||||
} else {
|
||||
"".to_string()
|
||||
}
|
||||
} else {
|
||||
"".to_string()
|
||||
};
|
||||
|
||||
if let Some(Value::Sequence(proxies)) = config.get_mut("proxies") {
|
||||
proxies.iter_mut().for_each(|proxy| {
|
||||
if let Some(proxy) = proxy.as_mapping_mut()
|
||||
@@ -118,66 +102,19 @@ impl IRuntime {
|
||||
});
|
||||
}
|
||||
|
||||
// 清除proxy_chain代理组
|
||||
if let Some(Value::Sequence(proxy_groups)) = config.get_mut("proxy-groups") {
|
||||
proxy_groups.retain(|proxy_group| {
|
||||
!matches!(proxy_group.get("name").and_then(|n| n.as_str()), Some(name) if name== "proxy_chain")
|
||||
});
|
||||
}
|
||||
|
||||
// 清除rules
|
||||
if let Some(Value::Sequence(rules)) = config.get_mut("rules") {
|
||||
rules.retain(|rule| rule.as_str() != Some("MATCH,proxy_chain"));
|
||||
rules.push(Value::String(format!("MATCH,{}", proxy_group_name)));
|
||||
}
|
||||
|
||||
// some 则写入新配置
|
||||
// 给proxy添加dialer-proxy字段
|
||||
// 第一个proxy不添加dialer-proxy
|
||||
// 然后第二个开始,dialer-proxy为上一个元素的name
|
||||
if let Some(Value::Sequence(dialer_proxies)) = proxy_chain_config {
|
||||
let mut proxy_chain_group = Mapping::new();
|
||||
|
||||
if let Some(Value::Sequence(proxies)) = config.get_mut("proxies") {
|
||||
for (i, dialer_proxy) in dialer_proxies.iter().enumerate() {
|
||||
if let Some(Value::Mapping(proxy)) = proxies
|
||||
.iter_mut()
|
||||
.find(|proxy| proxy.get("name") == Some(dialer_proxy))
|
||||
{
|
||||
if i != 0
|
||||
&& let Some(dialer_proxy) = dialer_proxies.get(i - 1)
|
||||
{
|
||||
proxy.insert("dialer-proxy".into(), dialer_proxy.to_owned());
|
||||
}
|
||||
if i == dialer_proxies.len() - 1 {
|
||||
// 添加proxy-groups
|
||||
proxy_chain_group
|
||||
.insert("name".into(), Value::String("proxy_chain".into()));
|
||||
proxy_chain_group
|
||||
.insert("type".into(), Value::String("select".into()));
|
||||
proxy_chain_group.insert(
|
||||
"proxies".into(),
|
||||
Value::Sequence(vec![dialer_proxy.to_owned()]),
|
||||
);
|
||||
}
|
||||
}
|
||||
if let Some(Value::Sequence(dialer_proxies)) = proxy_chain_config
|
||||
&& let Some(Value::Sequence(proxies)) = config.get_mut("proxies")
|
||||
{
|
||||
for (i, dialer_proxy) in dialer_proxies.iter().enumerate() {
|
||||
if let Some(Value::Mapping(proxy)) = proxies
|
||||
.iter_mut()
|
||||
.find(|proxy| proxy.get("name") == Some(dialer_proxy))
|
||||
&& i != 0
|
||||
&& let Some(dialer_proxy) = dialer_proxies.get(i - 1)
|
||||
{
|
||||
proxy.insert("dialer-proxy".into(), dialer_proxy.to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(Value::Sequence(proxy_groups)) = config.get_mut("proxy-groups") {
|
||||
proxy_groups.push(Value::Mapping(proxy_chain_group));
|
||||
}
|
||||
|
||||
// 添加rules
|
||||
if let Some(Value::Sequence(rules)) = config.get_mut("rules")
|
||||
&& let Ok(rule) = serde_yaml_ng::to_value("MATCH,proxy_chain")
|
||||
{
|
||||
rules.retain(|rule| {
|
||||
rule.as_str() != Some(&format!("MATCH,{}", proxy_group_name))
|
||||
});
|
||||
rules.push(rule);
|
||||
// *rules = vec![rule];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1034,9 +1034,9 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
|
||||
return;
|
||||
}
|
||||
if !is_in_lightweight_mode() {
|
||||
lightweight::entry_lightweight_mode().await;
|
||||
lightweight::entry_lightweight_mode().await; // Await async function
|
||||
} else {
|
||||
lightweight::exit_lightweight_mode().await;
|
||||
lightweight::exit_lightweight_mode().await; // Await async function
|
||||
}
|
||||
}
|
||||
"quit" => {
|
||||
|
||||
@@ -276,9 +276,15 @@ impl IpcManager {
|
||||
let payload = serde_json::json!({
|
||||
"name": proxy
|
||||
});
|
||||
|
||||
// println!("group: {}, proxy: {}", group, proxy);
|
||||
match self.send_request("PUT", &url, Some(&payload)).await {
|
||||
Ok(_) => Ok(()),
|
||||
Ok(_) => {
|
||||
// println!("updateProxy response: {:?}", response);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
// println!("updateProxy encountered error: {}", e);
|
||||
logging!(
|
||||
error,
|
||||
crate::utils::logging::Type::Ipc,
|
||||
|
||||
Reference in New Issue
Block a user