2025-03-01 22:52:43 +08:00
|
|
|
use super::CmdResult;
|
2025-09-15 07:44:54 +08:00
|
|
|
use crate::{config::*, core::CoreManager, log_err, wrap_err};
|
2025-03-01 22:52:43 +08:00
|
|
|
use anyhow::Context;
|
2025-08-30 02:24:47 +08:00
|
|
|
use serde_yaml_ng::Mapping;
|
2025-03-13 12:51:20 +08:00
|
|
|
use std::collections::HashMap;
|
2025-03-01 22:52:43 +08:00
|
|
|
|
|
|
|
|
/// 获取运行时配置
|
|
|
|
|
#[tauri::command]
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn get_runtime_config() -> CmdResult<Option<Mapping>> {
|
|
|
|
|
Ok(Config::runtime().await.latest_ref().config.clone())
|
2025-03-01 22:52:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 获取运行时YAML配置
|
|
|
|
|
#[tauri::command]
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn get_runtime_yaml() -> CmdResult<String> {
|
|
|
|
|
let runtime = Config::runtime().await;
|
2025-07-04 22:43:23 +08:00
|
|
|
let runtime = runtime.latest_ref();
|
2025-09-15 07:44:54 +08:00
|
|
|
|
2025-03-01 22:52:43 +08:00
|
|
|
let config = runtime.config.as_ref();
|
edition 2024 (#4702)
* feat: update Cargo.toml for 2024 edition and optimize release profiles
* feat: refactor environment variable settings for Linux and improve code organization
* Refactor conditional statements to use `&&` for improved readability
- Updated multiple files to combine nested `if let` statements using `&&` for better clarity and conciseness.
- This change enhances the readability of the code by reducing indentation levels and making the conditions more straightforward.
- Affected files include: media_unlock_checker.rs, profile.rs, clash.rs, profiles.rs, async_proxy_query.rs, core.rs, handle.rs, hotkey.rs, service.rs, timer.rs, tray/mod.rs, merge.rs, seq.rs, config.rs, proxy.rs, window.rs, general.rs, dirs.rs, i18n.rs, init.rs, network.rs, and window.rs in the resolve module.
* refactor: streamline conditional checks using `&&` for improved readability
* fix: update release profile settings for panic behavior and optimization
* fix: adjust optimization level in Cargo.toml and reorder imports in lightweight.rs
2025-09-10 09:49:06 +08:00
|
|
|
wrap_err!(
|
|
|
|
|
config
|
|
|
|
|
.ok_or(anyhow::anyhow!("failed to parse config to yaml file"))
|
|
|
|
|
.and_then(|config| serde_yaml_ng::to_string(config)
|
|
|
|
|
.context("failed to convert config to yaml"))
|
|
|
|
|
)
|
2025-03-01 22:52:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 获取运行时存在的键
|
|
|
|
|
#[tauri::command]
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn get_runtime_exists() -> CmdResult<Vec<String>> {
|
|
|
|
|
Ok(Config::runtime().await.latest_ref().exists_keys.clone())
|
2025-03-01 22:52:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 获取运行时日志
|
|
|
|
|
#[tauri::command]
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn get_runtime_logs() -> CmdResult<HashMap<String, Vec<(String, String)>>> {
|
|
|
|
|
Ok(Config::runtime().await.latest_ref().chain_logs.clone())
|
2025-03-01 22:52:43 +08:00
|
|
|
}
|
2025-09-15 07:44:54 +08:00
|
|
|
|
|
|
|
|
#[tauri::command]
|
2025-09-22 18:16:17 +08:00
|
|
|
pub async fn get_runtime_proxy_chain_config(proxy_chain_exit_node: String) -> CmdResult<String> {
|
2025-09-15 07:44:54 +08:00
|
|
|
let runtime = Config::runtime().await;
|
|
|
|
|
let runtime = runtime.latest_ref();
|
|
|
|
|
|
|
|
|
|
let config = wrap_err!(
|
|
|
|
|
runtime
|
|
|
|
|
.config
|
|
|
|
|
.as_ref()
|
|
|
|
|
.ok_or(anyhow::anyhow!("failed to parse config to yaml file"))
|
|
|
|
|
)?;
|
|
|
|
|
|
2025-09-22 18:16:17 +08:00
|
|
|
if let Some(serde_yaml_ng::Value::Sequence(proxies)) = config.get("proxies") {
|
|
|
|
|
let mut proxy_name = Some(Some(proxy_chain_exit_node.as_str()));
|
2025-09-15 07:44:54 +08:00
|
|
|
let mut proxies_chain = Vec::new();
|
|
|
|
|
|
|
|
|
|
while let Some(proxy) = proxies.iter().find(|proxy| {
|
|
|
|
|
if let serde_yaml_ng::Value::Mapping(proxy_map) = proxy {
|
2025-09-22 18:16:17 +08:00
|
|
|
proxy_map.get("name").map(|x| x.as_str()) == proxy_name
|
|
|
|
|
&& proxy_map.get("dialer-proxy").is_some()
|
2025-09-15 07:44:54 +08:00
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}) {
|
|
|
|
|
proxies_chain.push(proxy.to_owned());
|
2025-09-22 18:16:17 +08:00
|
|
|
proxy_name = proxy.get("dialer-proxy").map(|x| x.as_str());
|
2025-09-15 07:44:54 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-22 18:16:17 +08:00
|
|
|
if let Some(entry_proxy) = proxies
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|proxy| proxy.get("name").map(|x| x.as_str()) == proxy_name)
|
|
|
|
|
&& !proxies_chain.is_empty()
|
|
|
|
|
{
|
|
|
|
|
// 添加第一个节点
|
2025-09-15 07:44:54 +08:00
|
|
|
proxies_chain.push(entry_proxy.to_owned());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
proxies_chain.reverse();
|
|
|
|
|
|
|
|
|
|
let mut config: HashMap<String, Vec<serde_yaml_ng::Value>> = HashMap::new();
|
2025-09-22 18:16:17 +08:00
|
|
|
|
2025-09-15 07:44:54 +08:00
|
|
|
config.insert("proxies".to_string(), proxies_chain);
|
|
|
|
|
|
|
|
|
|
wrap_err!(serde_yaml_ng::to_string(&config).context("YAML generation failed"))
|
|
|
|
|
} else {
|
|
|
|
|
wrap_err!(Err(anyhow::anyhow!(
|
|
|
|
|
"failed to get proxies or proxy-groups".to_string()
|
|
|
|
|
)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 更新运行时链式代理配置
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn update_proxy_chain_config_in_runtime(
|
|
|
|
|
proxy_chain_config: Option<serde_yaml_ng::Value>,
|
|
|
|
|
) -> CmdResult<()> {
|
|
|
|
|
{
|
|
|
|
|
let runtime = Config::runtime().await;
|
|
|
|
|
let mut draft = runtime.draft_mut();
|
|
|
|
|
draft.update_proxy_chain_config(proxy_chain_config);
|
|
|
|
|
drop(draft);
|
|
|
|
|
runtime.apply();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 生成新的运行配置文件并通知 Clash 核心重新加载
|
|
|
|
|
let run_path = wrap_err!(Config::generate_file(ConfigType::Run).await)?;
|
|
|
|
|
log_err!(CoreManager::global().put_configs_force(run_path).await);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|