chore: trim excessive comments

This commit is contained in:
wonfen
2026-08-19 07:27:43 +08:00
parent 42b91b6294
commit 807f9a206d
103 changed files with 293 additions and 2517 deletions
-12
View File
@@ -4,14 +4,12 @@ use crate::{cmd::StringifyErr as _, feat, utils::dirs};
use smartstring::alias::String;
use tauri::{AppHandle, Manager as _};
/// 打开应用程序所在目录
#[tauri::command]
pub async fn open_app_dir() -> CmdResult<()> {
let app_dir = dirs::app_home_dir().stringify_err()?;
open::that(app_dir).stringify_err()
}
/// 打开核心所在目录
#[tauri::command]
pub async fn open_core_dir() -> CmdResult<()> {
let core_dir = tauri::utils::platform::current_exe().stringify_err()?;
@@ -19,20 +17,17 @@ pub async fn open_core_dir() -> CmdResult<()> {
open::that(core_dir).stringify_err()
}
/// 打开日志目录
#[tauri::command]
pub async fn open_logs_dir() -> CmdResult<()> {
let log_dir = dirs::app_logs_dir().stringify_err()?;
open::that(log_dir).stringify_err()
}
/// 打开网页链接
#[tauri::command]
pub fn open_web_url(url: String) -> CmdResult<()> {
open::that(url.as_str()).stringify_err()
}
/// 打开/关闭开发者工具
#[tauri::command]
pub fn open_devtools(app_handle: AppHandle) {
if let Some(window) = app_handle.get_webview_window("main") {
@@ -44,45 +39,38 @@ pub fn open_devtools(app_handle: AppHandle) {
}
}
/// 退出应用
#[tauri::command]
pub async fn exit_app() {
feat::quit().await;
}
/// 重启应用
#[tauri::command]
pub async fn restart_app() -> CmdResult<()> {
feat::restart_app().await;
Ok(())
}
/// 获取便携版标识
#[tauri::command]
pub fn get_portable_flag() -> bool {
*dirs::PORTABLE_FLAG.get().unwrap_or(&false)
}
/// 获取应用目录
#[tauri::command]
pub fn get_app_dir() -> CmdResult<String> {
let app_home_dir = dirs::app_home_dir().stringify_err()?.to_string_lossy().into();
Ok(app_home_dir)
}
/// 获取当前自启动状态
#[tauri::command]
pub fn get_auto_launch_status() -> CmdResult<bool> {
autostart::get_launch_status().stringify_err()
}
/// 下载图标缓存
#[tauri::command]
pub async fn download_icon_cache(url: String, name: String) -> CmdResult<String> {
feat::download_icon_cache(url, name).await
}
/// 复制图标文件
#[tauri::command]
pub async fn copy_icon_file(path: String, icon_info: feat::IconInfo) -> CmdResult<String> {
feat::copy_icon_file(path, icon_info).await
-6
View File
@@ -3,25 +3,21 @@ use crate::{cmd::StringifyErr as _, feat};
use feat::LocalBackupFile;
use smartstring::alias::String;
/// Create a local backup
#[tauri::command]
pub async fn create_local_backup() -> CmdResult<()> {
feat::create_local_backup().await.stringify_err()
}
/// List local backups
#[tauri::command]
pub async fn list_local_backup() -> CmdResult<Vec<LocalBackupFile>> {
feat::list_local_backup().await.stringify_err()
}
/// Delete local backup
#[tauri::command]
pub async fn delete_local_backup(filename: String) -> CmdResult<()> {
feat::delete_local_backup(filename).await.stringify_err()
}
/// Restore local backup
#[tauri::command]
pub async fn restore_local_backup(filename: String) -> CmdResult<()> {
feat::restore_local_backup(filename)
@@ -29,13 +25,11 @@ pub async fn restore_local_backup(filename: String) -> CmdResult<()> {
.map_err(|error| super::proxy_aware_error(&error))
}
/// Import local backup into the app's backup directory
#[tauri::command]
pub async fn import_local_backup(source: String) -> CmdResult<String> {
feat::import_local_backup(source).await.stringify_err()
}
/// Export local backup to a user selected destination
#[tauri::command]
pub async fn export_local_backup(filename: String, destination: String) -> CmdResult<()> {
feat::export_local_backup(filename, destination).await.stringify_err()
+1 -30
View File
@@ -16,20 +16,17 @@ use serde_yaml_ng::Mapping;
use smartstring::alias::String;
use tokio::fs;
/// 复制Clash环境变量
#[tauri::command]
pub async fn copy_clash_env() -> CmdResult {
feat::copy_clash_env().await;
Ok(())
}
/// 获取Clash信息
#[tauri::command]
pub async fn get_clash_info() -> CmdResult<ClashInfo> {
Ok(Config::clash().await.data_arc().get_client_info())
}
/// 修改Clash配置
#[tauri::command]
pub async fn patch_clash_config(payload: Mapping) -> CmdResult {
feat::patch_clash(&payload)
@@ -37,10 +34,6 @@ pub async fn patch_clash_config(payload: Mapping) -> CmdResult {
.map_err(|error| proxy_aware_coded_error(&error, "CLASH_CONFIG_UPDATE_FAILED"))
}
/// 修改Clash模式
///
/// 将 `change_clash_mode` 的失败上抛给前端,使前端 `catch` 能真正感知后端 PATCH 失败
/// 并提示用户(此前命令始终返回 `Ok(())`,吞掉了后端错误)。
#[tauri::command]
pub async fn patch_clash_mode(payload: String) -> CmdResult {
feat::change_clash_mode(payload)
@@ -48,16 +41,12 @@ pub async fn patch_clash_mode(payload: String) -> CmdResult {
.with_error_code("CLASH_MODE_UPDATE_FAILED")
}
/// 获取当前 Clash 模式(容错读取)
///
/// 直接读取已保存的 clash 配置中的 `mode`,绕开 mihomo `/configs` 的严格
/// `BaseConfig` 反序列化,作为主页 mode 显示的兜底来源。
/// Reads the saved mode without depending on strict mihomo `/configs` deserialization.
#[tauri::command]
pub async fn get_clash_mode() -> CmdResult<Option<String>> {
Ok(Config::clash().await.data_arc().get_mode().map(Into::into))
}
/// 切换Clash核心
#[tauri::command]
pub async fn change_clash_core(clash_core: String) -> CmdResult<Option<CommandFailure>> {
logging!(info, Type::Config, "changing core to {clash_core}");
@@ -66,7 +55,6 @@ pub async fn change_clash_core(clash_core: String) -> CmdResult<Option<CommandFa
Ok(_) => {
logging_error!(Type::Core, profiles_save_file_safe().await);
// 切换内核后重启内核
match CoreManager::global().restart_core().await {
Ok(_) => {
logging!(info, Type::Core, "core changed and restarted to {clash_core}");
@@ -92,7 +80,6 @@ pub async fn change_clash_core(clash_core: String) -> CmdResult<Option<CommandFa
}
}
/// 启动核心
#[tauri::command]
pub async fn start_core() -> CmdResult {
let result = CoreManager::global()
@@ -105,7 +92,6 @@ pub async fn start_core() -> CmdResult {
result
}
/// 关闭核心
#[tauri::command]
pub async fn stop_core() -> CmdResult {
logging_error!(Type::Core, profiles_save_file_safe().await);
@@ -119,7 +105,6 @@ pub async fn stop_core() -> CmdResult {
result
}
/// 重启核心
#[tauri::command]
pub async fn restart_core() -> CmdResult {
logging_error!(Type::Core, profiles_save_file_safe().await);
@@ -133,7 +118,6 @@ pub async fn restart_core() -> CmdResult {
result
}
/// 测试URL延迟
#[tauri::command]
pub async fn test_delay(url: String) -> CmdResult<u32> {
let result = match feat::test_delay(url).await {
@@ -146,16 +130,13 @@ pub async fn test_delay(url: String) -> CmdResult<u32> {
Ok(result)
}
/// 保存DNS配置到单独文件
#[tauri::command]
pub async fn save_dns_config(dns_config: Mapping) -> CmdResult {
use crate::utils::dirs;
use tokio::fs;
// 获取DNS配置文件路径
let dns_path = dirs::app_home_dir().stringify_err()?.join(constants::files::DNS_CONFIG);
// 保存DNS配置到文件
let yaml_str = yaml_emitter::to_mihomo_config_string(&dns_config).stringify_err()?;
fs::write(&dns_path, yaml_str).await.stringify_err()?;
logging!(info, Type::Config, "DNS config saved to {dns_path:?}");
@@ -163,11 +144,9 @@ pub async fn save_dns_config(dns_config: Mapping) -> CmdResult {
Ok(())
}
/// 应用或撤销DNS配置
#[tauri::command]
pub async fn apply_dns_config(apply: bool) -> CmdResult {
if apply {
// 读取DNS配置文件
let dns_path = dirs::app_home_dir().stringify_err()?.join(constants::files::DNS_CONFIG);
if !dns_path.exists() {
@@ -179,23 +158,19 @@ pub async fn apply_dns_config(apply: bool) -> CmdResult {
logging!(error, Type::Config, "Failed to read DNS config: {e}");
})?;
// 解析DNS配置
let patch_config = serde_yaml_ng::from_str::<serde_yaml_ng::Mapping>(&dns_yaml).stringify_err_log(|e| {
logging!(error, Type::Config, "Failed to parse DNS config: {e}");
})?;
logging!(info, Type::Config, "Applying DNS config from file");
// 创建包含DNS配置的patch
let mut patch = serde_yaml_ng::Mapping::new();
patch.insert("dns".into(), patch_config.into());
// 应用DNS配置到运行时配置
Config::runtime().await.edit_draft(|d| {
d.patch_config(&patch);
});
// 应用新配置
CoreManager::global().update_config_checked().await.map_err(|error| {
logging!(error, Type::Config, "failed to apply the config with DNS: {error:#}");
proxy_aware_error(&error)
@@ -203,7 +178,6 @@ pub async fn apply_dns_config(apply: bool) -> CmdResult {
logging!(info, Type::Config, "DNS config successfully applied");
} else {
// 当关闭DNS设置时,重新生成配置(不加载DNS配置文件)
logging!(info, Type::Config, "DNS settings disabled, regenerating config");
CoreManager::global().update_config_checked().await.map_err(|error| {
@@ -218,7 +192,6 @@ pub async fn apply_dns_config(apply: bool) -> CmdResult {
Ok(())
}
/// 检查DNS配置文件是否存在
#[tauri::command]
pub fn check_dns_config_exists() -> CmdResult<bool> {
use crate::utils::dirs;
@@ -228,7 +201,6 @@ pub fn check_dns_config_exists() -> CmdResult<bool> {
Ok(dns_path.exists())
}
/// 获取DNS配置文件内容
#[tauri::command]
pub async fn get_dns_config_content() -> CmdResult<String> {
use crate::utils::dirs;
@@ -244,7 +216,6 @@ pub async fn get_dns_config_content() -> CmdResult<String> {
Ok(content)
}
/// 验证DNS配置文件
#[tauri::command]
pub async fn validate_dns_config() -> CmdResult<ValidationOutcome> {
let app_dir = dirs::app_home_dir().stringify_err()?;
-8
View File
@@ -8,7 +8,6 @@ use serde_yaml_ng::Mapping;
use sysproxy::{Autoproxy, Sysproxy};
use tauri_plugin_clash_verge_sysinfo;
/// get the system proxy
#[tauri::command]
pub async fn get_sys_proxy() -> CmdResult<Mapping> {
logging!(debug, Type::Network, "异步获取系统代理配置");
@@ -38,7 +37,6 @@ pub async fn get_sys_proxy() -> CmdResult<Mapping> {
Ok(map)
}
/// 获取自动代理配置
#[tauri::command]
pub async fn get_auto_proxy() -> CmdResult<Mapping> {
Sysopt::global().wait_idle().await;
@@ -64,28 +62,22 @@ pub fn get_embedded_server_port() -> CmdResult<u16> {
crate::utils::server::embedded_server_port().stringify_err()
}
/// 获取系统主机名
#[tauri::command]
pub fn get_system_hostname() -> String {
// 获取系统主机名,处理可能的非UTF-8字符
match gethostname().into_string() {
Ok(name) => name,
Err(os_string) => {
// 对于包含非UTF-8的主机名,使用调试格式化
let fallback = format!("{os_string:?}");
// 去掉可能存在的引号
fallback.trim_matches('"').to_string()
}
}
}
/// 获取网络接口列表
#[tauri::command]
pub fn get_network_interfaces() -> Vec<String> {
tauri_plugin_clash_verge_sysinfo::list_network_interfaces()
}
/// 获取网络接口详细信息
#[tauri::command]
pub fn get_network_interfaces_info() -> CmdResult<Vec<NetworkInterface>> {
use network_interface::{NetworkInterface, NetworkInterfaceConfig as _};
+1 -22
View File
@@ -39,7 +39,6 @@ pub async fn get_profiles() -> CmdResult<SharedDraft<IProfiles>> {
Ok(data)
}
/// 增强配置文件
#[tauri::command]
pub async fn enhance_profiles() -> CmdResult<ValidationOutcome> {
match feat::enhance_profiles().await {
@@ -64,12 +63,10 @@ pub async fn enhance_profiles() -> CmdResult<ValidationOutcome> {
}
}
/// 导入配置文件
#[tauri::command]
pub async fn import_profile(url: std::string::String, option: Option<PrfOption>) -> CmdResult {
logging!(info, Type::Cmd, "[导入订阅] 开始导入: {}", help::mask_url(&url));
// 直接依赖 PrfItem::from_url 自身的超时/重试逻辑,不再使用 tokio::time::timeout 包裹
let item = &mut match PrfItem::from_url(&url, None, None, option.as_ref()).await {
Ok(it) => {
logging!(info, Type::Cmd, "[导入订阅] 下载完成,开始保存配置");
@@ -102,7 +99,6 @@ pub async fn import_profile(url: std::string::String, option: Option<PrfOption>)
Ok(())
}
/// 调整profile的顺序
#[tauri::command]
pub async fn reorder_profile(active_id: String, over_id: String) -> CmdResult {
match profiles_reorder_safe(&active_id, &over_id).await {
@@ -117,8 +113,6 @@ pub async fn reorder_profile(active_id: String, over_id: String) -> CmdResult {
}
}
/// 创建新的profile
/// 创建一个新的配置文件
#[tauri::command]
pub async fn create_profile(item: PrfItem, file_data: Option<String>) -> CmdResult {
match profiles_append_item_with_filedata_safe(&item, file_data).await {
@@ -127,7 +121,6 @@ pub async fn create_profile(item: PrfItem, file_data: Option<String>) -> CmdResu
.await
.with_error_code("PROFILE_CREATE_FAILED")?;
logging_error!(Type::Timer, Timer::global().refresh().await);
// 发送配置变更通知
if let Some(uid) = &item.uid {
logging!(info, Type::Cmd, "[创建订阅] 发送配置变更通知: {}", uid);
handle::Handle::notify_profile_changed(uid);
@@ -138,7 +131,6 @@ pub async fn create_profile(item: PrfItem, file_data: Option<String>) -> CmdResu
}
}
/// 更新配置文件
#[tauri::command]
pub async fn update_profile(index: String, option: Option<PrfOption>) -> CmdResult {
match feat::update_profile(&index, option.as_ref(), true, true, true).await {
@@ -150,7 +142,6 @@ pub async fn update_profile(index: String, option: Option<PrfOption>) -> CmdResu
}
}
/// 删除配置文件
#[tauri::command]
pub async fn delete_profile(index: String) -> CmdResult {
let profile_write_guard = PROFILE_WRITE_LOCK.lock().await;
@@ -209,7 +200,6 @@ pub async fn delete_profile(index: String) -> CmdResult {
Ok(())
}
/// 执行配置更新并处理结果
async fn restore_previous_profile(prev_profile: &String) -> CmdResult<()> {
logging!(info, Type::Cmd, "尝试恢复到之前的配置: {}", prev_profile);
let restore_profiles = IProfiles {
@@ -322,7 +312,6 @@ async fn perform_config_update(
}
}
/// 修改profiles的配置
#[tauri::command]
pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<ValidationOutcome> {
if CURRENT_SWITCHING_PROFILE
@@ -338,7 +327,6 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<ValidationO
logging!(info, Type::Cmd, "开始修改配置文件,目标profile: {:?}", target_profile);
// 保存当前配置,以便在验证失败时恢复
let previous_profile = Config::profiles().await.data_arc().current.clone();
logging!(info, Type::Cmd, "当前配置: {:?}", previous_profile);
@@ -349,7 +337,6 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<ValidationO
.map_err(|error| coded_error("PROFILE_SWITCH_FAILED", error))
}
/// 根据profile name修改profiles
pub async fn patch_profiles_config_by_profile_index(profile_index: String) -> CmdResult<ValidationOutcome> {
logging!(info, Type::Cmd, "切换配置到: {}", profile_index);
@@ -360,10 +347,8 @@ pub async fn patch_profiles_config_by_profile_index(profile_index: String) -> Cm
patch_profiles_config(profiles).await
}
/// 修改某个profile item的
#[tauri::command]
pub async fn patch_profile(index: String, profile: PrfItem) -> CmdResult {
// 保存修改前检查是否有更新 update_interval
let profiles = Config::profiles().await;
let should_refresh_timer = if let Ok(old_profile) = profiles.latest_arc().get_item(&index)
&& let Some(new_option) = profile.option.as_ref()
@@ -377,8 +362,7 @@ pub async fn patch_profile(index: String, profile: PrfItem) -> CmdResult {
false
};
// A selection written from the UI or the chain proxy is newer than anything a restore still
// in flight captured; without this it would be pushed back to the older node moments later.
// Prevent an in-flight restore from overwriting a newer UI or chain selection.
let records_a_selection = profile.selected.is_some();
profiles_patch_item_safe(&index, &profile)
@@ -389,14 +373,12 @@ pub async fn patch_profile(index: String, profile: PrfItem) -> CmdResult {
profiles::supersede_selected_activation();
}
// 如果更新间隔或允许自动更新变更,异步刷新定时器
if should_refresh_timer {
crate::process::AsyncHandler::spawn(move || async move {
logging!(info, Type::Timer, "Timer update settings changed, refreshing timer...");
if let Err(e) = crate::core::Timer::global().refresh().await {
logging!(error, Type::Timer, "Failed to refresh timer: {}", e);
} else {
// 刷新成功后发送自定义事件,不触发配置重载
crate::core::handle::Handle::notify_timer_updated(&index);
}
});
@@ -405,7 +387,6 @@ pub async fn patch_profile(index: String, profile: PrfItem) -> CmdResult {
Ok(())
}
/// 查看配置文件
#[tauri::command]
pub async fn view_profile(index: String) -> CmdResult {
let profiles = Config::profiles().await;
@@ -430,7 +411,6 @@ pub async fn view_profile(index: String) -> CmdResult {
help::open_file(path).with_error_code("PROFILE_OPEN_FAILED")
}
/// 读取配置文件内容
#[tauri::command]
pub async fn read_profile_file(index: String) -> CmdResult<String> {
let item = {
@@ -466,7 +446,6 @@ pub async fn read_profile_file(index: String) -> CmdResult<String> {
Ok(data)
}
/// 获取下一次更新时间
#[tauri::command]
pub async fn get_next_update_time(uid: String) -> CmdResult<Option<i64>> {
let timer = Timer::global();
+1 -11
View File
@@ -16,15 +16,7 @@ use std::{
sync::atomic::{AtomicBool, Ordering},
};
/// Record which node a group is on, in the current profile.
///
/// Takes the group and the node rather than the whole selection list, and merges them into the
/// profile on this side. The frontend used to send the list it had rendered, which made two
/// selections made before that list refreshed into one overwriting the other: both were built
/// from the same stale array, and the later write dropped the earlier group's choice. Since a
/// core start re-applies whatever the profile holds, the dropped one then came back on restart.
///
/// The tray already recorded this way. Now there is one way.
/// Merges one selection pair into fresh backend state to avoid stale-list overwrites.
#[tauri::command]
pub async fn record_selected_node(group_name: String, node: String) -> CmdResult<()> {
crate::config::profiles::record_selected_node(&group_name, &node)
@@ -32,7 +24,6 @@ pub async fn record_selected_node(group_name: String, node: String) -> CmdResult
.stringify_err()
}
/// Forget the persisted node selection for a proxy group in the current profile.
#[tauri::command]
pub async fn forget_selected_node(group_name: String) -> CmdResult<()> {
crate::config::profiles::forget_selected_node(&group_name)
@@ -76,7 +67,6 @@ pub async fn get_proxy_view() -> CmdResult<ProxyViewV1> {
}))
}
/// 同步托盘和GUI的代理选择状态
#[tauri::command]
pub async fn sync_tray_proxy_selection() -> CmdResult<()> {
if TRAY_SYNC_RUNNING
-6
View File
@@ -6,13 +6,11 @@ use serde_yaml_ng::Mapping;
use smartstring::alias::String;
use std::collections::{HashMap, HashSet};
/// 获取运行时配置
#[tauri::command]
pub async fn get_runtime_config() -> CmdResult<Option<Mapping>> {
Ok(Config::runtime().await.latest_arc().config.clone())
}
/// 获取运行时YAML配置
#[tauri::command]
pub async fn get_runtime_yaml() -> CmdResult<String> {
let runtime = Config::runtime().await;
@@ -29,13 +27,11 @@ pub async fn get_runtime_yaml() -> CmdResult<String> {
.stringify_err()
}
/// 获取运行时存在的键
#[tauri::command]
pub async fn get_runtime_exists() -> CmdResult<HashSet<String>> {
Ok(Config::runtime().await.latest_arc().exists_keys.clone())
}
/// 获取运行时日志
#[tauri::command]
pub async fn get_runtime_logs() -> CmdResult<HashMap<String, Vec<(String, String)>>> {
Ok(Config::runtime().await.latest_arc().chain_logs.clone())
@@ -72,7 +68,6 @@ pub async fn get_runtime_proxy_chain_config(proxy_chain_exit_node: String) -> Cm
.find(|proxy| proxy.get("name").map(|x| x.as_str()) == proxy_name)
&& !proxies_chain.is_empty()
{
// 添加第一个节点
proxies_chain.push(entry_proxy.to_owned());
}
@@ -91,7 +86,6 @@ pub async fn get_runtime_proxy_chain_config(proxy_chain_exit_node: String) -> Cm
}
}
/// 更新运行时链式代理配置
#[tauri::command]
pub async fn update_proxy_chain_config_in_runtime(proxy_chain_config: Option<serde_yaml_ng::Value>) -> CmdResult<()> {
match CoreManager::global()
+1 -6
View File
@@ -3,17 +3,12 @@ use crate::core::{
runstate::{RUN_STATE, RunStateView},
};
/// The whole Run State in one call.
///
/// Replaces the separate running-mode and service-install-state commands: they were three
/// caches on three refresh intervals describing one fact, which the frontend then had to keep
/// coherent by hand.
/// Returns one coherent core/service snapshot instead of independently refreshed state.
#[tauri::command]
pub async fn get_runtime_state() -> Result<RunStateView, String> {
Ok(RUN_STATE.settled().await.to_view())
}
/// Return unresolved failures without consuming them.
#[tauri::command]
pub async fn get_pending_failures() -> Vec<PendingFailure> {
notification::pending_failures()
-3
View File
@@ -1,6 +1,5 @@
use crate::cmd::CmdResult;
/// Platform-specific implementation for UWP functionality
#[cfg(windows)]
mod platform {
use crate::cmd::CmdResult;
@@ -12,7 +11,6 @@ mod platform {
}
}
/// Stub implementation for non-Windows platforms
#[cfg(not(windows))]
mod platform {
use super::CmdResult;
@@ -23,7 +21,6 @@ mod platform {
}
}
/// Command exposed to Tauri
#[tauri::command]
pub async fn invoke_uwp_tool() -> CmdResult {
platform::invoke_uwp_tool()
-5
View File
@@ -7,7 +7,6 @@ use crate::{
use reqwest_dav::list_cmd::ListFile;
use smartstring::alias::String;
/// 保存 WebDAV 配置
#[tauri::command]
pub async fn save_webdav_config(url: String, username: String, password: String) -> CmdResult<()> {
let patch = IVerge {
@@ -25,25 +24,21 @@ pub async fn save_webdav_config(url: String, username: String, password: String)
Ok(())
}
/// 创建 WebDAV 备份并上传
#[tauri::command]
pub async fn create_webdav_backup() -> CmdResult<()> {
feat::create_backup_and_upload_webdav().await.stringify_err()
}
/// 列出 WebDAV 上的备份文件
#[tauri::command]
pub async fn list_webdav_backup() -> CmdResult<Vec<ListFile>> {
feat::list_wevdav_backup().await.stringify_err()
}
/// 删除 WebDAV 上的备份文件
#[tauri::command]
pub async fn delete_webdav_backup(filename: String) -> CmdResult<()> {
feat::delete_webdav_backup(filename).await.stringify_err()
}
/// 从 WebDAV 恢复备份文件
#[tauri::command]
pub async fn restore_webdav_backup(filename: String) -> CmdResult<()> {
feat::restore_webdav_backup(filename)
+3 -21
View File
@@ -68,12 +68,11 @@ impl Config {
Self::global().await.runtime_config.clone()
}
/// Serialize transactions that share configuration draft layers.
/// Serializes transactions sharing configuration draft layers.
pub(crate) async fn lock_config_write() -> MutexGuard<'static, ()> {
CONFIG_WRITE_LOCK.lock().await
}
/// 初始化订阅
pub async fn init_config() -> Result<()> {
Self::init_config_before_window().await?;
Self::init_runtime_config().await
@@ -140,14 +139,12 @@ impl Config {
{
let profiles = Self::profiles().await.data_arc();
// Logging error internally
let _ = profiles.cleanup_orphaned_files().await;
}
Ok(())
}
// Ensure "Merge" and "Script" profile items exist, adding them if missing.
async fn ensure_default_profile_items() -> Result<()> {
let profiles = Self::profiles().await;
Self::ensure_default_profile_items_for(&profiles).await
@@ -175,7 +172,6 @@ impl Config {
}
async fn generate_and_validate() -> Result<Option<(&'static str, String)>> {
// 生成运行时配置
if let Err(err) = Self::generate().await {
let error_msg: String = err.to_string().into();
logging!(error, Type::Config, "生成运行时配置失败: {}", error_msg);
@@ -186,18 +182,14 @@ impl Config {
}
logging!(info, Type::Config, "生成运行时配置成功");
// 生成运行时配置文件并验证
let config_result = Self::generate_file(ConfigType::Run).await;
if config_result.is_ok() {
// 验证配置文件
logging!(info, Type::Config, "开始验证配置");
match CoreConfigValidator::global().validate_config_outcome().await {
Ok(outcome) if outcome.is_valid() => {
logging!(info, Type::Config, "配置验证成功");
// 前端没有必要知道验证成功的消息,也没有事件驱动
// Some(("config_validate::success", String::new()))
Ok(None)
}
Ok(outcome) => {
@@ -238,7 +230,6 @@ impl Config {
let runtime = Self::runtime().await;
let runtime_lastest = runtime.latest_arc();
// Fall back to committed config if runtime config is missing
let runtime_data = runtime.data_arc();
let config = runtime_lastest
.config
@@ -259,8 +250,7 @@ impl Config {
let (mut config, exists_keys, logs) = enhance::enhance(profiles).await?;
sanitize_tunnels_proxy(&mut config);
// Applied here rather than in the Merge Config: this is the only place the Core's config
// is built, and `config.yaml` must stay unaware so the next launch asks for the original.
// Apply only to generated core config so the saved choice survives the next launch.
if let Some(port) = MixedPort::session_fallback() {
config.insert(MIXED_PORT_KEY.into(), port.into());
}
@@ -300,8 +290,7 @@ impl Config {
}
}
// 升级草稿为正式数据,并写入文件。避免用户行为丢失。
// 仅在应用退出、重启、关机监听事件启用
/// Commits drafts during exit/restart/shutdown so user changes are not lost.
pub async fn apply_all_and_save_file() {
logging!(info, Type::Config, "save all draft data");
let save_clash_task = AsyncHandler::spawn(|| async {
@@ -329,7 +318,6 @@ impl Config {
}
fn sanitize_tunnels_proxy(config: &mut Mapping) {
// 检查是否存在 tunnels
if !config
.get("tunnels")
.and_then(|v| v.as_sequence())
@@ -338,7 +326,6 @@ fn sanitize_tunnels_proxy(config: &mut Mapping) {
return;
}
// 在需要时,收集可用目标(proxies + proxy-groups + 内建)
let mut valid: HashSet<String> = HashSet::with_capacity(64);
collect_names(config, "proxies", &mut valid);
collect_names(config, "proxy-groups", &mut valid);
@@ -350,7 +337,6 @@ fn sanitize_tunnels_proxy(config: &mut Mapping) {
return;
};
// 修改 tunnels:删除无效 proxy
for item in tunnels {
let Some(tunnel) = item.as_mapping_mut() else { continue };
@@ -368,7 +354,6 @@ fn sanitize_tunnels_proxy(config: &mut Mapping) {
}
}
// tunnels 存在且至少有一条 tunnel 的 proxy 需要校验时才返回 true
fn tunnels_need_validation(tunnels: &[Value]) -> bool {
tunnels.iter().any(|item| {
item.as_mapping()
@@ -411,11 +396,8 @@ mod tests {
fn test_prfitem_from_merge_size() {
let merge_item = PrfItem::from_merge(Some("Merge".into())).expect("Failed to create merge item in test");
let prfitem_size = mem::size_of_val(&merge_item);
// Boxed version
let boxed_merge_item = Box::new(merge_item);
let box_prfitem_size = mem::size_of_val(&boxed_merge_item);
// The size of Box<T> is always pointer-sized (usually 8 bytes on 64-bit)
// assert_eq!(box_prfitem_size, mem::size_of::<Box<PrfItem>>());
assert!(box_prfitem_size < prfitem_size);
}
-10
View File
@@ -15,41 +15,33 @@ tokio::task_local! {
static ENCRYPTION_ACTIVE: Cell<bool>;
}
/// Encrypt data
pub fn encrypt_data(data: &str) -> Result<String, Box<dyn std::error::Error>> {
let encryption_key = get_encryption_key()?;
let cipher = Aes256Gcm::new_from_slice(&encryption_key)?;
// Generate random nonce
let mut nonce = [0u8; NONCE_LENGTH];
getrandom::fill(&mut nonce)?;
// Encrypt data
let ciphertext = cipher
.encrypt((&nonce).into(), data.as_bytes())
.map_err(|e| format!("Encryption failed: {e}"))?;
// Concatenate nonce and ciphertext and encode them in base64
let mut combined = nonce.to_vec();
combined.extend(ciphertext);
Ok(STANDARD.encode(combined))
}
/// Decrypt data
pub fn decrypt_data(encrypted: &str) -> Result<String, Box<dyn std::error::Error>> {
let encryption_key = get_encryption_key()?;
let cipher = Aes256Gcm::new_from_slice(&encryption_key)?;
// Decode from base64
let data = STANDARD.decode(encrypted)?;
if data.len() < NONCE_LENGTH {
return Err("Invalid encrypted data".into());
}
// Separate nonce and ciphertext
let (nonce, ciphertext) = data.split_at(NONCE_LENGTH);
let nonce: &[u8; NONCE_LENGTH] = nonce.try_into()?;
// Decrypt data
let plaintext = cipher
.decrypt(nonce.into(), ciphertext)
.map_err(|e| format!("Decryption failed: {e}"))?;
@@ -57,7 +49,6 @@ pub fn decrypt_data(encrypted: &str) -> Result<String, Box<dyn std::error::Error
String::from_utf8(plaintext).map_err(|e| e.into())
}
/// Serialize encrypted function
pub fn serialize_encrypted<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: Serialize,
@@ -72,7 +63,6 @@ where
}
}
/// Deserialize decrypted function
pub fn deserialize_encrypted<'a, D, T>(deserializer: D) -> Result<T, D::Error>
where
T: for<'de> Deserialize<'de> + Default,
+6 -23
View File
@@ -1,8 +1,5 @@
//! Resolving the Mixed Port.
//!
//! Two questions, deliberately kept apart because the answers can differ:
//! [`MixedPort::desired`] — what we configured, answerable before the Core exists;
//! [`MixedPort::effective`] — what the Core reports it is serving on.
//! Resolves the configured [`MixedPort::desired`] separately from the Core's live
//! [`MixedPort::effective`] value.
use std::sync::atomic::{AtomicU16, Ordering};
@@ -14,18 +11,12 @@ use crate::core::handle::Handle;
/// The port mihomo listens on when nothing else is configured.
pub const DEFAULT_MIXED_PORT: u16 = 7897;
/// The port a startup fallback moved this session to, or 0 for none.
///
/// Never persisted: the user's port stays in `verge.yaml` and `config.yaml` so the next launch
/// asks for it again. Persisting it instead made a once-blocked port climb on every launch.
/// A startup fallback for this session only; persistence would make the port climb across launches.
static SESSION_FALLBACK: AtomicU16 = AtomicU16::new(0);
/// Resolving the Mixed Port.
pub struct MixedPort;
impl MixedPort {
/// Read by [`Self::desired`] and by runtime config generation — the two places that decide
/// what the Core is asked to listen on.
pub(crate) fn session_fallback() -> Option<u16> {
match SESSION_FALLBACK.load(Ordering::Acquire) {
0 => None,
@@ -41,12 +32,7 @@ impl MixedPort {
SESSION_FALLBACK.store(0, Ordering::Release);
}
/// The Mixed Port the app has configured. Safe before the Core exists.
///
/// Reads the draft layer, so this can name a port the Core has not moved to yet — which
/// matters because the PAC endpoint hands it outside the app. Safe only because
/// `feat::listener::save_proxy_ports` closes PAC across the staging (`core_starting` /
/// `core_start_settled`); any other path staging a listener port must do the same.
/// Returns the configured port. Paths staging it must close PAC until the Core catches up.
pub async fn desired() -> u16 {
let selected = Config::verge().await.latest_arc().verge_mixed_port;
// `get_mixed_port` already falls back to the default when the Merge Config is silent.
@@ -54,9 +40,7 @@ impl MixedPort {
resolve_desired(Self::session_fallback(), selected, merged)
}
/// The Mixed Port the Core is actually serving on.
///
/// Costs a round-trip, so use it on user-triggered paths, not per-request ones.
/// Returns the Core's live port; reserve this round-trip for user-triggered paths.
pub async fn effective() -> u16 {
let desired = Self::desired().await;
resolve_effective(
@@ -67,8 +51,7 @@ impl MixedPort {
}
}
/// Fallback first — it is the only value not written down, so the files still name the port the
/// user asked for while this session serves another.
/// The session-only fallback outranks persisted choices.
const fn resolve_desired(session_fallback: Option<u16>, selected: Option<u16>, merged: u16) -> u16 {
match (session_fallback, selected) {
(Some(port), _) | (None, Some(port)) => port,
+1 -46
View File
@@ -20,45 +20,34 @@ use tauri::Url;
pub struct PrfItem {
pub uid: Option<String>,
/// profile item type
/// enum value: remote | local | script | merge
#[serde(rename = "type")]
pub itype: Option<String>,
/// profile name
pub name: Option<String>,
/// profile file
pub file: Option<String>,
/// profile description
#[serde(skip_serializing_if = "Option::is_none")]
pub desc: Option<String>,
/// source url
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
/// selected information
#[serde(skip_serializing_if = "Option::is_none")]
pub selected: Option<Vec<PrfSelected>>,
/// subscription user info
#[serde(skip_serializing_if = "Option::is_none")]
pub extra: Option<PrfExtra>,
/// updated time
pub updated: Option<usize>,
/// some options of the item
#[serde(skip_serializing_if = "Option::is_none")]
pub option: Option<PrfOption>,
/// profile web page url
#[serde(skip_serializing_if = "Option::is_none")]
pub home: Option<String>,
/// the file data
#[serde(skip)]
pub file_data: Option<String>,
}
@@ -79,32 +68,23 @@ pub struct PrfExtra {
#[derive(Default, Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct PrfOption {
/// for `remote` profile's http request
/// see issue #13
#[serde(skip_serializing_if = "Option::is_none")]
pub user_agent: Option<String>,
/// for `remote` profile
/// use system proxy
#[serde(skip_serializing_if = "Option::is_none")]
pub with_proxy: Option<bool>,
/// for `remote` profile
/// use self proxy
#[serde(skip_serializing_if = "Option::is_none")]
pub self_proxy: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub update_interval: Option<u64>,
/// for `remote` profile
/// HTTP request timeout in seconds
/// default is 60 seconds
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout_seconds: Option<u64>,
/// for `remote` profile
/// disable certificate validation
/// default is `false`
#[serde(skip_serializing_if = "Option::is_none")]
pub danger_accept_invalid_certs: Option<bool>,
@@ -151,8 +131,7 @@ impl PrfOption {
}
impl PrfItem {
/// From partial item
/// must contain `itype`
/// Builds an item from a partial value that must include `itype`.
pub async fn from(item: &Self, file_data: Option<String>) -> Result<Self> {
if item.itype.is_none() {
bail!("type should not be null");
@@ -183,8 +162,6 @@ impl PrfItem {
}
}
/// ## Local type
/// create a new item from name/desc
pub async fn from_local(
name: String,
desc: String,
@@ -250,8 +227,6 @@ impl PrfItem {
})
}
/// ## Remote type
/// create a new item from url
pub async fn from_url(
url: &str,
name: Option<&String>,
@@ -271,7 +246,6 @@ impl PrfItem {
let mut proxies = option.and_then(|o| o.proxies.clone());
let mut groups = option.and_then(|o| o.groups.clone());
// 选择代理类型
let proxy_type = if self_proxy {
ProxyType::Localhost
} else if with_proxy {
@@ -282,7 +256,6 @@ impl PrfItem {
let url = fix_dirty_url(url)?;
// 使用网络管理器发送请求
let resp = match NetworkManager::new()
.get_with_interrupt(
url.as_str(),
@@ -307,7 +280,6 @@ impl PrfItem {
let header = resp.headers();
// parse the Subscription UserInfo
let extra;
'extra: {
for (k, v) in header.iter() {
@@ -330,7 +302,6 @@ impl PrfItem {
extra = None;
}
// parse the Content-Disposition
let filename = match header.get("Content-Disposition") {
Some(value) => {
let filename = format!("{value:?}");
@@ -380,10 +351,8 @@ impl PrfItem {
.unwrap_or_else(|| filename.map(|s| s.into()).unwrap_or_else(|| "Remote File".into()));
let data = resp.text_with_charset()?;
// process the charset "UTF-8 with BOM"
let data = data.trim_start_matches('\u{feff}');
// check the data whether the valid yaml format
let yaml = serde_yaml_ng::from_str::<Mapping>(data).context("the remote profile data is invalid yaml")?;
if !yaml.contains_key("proxies") && !yaml.contains_key("proxy-providers") {
@@ -441,8 +410,6 @@ impl PrfItem {
})
}
/// ## Merge type (enhance)
/// create the enhanced item by using `merge` rule
pub fn from_merge(uid: Option<String>) -> Result<Self> {
let (id, template) = if let Some(uid) = uid {
(uid, tmpl::ITEM_MERGE.into())
@@ -461,8 +428,6 @@ impl PrfItem {
})
}
/// ## Script type (enhance)
/// create the enhanced item by using javascript quick.js
pub fn from_script(uid: Option<String>) -> Result<Self> {
let id = if let Some(uid) = uid {
uid
@@ -480,7 +445,6 @@ impl PrfItem {
})
}
/// ## Rules type (enhance)
pub fn from_rules() -> Result<Self> {
let uid = help::get_uid("r").into();
let file = format!("{uid}.yaml").into(); // yaml ext
@@ -495,7 +459,6 @@ impl PrfItem {
})
}
/// ## Proxies type (enhance)
pub fn from_proxies() -> Result<Self> {
let uid = help::get_uid("p").into();
let file = format!("{uid}.yaml").into(); // yaml ext
@@ -510,7 +473,6 @@ impl PrfItem {
})
}
/// ## Groups type (enhance)
pub fn from_groups() -> Result<Self> {
let uid = help::get_uid("g").into();
let file = format!("{uid}.yaml").into(); // yaml ext
@@ -525,7 +487,6 @@ impl PrfItem {
})
}
/// get the file data
pub async fn read_file(&self) -> Result<String> {
let file = self
.file
@@ -538,7 +499,6 @@ impl PrfItem {
Ok(content.into())
}
/// save the file data
pub async fn save_file(&self, data: String) -> Result<()> {
let file = self
.file
@@ -552,27 +512,22 @@ impl PrfItem {
}
impl PrfItem {
/// 获取current指向的订阅的merge
pub fn current_merge(&self) -> Option<&String> {
self.option.as_ref().and_then(|o| o.merge.as_ref())
}
/// 获取current指向的订阅的script
pub fn current_script(&self) -> Option<&String> {
self.option.as_ref().and_then(|o| o.script.as_ref())
}
/// 获取current指向的订阅的rules
pub fn current_rules(&self) -> Option<&String> {
self.option.as_ref().and_then(|o| o.rules.as_ref())
}
/// 获取current指向的订阅的proxies
pub fn current_proxies(&self) -> Option<&String> {
self.option.as_ref().and_then(|o| o.proxies.as_ref())
}
/// 获取current指向的订阅的groups
pub fn current_groups(&self) -> Option<&String> {
self.option.as_ref().and_then(|o| o.groups.as_ref())
}
+18 -123
View File
@@ -30,19 +30,11 @@ use tokio::{fs, task::JoinHandle};
pub(crate) static PROFILE_WRITE_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
/// Regex to check profile file names, eg.
/// R12345678.yaml (remote)
/// L12345678.yaml (local)
/// m12345678.yaml (merge)
/// s12345678.js (script)
/// r12345678.yaml (rules)
/// p12345678.yaml (proxies)
/// g12345678.yaml (groups)
/// Matches generated profile files such as `R12345678.yaml` or `s12345678.js`.
#[allow(clippy::unwrap_used)]
static REGEX_PROFILE_FILE: LazyLock<regex::Regex> =
LazyLock::new(|| regex::Regex::new(r"^(?:[RLmrpg][a-zA-Z0-9]+\.yaml|s[a-zA-Z0-9]+\.js)$").unwrap());
// activate selected nodes task handle
static ACTIVATE_SELECTED_TASK: LazyLock<Mutex<Option<JoinHandle<()>>>> = LazyLock::new(|| Mutex::new(None));
static ACTIVATE_SELECTED_GENERATION: AtomicU64 = AtomicU64::new(0);
@@ -50,29 +42,18 @@ static ACTIVATE_SELECTED_GENERATION: AtomicU64 = AtomicU64::new(0);
// lock acquisition, connection-pool waiting, and local-socket connection establishment.
const MIHOMO_OPERATION_TIMEOUT: Duration = Duration::from_secs(10);
const SELECTED_NODES_RECHECK_DELAY: Duration = Duration::from_secs(1);
/// How long a restore keeps trying to put back selections the core has not loaded yet.
///
/// A provider-backed group is present but empty until its provider finishes loading, which on a
/// cold start is exactly when restoring runs. Bounded rather than indefinite because a record
/// naming a node the profile genuinely no longer has looks identical from here, and would
/// otherwise be retried for the life of the process.
/// Bounds retries while provider-backed groups finish loading.
const SELECTED_NODES_SETTLE_DEADLINE: Duration = Duration::from_secs(30);
/// How often a restore looks again while waiting for those groups.
const SELECTED_NODES_SETTLE_INTERVAL: Duration = Duration::from_secs(1);
/// How long a start waits for the selections that *can* be put back before carrying on.
///
/// Covers a healthy core answering one query and a handful of selects. Past that the start
/// proceeds: the remaining groups need the core to finish loading, which is not something a
/// start can usefully wait for.
/// Bounds startup waiting; remaining selections continue restoring in the background.
const SELECTED_NODES_FIRST_PASS_BUDGET: Duration = Duration::from_secs(3);
/// Define the `profiles.yaml` schema
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct IProfiles {
/// same as PrfConfig.current
pub current: Option<String>,
/// profile list
pub items: Option<Vec<PrfItem>>,
}
@@ -116,7 +97,6 @@ macro_rules! patch {
}
impl IProfiles {
// Helper to find and remove an item by uid from the items vec, returning its file name (if any).
fn take_item_file_by_uid(items: &mut Vec<PrfItem>, target_uid: Option<&str>) -> Option<String> {
let index = items.iter().position(|item| item.uid.as_deref() == target_uid)?;
items.remove(index).file
@@ -178,12 +158,10 @@ impl IProfiles {
self.current.as_ref()
}
/// get items ref
pub const fn get_items(&self) -> Option<&Vec<PrfItem>> {
self.items.as_ref()
}
/// find the item by the uid
pub fn get_item(&self, uid: impl AsRef<str>) -> Result<&PrfItem> {
let uid_str = uid.as_ref();
@@ -200,17 +178,12 @@ impl IProfiles {
bail!("failed to get the profile item \"uid:{}\"", uid_str);
}
/// append new item
/// if the file_data is some
/// then should save the data to file
pub async fn append_item(&mut self, item: &mut PrfItem) -> Result<()> {
let uid = &item.uid;
if uid.is_none() {
bail!("the uid should not be null");
}
// save the file data
// move the field value after save
if let Some(file_data) = item.file_data.take() {
if item.file.is_none() {
bail!("the file should not be null");
@@ -242,7 +215,6 @@ impl IProfiles {
Ok(())
}
/// reorder items
pub async fn reorder(&mut self, active_id: &String, over_id: &String) -> Result<()> {
let mut items = self.items.take().unwrap_or_default();
let mut old_index = None;
@@ -267,7 +239,6 @@ impl IProfiles {
self.save_file().await
}
/// update the item value
pub async fn patch_item(&mut self, uid: &String, item: &PrfItem) -> Result<()> {
if let Some(file) = &item.file {
Self::validate_profile_file(file)?;
@@ -312,14 +283,12 @@ impl IProfiles {
Ok(())
}
/// be used to update the remote item
/// only patch `updated` `extra` `file_data`
/// Updates fields returned by a remote profile refresh.
pub async fn update_item(&mut self, uid: &String, item: &mut PrfItem) -> Result<()> {
if self.items.is_none() {
self.items = Some(vec![]);
}
// find the item
let _ = self.get_item(uid)?;
if let Some(items) = self.items.as_mut() {
@@ -331,14 +300,11 @@ impl IProfiles {
each.updated = item.updated;
each.home = item.home.to_owned();
each.option = PrfOption::merge(each.option.as_ref(), item.option.as_ref());
// save the file data
// move the field value after save
if let Some(file_data) = item.file_data.take() {
let file = each.file.take();
let file =
file.unwrap_or_else(|| item.file.take().unwrap_or_else(|| format!("{}.yaml", uid).into()));
// the file must exists
each.file = Some(file.clone());
let path = dirs::app_profiles_dir()?.join(file.as_str());
@@ -434,12 +400,10 @@ impl IProfiles {
}
}
/// 判断profile是否是current指向的
pub fn is_current_profile_index(&self, index: &String) -> bool {
self.current.as_ref() == Some(index)
}
/// 获取所有的profiles(uid,名称, 是否为 current)
pub fn profiles_preview(&self) -> Option<Vec<IProfilePreview<'_>>> {
self.items.as_ref().map(|items| {
items
@@ -457,7 +421,6 @@ impl IProfiles {
})
}
/// 通过 uid 获取名称
pub fn get_name_by_uid(&self, uid: &String) -> Option<&String> {
if let Some(items) = &self.items {
for item in items {
@@ -480,10 +443,8 @@ impl IProfiles {
return Ok(());
}
// 如果 items 为空(例如 profiles.yaml 解析失败导致
// IProfiles::new() 返回 default),此时无法判断哪些文件是活跃的,
// 跳过清理以避免误删用户正在使用的订阅配置文件。
// See: https://github.com/clash-verge-rev/clash-verge-rev/issues/7577
// An empty list may mean loading failed, so it is not safe evidence that every file is orphaned.
// https://github.com/clash-verge-rev/clash-verge-rev/issues/7577
if self.items.as_ref().is_none_or(|v| v.is_empty()) {
logging!(
warn,
@@ -493,13 +454,10 @@ impl IProfiles {
return Ok(());
}
// 获取所有 active profile 的文件名集合
let active_files = self.get_all_active_files();
// 添加全局扩展配置文件到保护列表
let protected_files = self.get_protected_global_files();
// 扫描 profiles 目录下的所有文件
let mut total_files = 0;
let mut deleted_files = 0;
let mut failed_deletions = 0;
@@ -517,13 +475,11 @@ impl IProfiles {
if let Some(file_name) = path.file_name().and_then(|n| n.to_str())
&& Self::is_profile_file(file_name)
{
// 检查是否为全局扩展文件
if protected_files.contains(file_name) {
logging!(debug, Type::Config, "保护全局扩展配置文件: {file_name}");
continue;
}
// 检查是否为活跃文件
if !active_files.contains(file_name) {
match path.to_path_buf().remove_if_exists().await {
Ok(_) => {
@@ -557,7 +513,6 @@ impl IProfiles {
Ok(())
}
/// 不删除全局扩展配置
fn get_protected_global_files(&self) -> HashSet<String> {
let mut protected_files = HashSet::new();
@@ -567,23 +522,19 @@ impl IProfiles {
protected_files
}
/// 获取所有 active profile 关联的文件名
fn get_all_active_files(&self) -> HashSet<&str> {
let mut active_files: HashSet<&str> = HashSet::new();
if let Some(items) = &self.items {
for item in items {
// 收集所有类型 profile 的文件
if let Some(file) = &item.file {
active_files.insert(file);
}
// 对于主 profile 类型(remote/local),还需要收集其关联的扩展文件
if let Some(itype) = &item.itype
&& (itype == "remote" || itype == "local")
&& let Some(option) = &item.option
{
// 收集关联的扩展文件
if let Some(merge_uid) = &option.merge
&& let Ok(merge_item) = self.get_item(merge_uid)
&& let Some(file) = &merge_item.file
@@ -625,13 +576,12 @@ impl IProfiles {
active_files
}
/// 检查文件名是否符合 profile 文件的命名规则
fn is_profile_file(filename: &str) -> bool {
REGEX_PROFILE_FILE.is_match(filename)
}
}
// 特殊的Send-safe helper函数,完全避免跨await持有guard
// These helpers avoid holding a parking_lot guard across `await`.
use crate::config::Config;
pub async fn profiles_append_item_with_filedata_safe(item: &PrfItem, file_data: Option<String>) -> Result<()> {
@@ -814,16 +764,7 @@ fn reconcile_selected_nodes(
plan
}
/// Abandon any activation still in flight.
///
/// An activation reads the profile, then polls the core until its groups are readable, then puts
/// its selections. A choice the user makes inside that window is newer than what the activation
/// captured, so letting it finish would push the core back to the older node — and the profile,
/// already holding the newer one, would then disagree with the core. Bumping the generation is
/// how an activation is told it has been overtaken.
///
/// Called after a newer selection is written, or before a candidate Core transition whose caller
/// will reactivate whichever Profiles snapshot ultimately commits.
/// Cancels restoration so it cannot overwrite a newer selection or profile snapshot.
pub fn supersede_selected_activation() {
ACTIVATE_SELECTED_GENERATION.fetch_add(1, Ordering::AcqRel);
}
@@ -924,12 +865,7 @@ async fn update_tray_after_activation(generation: u64) {
}
}
/// Record which node a group is on, in the current profile.
///
/// The counterpart of the frontend's `useRecordSelection`, for the selections the backend makes
/// on the user's behalf — the tray is the one that does. What the profile holds is what
/// [`activate_selected_nodes`] re-applies when a core starts, so a selection it never learned
/// about is one the next start silently undoes.
/// Records a backend-made selection so the next core start restores it.
pub async fn record_selected_node(group_name: &str, node: &str) -> Result<()> {
let group_name = String::from(group_name);
let node = String::from(node);
@@ -970,7 +906,6 @@ pub async fn record_selected_node(group_name: &str, node: &str) -> Result<()> {
.await?;
if recorded {
// Newer than anything an activation still in flight captured.
supersede_selected_activation();
handle::Handle::refresh_profiles();
}
@@ -1056,12 +991,7 @@ async fn persist_reconciled_selected(
Ok(())
}
/// The recorded selections the core is not currently on.
///
/// Deliberately "is the core on it" rather than "did we send a select": a group whose provider
/// has not loaded is present but empty, a `select` can fail while the core is still settling, and
/// a group that was already correct never produces an activation at all. Only the running state
/// tells those apart.
/// Returns recorded selections whose groups have not reached the requested node.
fn unsettled_selections(selected: &[PrfSelected], proxies: &Proxies) -> Vec<String> {
selected
.iter()
@@ -1077,16 +1007,7 @@ fn unsettled_selections(selected: &[PrfSelected], proxies: &Proxies) -> Vec<Stri
.collect()
}
/// Keep putting back the selections the core could not be moved to yet.
///
/// The single re-check a profile switch needs is enough there, because a switch happens once the
/// configuration is loaded. A core start is the opposite case: the groups a restore is trying to
/// put back may not exist yet. Without this the restore reported success having applied nothing,
/// and the group stayed on the first entry of its `proxies:` list — the outcome restoring exists
/// to prevent.
///
/// Only ever reached with [`SelectionRepair::KeepRecords`]: a profile switch is entitled to
/// conclude that a group it cannot see is gone, so it has nothing to wait for.
/// Retries selections while provider-backed groups finish loading.
async fn settle_pending_selections(selected: &[PrfSelected], completed: &mut HashMap<String, String>, generation: u64) {
let deadline = Instant::now() + SELECTED_NODES_SETTLE_DEADLINE;
loop {
@@ -1133,11 +1054,7 @@ async fn settle_pending_selections(selected: &[PrfSelected], completed: &mut Has
}
}
/// Fires once the selections that could be applied have been.
///
/// Dropping it without firing releases the waiter too: every way the worker can end early is a
/// way of saying "nothing more is coming", and a start that waits forever for one of them would
/// be a worse failure than the one this exists to prevent.
/// Releases the first-pass waiter even when restoration exits early.
struct FirstPassSignal(Option<tokio::sync::oneshot::Sender<()>>);
impl FirstPassSignal {
@@ -1212,8 +1129,7 @@ async fn activate_selected_nodes_worker(
}
if repair == SelectionRepair::KeepRecords {
// Everything the core could be moved to has been. Whoever is waiting to point traffic
// here may go; what is left needs the core to finish loading and cannot be waited on.
// Remaining selections depend on provider loading and continue in the background.
first_pass_done.notify();
settle_pending_selections(&selected, &mut completed_activations, generation).await;
return Ok(());
@@ -1235,14 +1151,9 @@ async fn activate_selected_nodes_worker(
/// Whether an activation may also prune the records it cannot match.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SelectionRepair {
/// Drop records whose group or node the core does not have. Only safe once the configuration
/// they belong to is known to be fully loaded, which is true right after a profile switch.
/// Prune records after a fully loaded profile switch.
Prune,
/// Apply what can be applied and leave the records alone.
///
/// What a core start needs. A provider-backed group can be empty or absent for seconds after
/// the core comes up, and a record pruned on that evidence is gone from `profiles.yaml` for
/// good — the provider finishing later cannot bring it back.
/// Preserve records during startup while provider-backed groups may still be absent.
KeepRecords,
}
@@ -1253,18 +1164,7 @@ pub fn activate_selected_nodes() -> Result<()> {
Ok(())
}
/// Put the profile's selections back before anything is pointed at the core.
///
/// Awaited, unlike a profile switch's activation, because the caller enables the system proxy as
/// soon as this returns: a proxy pointed at a core still sitting on the first entry of every
/// group is the outcome restoring exists to prevent. Bounded so that a core which will not answer
/// delays a start rather than blocking it — the selections keep being retried either way, and
/// being proxied through the wrong node beats not being proxied at all.
///
/// TUN is deliberately not covered, because it cannot be: it is configured in the core's own
/// file, so it is carrying traffic from the moment the process starts, before anything here could
/// run. What puts a TUN user on the right node from the first packet is the core's own
/// `cache.db`, which is why the service keeps an owner's runtime generation across restarts.
/// Restores available selections before the system proxy points at the core, then retries the rest.
pub async fn restore_selected_nodes() {
let first_pass = activate_selected_nodes_with(SelectionRepair::KeepRecords);
if tokio::time::timeout(SELECTED_NODES_FIRST_PASS_BUDGET, first_pass)
@@ -1287,8 +1187,7 @@ fn activate_selected_nodes_with(repair: SelectionRepair) -> tokio::sync::oneshot
let (first_pass_sender, first_pass_done) = tokio::sync::oneshot::channel();
let handle = tokio::spawn(async move {
// Held for the whole task so that every exit — superseded, no profile, an error — drops
// it and releases anyone waiting. Only the worker fires it deliberately.
// Dropping this releases the startup waiter on every early exit.
let first_pass = FirstPassSignal(Some(first_pass_sender));
if let Some(previous_task) = previous_task {
let _ = previous_task.await;
@@ -1298,11 +1197,7 @@ fn activate_selected_nodes_with(repair: SelectionRepair) -> tokio::sync::oneshot
}
let result = async {
// Committed, not the draft. A profile switch stages its target before validating
// it, and a switch that then fails discards that draft — but an activation which had
// already read it would go on to apply the rejected profile's selections to the one
// still running. The switch that succeeds commits before it activates, so this is the
// same value there.
// A draft may be a profile switch that has not passed validation yet.
let profiles = Config::profiles().await.data_arc();
let current = profiles.get_current().context("no current profile running")?.clone();
let selected = profiles
+2 -51
View File
@@ -9,8 +9,7 @@ const PATCH_CONFIG_INNER: [&str; 5] = ["allow-lan", "ipv6", "log-level", "unifie
#[derive(Default, Clone)]
pub struct IRuntime {
pub config: Option<Mapping>,
// 记录在订阅中(包括merge和script生成的)出现过的keys
// 这些keys不一定都生效
// Keys seen in the profile pipeline, including merge and script output.
pub exists_keys: HashSet<String>,
// TODO 或许可以用 FixMap 来存储以提升效率
pub chain_logs: HashMap<String, Vec<(String, String)>>,
@@ -22,7 +21,6 @@ impl IRuntime {
Self::default()
}
// 这里只更改 allow-lan | ipv6 | log-level | tun | tunnels
#[inline]
pub fn patch_config(&mut self, patch: &Mapping) {
let config = if let Some(config) = self.config.as_mut() {
@@ -56,54 +54,7 @@ impl IRuntime {
}
}
/// 更新链式代理配置
///
/// 该函数更新 `proxies` 和 `proxy-groups` 配置,并处理链式代理的修改或(传入 None )删除。
///
/// 配置示例:
///
/// ```json
/// {
/// "proxies": [
/// {
/// "name": "入口节点",
/// "type": "xxx",
/// "server": "xxx",
/// "port": "xxx",
/// "ports": "xxx",
/// "password": "xxx",
/// "skip-cert-verify": "xxx"
/// },
/// {
/// "name": "hop_node_1_xxxx",
/// "type": "xxx",
/// "server": "xxx",
/// "port": "xxx",
/// "ports": "xxx",
/// "password": "xxx",
/// "skip-cert-verify": "xxx",
/// "dialer-proxy": "入口节点"
/// },
/// {
/// "name": "出口节点",
/// "type": "xxx",
/// "server": "xxx",
/// "port": "xxx",
/// "ports": "xxx",
/// "password": "xxx",
/// "skip-cert-verify": "xxx",
/// "dialer-proxy": "hop_node_1_xxxx"
/// }
/// ],
/// "proxy-groups": [
/// {
/// "name": "proxy_chain",
/// "type": "select",
/// "proxies": ["出口节点"]
/// }
/// ]
/// }
/// ```
/// Rebuilds `dialer-proxy` links from an ordered proxy chain, or removes them for `None`.
#[inline]
pub fn update_proxy_chain_config(&mut self, proxy_chain_config: Option<Value>) {
let config = if let Some(config) = self.config.as_mut() {
+1 -69
View File
@@ -12,175 +12,127 @@ use smartstring::alias::String;
/// ### `verge.yaml` schema
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct IVerge {
/// app log level
/// silent | error | warn | info | debug | trace
pub app_log_level: Option<String>,
/// app log max size in KB
/// Maximum app log size in KiB.
pub app_log_max_size: Option<u64>,
/// app log max count
pub app_log_max_count: Option<usize>,
// i18n
pub language: Option<String>,
/// `light` or `dark` or `system`
pub theme_mode: Option<String>,
/// tray click event
pub tray_event: Option<String>,
/// copy env type
pub env_type: Option<String>,
/// start page
pub start_page: Option<String>,
/// startup script path
pub startup_script: Option<String>,
/// enable traffic graph default is true
pub traffic_graph: Option<bool>,
/// show memory info (only for Clash Meta)
pub enable_memory_usage: Option<bool>,
/// enable group icon
#[serde(skip_serializing_if = "Option::is_none")]
pub enable_group_icon: Option<bool>,
/// pause render traffic stats on blur
pub pause_render_traffic_stats_on_blur: Option<bool>,
/// common tray icon
#[serde(skip_serializing_if = "Option::is_none")]
pub common_tray_icon: Option<bool>,
/// tray icon
#[cfg(target_os = "macos")]
#[serde(skip_serializing_if = "Option::is_none")]
pub tray_icon: Option<String>,
/// menu icon
#[serde(skip_serializing_if = "Option::is_none")]
pub menu_icon: Option<String>,
/// menu order
#[serde(skip_serializing_if = "Option::is_none")]
pub menu_order: Option<Vec<String>>,
/// toast / notice position on screen
#[serde(skip_serializing_if = "Option::is_none")]
pub notice_position: Option<String>,
/// collapse navigation bar
pub collapse_navbar: Option<bool>,
/// sysproxy tray icon
pub sysproxy_tray_icon: Option<bool>,
/// tun tray icon
pub tun_tray_icon: Option<bool>,
/// clash tun mode
pub enable_tun_mode: Option<bool>,
/// can the app auto startup
pub enable_auto_launch: Option<bool>,
/// not show the window on launch
pub enable_silent_start: Option<bool>,
/// set system proxy
pub enable_system_proxy: Option<bool>,
/// enable proxy guard
pub enable_proxy_guard: Option<bool>,
/// enable bypass format check
pub enable_bypass_check: Option<bool>,
/// enable dns settings - this controls whether dns_config.yaml is applied
pub enable_dns_settings: Option<bool>,
/// always use default bypass
pub use_default_bypass: Option<bool>,
/// set system proxy bypass
pub system_proxy_bypass: Option<String>,
/// proxy guard duration
pub proxy_guard_duration: Option<u64>,
/// use pac mode
pub proxy_auto_config: Option<bool>,
/// pac script content
pub pac_file_content: Option<String>,
/// proxy host address
pub proxy_host: Option<String>,
/// theme setting
pub theme_setting: Option<IVergeTheme>,
/// web ui list
pub web_ui_list: Option<Vec<String>>,
/// clash core path
#[serde(skip_serializing_if = "Option::is_none")]
pub clash_core: Option<String>,
/// hotkey map
/// format: {func},{key}
#[serde(skip_serializing_if = "Option::is_none")]
pub hotkeys: Option<Vec<String>>,
/// enable global hotkey
pub enable_global_hotkey: Option<bool>,
/// 首页卡片设置
/// 控制首页各个卡片的显示和隐藏
pub home_cards: Option<serde_json::Value>,
/// 切换代理时自动关闭连接
pub auto_close_connection: Option<bool>,
/// 是否自动检查更新
pub auto_check_update: Option<bool>,
/// 默认的延迟测试连接
pub default_latency_test: Option<String>,
/// 默认的延迟测试超时时间
pub default_latency_timeout: Option<i16>,
/// 是否自动检测当前节点延迟
pub enable_auto_delay_detection: Option<bool>,
/// 自动检测当前节点延迟的间隔(分钟)
pub auto_delay_detection_interval_minutes: Option<u64>,
/// 是否使用内部的脚本支持,默认为真
pub enable_builtin_enhanced: Option<bool>,
/// proxy 页面布局 列数
pub proxy_layout_column: Option<u8>,
/// 测试站列表
pub test_list: Option<Vec<IVergeTestItem>>,
/// 日志清理
/// 0: 不清理; 1: 1天;2: 7天; 3: 30天; 4: 90天
pub auto_log_clean: Option<i32>,
/// Enable scheduled automatic backups
pub enable_auto_backup_schedule: Option<bool>,
/// Automatic backup interval in hours
pub auto_backup_interval_hours: Option<u64>,
/// Create backups automatically when critical configs change
pub auto_backup_on_change: Option<bool>,
/// verge 的各种 port 用于覆盖 clash 的各种 port
@@ -206,7 +158,6 @@ pub struct IVerge {
pub verge_http_enabled: Option<bool>,
/// WebDAV 配置 (加密存储)
#[serde(
serialize_with = "serialize_encrypted",
deserialize_with = "deserialize_encrypted",
@@ -215,7 +166,6 @@ pub struct IVerge {
)]
pub webdav_url: Option<String>,
/// WebDAV 用户名 (加密存储)
#[serde(
serialize_with = "serialize_encrypted",
deserialize_with = "deserialize_encrypted",
@@ -224,7 +174,6 @@ pub struct IVerge {
)]
pub webdav_username: Option<String>,
/// WebDAV 密码 (加密存储)
#[serde(
serialize_with = "serialize_encrypted",
deserialize_with = "deserialize_encrypted",
@@ -236,26 +185,20 @@ pub struct IVerge {
#[cfg(target_os = "macos")]
pub enable_tray_speed: Option<bool>,
// pub enable_tray_icon: Option<bool>,
/// show proxy groups directly on tray root menu
#[serde(skip_serializing_if = "Option::is_none")]
pub tray_proxy_groups_display_mode: Option<String>,
/// show outbound modes directly on tray root menu
pub tray_inline_outbound_modes: Option<bool>,
/// 自动进入轻量模式
pub enable_auto_light_weight_mode: Option<bool>,
/// 自动进入轻量模式的延迟(分钟)
pub auto_light_weight_minutes: Option<u64>,
/// 启用代理页面自动滚动
pub enable_hover_jump_navigator: Option<bool>,
/// 代理页面自动滚动延迟(毫秒)
pub hover_jump_navigator_delay: Option<u64>,
/// 启用外部控制器
pub enable_external_controller: Option<bool>,
}
@@ -284,10 +227,8 @@ pub struct IVergeTheme {
}
impl IVerge {
/// 有效的clash核心名称
pub const VALID_CLASH_CORES: &'static [&'static str] = &["verge-mihomo", "verge-mihomo-alpha"];
/// 验证并修正配置文件中的clash_core值
pub async fn validate_and_fix_config() -> Result<()> {
let config_path = dirs::verge_path()?;
let mut config = match help::read_yaml::<Self>(&config_path).await {
@@ -319,7 +260,6 @@ impl IVerge {
needs_fix = true;
}
// 修正后保存配置
if needs_fix {
logging!(info, Type::Config, "正在保存修正后的配置文件...");
help::save_yaml(&config_path, &config, Some("# Clash Verge Config")).await?;
@@ -333,7 +273,6 @@ impl IVerge {
Ok(())
}
/// 配置修正后重新加载配置
async fn reload_config_after_fix(updated_config: Self) -> Result<()> {
logging!(
info,
@@ -359,7 +298,6 @@ impl IVerge {
match dirs::verge_path() {
Ok(path) => match help::read_yaml::<Self>(&path).await {
Ok(mut config) => {
// compatibility
if let Some(start_page) = config.start_page.clone()
&& start_page == "/home"
{
@@ -440,7 +378,6 @@ impl IVerge {
webdav_password: None,
#[cfg(target_os = "macos")]
enable_tray_speed: Some(false),
// enable_tray_icon: Some(true),
tray_proxy_groups_display_mode: Some("default".into()),
tray_inline_outbound_modes: Some(false),
enable_global_hotkey: Some(true),
@@ -453,13 +390,10 @@ impl IVerge {
}
}
/// Save IVerge App Config
pub async fn save_file(&self) -> Result<()> {
help::save_yaml(&dirs::verge_path()?, &self, Some("# Clash Verge Config")).await
}
/// patch verge config
/// only save to file
#[allow(clippy::cognitive_complexity)]
pub fn patch_config(&mut self, patch: &Self) {
macro_rules! patch {
@@ -546,7 +480,6 @@ impl IVerge {
patch!(webdav_password);
#[cfg(target_os = "macos")]
patch!(enable_tray_speed);
// patch!(enable_tray_icon);
patch!(tray_proxy_groups_display_mode);
patch!(tray_inline_outbound_modes);
patch!(enable_auto_light_weight_mode);
@@ -556,7 +489,6 @@ impl IVerge {
patch!(enable_external_controller);
}
/// 获取日志等级
pub fn get_log_level(&self) -> LevelFilter {
if let Some(level) = self.app_log_level.as_ref() {
match level.to_lowercase().as_str() {
+20 -94
View File
@@ -22,36 +22,23 @@ use std::{
};
use tauri_plugin_mihomo::Error as MihomoError;
/// What came back from asking the Service to stage a runtime.
///
/// Separated from the decision below so the decision stays a pure function of it. The variants are
/// the four ways an attempt can end, and what distinguishes them is how much they let us conclude:
/// a refusal is the Service's verdict on this bundle, while silence says nothing at all.
/// A staging result separated from the policy that acts on it.
#[derive(Debug, Clone, PartialEq, Eq)]
enum StageAttempt {
/// The Service that owns the running Core predates staging.
Unsupported,
/// The Service answered, and its answer was a refusal about the bundle itself — the kind a
/// fresh start would run into again, because it would materialise the same bundle.
/// Restarting would present the same rejected bundle.
RefusedTheBundle(String),
/// The Service answered with a refusal about something other than the bundle: the session, the
/// protocol, the request. Starting is how several of those get resolved, so it is not a reason
/// to leave the Core where it is.
/// A new service session may resolve this refusal.
RefusedForAnotherReason(String),
/// The request did not come back. The Service is authoritative about its own runtime, so
/// without an answer nothing may be assumed about what it did or did not write.
/// The service may have staged the bundle without delivering its reply.
Unanswered(String),
Answered(StageRuntimeOutcome),
}
/// How the Core should be made to pick up a configuration.
#[derive(Debug, PartialEq, Eq)]
enum ConfigApplication {
/// Point the running Core at this path.
ReloadFrom(String),
/// Stop the Core and start it again from a freshly materialised runtime.
ReplaceCore,
/// Leave the Core alone and report why the configuration cannot be applied.
Fail(String),
}
@@ -63,21 +50,8 @@ impl Drop for ConfigUpdateGuard<'_> {
}
}
/// Decide how to apply a configuration, given how staging went.
///
/// Two branches avoid replacing the Core, for opposite reasons. One is success. The other is a
/// refusal *about the bundle*: the Service inspected it and would not take it, and a fresh start
/// materialises the same bundle — so it would fail the same way, having stopped a Core that was
/// working.
///
/// The distinction matters more than it looks. A refusal about the session is not a refusal about
/// the bundle: starting proposes a new session rather than presenting the old one, and the restart
/// path is also what notices that ownership was lost and clears the system proxy. Treating every
/// non-zero code alike would skip that.
///
/// Everything else — an older Service, a request that never came back, or a Service that asked to
/// be restarted — replaces the Core, which is what this did before staging existed and what the
/// Service guarantees is still safe after every way of declining.
/// Avoids replacing a working core only when the service staged successfully or rejected the
/// bundle itself; session-level failures may be repaired by replacement.
fn plan_config_application(attempt: &StageAttempt) -> ConfigApplication {
match attempt {
StageAttempt::Answered(StageRuntimeOutcome::Staged { config_path }) => {
@@ -112,7 +86,6 @@ fn plan_config_application(attempt: &StageAttempt) -> ConfigApplication {
}
}
/// One round trip to the Service, classified but not judged.
async fn ask_to_stage(path: &std::path::Path) -> StageAttempt {
match crate::core::service::stage_runtime_by_service(path).await {
Ok(StageRequest::Answered(outcome)) => StageAttempt::Answered(outcome),
@@ -128,21 +101,8 @@ async fn ask_to_stage(path: &std::path::Path) -> StageAttempt {
}
}
/// Ask once, and ask again only when the first attempt came back silent.
///
/// Silence is the one answer that has to be resolved rather than acted on. The Service commits the
/// generation before it replies, so a reply that never arrives can leave `config.yaml` already
/// replaced while this side concludes nothing happened — and the Service restarts a Core that dies
/// later from exactly that file. Treating silence as "nothing happened" is therefore a guess, and
/// the expensive half of it is silent: the Core keeps running the previous configuration until
/// something restarts it, and then it does not.
///
/// Asking again is cheap precisely where it matters. Staging plans against the manifest it wrote,
/// so a bundle that already landed copies nothing and answers in milliseconds; and because the
/// Service holds its lifecycle lock for the whole operation, a second ask that overtakes a first
/// one still in flight waits for it rather than interleaving with it. `confirm_within` is what
/// keeps that wait from being the caller's problem — past it, the answer has stopped being worth
/// more than the fallback, which is where a second silence goes anyway.
/// Confirms one silent request because the service may commit before its reply is lost.
/// Re-staging an already committed bundle is idempotent and bounded by `confirm_within`.
async fn stage_with_confirmation<Ask, Fut>(confirm_within: Duration, ask: Ask) -> StageAttempt
where
Ask: Fn() -> Fut,
@@ -326,10 +286,7 @@ impl CoreManager {
self.validate_and_apply(transaction).await
}
/// Validate the staged Runtime Config and hand it to the Core, committing only if both work.
///
/// Takes the transaction rather than opening one, so it covers the staging its callers did.
/// Every way out of here other than the last line rolls that staging back.
/// Validates and applies the caller's transaction, committing only on success.
async fn validate_and_apply(&self, transaction: DraftTransaction<'_>) -> Result<ValidationOutcome> {
let outcome = CoreConfigValidator::global().validate_config_outcome().await?;
if !outcome.is_valid() {
@@ -342,15 +299,7 @@ impl CoreManager {
Ok(ValidationOutcome::Valid)
}
/// Hand the generated configuration to the Core.
///
/// Says nothing about drafts: whether the staged Runtime Config is kept follows from
/// whether this succeeded, and the caller's transaction decides that.
///
/// Both modes have the same shape — put the configuration where the Core can read it, ask the
/// Core to reload, restart it if that did not work. Only the first step differs: in Sidecar
/// mode the Core already reads the app's own directory, while in Service mode the Service has
/// to materialise the configuration into the directory it started the Core in.
/// Applies a generated configuration through the active core owner.
async fn apply_config(&self, path: PathBuf) -> Result<()> {
if matches!(*self.get_running_mode(), RunningMode::Service) {
let _lifecycle = self.lifecycle_lock.lock().await;
@@ -364,18 +313,12 @@ impl CoreManager {
self.reload_or_restart(path).await
}
/// Apply a configuration in Service mode, staging it in place when that is possible.
///
/// Every way of not staging leads to the same place it always led: stop the Core and start it
/// again from a freshly materialised runtime. That is what makes declining safe — the Service
/// leaves the runtime it could not stage exactly as the running Core left it.
///
/// Applies through the service, replacing the core when in-place staging is unavailable.
/// Caller must hold `lifecycle_lock`.
async fn apply_config_by_service(&self, path: &std::path::Path) -> Result<()> {
match plan_config_application(&self.attempt_staging(path).await) {
ConfigApplication::Fail(message) => {
// The Service looked at this bundle and would not take it. Replacing the Core would
// hand it the same bundle, so the outage would buy nothing.
// Replacement would hand the service the same rejected bundle.
logging!(
warn,
Type::Core,
@@ -402,7 +345,6 @@ impl CoreManager {
Ok(())
}
/// Ask the Service to stage the runtime, reporting the attempt rather than judging it.
async fn attempt_staging(&self, path: &std::path::Path) -> StageAttempt {
if !crate::core::service::active_service_supports_runtime_staging() {
return StageAttempt::Unsupported;
@@ -466,12 +408,7 @@ mod tests {
);
}
/// Wrap a rejection as the answer the Service would have sent.
///
/// The `match` is the point, not the wrapping: it is exhaustive, so a new way for the Service
/// to decline cannot be added upstream without this failing to compile — which is what sends
/// whoever adds it to the test below to say what it should lead to. `CoreRestarted` reached
/// production without one, and inherited its verdict silently.
/// Exhaustively maps every service rejection into the policy under test.
fn declined(reason: StageRejection) -> StageAttempt {
match &reason {
StageRejection::CoreNotRunning
@@ -490,9 +427,7 @@ mod tests {
StageRejection::RuntimeUnwritable {
detail: "held open".to_owned(),
},
// The service's own watchdog can restart the core mid-staging, which leaves the
// generation holding a mixture no manifest describes. Rebuilding it from nothing is
// the only answer that does not guess at what is in there.
// A watchdog restart mid-staging can leave a generation that no manifest describes.
StageRejection::CoreRestarted,
] {
assert_eq!(
@@ -514,9 +449,7 @@ mod tests {
#[test]
fn a_refusal_about_the_bundle_leaves_the_core_alone() {
// Replacing the core would materialise the same bundle the Service just rejected, so the
// outage buys nothing: the configuration update fails either way, and this way the user's
// proxy is still working when it does.
// Keep the working core when replacement would present the same rejected bundle.
assert_eq!(
plan_config_application(&StageAttempt::RefusedTheBundle("runtime asset is unavailable".into())),
ConfigApplication::Fail("runtime asset is unavailable".into())
@@ -525,9 +458,7 @@ mod tests {
#[test]
fn a_refusal_about_anything_else_still_replaces_the_core() {
// A stale session is the case that matters: starting proposes a new one, so it is the cure
// rather than a repeat of the failure — and the restart path is also what notices ownership
// was lost and clears the system proxy.
// A new session can repair stale ownership and clear the inherited system proxy.
assert_eq!(
plan_config_application(&StageAttempt::RefusedForAnotherReason("owner session is stale".into())),
ConfigApplication::ReplaceCore
@@ -559,8 +490,7 @@ mod tests {
#[test]
fn a_request_that_never_came_back_makes_the_core_be_replaced() {
// Not knowing what the service did is the one case where reloading would be a guess: the
// staged path might not exist, or might hold the previous configuration.
// Silence gives no trustworthy path to reload.
assert_eq!(
plan_config_application(&StageAttempt::Unanswered("connection reset".into())),
ConfigApplication::ReplaceCore
@@ -569,8 +499,7 @@ mod tests {
#[tokio::test]
async fn an_answer_is_taken_at_its_word_and_not_asked_for_twice() {
// Staging is only idempotent in what it writes, not in what it costs: a second ask after a
// perfectly good answer would re-send the whole bundle for nothing.
// Do not pay for a confirmation after a conclusive answer.
for answer in [
staged(),
StageAttempt::Answered(StageRuntimeOutcome::RestartRequired {
@@ -593,9 +522,7 @@ mod tests {
#[tokio::test]
async fn silence_is_resolved_by_asking_again_rather_than_assumed() {
// The case this exists for: the service committed the generation and the reply was lost.
// Asking again reaches a service that has already done the work, so the second answer is
// the truth the first one failed to deliver — and it keeps the core off the replace path.
// Confirm a committed generation whose first reply was lost.
let asks = Cell::new(0);
let outcome = stage_with_confirmation(CONFIRM_WITHIN, || {
asks.set(asks.get() + 1);
@@ -639,8 +566,7 @@ mod tests {
#[tokio::test(start_paused = true)]
async fn a_second_ask_that_keeps_working_is_not_waited_out() {
// A service still copying assets is why the deadline exists: the answer would be worth
// having, but not at the cost of leaving the user's configuration unapplied indefinitely.
// Do not wait indefinitely for a service still copying assets.
let asks = Cell::new(0);
let started = tokio::time::Instant::now();
let outcome = stage_with_confirmation(CONFIRM_WITHIN, || async {
+2 -33
View File
@@ -24,15 +24,12 @@ enum StartupDecision {
Wait,
}
/// Proxy action captured before a controlled stop.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ProxyStopIntent {
Clear,
/// Let the service overwrite the inherited proxy state.
HandOverToService,
}
/// Select whether a controlled stop clears or hands over proxy state.
const fn proxy_stop_intent(is_macos: bool, running_mode: RunningMode, decision: StartupDecision) -> ProxyStopIntent {
match (is_macos, running_mode, decision) {
(true, RunningMode::Sidecar, StartupDecision::Service) => ProxyStopIntent::HandOverToService,
@@ -280,14 +277,10 @@ where
start_sidecar().await
}
/// sidecar→service 交接结果
#[cfg(target_os = "windows")]
enum HandoffOutcome {
/// 服务尚未就绪
NotReady,
/// 已完成或无需交接
Done,
/// 交接失败并已回退
Failed,
}
@@ -408,7 +401,6 @@ impl CoreManager {
run_sidecar_termination_transition(proxy_control::clear, || self.stop_core_by_sidecar_unprepared()).await
}
/// Replaces a Service-owned core while the caller holds `lifecycle_lock`.
pub(super) async fn replace_service_core_with_config(&self, config_file: &Path) -> Result<()> {
run_service_config_replacement_transition(
cfg!(target_os = "macos"),
@@ -457,14 +449,11 @@ impl CoreManager {
Ok(())
}
/// 调用者须已持有 `lifecycle_lock`。
async fn start_core_inner(&self) -> Result<()> {
// 退出中不再启动新内核。
if Handle::global().is_exiting() {
return Ok(());
}
// 已有内核运行时保持幂等,重启请走 restart_core。
if !matches!(*self.get_running_mode(), RunningMode::NotRunning) {
logging!(
info,
@@ -479,7 +468,6 @@ impl CoreManager {
self.rollback_failed_start().await;
return Ok(());
}
// 等待服务期间可能进入退出;未真正启动时回滚状态。
if Handle::global().is_exiting() {
self.core_stopped();
return Ok(());
@@ -491,15 +479,12 @@ impl CoreManager {
StartupDecision::Wait => Ok(()),
};
// No startup failure may leave a locally reported running mode. PAC is
// still fail-closed, and any service owner monitor will exit after this
// transition instead of treating an unconfirmed core as running.
// A failed start must not leave a locally reported running mode.
if result.is_err() {
self.rollback_failed_start().await;
return result;
}
// 回退 sidecar 后,后台等待服务就绪再交接
#[cfg(target_os = "windows")]
if matches!(*self.get_running_mode(), RunningMode::Sidecar) {
self.spawn_service_handoff_watcher().await;
@@ -513,12 +498,10 @@ impl CoreManager {
self.controlled_stop_core_inner().await
}
/// 调用者须已持有 `lifecycle_lock`。
pub(crate) async fn controlled_stop_core_inner(&self) -> Result<()> {
self.controlled_stop_core_with_intent(ProxyStopIntent::Clear).await
}
/// 调用者须已持有 `lifecycle_lock`。
async fn controlled_stop_core_with_intent(&self, proxy_intent: ProxyStopIntent) -> Result<()> {
let running_mode = *self.get_running_mode();
run_controlled_stop_transition(
@@ -532,7 +515,6 @@ impl CoreManager {
.await
}
/// Capture handoff intent before stopping; startup state may change afterward.
async fn proxy_stop_intent(&self) -> ProxyStopIntent {
let running_mode = *self.get_running_mode();
if !cfg!(target_os = "macos") || !matches!(running_mode, RunningMode::Sidecar) {
@@ -541,7 +523,6 @@ impl CoreManager {
proxy_stop_intent(cfg!(target_os = "macos"), running_mode, self.prepare_startup().await)
}
/// 调用者须已持有 `lifecycle_lock`,且已完成受控代理清理。
async fn stop_core_unprepared_inner(&self) -> Result<()> {
CLASH_LOGGER.clear_logs().await;
match *self.get_running_mode() {
@@ -565,7 +546,6 @@ impl CoreManager {
}
pub(crate) async fn restart_core_during_config_update(&self) -> Result<()> {
// 持锁覆盖 stop+start,避免生命周期操作插入。
let _life = self.lifecycle_lock.lock().await;
logging!(info, Type::Core, "Restarting core");
let proxy_intent = self.proxy_stop_intent().await;
@@ -583,7 +563,6 @@ impl CoreManager {
.await
}
/// Switch the core binary and reload configuration.
pub async fn change_core(&self, clash_core: &String) -> Result<()> {
if !IVerge::VALID_CLASH_CORES.contains(&clash_core.as_str()) {
anyhow::bail!("invalid clash core: {clash_core}");
@@ -644,7 +623,6 @@ impl CoreManager {
let _ = tokio::time::timeout(timing::SERVICE_WAIT_MAX, wait).await;
}
/// 在窗口内等待服务就绪,再从 sidecar 交接到 service
#[cfg(target_os = "windows")]
async fn spawn_service_handoff_watcher(&self) {
use crate::constants::timing;
@@ -652,14 +630,12 @@ impl CoreManager {
use std::sync::atomic::Ordering;
use std::time::Instant;
// 仅 TUN 模式需要服务交接
let needs_service = Config::verge().await.latest_arc().enable_tun_mode.unwrap_or(false)
&& !Config::tun_suppressed_for_session();
if !needs_service {
return;
}
// 单实例,避免并发交接
if self.handoff_watcher_running.swap(true, Ordering::AcqRel) {
return;
}
@@ -684,14 +660,11 @@ impl CoreManager {
}
tokio::time::sleep(timing::SERVICE_HANDOFF_INTERVAL).await;
// 模式已变更时退出
if !matches!(*manager.get_running_mode(), RunningMode::Sidecar) {
break;
}
match manager.try_handoff_sidecar_to_service().await {
// 已交接或无需交接
HandoffOutcome::Done => break,
// 已回退 sidecar,停止重试
HandoffOutcome::Failed => {
logging!(warn, Type::Core, "handoff attempt failed; staying in sidecar mode");
break;
@@ -705,21 +678,18 @@ impl CoreManager {
#[cfg(target_os = "windows")]
async fn refresh_service_readiness_for_handoff() -> bool {
// 主动刷新服务状态,避免缓存状态阻止交接
if SERVICE_MANAGER.confirm_ready().await.is_err() {
return false;
}
crate::core::runstate::RUN_STATE.state().service_usable()
}
/// 服务就绪后停止 sidecar,再以 service 重启内核
#[cfg(target_os = "windows")]
async fn try_handoff_sidecar_to_service(&self) -> HandoffOutcome {
if !Self::refresh_service_readiness_for_handoff().await {
return HandoffOutcome::NotReady;
}
// 先抢 config 锁;失败则让位给正在进行的更新。
if !self.try_start_config_update() {
return HandoffOutcome::NotReady;
}
@@ -727,10 +697,9 @@ impl CoreManager {
self.finish_config_update();
}
// 再取 lifecycle 锁;锁序固定为 config→lifecycle。
// Lock order is config then lifecycle.
let _life = self.lifecycle_lock.lock().await;
// 持锁后复检运行模式和 TUN 状态
if !matches!(*self.get_running_mode(), RunningMode::Sidecar)
|| !Config::verge().await.latest_arc().enable_tun_mode.unwrap_or(false)
|| Config::tun_suppressed_for_session()
+4 -25
View File
@@ -20,22 +20,8 @@ use tauri_plugin_shell::ShellExt as _;
const SIDECAR_READINESS_ATTEMPTS: usize = 30;
impl CoreManager {
/// A core process is up: put back the node selections the user made.
///
/// mihomo restores these itself, from the `cache.db` it keeps in the directory it was started
/// against — but only when `profile.store-selected` is on and that file survived, and neither
/// is something the app can assume. The setting comes from a merge template a user is free to
/// replace, and a service older than the durable runtime generation hands every start a
/// directory nothing has ever run in. The app holds the same selections in the profile, so it
/// is the one that can say for certain.
///
/// Awaited, and deliberately here rather than beside the proxy: the caller enables the system
/// proxy as soon as the start returns, and pointing it at a core still on the first entry of
/// every group is what this exists to prevent. The wait is bounded — what cannot be put back
/// yet keeps being retried in the background — so a core that will not answer delays a start
/// instead of blocking it.
///
/// Repeat calls supersede each other, so every start path may call this without coordinating.
/// Restores profile selections before callers enable the system proxy.
/// The bounded first pass continues in the background, and later calls supersede earlier ones.
async fn restore_selected_nodes(&self) {
if PROFILE_SELECTIONS_PENDING_COMMIT
.try_with(|pending| *pending)
@@ -469,8 +455,7 @@ mod tests {
time::{Duration, Instant},
};
// 起一个长命子进程用于验证 Job Object 的生命周期绑定。
// 直接使用 System32 下的 ping.exe,避免 cmd 中间层。
// Use ping directly as a long-lived process without a cmd.exe intermediary.
fn spawn_long_lived() -> Result<Child> {
let child = Command::new("ping")
.args(["-n", "999", "127.0.0.1"])
@@ -480,7 +465,6 @@ mod tests {
Ok(child)
}
// 在超时内轮询子进程是否退出,返回是否已退出。
fn wait_until_exited(child: &mut Child, timeout: Duration) -> Result<bool> {
let deadline = Instant::now() + timeout;
loop {
@@ -494,21 +478,17 @@ mod tests {
}
}
// 成功路径:进程被分配进 Job Object 后仍存活;drop Job 句柄触发
// KILL_ON_JOB_CLOSE,进程应在超时内被 OS 终止。
#[test]
fn job_kills_child_on_handle_drop() -> Result<()> {
let mut child = spawn_long_lived()?;
let job = create_and_assign_sidecar_job(child.id())?;
// 分配后进程应仍在运行。
assert!(
child.try_wait()?.is_none(),
"child should still be running after being assigned to the job"
);
// 关闭 Job 句柄,OS 应连带终止其成员进程。
drop(job);
assert!(
@@ -519,10 +499,9 @@ mod tests {
Ok(())
}
// 失败路径:对一个不存在的 PID 调用时 OpenProcess 应失败,函数返回 Err。
#[test]
fn returns_err_for_invalid_pid() {
// PID 必须为 4 的倍数且极不可能存在;0xFFFF_FFFC 对应不到真实进程。
// Windows PIDs are multiples of four; this one is effectively impossible.
let result = create_and_assign_sidecar_job(0xFFFF_FFFC);
assert!(result.is_err(), "expected Err for a non-existent PID");
}
-16
View File
@@ -41,7 +41,6 @@ pub enum SysproxyFailure {
}
impl SysproxyFailure {
/// Stable frontend error code.
#[inline]
pub const fn code(&self) -> &'static str {
match self {
@@ -52,7 +51,6 @@ impl SysproxyFailure {
}
}
/// Find a classification using `anyhow`'s context-aware downcast.
#[inline]
pub fn from_chain(error: &anyhow::Error) -> Option<&Self> {
error.downcast_ref::<Self>()
@@ -83,7 +81,6 @@ impl std::error::Error for SysproxyFailure {}
pub struct SystemProxyStateUnknown;
impl SystemProxyStateUnknown {
/// Whether this marker appears in the error chain.
#[inline]
pub fn is_in(error: &anyhow::Error) -> bool {
error.downcast_ref::<Self>().is_some()
@@ -98,7 +95,6 @@ impl std::fmt::Display for SystemProxyStateUnknown {
impl std::error::Error for SystemProxyStateUnknown {}
/// Whether the error carries a frontend-mappable classification.
#[inline]
fn carries_a_mappable_code(error: &anyhow::Error) -> bool {
SysproxyFailure::from_chain(error).is_some()
@@ -120,7 +116,6 @@ pub fn rollback_failure(caused_by: anyhow::Error, rollback: anyhow::Error) -> an
combined
}
/// Classify local macOS proxy failures without replacing their source chain.
fn classify_local_failure(error: anyhow::Error) -> anyhow::Error {
if !was_refused_locally(&error) {
return error;
@@ -128,7 +123,6 @@ fn classify_local_failure(error: anyhow::Error) -> anyhow::Error {
error.context(SysproxyFailure::PrivilegeRequired)
}
/// Whether macOS refused a local proxy write.
fn was_refused_locally(error: &anyhow::Error) -> bool {
cfg!(target_os = "macos")
&& error.chain().any(|cause| {
@@ -141,7 +135,6 @@ fn was_refused_locally(error: &anyhow::Error) -> bool {
/// Distinguish a misplaced sidecar from a missing privileged service.
async fn classify_local_apply_failure(error: anyhow::Error) -> anyhow::Error {
// Preserve any existing, more specific classification.
if SysproxyFailure::from_chain(&error).is_some() || !was_refused_locally(&error) {
return error;
}
@@ -153,7 +146,6 @@ async fn classify_local_apply_failure(error: anyhow::Error) -> anyhow::Error {
error.context(refusal_classification(probed))
}
/// Bound the diagnostic service probe.
const SERVICE_PROBE_TIMEOUT: Duration = Duration::from_secs(2);
/// Classify a refusal from a fresh service probe only.
@@ -206,7 +198,6 @@ impl ServiceProxyOperations {
self.run_service_operation(operation).await
}
/// Invalidate the guard generation and wait for cancellation.
async fn cancel_and_drain<F, Fut, T>(&self, cancel: F) -> (u64, T)
where
F: FnOnce() -> Fut,
@@ -217,7 +208,6 @@ impl ServiceProxyOperations {
(generation, cancelled)
}
/// Record under the operation lock only for the current guard generation.
async fn record_if_current<Record>(&self, captured_generation: u64, record: Record) -> bool
where
Record: FnOnce() -> bool,
@@ -339,12 +329,10 @@ async fn current_service_proxy_config(verge: &IVerge) -> Result<MacosProxyConfig
if verge.proxy_auto_config.unwrap_or_default() {
return service_proxy_config(verge, 0, server::embedded_server_port()?);
}
// Configured: this builds the proxy settings handed to the service alongside a start.
let mixed_port = MixedPort::desired().await;
service_proxy_config(verge, mixed_port, 0)
}
/// Return whether a classified failure remains pending.
pub fn is_reportable(error: &anyhow::Error) -> bool {
is_reportable_given(error, notification::has_pending_failure)
}
@@ -549,10 +537,8 @@ pub async fn refresh_guard() -> Result<()> {
Ok(())
}
/// Consecutive failures allowed before the guard stops.
const GUARD_FAILURES_BEFORE_STOPPING: u32 = 3;
/// Report that the current guard stopped.
async fn report_guard_stopped(captured_generation: u64, error: &anyhow::Error) {
let stopped = anyhow::Error::new(SysproxyFailure::GuardStopped {
detail: format!("{error:#}"),
@@ -569,7 +555,6 @@ async fn report_guard_stopped(captured_generation: u64, error: &anyhow::Error) {
}
}
/// Stop the local guard and report whether it drained.
pub async fn stop_guard() -> bool {
SERVICE_PROXY_OPERATIONS
.cancel_and_drain(|| Sysopt::global().stop_proxy_guard())
@@ -593,7 +578,6 @@ mod tests {
use std::task::Poll;
use tokio::sync::Barrier;
/// Proxy state maintained by a running guard.
fn guarded_proxy() -> MacosProxyConfig {
MacosProxyConfig::Global {
host: "127.0.0.1".to_owned(),
+6 -52
View File
@@ -1,9 +1,4 @@
//! The seam between Run State and the machine it observes.
//!
//! Two adapters satisfy it: [`RealEnv`] talks to the Service over IPC and to the platform's
//! service registry, [`FakeEnv`] replays scripted answers. Because the store is generic over
//! this trait rather than reaching for globals, the state machine can be exercised without
//! a running app, an installed Service, or elevation.
//! Adapters between Run State and the real or scripted machine it observes.
use anyhow::{Context as _, Result};
@@ -12,41 +7,21 @@ use super::probe::ServiceVersionReply;
/// Everything Run State needs from outside itself.
pub trait RunStateEnv: Send + Sync + 'static {
/// Ask the Service which protocol it speaks. Fails when it cannot be reached at all.
fn probe_service_version(&self) -> impl Future<Output = Result<ServiceVersionReply>> + Send;
/// Whether the platform's service registry has a trusted record of an installation.
///
/// Fails when the registry itself could not be inspected, which is different from
/// "no installation" and is treated as unavailable rather than not-installed.
///
/// Async so blocking systemd and Windows SCM checks can run off the runtime workers.
/// Returns an error when installation evidence cannot be inspected, not when it is absent.
fn trusted_install_evidence(&self) -> impl Future<Output = Result<bool>> + Send;
/// Whether this app process is running elevated.
fn is_elevated(&self) -> bool;
/// Open or close the local PAC endpoint.
///
/// Derived from the Running Mode, never set independently: a PAC script that points at a
/// proxy port nothing is listening on is worse than no PAC at all.
/// Keeps the PAC endpoint derived from Running Mode.
fn set_pac_available(&self, available: bool);
/// Publish the Run State to observers outside the store.
///
/// Two of them today: the diagnostic snapshot, which reports the Running Mode, and the
/// frontend, which gets the whole thing pushed rather than polling for it.
fn publish(&self, state: &RunState);
/// Carry out a privileged operation against the Service.
///
/// Blocking and platform-specific — SCM, systemd, launchd, elevation prompts — which is
/// exactly why it sits behind the seam: the state machine around it can then be exercised
/// without installing anything.
fn run_privileged(&self, action: PendingAction) -> Result<()>;
}
/// The production adapter: real IPC, real platform registry, real elevation check.
#[derive(Debug, Default, Clone, Copy)]
pub struct RealEnv;
@@ -67,9 +42,7 @@ impl RunStateEnv for RealEnv {
}
fn is_elevated(&self) -> bool {
// Deliberately non-panicking, unlike `Handle::app_handle()`: Run State is read from
// startup paths that run before the app handle exists, and "not yet known" must
// degrade to "not elevated" rather than abort the process.
// Startup may read Run State before the app handle exists; fail closed instead of panicking.
crate::APP_HANDLE
.get()
.is_some_and(tauri_plugin_clash_verge_sysinfo::is_current_app_handle_admin)
@@ -80,17 +53,11 @@ impl RunStateEnv for RealEnv {
}
fn publish(&self, state: &RunState) {
// Reconciling capability is a reaction to the new state, not part of publishing it, so
// it is spawned: a transition must not wait on a config write it triggered. It is also
// spawned *before* the app-handle gate below, because a Core that stops and starts
// before the app handle exists still needs reconciling; the task re-reads the Run
// State itself rather than closing over this snapshot.
// Reconciliation must run before the app handle exists and must not block this transition.
crate::process::AsyncHandler::spawn(|| async {
crate::feat::reconcile_tun_availability().await;
});
// Non-panicking for the same reason as `is_elevated`: the Core can stop and start
// before the app handle exists, and a missed notification is better than an abort.
let Some(app_handle) = crate::APP_HANDLE.get() else {
return;
};
@@ -115,10 +82,7 @@ mod fake {
use super::{PendingAction, RunState, RunStateEnv, ServiceVersionReply};
use crate::core::manager::RunningMode;
/// A scripted stand-in for the machine.
///
/// Defaults to the least capable environment — no Service, no elevation — so a test only
/// has to say what it needs. Outbound effects are recorded rather than performed.
/// A fail-closed scripted machine that records outbound effects.
#[derive(Debug)]
pub struct FakeEnv {
version_replies: Mutex<Vec<Result<ServiceVersionReply, String>>>,
@@ -152,7 +116,6 @@ mod fake {
Self::default()
}
/// An installed, reachable, protocol-compatible Service.
#[must_use]
pub fn service_ready(self) -> Self {
self.with_evidence(true).always_replying(Ok(ServiceVersionReply {
@@ -162,7 +125,6 @@ mod fake {
}))
}
/// An installed Service that answers with an incompatible protocol.
#[must_use]
pub fn service_version_mismatch(self) -> Self {
self.with_evidence(true).always_replying(Ok(ServiceVersionReply {
@@ -172,14 +134,12 @@ mod fake {
}))
}
/// Registered with the platform but not answering.
#[must_use]
pub fn service_unreachable(self) -> Self {
self.with_evidence(true)
.always_replying(Err("ipc transport refused".to_owned()))
}
/// The platform registry itself could not be inspected.
#[must_use]
pub fn evidence_unavailable(mut self) -> Self {
self.evidence = Err("registry probe failed".to_owned());
@@ -210,38 +170,32 @@ mod fake {
self.replying(vec![reply])
}
/// How many times the Service was probed — asserts that retries actually retried.
#[must_use]
pub fn probe_count(&self) -> usize {
*self.probe_count.lock()
}
/// The last PAC availability the store asked for, or `None` if it never asked.
#[must_use]
pub fn pac_available(&self) -> Option<bool> {
*self.pac_available.lock()
}
/// Every Running Mode published outward, in order.
#[must_use]
pub fn published_modes(&self) -> Vec<RunningMode> {
self.published.lock().iter().map(|state| state.mode).collect()
}
/// Every Run State published outward, in order.
#[must_use]
pub fn published(&self) -> Vec<RunState> {
self.published.lock().clone()
}
/// Make every privileged operation fail with `reason`.
#[must_use]
pub fn privileged_operations_fail(self, reason: &str) -> Self {
*self.privileged_outcome.lock() = Err(reason.to_owned());
self
}
/// Every privileged operation actually attempted, in order.
#[must_use]
pub fn privileged_actions(&self) -> Vec<PendingAction> {
self.privileged_actions.lock().clone()
+2 -21
View File
@@ -3,19 +3,13 @@
use crate::core::manager::RunningMode;
/// Last observed Service state.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum ServiceHealth {
/// Not probed yet.
#[default]
Unknown,
/// Installed, reachable, and speaking a compatible protocol.
Ready,
/// No trusted installation evidence on this machine.
NotInstalled,
/// Installed but speaking an incompatible protocol; needs reinstall.
VersionMismatch,
/// Installed but unusable, with the reason we recorded.
Unavailable(String),
}
@@ -38,7 +32,6 @@ impl ServiceHealth {
}
}
/// Requested privileged Service operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub enum PendingAction {
@@ -48,7 +41,6 @@ pub enum PendingAction {
ForceReinstall,
}
/// Snapshot used for Core availability decisions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RunState {
pub health: ServiceHealth,
@@ -61,26 +53,22 @@ pub struct RunState {
}
impl RunState {
/// Whether the last probe observed a ready Service; ignores in-flight operations.
#[must_use]
pub const fn service_ready(&self) -> bool {
matches!(self.health, ServiceHealth::Ready)
}
/// Whether the Service is ready and not mid-operation.
#[must_use]
pub const fn service_usable(&self) -> bool {
self.service_ready() && !self.op_in_flight
}
/// Whether elevation or a usable Service can support TUN.
#[must_use]
pub const fn tun_capable(&self) -> bool {
self.is_admin || self.service_usable()
}
/// Whether the user must resolve a pending request or a present-but-unusable Service.
/// In-flight, absent, and accepted-Sidecar states need no decision.
/// In-flight, absent, and accepted-Sidecar states require no user decision.
#[must_use]
pub const fn service_needs_attention(&self) -> bool {
if self.op_in_flight {
@@ -98,7 +86,6 @@ impl RunState {
)
}
/// Whether enabled TUN is certainly unusable and may be disabled.
/// Unknown, in-flight, and attention-required states are inconclusive.
#[must_use]
pub const fn tun_should_be_disabled(&self, tun_enabled: bool) -> bool {
@@ -108,14 +95,12 @@ impl RunState {
!matches!(self.health, ServiceHealth::Unknown) && !self.op_in_flight && !self.service_needs_attention()
}
/// Whether startup must suppress TUN for a confirmed absent Service.
/// Present-but-broken Services remain in the repair flow.
/// Present-but-broken services remain in the repair flow rather than suppressing TUN.
#[must_use]
pub const fn startup_tun_should_be_disabled(&self, tun_enabled: bool) -> bool {
matches!(self.health, ServiceHealth::NotInstalled) && self.tun_should_be_disabled(tun_enabled)
}
/// Frontend view with derived availability decisions.
#[must_use]
pub fn to_view(&self) -> RunStateView {
RunStateView {
@@ -133,7 +118,6 @@ impl RunState {
}
}
/// Frontend Run State.
#[derive(Debug, Clone, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RunStateView {
@@ -159,20 +143,17 @@ pub(super) struct StoredService {
}
impl StoredService {
/// Record an observation, clearing any request it answers.
pub fn observe(&mut self, health: ServiceHealth) {
self.health = health;
self.pending = None;
self.sidecar_allowed = false;
}
/// Record a requested privileged operation, preserving the last observation.
pub const fn request(&mut self, action: PendingAction) {
self.pending = Some(action);
self.sidecar_allowed = false;
}
/// Record that the user accepted Sidecar for this session.
pub const fn allow_sidecar(&mut self) {
self.pending = None;
self.sidecar_allowed = true;
+25 -134
View File
@@ -1,15 +1,7 @@
//! Run State — the single answer to "how is the Core running, and what backs it".
//! The single answer to how the Core runs and what backs it.
//!
//! This module owns Service Health, Running Mode, Pending Action and the privileged-operation
//! lock. It deliberately does **not** own starting, stopping or restarting the Core — those
//! stay in `CoreManager`, which reports transitions in and reads snapshots back. Merging the
//! two would leave no point at which the state is consistent, since a snapshot taken
//! mid-restart would describe a transition rather than a state.
//!
//! Reads come in three flavours that differ only in *freshness* — [`RunStateStore::state`]
//! (cached), [`RunStateStore::settled`] (waits for any in-flight operation) and
//! [`RunStateStore::probe`] (forces live IPC). What the answer *means* is decided by methods
//! on [`RunState`], so no caller writes its own availability formula.
//! Core lifecycle stays in `CoreManager`; this module owns its state, service health, pending
//! actions, and the privileged-operation lock. Reads differ only in freshness.
mod env;
mod health;
@@ -45,11 +37,7 @@ use probe::{CurrentServiceProbe, classify_service_health, probe_outcome};
/// The process-wide Run State, observing the real machine.
pub static RUN_STATE: Lazy<RunStateStore<RealEnv>> = Lazy::new(|| RunStateStore::new(RealEnv));
/// Why a wait for Service readiness ended without a ready Service.
///
/// The two are not interchangeable: an unreachable Service told us nothing, so the caller
/// decides whether silence means unavailable; a rejected one already updated health and must
/// not have that verdict overwritten with a vaguer one.
/// Distinguishes silence from a reply that updated Service health with a rejection.
#[derive(Debug)]
pub enum ReadyWaitError {
/// No readable reply within the attempts budget.
@@ -66,11 +54,7 @@ impl std::fmt::Display for ReadyWaitError {
}
}
/// The service-owned state together with the counters that version it.
///
/// They share one lock so a reader can never see a change without its version bump.
/// `generation` tracks visible changes; `observation` also tracks assertions that invalidate
/// older health probes without changing visible state.
/// Service state and revisions share one lock so observations cannot overtake their version bump.
#[derive(Debug, Default)]
struct VersionedService {
service: StoredService,
@@ -79,13 +63,11 @@ struct VersionedService {
}
impl VersionedService {
/// Record a visible change and invalidate older health probes.
const fn bump(&mut self) {
self.generation = self.generation.wrapping_add(1);
self.observation = self.observation.wrapping_add(1);
}
/// Reserve a revision that invalidates older health probes.
const fn next_observation(&mut self) -> u64 {
self.observation = self.observation.wrapping_add(1);
self.observation
@@ -113,33 +95,19 @@ impl<E: RunStateEnv> RunStateStore<E> {
}
}
// ─────────────────────────── reads: freshness only ───────────────────────────
/// The last known Run State, without waiting and without probing.
///
/// May be stale while a privileged operation is in flight — the snapshot says so via
/// [`RunState::op_in_flight`], and [`RunState::service_usable`] accounts for it.
/// It reports when a privileged operation may make the snapshot stale.
pub fn state(&self) -> RunState {
let service = self.service.lock().service.clone();
self.snapshot(service)
}
/// The Run State once any in-flight privileged operation has finished.
///
/// The snapshot is the decision, not a separate check before it. Reading the slot twice —
/// once to decide whether to wait, once to build the snapshot — leaves a window in which an
/// operation claims it, and the snapshot then *describes* that operation instead of waiting
/// for it. `prepare_startup` reads this: it would see a requested install as a reason not to
/// start, where waiting would have told it whether the install worked.
/// Returns one snapshot taken after any operation visible in that snapshot has settled.
pub async fn settled(&self) -> RunState {
loop {
let notified = self.operation_done.notified();
tokio::pin!(notified);
// Register as a waiter *before* reading the state. `notify_waiters` wakes only
// those already registered and leaves no permit behind, so without this an
// operation finishing between the read and the await is a wakeup lost for good
// — and this future would then wait for some unrelated later operation, or
// forever.
// Register before reading; `notify_waiters` leaves no permit for late waiters.
notified.as_mut().enable();
let state = self.state();
@@ -150,11 +118,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
}
}
/// Probe the Service now, record what came back, and return the fresh Run State.
///
/// A *transport* failure is not an observation: the Service may simply be restarting, so
/// the last confirmed health survives and the caller decides whether to retry. Only a
/// reply we could read and reject updates health.
/// Records readable replies; transport failures preserve the last confirmed health.
pub async fn probe(&self) -> Result<RunState> {
let reply = self
.env
@@ -164,11 +128,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
self.record_reply(&reply)
}
/// Probe until the Service answers, giving it `attempts` tries `interval` apart.
///
/// Only an unreachable Service is worth retrying. A reply we could read is the Service's
/// answer — retrying an incompatible version twenty times would just delay telling the
/// user to reinstall — so the wait ends on the first readable reply either way.
/// Retries transport failures but stops at the first readable reply, including a rejection.
pub async fn await_ready(&self, attempts: usize, interval: Duration) -> Result<RunState, ReadyWaitError> {
let mut last_error = None;
for attempt in 0..attempts {
@@ -186,7 +146,6 @@ impl<E: RunStateEnv> RunStateStore<E> {
})))
}
/// Record a readable reply as an observation, and report whether it was acceptable.
fn record_reply(&self, reply: &ServiceVersionReply) -> Result<RunState> {
match classify_service_version_reply(reply) {
ServiceVersionCheck::Ready => {
@@ -200,10 +159,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
}
}
/// Work out the Service's health from scratch: platform evidence first, then a live probe.
///
/// Platform evidence that cannot be inspected is *unavailable*, not *absent* — assuming
/// "not installed" there would offer the user an install that is already there.
/// Detects health from platform evidence and then live IPC; unknown evidence is not absence.
pub async fn detect_service_health(&self) -> ServiceHealth {
let has_marker = match self.env.trusted_install_evidence().await {
Ok(exists) => exists,
@@ -234,26 +190,18 @@ impl<E: RunStateEnv> RunStateStore<E> {
}
}
// ─────────────────────────── writes ───────────────────────────
/// Probe and record Service health unless a newer observation arrived first.
///
/// Reserving before I/O prevents slower probes from overwriting later observations, including
/// assertions that leave visible state unchanged. Returns the health that remains recorded.
/// Records detected health unless a newer observation arrived during I/O.
pub async fn observe_current_health(&self) -> ServiceHealth {
let reservation = self.reserve_health_observation();
let health = self.detect_service_health().await;
self.commit_reserved_observation(health, reservation)
}
/// Reserve the revision for a health probe.
fn reserve_health_observation(&self) -> u64 {
self.service.lock().next_observation()
}
/// Commit `health` only if `reservation` is still latest; otherwise return the newer health.
///
/// The comparison and write share one lock. A successful commit reuses its reserved revision.
/// Commits only the latest reserved observation.
fn commit_reserved_observation(&self, health: ServiceHealth, reservation: u64) -> ServiceHealth {
let mut state = self.service.lock();
if state.observation != reservation {
@@ -276,7 +224,6 @@ impl<E: RunStateEnv> RunStateStore<E> {
health
}
/// Record an observation about the Service, clearing any request it answers.
pub fn observe(&self, health: ServiceHealth) {
let mut state = self.service.lock();
if state.service.health == health && state.service.pending.is_none() && !state.service.sidecar_allowed {
@@ -290,11 +237,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
self.announce();
}
/// Record an authorised privileged operation and return whether it displaced the session's
/// Sidecar allowance.
///
/// Reading and clearing the allowance in one transition lets the caller safely restore it if
/// the operation fails.
/// Records an action and returns the Sidecar allowance it displaced for possible rollback.
pub fn request_action(&self, action: PendingAction) -> bool {
let mut state = self.service.lock();
let displaced = state.service.sidecar_allowed;
@@ -310,9 +253,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
displaced
}
/// Accept Sidecar for the rest of this app session without any eligibility check.
///
/// For builds and paths where Sidecar is the decided outcome rather than a fallback.
/// Accepts Sidecar when it is the predetermined outcome rather than a fallback.
pub fn accept_sidecar(&self) {
let mut state = self.service.lock();
if state.service.sidecar_allowed && state.service.pending.is_none() {
@@ -326,10 +267,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
self.announce();
}
/// Restore a displaced Sidecar allowance unless one already exists or the Service is ready.
///
/// The check and update are atomic so a restored allowance cannot shadow a newly ready
/// Service. Returns whether the allowance was restored.
/// Atomically restores an allowance unless Sidecar is already allowed or the Service is ready.
pub fn restore_sidecar_allowance(&self) -> bool {
let mut state = self.service.lock();
if state.service.sidecar_allowed {
@@ -348,9 +286,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
true
}
/// Revoke an allowance after Sidecar startup fails without changing Service health.
///
/// Returns whether the allowance was revoked.
/// Revokes a failed Sidecar allowance without changing Service health.
pub fn withdraw_sidecar_allowance(&self) -> bool {
let mut state = self.service.lock();
if !state.service.sidecar_allowed {
@@ -363,10 +299,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
true
}
/// Accept Sidecar for the rest of this app session.
///
/// Rejected while an operation is in flight, or from a state where the Service is
/// already usable or is mid-privileged-operation other than an install.
/// Accepts Sidecar only while the Service is unusable and no conflicting operation is active.
pub fn allow_sidecar_for_session(&self) -> Result<()> {
let mut state = self.service.lock();
if self.operation_running.load(Ordering::Acquire) {
@@ -390,10 +323,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
Ok(())
}
/// Require the Service to be installed before the Core starts, when it is simply absent.
///
/// A no-op from any other state, mirroring the rule that an already-requested or
/// already-usable Service must not be downgraded to "needs installing".
/// Requests installation only for a confirmed absent Service.
pub fn require_install_for_session(&self) -> Result<()> {
let mut state = self.service.lock();
if self.operation_running.load(Ordering::Acquire) {
@@ -414,11 +344,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
Ok(())
}
/// Carry out a privileged operation and record what it did to the Service.
///
/// A successful uninstall records an absent Service. Other successes rely on the caller's
/// readiness check. Failures re-probe the Service so the request is retired and health
/// reflects the machine's current state.
/// Runs an operation, recording uninstall success or re-probing after failure.
pub async fn perform(&self, action: PendingAction) -> Result<()> {
let outcome = self.env.run_privileged(action);
match &outcome {
@@ -436,56 +362,32 @@ impl<E: RunStateEnv> RunStateStore<E> {
outcome
}
// ─────────────────────────── running mode ───────────────────────────
/// The Running Mode, shared so hot callers such as the tray do not allocate.
pub fn mode_arc(&self) -> Arc<RunningMode> {
Arc::clone(&self.mode.load())
}
/// The Core is now running, and serving, in `mode`.
pub fn core_started(&self, mode: RunningMode) {
self.enter_mode(mode);
}
/// The Core is no longer running.
pub fn core_stopped(&self) {
self.enter_mode(RunningMode::NotRunning);
}
/// A start attempt is under way: the Core is not serving yet, whatever the mode says.
///
/// Closes the PAC endpoint without disturbing the Running Mode, so a handover cannot
/// hand out a PAC script for a proxy port that is between owners.
///
/// Every caller must pair this with [`Self::core_start_settled`]. A start attempt that
/// never happens — a candidate rejected before anything was stopped — otherwise leaves PAC
/// closed for a Core that is still serving, and nothing else would ever reopen it: PAC is
/// re-derived only when the Running Mode changes, and that is exactly what did not happen.
/// Closes PAC during a start or handover without changing the last running mode.
/// Every call must be paired with [`Self::core_start_settled`].
pub fn core_starting(&self) {
self.env.set_pac_available(false);
}
/// The start attempt is over, however it ended: PAC goes back to following the Running Mode.
///
/// Idempotent and safe on every path, because it re-derives rather than restores. After a
/// start that succeeded the mode is already running and this confirms PAC open; after one
/// that was abandoned the mode never moved and this reopens PAC for the Core that kept
/// serving; after one that failed and stopped the Core the mode says NotRunning and PAC
/// stays shut.
/// Idempotently re-derives PAC availability after a start attempt settles.
pub fn core_start_settled(&self) {
let running = !matches!(**self.mode.load(), RunningMode::NotRunning);
self.env.set_pac_available(running);
}
/// Move to `mode` and re-derive everything that follows from it.
///
/// PAC availability and the outward mirror are derived here and nowhere else, so they
/// cannot drift from the Running Mode the way ten hand-paired call sites could.
///
/// PAC closes *before* the mode changes and opens *after*, so whatever a concurrent
/// reader catches mid-transition is the closed state: being refused a PAC script beats
/// being handed one that points at a proxy port between owners.
/// Moves to `mode`, closing PAC before the transition and reopening it only afterward.
fn enter_mode(&self, mode: RunningMode) {
let running = !matches!(mode, RunningMode::NotRunning);
if !running {
@@ -498,8 +400,6 @@ impl<E: RunStateEnv> RunStateStore<E> {
self.announce();
}
// ─────────────────────────── privileged operations ───────────────────────────
/// Claim the privileged-operation slot, releasing it when the guard drops.
///
/// Only one privileged operation may be in flight; [`Self::settled`] waits on this.
@@ -514,31 +414,22 @@ impl<E: RunStateEnv> RunStateStore<E> {
Ok(OperationGuard { store: self })
}
/// Whether a privileged operation is in flight right now.
///
/// Production code reads this through [`RunState::op_in_flight`]; this accessor exists for
/// tests that assert on the slot without taking a snapshot.
#[cfg(test)]
pub fn operation_in_flight(&self) -> bool {
self.operation_running.load(Ordering::Acquire)
}
/// The environment this store observes, so tests can assert on the effects it recorded.
#[cfg(test)]
pub const fn env(&self) -> &E {
&self.env
}
/// Bumped on every state change; lets tests assert that a no-op really was a no-op.
#[cfg(test)]
pub fn generation_count(&self) -> u64 {
self.service.lock().generation
}
/// Tell the outside world the Run State moved.
///
/// Called after every mutation and never while the state lock is held, so a publisher that
/// reads back the state cannot deadlock against the writer that triggered it.
/// Publishes after dropping the state lock so observers may safely read it back.
fn announce(&self) {
self.env.publish(&self.state());
}
+5 -25
View File
@@ -1,20 +1,8 @@
//! Deciding when the Service has stopped being ours.
//!
//! While the Core runs under the Service, the app polls the Service for owner status. Three
//! things can go wrong — we cannot read a reply, the Service says someone else owns it, or it
//! answers but describes a Core that is gone — and each needs a different response. This is
//! the decision, separated from the polling and from the recovery it triggers.
//!
//! Recovery *actions* deliberately live with the Core lifecycle rather than here: this module
//! decides that the Service can no longer be trusted, and `CoreManager` is what tears the Core
//! down in response.
//! Classifies service ownership samples; `CoreManager` performs any recovery it requests.
use clash_verge_service_ipc::ServiceLifecycleState;
/// How many consecutive bad samples we tolerate before acting.
///
/// Service restarts and slow status calls are normal; acting on the first one would tear down
/// a working proxy for a blip.
/// Tolerates transient service restarts and slow status calls.
const SUSTAINED_SAMPLES: u8 = 3;
/// Why the app stopped trusting the Service that was running its Core.
@@ -49,11 +37,7 @@ pub enum OwnerSample {
pub enum OwnerStep {
/// Nothing is wrong, or not wrong for long enough yet.
Continue,
/// Unreadable for long enough that it might be real. Check whether the Core's own endpoint
/// still answers, then report back via [`OwnerWatch::resolve_transport`].
///
/// A Service we cannot reach while the Core is still serving is a broken status channel,
/// not a lost Core — tearing down the proxy there would be the cure causing the disease.
/// Check the Core endpoint before treating a sustained status-channel failure as lost ownership.
VerifyTransport,
/// Stop trusting the Service and recover.
Recover(OwnerRecoveryReason),
@@ -75,8 +59,7 @@ impl OwnerWatch {
}
}
/// Whether this sample warrants a log line: only the moment a run becomes sustained, so a
/// permanently broken Service does not fill the log at the poll interval.
/// True only when a failure first becomes sustained, to avoid repeated logs.
#[must_use]
pub const fn just_became_sustained(&self) -> bool {
self.unreadable_samples == SUSTAINED_SAMPLES
@@ -120,10 +103,7 @@ impl OwnerWatch {
}
}
/// Report what the transport check found after [`OwnerStep::VerifyTransport`].
///
/// If the Core's own endpoint still answers, only the status channel is broken: the run is
/// reset so the next stretch of silence has to earn its own verdict.
/// Resets the failure run when only the status channel is unavailable.
pub const fn resolve_transport(&mut self, owner_endpoint_available: bool) -> OwnerStep {
if owner_endpoint_available {
self.unreadable_samples = 0;
+7 -32
View File
@@ -61,13 +61,8 @@ pub(crate) async fn collect_runtime_bundle(config_file: &Path, core_path: &Path)
})
}
/// Rewrite one provider section's paths to service-side destinations, and record what each
/// destination is going to hold.
///
/// The two outputs are not the same thing. `assets` are files the app owns and the service copies;
/// `remote_providers` are files the *core* downloads, which the service never writes and only
/// needs to recognise — a download cache is reusable exactly while the url that produced it is
/// unchanged, and the service has no other way to tell.
/// Rewrites provider paths and records copied assets separately from core-downloaded providers.
/// Remote URLs let the service decide whether a core download cache remains reusable.
fn collect_provider_assets(
config: &mut Value,
section: &str,
@@ -91,28 +86,13 @@ fn collect_provider_assets(
let Some(raw_path) = provider.get("path").and_then(Value::as_str) else {
continue;
};
// The Core decides what a provider is by its `type`, so anything else here would disagree
// with it. Keying off the presence of a `url` used to do that: a `file` provider carrying
// a stray `url` was treated as remote, its local file left uncopied, and the Core then
// could not find what the rewritten path pointed at.
//
// Disagreeing the other way is just as bad, though, and it is why neither branch may fail
// the whole bundle over one odd provider. Measured against mihomo v1.19.26: it accepts a
// `http` provider with no url, an `inline` provider carrying both a url and a path that
// does not exist, and a `file` provider whose path is missing. Refusing any of those here
// would turn a configuration the Core is willing to load into one that cannot be applied
// at all — including at start, since this is where a Service-mode start gets its bundle.
// A provider we cannot classify keeps its path unrewritten and is simply not declared.
// Match mihomo's `type` classification and leave malformed-but-accepted providers untouched.
let is_remote = provider.get("type").and_then(Value::as_str) == Some("http");
let url = provider.get("url").and_then(Value::as_str).map(str::to_owned);
let destination = match (is_remote, url) {
(true, Some(url)) => {
let destination = provider_destination(config_root, raw_path)?;
// Remote providers take their destination out of circulation as much as local ones
// do: the Service refuses a bundle where one path is claimed both by a file it must
// copy and by a file the Core downloads, because the two would overwrite each
// other. Two providers naming the same file *and* the same source are not that —
// they are one file with two names for it, which the Service folds.
// Reserve remote destinations too; copied and downloaded files must not collide.
match remote_providers
.iter()
.find(|declared| declared.destination == destination)
@@ -281,9 +261,7 @@ mod tests {
);
assert!(bundle.assets.iter().any(|asset| asset.destination == "Country.mmdb"));
assert!(!bundle.assets.iter().any(|asset| asset.destination.contains("remote")));
// A remote provider is not copied, but it must still be declared: the Service decides
// whether the Core's download cache is reusable by comparing the url that produced it,
// and an undeclared provider is one it can only ever discard.
// Remote declarations preserve service-side cache reuse.
assert_eq!(
bundle
.remote_providers
@@ -305,9 +283,7 @@ mod tests {
#[tokio::test]
async fn two_names_for_one_remote_file_are_folded_rather_than_refused() -> anyhow::Result<()> {
// The Service folds an identical repeat, so refusing it here would block a configuration
// mihomo itself accepts — and this is also where a Service-mode *start* gets its bundle,
// so the refusal would stop the core coming up at all.
// Identical repeats are one service-side file and are safe to fold.
let root = std::env::temp_dir().join(format!("clash-verge-bundle-dup-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root)?;
@@ -351,8 +327,7 @@ mod tests {
#[tokio::test]
async fn an_odd_provider_is_left_alone_instead_of_failing_the_whole_bundle() -> anyhow::Result<()> {
// Measured against mihomo v1.19.26: it accepts a `http` provider with no url, and a
// non-http provider whose path does not exist. Neither may stop the rest being materialised.
// Odd providers accepted by mihomo must not block the rest of the bundle.
let root = std::env::temp_dir().join(format!("clash-verge-bundle-odd-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root)?;
+17 -70
View File
@@ -39,12 +39,8 @@ use std::{
static OWNER_MONITOR_GENERATION: AtomicU64 = AtomicU64::new(0);
static ACTIVE_SERVICE_SESSION: Lazy<Mutex<Option<ActiveServiceSession>>> = Lazy::new(|| Mutex::new(None));
/// The Service session that owns the running Core, and what that Service can do.
///
/// The capability is learned once, when the Core is started, and discarded with the session
/// rather than cached globally — it describes *the Service instance that owns this Core*, which
/// is the only scope where it is safe to act on. A Service upgraded underneath a running Core
/// does not silently gain abilities the Core it owns was not started under.
/// Capabilities of the service session that owns the running Core.
/// They are discarded with that session rather than cached across service upgrades.
#[derive(Clone)]
struct ActiveServiceSession {
proof: OwnerSessionProof,
@@ -65,10 +61,7 @@ pub(crate) fn active_service_session() -> Result<OwnerSessionProof> {
.context("service owner session is not active")
}
/// Whether the Service that started the running Core can stage a runtime in place.
///
/// False for every reason that is not "yes": no session, an older Service, or a protocol query
/// that did not come back. All of them mean the same thing to the caller — take the slow path.
/// Returns false unless the active service session explicitly supports in-place staging.
pub(crate) fn active_service_supports_runtime_staging() -> bool {
ACTIVE_SERVICE_SESSION
.lock()
@@ -80,10 +73,7 @@ pub(crate) fn clear_active_service_session() {
ACTIVE_SERVICE_SESSION.lock().take();
}
/// Ask the Service whether it speaks the staging half of the protocol.
///
/// A failure here is not a failure to start: it only costs the fast path, so it is reported as
/// "no" and logged rather than propagated.
/// Probes staging support without failing startup when the fast path is unavailable.
async fn probe_runtime_staging_support() -> bool {
match clash_verge_service_ipc::get_version().await {
Ok(response) if response.code == 0 => response
@@ -130,11 +120,7 @@ pub enum ServiceStatus {
}
impl ServiceStatus {
/// Flatten a Run State snapshot into the legacy single-slot status.
///
/// Precedence mirrors what the single slot used to hold: a requested action shadows an
/// accepted Sidecar, which shadows the last observation. Kept until the frontend seam
/// moves to `RunState` wholesale.
/// Flattens Run State using legacy precedence: action, Sidecar allowance, then health.
fn from_run_state(state: &RunState) -> Self {
if let Some(action) = state.pending {
return match action {
@@ -240,11 +226,7 @@ pub(crate) fn trusted_service_evidence() -> Result<bool> {
macos_service_install_marker_exists().context("failed to inspect launchd service registration")
}
/// Legacy façade over [`RUN_STATE`].
///
/// Holds no state of its own: Service Health, the requested action and the privileged-operation
/// lock all live in `core::runstate`. This type survives only so that existing call sites keep
/// compiling while the seam moves; it is retired once they read `RunState` directly.
/// Stateless legacy façade over [`RUN_STATE`] retained for existing call sites.
pub struct ServiceManager;
#[cfg(any(all(target_os = "macos", feature = "verge-dev"), test))]
@@ -812,9 +794,7 @@ fn force_reinstall_service() -> Result<()> {
})
}
/// Dispatch a privileged operation to the platform implementation.
///
/// Blocking work, so it is handed to a blocking thread: the caller is async.
/// Dispatches a privileged platform operation on a blocking thread.
pub(crate) fn run_privileged_service_action(action: PendingAction) -> Result<()> {
let (operation, label): (fn() -> Result<()>, &'static str) = match action {
PendingAction::Install => (install_service, "install service"),
@@ -825,11 +805,7 @@ pub(crate) fn run_privileged_service_action(action: PendingAction) -> Result<()>
tokio::task::block_in_place(operation).with_context(|| format!("{label} failed"))
}
/// Describe what the Service should hold, for the Core binary this app would start.
///
/// Shared by starting and staging so the two cannot disagree about the binary or about how the
/// configuration's provider paths are rewritten — a staged bundle naming a different core is
/// exactly what makes the Service refuse to stage.
/// Builds the same runtime bundle description for both service start and staging.
async fn collect_service_runtime_bundle(config_file: &Path) -> Result<RuntimeBundle> {
let verge_config = Config::verge().await;
let clash_core = verge_config.latest_arc().get_valid_clash_core();
@@ -840,13 +816,7 @@ async fn collect_service_runtime_bundle(config_file: &Path) -> Result<RuntimeBun
collect_runtime_bundle(config_file, &bin_path).await
}
/// What asking the Service to stage a runtime produced.
///
/// A refusal carries its code because only some refusals say anything about starting. "This bundle
/// names an asset I will not accept" does: a fresh start materialises the same bundle and is
/// refused the same way, so stopping a working Core would add an outage to a failure that already
/// happened. "You do not own this Core" does not — `start_clash` proposes a new session rather than
/// presenting the old one, so starting is exactly how that gets resolved.
/// A staging response whose refusal code tells callers whether a fresh start can help.
pub(super) enum StageRequest {
Refused { code: u16, message: CompactString },
Answered(StageRuntimeOutcome),
@@ -859,11 +829,7 @@ impl StageRequest {
}
}
/// Have the Service make the running Core's runtime match `config_file`, without restarting it.
///
/// `Err` means the request did not get an answer. A refusal, and `RestartRequired`, both come back
/// as `Ok`: neither is a failure of this function, and only the caller can decide what to do about
/// them.
/// Requests in-place staging. `Err` means no answer; refusals are returned for caller policy.
pub(super) async fn stage_runtime_by_service(config_file: &Path) -> Result<StageRequest> {
let session = active_service_session()?;
let credentials = current_owner_credentials()?;
@@ -1141,10 +1107,7 @@ fn start_owner_monitor() {
});
}
/// Ask the Service who owns it, flattening every unusable answer into one sample.
///
/// A transport error, an error code and an empty payload are the same thing to the watch:
/// we did not learn anything. Only the log line distinguishes them.
/// Samples ownership, treating every unusable reply as unreadable.
async fn read_owner_sample() -> OwnerSample {
let response = match current_owner_credentials() {
Ok(credentials) => clash_verge_service_ipc::get_status(&credentials).await,
@@ -1270,11 +1233,8 @@ async fn recover_after_owner_loss_while_locked(reason: OwnerRecoveryReason) {
}
}
/// Wait for a freshly installed or repaired Service to answer.
///
/// Silence for the whole budget *is* an observation here — the Service had its window and
/// never spoke — unlike a single failed probe. A readable but rejected reply already recorded
/// its own verdict, which must not be flattened into "unavailable".
/// Waits for a repaired service, preserving readable rejection details but classifying sustained
/// silence as unavailable.
async fn wait_for_service_ipc() -> Result<()> {
const CONTEXT: &str = "service IPC did not become available";
let config = ServiceManager::config();
@@ -1354,9 +1314,7 @@ impl ServiceManager {
}
async fn apply_service_status(&self, status: ServiceStatus) -> Result<()> {
// Derived from the caller's own argument, not read back out of the store: an
// observation racing in between would clear the pending action and silently turn a
// user-authorised install into a no-op.
// Use the caller's action; a racing observation may clear the stored pending action.
let Some(action) = requested_action(&status) else {
self.set_status(status.clone());
return report_non_actionable_status(status);
@@ -1377,9 +1335,7 @@ impl ServiceManager {
}
}
/// Run the full action workflow and restore the session's previous Sidecar allowance on failure.
///
/// This includes the readiness wait: an action has not landed until the Service responds.
/// Runs through readiness and restores a displaced Sidecar allowance on failure.
async fn run_action_restoring_sidecar<E: RunStateEnv>(
store: &RunStateStore<E>,
was_allowed: bool,
@@ -1428,11 +1384,7 @@ fn report_non_actionable_status(status: ServiceStatus) -> Result<()> {
Ok(())
}
/// Run a privileged operation while holding the Run State operation slot.
///
/// The slot is released — and `settled` waiters woken — before `post_operation` runs, so a
/// post-operation refresh such as the tray menu observes the final state rather than a
/// state still flagged as in-flight.
/// Releases the Run State operation slot before post-operation observers refresh.
async fn run_operation_and_then<E, Post, PostFuture>(
store: &RunStateStore<E>,
operation: impl Future<Output = Result<()>>,
@@ -1451,12 +1403,7 @@ where
post_operation().await
}
/// Apply a legacy single-slot status to a Run State, splitting it back into observation and
/// request. The inverse of [`ServiceStatus::from_run_state`].
/// The privileged operation a status is asking for, if any.
///
/// A pure function of the status so that a caller can decide what to run without reading the
/// store back and racing an observation.
/// Maps a legacy status to its requested action without racing a store reread.
const fn requested_action(status: &ServiceStatus) -> Option<PendingAction> {
match status {
ServiceStatus::InstallRequired => Some(PendingAction::Install),
-22
View File
@@ -156,7 +156,6 @@ impl Tray {
logging!(info, Type::Tray, "System tray created successfully");
}
Err(e) => {
// Don't return error, let application continue running without tray
logging!(
warn,
Type::Tray,
@@ -167,7 +166,6 @@ impl Tray {
Ok(())
}
/// 更新托盘点击行为
pub async fn update_click_behavior(&self) -> Result<()> {
if handle::Handle::global().is_exiting() {
logging!(debug, Type::Tray, "应用正在退出,跳过托盘点击行为更新");
@@ -187,7 +185,6 @@ impl Tray {
Ok(())
}
/// 更新托盘菜单
pub async fn update_menu(&self) -> Result<()> {
if handle::Handle::global().is_exiting() {
logging!(debug, Type::Tray, "应用正在退出,跳过托盘菜单更新");
@@ -245,7 +242,6 @@ impl Tray {
Ok(())
}
/// 更新托盘图标
pub async fn update_icon(&self, verge: &IVerge) -> Result<()> {
if handle::Handle::global().is_exiting() {
logging!(debug, Type::Tray, "应用正在退出,跳过托盘图标更新");
@@ -278,7 +274,6 @@ impl Tray {
Ok(())
}
/// 更新托盘提示
pub async fn update_tooltip(&self) -> Result<()> {
if handle::Handle::global().is_exiting() {
logging!(debug, Type::Tray, "应用正在退出,跳过托盘提示更新");
@@ -309,7 +304,6 @@ impl Tray {
}
}
// Get localized strings before using them
let sys_proxy_text = clash_verge_i18n::t!("tray.tooltip.systemProxy");
let tun_text = clash_verge_i18n::t!("tray.tooltip.tun");
let profile_text = clash_verge_i18n::t!("tray.tooltip.profile");
@@ -413,7 +407,6 @@ impl Tray {
allow
}
/// 根据配置统一更新托盘速率采集任务状态(macOS)
#[cfg(target_os = "macos")]
pub fn update_speed_task(&self, enable_tray_speed: bool) {
self.speed_controller.update_task(enable_tray_speed);
@@ -476,7 +469,6 @@ fn create_subcreate_proxy_menu_item(
// TODO: 应用启动时,内核还未启动完全,无法获取代理节点信息
if let Some(proxy_nodes_data) = proxy_nodes_data {
for (group_name, group_data) in proxy_nodes_data.proxies.iter() {
// Filter groups based on mode and hidden flag
let should_show = match proxy_mode {
"global" => group_name == "GLOBAL",
_ => group_name != "GLOBAL",
@@ -492,14 +484,12 @@ fn create_subcreate_proxy_menu_item(
let now_proxy = group_data.now.as_deref().unwrap_or_default();
// Create proxy items
let group_items: Vec<CheckMenuItem<Wry>> = all_proxies
.iter()
.filter_map(|proxy_str| {
let is_selected = *proxy_str == now_proxy;
let item_id = format!("proxy_{}_{}", group_name, proxy_str);
// Get delay for display
let delay_text = proxy_nodes_data
.proxies
.get(proxy_str)
@@ -565,7 +555,6 @@ fn create_proxy_menu_item(
proxy_submenus: Vec<Submenu<Wry>>,
proxies_text: &str,
) -> Result<ProxyMenuItem> {
// 创建代理主菜单
let (proxies_submenu, inline_proxy_items) = if show_proxy_groups_inline {
(
None,
@@ -660,9 +649,7 @@ async fn create_tray_menu(
let profile_menu_items: Vec<CheckMenuItem<Wry>> = create_profile_menu_item(app_handle, profiles_preview)?;
// Pre-fetch all localized strings
let texts = MenuTexts::new();
// Convert to references only when needed
let profile_menu_items_refs: Vec<&dyn IsMenuItem<Wry>> = profile_menu_items
.iter()
.map(|item| item as &dyn IsMenuItem<Wry>)
@@ -842,7 +829,6 @@ async fn create_tray_menu(
let separator = &PredefinedMenuItem::separator(app_handle)?;
// 动态构建菜单项
let mut menu_items: Vec<&dyn IsMenuItem<Wry>> = vec![open_window, separator];
if show_outbound_modes_inline {
@@ -857,7 +843,6 @@ async fn create_tray_menu(
menu_items.extend_from_slice(&[separator, profiles]);
// 如果有代理节点,添加代理节点菜单
match tray_proxy_groups_display_mode {
"default" => {
menu_items.extend(proxies_menu.iter().map(|item| item as &dyn IsMenuItem<_>));
@@ -898,7 +883,6 @@ fn on_tray_icon_event(_tray_icon: &TrayIcon, tray_event: TrayIconEvent) {
..
} = tray_event
{
// 添加防抖检查,防止快速连击
#[allow(clippy::use_self)]
if !Tray::global().should_handle_tray_click() {
return;
@@ -942,12 +926,10 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
AsyncHandler::spawn(|| async move {
match event.id.as_ref() {
mode @ (MenuIds::RULE_MODE | MenuIds::GLOBAL_MODE | MenuIds::DIRECT_MODE) => {
// Removing the the "tray_" prefix and "_mode" suffix
if let Some(stripped) = mode.strip_prefix("tray_")
&& let Some(final_mode) = stripped.strip_suffix("_mode")
{
logging!(info, Type::ProxyMode, "Switch Proxy Mode To: {}", final_mode);
// 错误已在 change_clash_mode 内部记录,此处显式忽略返回值
let _ = feat::change_clash_mode(final_mode.into()).await;
}
}
@@ -1004,7 +986,6 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
feat::toggle_proxy_profile(profile_index.into()).await;
}
id if id.starts_with("proxy_") => {
// proxy_{group_name}_{proxy_name}
let rest = match id.strip_prefix("proxy_") {
Some(r) => r,
None => return,
@@ -1019,8 +1000,5 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
logging!(debug, Type::Tray, "Unhandled tray menu event: {:?}", event.id);
}
}
// We dont expected to refresh tray state here
// as the inner handle function (SHOULD) already takes care of it
});
}
+2 -12
View File
@@ -8,12 +8,9 @@ use std::time::Duration;
use tauri::async_runtime::JoinHandle;
use tauri_plugin_mihomo::models::WsConnectionId;
/// 托盘速率流异常后的重连间隔。
const TRAY_SPEED_RETRY_DELAY: Duration = Duration::from_secs(1);
/// 托盘速率流在此时间内收不到有效数据时,触发重连并降级到 0/0。
const TRAY_SPEED_STALE_TIMEOUT: Duration = Duration::from_secs(5);
/// macOS 托盘速率任务控制器。
#[derive(Clone)]
pub struct TraySpeedController {
speed_task: Arc<Mutex<Option<JoinHandle<()>>>>,
@@ -42,13 +39,11 @@ impl TraySpeedController {
}
}
/// 启动托盘速率采集后台任务(基于 `/traffic` WebSocket 流)。
fn start_task(&self) {
if handle::Handle::global().is_exiting() {
return;
}
// 关键步骤:托盘不可用时不启动速率任务,避免无效连接重试。
if !Self::has_main_tray() {
logging!(warn, Type::Tray, "托盘不可用,跳过启动托盘速率任务");
return;
@@ -111,7 +106,7 @@ impl TraySpeedController {
break;
}
// Stale 分支在内层 loop 中已重置为 0/0;此处兜底 Closed 分支(流被远端关闭)。
// `Stale` already reset the display; this covers remote close.
Self::apply_tray_speed(0, 0);
tokio::time::sleep(TRAY_SPEED_RETRY_DELAY).await;
}
@@ -122,17 +117,12 @@ impl TraySpeedController {
*guard = Some(task);
}
/// 停止托盘速率采集后台任务并清除速率显示。
fn stop_task(&self) {
// 取出任务句柄,与 speed_connection_id 一同传入清理任务。
let task = self.speed_task.lock().take();
let speed_connection_id = Arc::clone(&self.speed_connection_id);
AsyncHandler::spawn(move || async move {
// 关键步骤:先等待 abort 完成,再断开 WebSocket 连接。
// 若直接 abort 后立即 disconnect,任务可能已通过 take 取走 connection_id
// 但尚未完成断开,导致 connection_id 丢失、连接泄漏。
// await task handle 可保证原任务已退出,connection_id 不再被占用。
// Await abort before disconnect so the task cannot consume and lose the connection ID.
if let Some(task) = task {
task.abort();
let _ = task.await;
+10 -53
View File
@@ -34,8 +34,6 @@ impl SilentUpdater {
}
}
// ─── Disk Cache ───────────────────────────────────────────────────────────────
#[derive(Serialize, Deserialize)]
struct UpdateCacheMeta {
version: String,
@@ -95,10 +93,7 @@ impl SilentUpdater {
}
}
// ─── Version Comparison ───────────────────────────────────────────────────────
/// Returns true if version `a` <= version `b` using semver-like comparison.
/// Strips leading 'v', splits on '.', handles pre-release suffixes.
/// Compares numeric version components after stripping `v` and prerelease suffixes.
fn version_lte(a: &str, b: &str) -> bool {
let parse = |v: &str| -> Vec<u64> {
v.trim_start_matches('v')
@@ -127,12 +122,7 @@ fn version_lte(a: &str, b: &str) -> bool {
true // equal
}
/// Map the app's resolved UI language code to an NSIS installer language ID.
///
/// The Windows installer only ships SimpChinese, English and Russian
/// (`tauri.windows.conf.json` `nsis.languages`). Languages without a matching
/// NSIS translation fall back to English; Traditional Chinese ("zhtw") is
/// mapped to SimpChinese so Chinese users don't get an English installer.
/// Maps UI language to one of the three NSIS translations, defaulting to English.
#[cfg(target_os = "windows")]
fn nsis_language_id(app_language: &str) -> &'static str {
match app_language {
@@ -142,12 +132,8 @@ fn nsis_language_id(app_language: &str) -> &'static str {
}
}
// ─── Startup Install & Cache Management ─────────────────────────────────────
impl SilentUpdater {
/// Called at app startup. If a cached update exists and is newer than the current version,
/// attempt to install it immediately (before the main app initializes).
/// Returns true if install was triggered (app should relaunch), false otherwise.
/// Installs a newer cached update before normal startup, if the user confirms.
pub async fn try_install_on_startup(&self, app_handle: &tauri::AppHandle) -> bool {
let current_version = env!("CARGO_PKG_VERSION");
@@ -178,14 +164,12 @@ impl SilentUpdater {
current_version
);
// Ask user for confirmation — they can skip and use the app normally.
// The cache is preserved so next launch will ask again.
// Preserve the cache when skipped so the next launch asks again.
if !Self::ask_user_to_install(app_handle, cached_version).await {
logging!(info, Type::System, "User skipped update install, starting normally");
return false;
}
// Read cached bytes
let bytes = match Self::read_cache_bytes() {
Ok(b) => b,
Err(e) => {
@@ -199,14 +183,8 @@ impl SilentUpdater {
}
};
// Need a fresh Update object from the server to call install().
// This is a lightweight HTTP request (< 1s), not a re-download.
//
// The updater is built with the app's current language forwarded as
// `/LANG=<NSIS-lang-id>`. On Windows the NSIS installer parses this in
// `.onInit` (see `packages/windows/installer.nsi`) and sets `$LANGUAGE`
// directly, skipping its language-selection dialog so the update starts
// without prompting. Other platforms don't run an installer.
// Refresh metadata without re-downloading. Windows receives `/LANG` to suppress the
// NSIS language dialog; see `packages/windows/installer.nsi`.
let updater_builder = app_handle.updater_builder();
#[cfg(target_os = "windows")]
let updater_builder = {
@@ -245,8 +223,7 @@ impl SilentUpdater {
}
};
// Verify the server's version matches the cached version.
// If server now has a newer version, our cached bytes are stale.
// A changed server version invalidates the cached bytes.
if update.version != *cached_version {
logging!(
info,
@@ -262,11 +239,9 @@ impl SilentUpdater {
let version = update.version.clone();
logging!(info, Type::System, "Installing cached update v{version} at startup...");
// Show splash window so user knows the app is updating, not frozen
Self::show_update_splash(app_handle, &version);
// install() is sync and may hang (known bug #2558), so run with a timeout.
// On Windows, NSIS takes over the process so install() may never return — that's OK.
// `install()` may hang (#2558); on Windows NSIS can take over without returning.
let install_result = tokio::task::spawn_blocking({
let bytes = bytes.clone();
let update = update.clone();
@@ -305,7 +280,6 @@ impl SilentUpdater {
}
};
// Close splash window if install failed and app continues normally
if !success {
Self::close_update_splash(app_handle);
}
@@ -314,11 +288,7 @@ impl SilentUpdater {
}
}
// ─── User Confirmation Dialog ────────────────────────────────────────────────
impl SilentUpdater {
/// Show a native dialog asking the user to install or skip the update.
/// Returns true if user chose to install, false if they chose to skip.
async fn ask_user_to_install(app_handle: &tauri::AppHandle, version: &str) -> bool {
use tauri_plugin_dialog::{DialogExt as _, MessageDialogButtons, MessageDialogKind};
@@ -343,12 +313,8 @@ impl SilentUpdater {
}
}
// ─── Update Splash Window ────────────────────────────────────────────────────
impl SilentUpdater {
/// Show a small centered splash window indicating update is being installed.
/// Injects HTML via eval() after window creation so it doesn't depend on any
/// external file in the bundle.
/// Uses injected HTML so the splash has no bundled-file dependency.
fn show_update_splash(app_handle: &tauri::AppHandle, version: &str) {
use tauri::{WebviewUrl, WebviewWindowBuilder};
@@ -405,7 +371,7 @@ impl SilentUpdater {
"#
);
// Retry eval a few times — the webview may not be ready immediately
// The new webview may not accept evaluation immediately.
std::thread::spawn(move || {
for i in 0..10 {
std::thread::sleep(std::time::Duration::from_millis(100 * (i + 1)));
@@ -418,7 +384,6 @@ impl SilentUpdater {
logging!(info, Type::System, "Update splash window shown");
}
/// Close the update splash window (e.g. after install failure).
fn close_update_splash(app_handle: &tauri::AppHandle) {
use tauri::Manager as _;
if let Some(window) = app_handle.get_webview_window("update-splash") {
@@ -428,8 +393,6 @@ impl SilentUpdater {
}
}
// ─── Background Check and Download ───────────────────────────────────────────
impl SilentUpdater {
async fn check_and_download(&self, app_handle: &tauri::AppHandle) -> Result<()> {
let is_portable = *dirs::PORTABLE_FLAG.get().unwrap_or(&false);
@@ -535,8 +498,6 @@ impl SilentUpdater {
mod tests {
use super::*;
// ─── version_lte tests ──────────────────────────────────────────────────
#[test]
fn test_version_equal() {
assert!(version_lte("2.4.7", "2.4.7"));
@@ -565,10 +526,8 @@ mod tests {
#[test]
fn test_version_with_prerelease() {
// "2.4.8-alpha" → numeric part is still "2.4.8"
assert!(version_lte("2.4.7", "2.4.8-alpha"));
assert!(version_lte("2.4.8-alpha", "2.4.8"));
// Both have same numeric part, so equal → true
assert!(version_lte("2.4.8-alpha", "2.4.8-beta"));
}
@@ -579,8 +538,6 @@ mod tests {
assert!(version_lte("2.4.0", "2.4"));
}
// ─── Cache metadata tests ───────────────────────────────────────────────
#[test]
fn test_cache_meta_serialize_roundtrip() {
let meta = UpdateCacheMeta {
+4 -33
View File
@@ -133,16 +133,13 @@ impl CoreConfigValidator {
}
impl CoreConfigValidator {
/// 检查文件是否为脚本文件
async fn is_script_file(path: &str) -> Result<bool> {
// 1. 先通过扩展名快速判断
if has_ext(path, "yaml") || has_ext(path, "yml") {
return Ok(false); // YAML文件不是脚本文件
return Ok(false);
} else if has_ext(path, "js") {
return Ok(true); // JS文件是脚本文件
return Ok(true);
}
// 2. 读取文件内容
let content = match fs::read_to_string(path).await {
Ok(content) => content,
Err(err) => {
@@ -151,13 +148,11 @@ impl CoreConfigValidator {
}
};
// 3. 检查是否存在明显的YAML特征
let has_yaml_features = content.contains(": ")
|| content.contains("#")
|| content.contains("---")
|| content.lines().any(|line| line.trim().starts_with("- "));
// 4. 检查是否存在明显的JS特征
let has_js_features = content.contains("function ")
|| content.contains("const ")
|| content.contains("let ")
@@ -168,16 +163,11 @@ impl CoreConfigValidator {
|| content.contains("export ")
|| content.contains("import ");
// 5. 决策逻辑
if has_yaml_features && !has_js_features {
// 只有YAML特征,没有JS特征
return Ok(false);
} else if has_js_features && !has_yaml_features {
// 只有JS特征,没有YAML特征
return Ok(true);
} else if has_yaml_features && has_js_features {
// 两种特征都有,需要更精细判断
// 优先检查是否有明确的JS结构特征
if content.contains("function main")
|| content.contains("module.exports")
|| content.contains("export default")
@@ -185,24 +175,21 @@ impl CoreConfigValidator {
return Ok(true);
}
// 检查冒号后是否有空格(YAML的典型特征)
let yaml_pattern_count = content.lines().filter(|line| line.contains(": ")).count();
if yaml_pattern_count > 2 {
return Ok(false); // 多个键值对格式,更可能是YAML
return Ok(false);
}
}
// 默认情况:无法确定时,假设为非脚本文件(更安全)
// Unknown files take the safer YAML validation path.
logging!(debug, Type::Validate, "无法确定文件类型,默认当作YAML处理: {}", path);
Ok(false)
}
/// 只进行文件语法检查,不进行完整验证
async fn validate_file_syntax_outcome(config_path: &str) -> Result<ValidationOutcome> {
logging!(info, Type::Validate, "开始检查文件: {}", config_path);
// 读取文件内容
let content = match fs::read_to_string(config_path).await {
Ok(content) => content,
Err(err) => {
@@ -211,7 +198,6 @@ impl CoreConfigValidator {
return Ok(ValidationOutcome::invalid_from_message(error_msg));
}
};
// 对YAML文件尝试解析,只检查语法正确性
logging!(info, Type::Validate, "进行YAML语法检查");
match serde_yaml_ng::from_str::<serde_yaml_ng::Value>(&content) {
Ok(_) => {
@@ -226,9 +212,7 @@ impl CoreConfigValidator {
}
}
/// 验证脚本文件语法
async fn validate_script_file_outcome(path: &str) -> Result<ValidationOutcome> {
// 读取脚本内容
let content = match fs::read_to_string(path).await {
Ok(content) => content,
Err(err) => {
@@ -240,7 +224,6 @@ impl CoreConfigValidator {
logging!(debug, Type::Validate, "验证脚本文件: {}", path);
// 使用boa引擎进行基本语法检查
use boa_engine::{Context, Source};
let mut context = Context::default();
@@ -258,7 +241,6 @@ impl CoreConfigValidator {
Ok(_) => {
logging!(debug, Type::Validate, "脚本语法验证通过: {}", path);
// 检查脚本是否包含main函数
if !content.contains("function main")
&& !content.contains("const main")
&& !content.contains("let main")
@@ -278,12 +260,10 @@ impl CoreConfigValidator {
}
}
/// 验证指定的配置文件
pub async fn validate_config_file_outcome(
config_path: &str,
is_merge_file: Option<bool>,
) -> Result<ValidationOutcome> {
// 检查程序是否正在退出,如果是则跳过验证
if handle::Handle::global().is_exiting() {
logging!(info, Type::Core, "应用正在退出,跳过验证");
return Ok(ValidationOutcome::Skipped {
@@ -291,13 +271,11 @@ impl CoreConfigValidator {
});
}
// 检查文件是否存在
if !std::path::Path::new(config_path).exists() {
let error_msg: String = format!("File not found: {config_path}").into();
return Ok(ValidationOutcome::invalid_from_message(error_msg));
}
// 如果是合并文件且不是强制验证,执行语法检查但不进行完整验证
if is_merge_file.unwrap_or(false) {
logging!(info, Type::Validate, "检测到Merge文件,仅进行语法检查: {}", config_path);
return Self::validate_file_syntax_outcome(config_path).await;
@@ -306,7 +284,6 @@ impl CoreConfigValidator {
let is_script = match Self::is_script_file(config_path).await {
Ok(result) => result,
Err(err) => {
// 如果无法确定文件类型,尝试使用Clash内核验证
logging!(warn, Type::Validate, "无法确定文件类型: {}, 错误: {}", config_path, err);
return Self::validate_config_internal_outcome(config_path).await;
}
@@ -322,14 +299,11 @@ impl CoreConfigValidator {
return Self::validate_script_file_outcome(config_path).await;
}
// 对YAML配置文件使用Clash内核验证
logging!(info, Type::Validate, "使用Clash内核验证配置文件: {}", config_path);
Self::validate_config_internal_outcome(config_path).await
}
/// 内部验证配置文件的实现
async fn validate_config_internal_outcome(config_path: &str) -> Result<ValidationOutcome> {
// 检查程序是否正在退出,如果是则跳过验证
if handle::Handle::global().is_exiting() {
logging!(info, Type::Validate, "应用正在退出,跳过验证");
return Ok(ValidationOutcome::Skipped {
@@ -347,7 +321,6 @@ impl CoreConfigValidator {
let app_dir_str = dirs::path_to_str(&app_dir)?;
logging!(info, Type::Validate, "验证目录: {}", app_dir_str);
// 使用子进程运行clash验证配置
let command = app_handle.shell().sidecar(clash_core.as_str()).map_err(|error| {
anyhow::anyhow!("failed to build validation command for core {clash_core:?}: {error:#}")
})?;
@@ -363,7 +336,6 @@ impl CoreConfigValidator {
let stderr = &output.stderr;
let stdout = &output.stdout;
// 检查进程退出状态和错误输出
let error_keywords = ["FATA", "fatal", "Parse config error", "level=fatal"];
let has_error = !status.success() || contains_any_keyword(stderr, &error_keywords);
@@ -399,7 +371,6 @@ impl CoreConfigValidator {
}
}
/// 验证运行时配置
pub async fn validate_config_outcome(&self) -> Result<ValidationOutcome> {
if !self.try_start() {
logging!(info, Type::Validate, "验证已在进行中,跳过新的验证请求");
-36
View File
@@ -28,42 +28,6 @@ pub enum ChainSupport {
ClashMetaAlpha,
}
// impl From<&PrfItem> for Option<ChainItem> {
// fn from(item: &PrfItem) -> Self {
// let itype = item.itype.as_ref()?.as_str();
// let file = item.file.clone()?;
// let uid = item.uid.clone().unwrap_or("".into());
// let path = dirs::app_profiles_dir().ok()?.join(file);
// if !path.exists() {
// return None;
// }
// match itype {
// "script" => Some(ChainItem {
// uid,
// data: ChainType::Script(fs::read_to_string(path).ok()?),
// }),
// "merge" => Some(ChainItem {
// uid,
// data: ChainType::Merge(help::read_mapping(&path).ok()?),
// }),
// "rules" => Some(ChainItem {
// uid,
// data: ChainType::Rules(help::read_seq_map(&path).ok()?),
// }),
// "proxies" => Some(ChainItem {
// uid,
// data: ChainType::Proxies(help::read_seq_map(&path).ok()?),
// }),
// "groups" => Some(ChainItem {
// uid,
// data: ChainType::Groups(help::read_seq_map(&path).ok()?),
// }),
// _ => None,
// }
// }
// }
// Helper trait to allow async conversion
pub trait AsyncChainItemFrom {
async fn from_async(item: &PrfItem) -> Option<ChainItem>;
+2 -14
View File
@@ -521,12 +521,10 @@ fn merge_default_config(
continue;
}
}
// 处理 external-controller 键的开关逻辑
if key.as_str() == Some("external-controller") {
if enable_external_controller {
config.insert(key, value);
} else {
// 如果禁用了外部控制器,设置为空字符串
config.insert(key, "".into());
}
} else {
@@ -656,7 +654,7 @@ fn ensure_fake_ip_range6(dns: &mut Mapping) {
.map(|m| m == "fake-ip")
.unwrap_or(true);
// 缺失或为空字符串(可能来自手动编辑的 YAML)时都需要补充
// Hand-edited YAML may leave the key present but empty.
let range6_missing = dns
.get("fake-ip-range6")
.and_then(|v| v.as_str())
@@ -702,10 +700,8 @@ async fn apply_dns_settings(mut config: Mapping, enable_dns_settings: bool) -> M
config
}
/// Enhance mode
/// 返回最终订阅、该订阅包含的键、和script执行的结果
/// Returns the enhanced profile, its original keys, and script logs.
pub async fn enhance(profiles: &IProfiles) -> Result<(Mapping, HashSet<String>, HashMap<String, ResultLog>)> {
// gather config values
let cfg_vals = get_config_values().await;
let ConfigValues {
clash_config,
@@ -722,7 +718,6 @@ pub async fn enhance(profiles: &IProfiles) -> Result<(Mapping, HashSet<String>,
tproxy_enabled,
} = cfg_vals;
// collect profile items
let profile = collect_profile_items(profiles).await?;
let config = profile.config;
let merge_item = profile.merge_item;
@@ -736,11 +731,9 @@ pub async fn enhance(profiles: &IProfiles) -> Result<(Mapping, HashSet<String>,
let result_map = HashMap::new();
// 顺序项先于手动覆盖。
let config = process_seq_items(config, rules_item, proxies_item, groups_item);
let exists_keys = use_keys(&config).collect::<Vec<_>>();
// merge default clash config
let config = merge_default_config(
config,
clash_config,
@@ -753,15 +746,12 @@ pub async fn enhance(profiles: &IProfiles) -> Result<(Mapping, HashSet<String>,
tproxy_enabled,
);
// app 生成项先于手动覆盖。
let config = apply_builtin_scripts(config, clash_core, enable_builtin).await;
let config = use_tun(config, enable_tun);
let config = apply_dns_settings(config, enable_dns_settings).await;
// 手动覆盖前锁定 app 权威字段,覆盖后由同一个值恢复。
let authoritative = AuthoritativeFields::capture(&config, enable_dns_settings);
// 全局手动覆盖。
let (config, exists_keys, result_map) = process_global_items(
config,
exists_keys,
@@ -772,11 +762,9 @@ pub async fn enhance(profiles: &IProfiles) -> Result<(Mapping, HashSet<String>,
)
.await;
// 当前 profile 手动覆盖。
let (config, exists_keys, result_map) =
process_profile_items(config, exists_keys, result_map, merge_item, script_item, &profile_name).await;
// 手动覆盖后恢复 app 权威字段。
let config = authoritative.enforce(config);
let config = ensure_lan_bind_address(config);
-7
View File
@@ -18,7 +18,6 @@ pub fn use_tun(mut config: Mapping, enable: bool) -> Mapping {
});
if enable {
// 读取DNS配置
let dns_key = Value::from("dns");
let dns_val = config.get(&dns_key);
let mut dns_val = dns_val.map_or_else(Mapping::new, |val| {
@@ -27,13 +26,11 @@ pub fn use_tun(mut config: Mapping, enable: bool) -> Mapping {
let ipv6_key = Value::from("ipv6");
let ipv6_val = config.get(&ipv6_key).and_then(|v| v.as_bool()).unwrap_or(false);
// 检查现有的 enhanced-mode 设置
let current_mode = dns_val
.get(Value::from("enhanced-mode"))
.and_then(|v| v.as_str())
.unwrap_or("fake-ip");
// 只有当 enhanced-mode 是 fake-ip 或未设置时才修改 DNS 配置
if current_mode == "fake-ip" || !dns_val.contains_key(Value::from("enhanced-mode")) {
revise!(dns_val, "enable", true);
revise!(dns_val, "ipv6", ipv6_val);
@@ -46,7 +43,6 @@ pub fn use_tun(mut config: Mapping, enable: bool) -> Mapping {
revise!(dns_val, "fake-ip-range", "198.18.0.1/16");
}
// 当启用 IPv6 时,补充 IPv6 的 fake-ip 范围
if ipv6_val && !dns_val.contains_key(Value::from("fake-ip-range6")) {
revise!(dns_val, "fake-ip-range6", "fdfe:dcba:9876::1/64");
}
@@ -60,17 +56,14 @@ pub fn use_tun(mut config: Mapping, enable: bool) -> Mapping {
}
}
// 当TUN启用时,将修改后的DNS配置写回
revise!(config, "dns", dns_val);
} else {
// TUN未启用时,仅恢复系统DNS,不修改配置文件中的DNS设置
#[cfg(target_os = "macos")]
AsyncHandler::spawn(move || async move {
crate::utils::resolve::dns::restore_public_dns().await;
});
}
// 更新TUN配置
revise!(tun_val, "enable", enable);
revise!(config, "tun", tun_val);
+4 -22
View File
@@ -24,15 +24,13 @@ pub struct LocalBackupFile {
pub content_length: u64,
}
/// Load restored verge.yaml from disk, merge back WebDAV creds, save, and sync memory.
/// Also reload other restored configs so restarts won't overwrite them.
/// Reloads restored configs into memory while preserving WebDAV credentials.
async fn finalize_restored_verge_config(
webdav_url: Option<String>,
webdav_username: Option<String>,
webdav_password: Option<String>,
) -> Result<()> {
// Do NOT silently fallback to defaults; a broken/missing verge.yaml means restore failed.
// Propagate the error so the UI/user can react accordingly.
// A broken restored `verge.yaml` is a restore failure, not a reason to load defaults.
let mut restored = help::read_yaml::<IVerge>(&verge_path()?).await?;
restored.webdav_url = webdav_url;
restored.webdav_username = webdav_username;
@@ -59,8 +57,7 @@ async fn finalize_restored_verge_config(
});
verge_draft.apply();
// Ensure side-effects (flags, tray, sysproxy, hotkeys, auto-backup refresh, etc.) run.
// Use not_save_file = true to avoid extra I/O (we already persisted the restored file).
// Run configuration side effects without rewriting the already-restored file.
if let Err(err) = super::patch_verge(&restored, true).await {
logging!(error, Type::Backup, "Failed to apply restored verge config: {err:#?}");
// Propagate unknown proxy state; ordinary side-effect failures stay logged.
@@ -71,7 +68,6 @@ async fn finalize_restored_verge_config(
Ok(())
}
/// Create a backup and upload to WebDAV
pub async fn create_backup_and_upload_webdav() -> Result<()> {
let (file_name, temp_file_path) = backup::create_backup().await.map_err(|err| {
logging!(error, Type::Backup, "Failed to create backup: {err:#?}");
@@ -83,7 +79,6 @@ pub async fn create_backup_and_upload_webdav() -> Result<()> {
.await
{
logging!(error, Type::Backup, "Failed to upload to WebDAV: {err:#?}");
// 上传失败时重置客户端缓存
backup::WebDavClient::global().reset();
return Err(err);
}
@@ -95,7 +90,6 @@ pub async fn create_backup_and_upload_webdav() -> Result<()> {
Ok(())
}
/// List WebDAV backups
pub async fn list_wevdav_backup() -> Result<Vec<ListFile>> {
backup::WebDavClient::global().list().await.map_err(|err| {
logging!(error, Type::Backup, "Failed to list WebDAV backup files: {err:#?}");
@@ -103,7 +97,6 @@ pub async fn list_wevdav_backup() -> Result<Vec<ListFile>> {
})
}
/// Delete WebDAV backup
pub async fn delete_webdav_backup(filename: String) -> Result<()> {
backup::WebDavClient::global().delete(filename).await.map_err(|err| {
logging!(error, Type::Backup, "Failed to delete WebDAV backup file: {err:#?}");
@@ -111,7 +104,6 @@ pub async fn delete_webdav_backup(filename: String) -> Result<()> {
})
}
/// Restore WebDAV backup
pub async fn restore_webdav_backup(filename: String) -> Result<()> {
let verge = Config::verge().await;
let verge_data = verge.latest_arc();
@@ -130,19 +122,16 @@ pub async fn restore_webdav_backup(filename: String) -> Result<()> {
err
})?;
// extract zip file
let value = backup_storage_path.clone();
let file = AsyncHandler::spawn_blocking(move || std::fs::File::open(&value)).await??;
let mut zip = zip::ZipArchive::new(file)?;
let _profile_write = crate::config::profiles::PROFILE_WRITE_LOCK.lock().await;
zip.extract(app_home_dir()?)?;
let res = finalize_restored_verge_config(webdav_url, webdav_username, webdav_password).await;
// Finally remove the temp file (attempt cleanup even if finalize fails)
let _ = backup_storage_path.remove_if_exists().await;
res
}
/// Create a backup and save to local storage
pub async fn create_local_backup() -> Result<()> {
create_local_backup_with_namer(|name| name.to_string().into())
.await
@@ -164,7 +153,6 @@ where
if let Err(err) = move_file(temp_file_path.clone(), target_path.clone()).await {
logging!(error, Type::Backup, "Failed to move local backup file: {err:#?}");
// 清理临时文件
if let Err(clean_err) = temp_file_path.remove_if_exists().await {
logging!(
warn,
@@ -178,7 +166,6 @@ where
Ok(final_name)
}
/// Import an existing backup file into the local backup directory
pub async fn import_local_backup(source: String) -> Result<String> {
let source_path = PathBuf::from(source.as_str());
if !source_path.exists() {
@@ -206,7 +193,6 @@ pub async fn import_local_backup(source: String) -> Result<String> {
let target_path = backup_dir.join(file_name);
if target_path == source_path {
// Already located in the backup directory
return Ok(file_name.to_string().into());
}
@@ -233,7 +219,7 @@ async fn move_file(from: PathBuf, to: PathBuf) -> Result<()> {
match fs::rename(&from, &to).await {
Ok(_) => Ok(()),
Err(rename_err) => {
// Attempt copy + remove as fallback, covering cross-device moves
// Rename can fail across filesystems; fall back to copy then remove.
logging!(
warn,
Type::Backup,
@@ -250,7 +236,6 @@ async fn move_file(from: PathBuf, to: PathBuf) -> Result<()> {
}
}
/// List local backups
pub async fn list_local_backup() -> Result<Vec<LocalBackupFile>> {
let backup_dir = local_backup_dir()?;
if !backup_dir.exists() {
@@ -286,7 +271,6 @@ pub async fn list_local_backup() -> Result<Vec<LocalBackupFile>> {
Ok(backups)
}
/// Delete local backup
pub async fn delete_local_backup(filename: String) -> Result<()> {
let backup_dir = local_backup_dir()?;
let target_path = backup_dir.join(filename.as_str());
@@ -298,7 +282,6 @@ pub async fn delete_local_backup(filename: String) -> Result<()> {
Ok(())
}
/// Restore local backup
pub async fn restore_local_backup(filename: String) -> Result<()> {
let backup_dir = local_backup_dir()?;
let target_path = backup_dir.join(filename.as_str());
@@ -324,7 +307,6 @@ pub async fn restore_local_backup(filename: String) -> Result<()> {
Ok(())
}
/// Export local backup file to user selected destination
pub async fn export_local_backup(filename: String, destination: String) -> Result<()> {
let backup_dir = local_backup_dir()?;
let source_path = backup_dir.join(filename.as_str());
+1 -11
View File
@@ -23,7 +23,6 @@ static TLS_CONFIG: Lazy<Arc<rustls::ClientConfig>> = Lazy::new(|| {
Arc::new(config)
});
/// Restart the Clash core
pub async fn restart_clash_core() {
match CoreManager::global().restart_core().await {
Ok(_) => {
@@ -37,10 +36,8 @@ pub async fn restart_clash_core() {
}
}
/// Restart the application
pub async fn restart_app() {
logging!(debug, Type::System, "启动重启应用流程");
// 设置退出标志
handle::Handle::global().set_is_exiting();
Config::apply_all_and_save_file().await;
@@ -84,14 +81,10 @@ fn after_change_clash_mode() {
});
}
/// Change Clash mode (rule/global/direct/script)
///
/// mihomo `/configs` PATCH 失败时返回 `Err`,以便命令层把失败上抛给前端。
/// (此前该函数吞掉错误并始终视为成功,导致 UI 误判"切换成功"、看似"切不动"。)
/// Propagates mihomo PATCH failures so the frontend can roll back its optimistic mode.
pub async fn change_clash_mode(mode: String) -> Result<(), String> {
let mut mapping = Mapping::new();
mapping.insert(Value::from("mode"), Value::from(mode.as_str()));
// Convert YAML mapping to JSON Value
let json_value = serde_json::json!({
"mode": mode
});
@@ -101,12 +94,10 @@ pub async fn change_clash_mode(mode: String) -> Result<(), String> {
return Err(err.to_string().into());
}
// 更新订阅
let clash = Config::clash().await;
clash.edit_draft(|d| d.patch_config(&mapping));
clash.apply();
// 分离数据获取和异步调用
let clash_data = clash.data_arc();
if clash_data.save_config().await.is_ok() {
handle::Handle::refresh_clash();
@@ -184,7 +175,6 @@ pub async fn test_delay(url: String) -> anyhow::Result<u32> {
let _ = stream.read(&mut buf).await?;
}
// frontend treats 0 as timeout
Ok((start.elapsed().as_millis() as u32).max(1))
})
.await
+2 -15
View File
@@ -14,13 +14,11 @@ use clash_verge_logging::{Type, logging};
use std::env;
use tauri_plugin_clipboard_manager::ClipboardExt as _;
/// Toggle system proxy, returning `None` when it does not change.
pub async fn toggle_system_proxy() -> Option<bool> {
let verge = Config::verge().await;
let current = verge.latest_arc().enable_system_proxy.unwrap_or(false);
let auto_close_connection = verge.latest_arc().auto_close_connection.unwrap_or(false);
// 如果当前系统代理即将关闭,且自动关闭连接设置为true,则关闭所有连接
if current
&& auto_close_connection
&& let Err(err) = handle::Handle::mihomo().close_all_connections().await
@@ -42,7 +40,6 @@ pub async fn toggle_system_proxy() -> Option<bool> {
.await;
match patch_result {
// Patch processing refreshes every caller.
Ok(_) => Some(requested),
Err(err) => {
logging!(error, Type::ProxyMode, "{err:#}");
@@ -52,7 +49,6 @@ pub async fn toggle_system_proxy() -> Option<bool> {
}
}
/// Notify only when a classified failure remains pending.
async fn report_toggle_failure(error: &anyhow::Error) {
let recorded = proxy_control::is_reportable(error);
@@ -69,8 +65,6 @@ const fn toggle_operation(requested: bool) -> FailedOperation {
}
}
/// Toggle TUN mode on/off
/// Returns the updated toggle state
pub async fn toggle_tun_mode(not_save_file: Option<bool>) -> bool {
let current = Config::verge().await.latest_arc().enable_tun_mode.unwrap_or(false);
let enable = !current;
@@ -86,11 +80,7 @@ pub async fn toggle_tun_mode(not_save_file: Option<bool>) -> bool {
{
Ok(_) => {
handle::Handle::refresh_verge();
// Read back rather than returning what was asked for: patching TUN reconciles it
// afterwards, and where TUN cannot work that reconciliation turns it straight off
// again. This path is not gated on availability — it is the global hotkey — so
// reporting the request would tell the caller TUN is on moments before the notice
// saying it was disabled.
// Reconciliation may immediately disable unavailable TUN; report the resulting state.
Config::verge().await.latest_arc().enable_tun_mode.unwrap_or(false)
}
Err(err) => {
@@ -100,7 +90,6 @@ pub async fn toggle_tun_mode(not_save_file: Option<bool>) -> bool {
}
}
/// Copy proxy environment variables to clipboard
pub async fn copy_clash_env() {
let env_ip = env::var("CLASH_VERGE_REV_IP").ok();
let verge_cfg = Config::verge().await.latest_arc();
@@ -109,9 +98,7 @@ pub async fn copy_clash_env() {
.unwrap_or_else(|| verge_cfg.proxy_host.as_deref().unwrap_or("127.0.0.1"));
let app_handle = handle::Handle::app_handle();
// The user is about to paste this into a shell, so it has to be the port the Core is
// really on — and this path is user-triggered, so a round-trip is affordable. It also
// used to fall back to a hardcoded 7897, ignoring the Merge Config entirely.
// Clipboard output must use the core's live port, including merge-config overrides.
let port = MixedPort::effective().await;
let http_proxy = format!("http://{ip}:{port}");
let socks5_proxy = format!("socks5://{ip}:{port}");
+2 -26
View File
@@ -6,27 +6,20 @@ use tauri_plugin_mihomo::models::WsConnectionId;
use tokio::sync::mpsc;
use tokio::time::Instant;
/// Mihomo WebSocket 流的有界队列容量,避免异常场景下内存无限增长。
/// Bounds memory use if the consumer falls behind the WebSocket.
const MIHOMO_WS_STREAM_BUFFER_SIZE: usize = 8;
/// 断开 Mihomo WebSocket 连接时使用的关闭码(RFC 6455 标准正常关闭)。
const MIHOMO_WS_STREAM_CLOSE_CODE: u64 = 1000;
/// `/traffic` 即时速率事件(字节/秒)。
#[derive(Debug, Clone, Copy)]
pub struct TrafficSpeedEvent {
pub up: u64,
pub down: u64,
}
/// Mihomo WebSocket 流消费状态。
pub enum StreamConsumeState<T> {
/// 收到一条业务事件。
Event(T),
/// 连接关闭或消息流结束。
Closed,
/// 在超时时间内未收到有效事件,需要重连。
Stale,
/// 上层请求退出消费循环。
ExitRequested,
}
@@ -34,13 +27,9 @@ enum InternalWsEvent<T> {
Data(T),
}
/// Mihomo WebSocket 订阅句柄(通用事件流)。
pub struct MihomoWsEventStream<T> {
/// 当前订阅连接 ID,用于主动断开。
pub connection_id: WsConnectionId,
/// 当前订阅消息接收器。
receiver: mpsc::Receiver<InternalWsEvent<T>>,
/// 最近一次收到有效事件的时间戳。
last_valid_event_at: Instant,
}
@@ -61,19 +50,15 @@ fn parse_traffic_event(data: &[u8]) -> Option<InternalWsEvent<TrafficSpeedEvent>
fn try_send_internal_event<T>(message_tx: &mpsc::Sender<InternalWsEvent<T>>, event: InternalWsEvent<T>) {
if let Err(err) = message_tx.try_send(event) {
match err {
// 队列满时丢弃本次事件,下一次事件会继续覆盖更新。
// A later real-time sample supersedes one dropped from a full queue.
tokio::sync::mpsc::error::TrySendError::Full(_) => {}
// 任务已结束时通道可能关闭,忽略即可。
tokio::sync::mpsc::error::TrySendError::Closed(_) => {}
}
}
}
/// 建立 `/traffic` WebSocket 订阅(通用流)。
pub async fn connect_traffic_stream() -> Result<MihomoWsEventStream<TrafficSpeedEvent>> {
// 使用有界 mpsc 通道承接回调事件,限制消息积压上限。
let (message_tx, message_rx) = mpsc::channel::<InternalWsEvent<TrafficSpeedEvent>>(MIHOMO_WS_STREAM_BUFFER_SIZE);
// 建立 Mihomo `/traffic` WebSocket 订阅。
let connection_id = handle::Handle::mihomo()
.ws_traffic({
let message_tx = message_tx.clone();
@@ -93,11 +78,6 @@ pub async fn connect_traffic_stream() -> Result<MihomoWsEventStream<TrafficSpeed
}
impl<T> MihomoWsEventStream<T> {
/// 等待下一次可用事件或结束状态。
///
/// # Arguments
/// * `stale_timeout` - 无有效事件超时时间
/// * `should_exit` - 上层退出判定函数
pub async fn next_event<F>(&mut self, stale_timeout: Duration, should_exit: F) -> StreamConsumeState<T>
where
F: Fn() -> bool,
@@ -132,10 +112,6 @@ impl<T> MihomoWsEventStream<T> {
}
}
/// 断开指定 Mihomo WebSocket 连接。
///
/// # Arguments
/// * `connection_id` - 目标连接 ID
pub async fn disconnect_connection(connection_id: WsConnectionId) {
if let Err(err) = handle::Handle::mihomo()
.disconnect(connection_id, Some(MIHOMO_WS_STREAM_CLOSE_CODE))
+1 -16
View File
@@ -24,7 +24,6 @@ pub static PROFILE_YAML: &str = "profiles.yaml";
/// Marks that the one-shot raise of too-short auto-update intervals has already run.
pub static UPDATE_INTERVAL_MIGRATED: &str = ".update-interval-migrated";
/// init portable flag
pub fn init_portable_flag() -> Result<()> {
use tauri::utils::platform::current_exe;
@@ -40,7 +39,6 @@ pub fn init_portable_flag() -> Result<()> {
Ok(())
}
/// get the verge app home dir
pub fn app_home_dir() -> Result<PathBuf> {
use tauri::utils::platform::current_exe;
@@ -54,7 +52,7 @@ pub fn app_home_dir() -> Result<PathBuf> {
return Ok(PathBuf::from(app_dir).join(".config").join(APP_ID));
}
// 避免在Handle未初始化时崩溃
// Directory helpers can run before the Tauri handle is initialized.
let app_handle = handle::Handle::app_handle();
match app_handle.path().data_dir() {
@@ -88,9 +86,7 @@ pub fn preinit_app_data_dir() -> Result<PathBuf> {
Ok(root.join(APP_ID))
}
/// get the resources dir
pub fn app_resources_dir() -> Result<PathBuf> {
// 避免在Handle未初始化时崩溃
let app_handle = handle::Handle::app_handle();
match app_handle.path().resource_dir() {
@@ -102,12 +98,10 @@ pub fn app_resources_dir() -> Result<PathBuf> {
}
}
/// profiles dir
pub fn app_profiles_dir() -> Result<PathBuf> {
Ok(app_home_dir()?.join("profiles"))
}
/// icons dir
pub fn app_icons_dir() -> Result<PathBuf> {
Ok(app_home_dir()?.join("icons"))
}
@@ -131,29 +125,24 @@ pub fn find_target_icons(target: &str) -> Result<Option<String>> {
icon_path.map(|path| path_to_str(&path).map(|s| s.into())).transpose()
}
/// logs dir
pub fn app_logs_dir() -> Result<PathBuf> {
Ok(app_home_dir()?.join("logs"))
}
/// service logs dir
#[cfg(target_os = "macos")]
pub fn service_logs_root_dir() -> Result<PathBuf> {
Ok(app_home_dir()?.join("service-logs"))
}
/// service logs dir
#[cfg(not(target_os = "macos"))]
pub fn service_logs_root_dir() -> Result<PathBuf> {
app_logs_dir()
}
// latest verge log
pub fn app_latest_log() -> Result<PathBuf> {
Ok(app_logs_dir()?.join("latest.log"))
}
/// local backups dir
pub fn local_backup_dir() -> Result<PathBuf> {
let dir = app_home_dir()?.join(BACKUP_DIR);
fs::create_dir_all(&dir)?;
@@ -222,18 +211,14 @@ pub fn get_encryption_key() -> Result<Vec<u8>> {
let key_path = app_dir.join(".encryption_key");
if key_path.exists() {
// Read existing key
fs::read(&key_path).map_err(|e| anyhow::anyhow!("Failed to read encryption key: {}", e))
} else {
// Generate and save new key
let mut key = vec![0u8; 32];
getrandom::fill(&mut key)?;
// Ensure directory exists
if let Some(parent) = key_path.parent() {
fs::create_dir_all(parent).map_err(|e| anyhow::anyhow!("Failed to create key directory: {}", e))?;
}
// Save key
fs::write(&key_path, &key).map_err(|e| anyhow::anyhow!("Failed to save encryption key: {}", e))?;
Ok(key)
}
+3 -27
View File
@@ -9,7 +9,6 @@ use std::{
str::FromStr,
};
/// read data from yaml as struct T
pub async fn read_yaml<T: DeserializeOwned>(path: &PathBuf) -> Result<T> {
if !tokio::fs::try_exists(path).await.unwrap_or(false) {
bail!("file not found \"{}\"", path.display());
@@ -20,7 +19,6 @@ pub async fn read_yaml<T: DeserializeOwned>(path: &PathBuf) -> Result<T> {
Ok(with_encryption(|| async { serde_yaml_ng::from_str::<T>(&yaml_str) }).await?)
}
/// read mapping from yaml
pub async fn read_mapping(path: &PathBuf) -> Result<Mapping> {
if !tokio::fs::try_exists(path).await.unwrap_or(false) {
bail!("file not found \"{}\"", path.display());
@@ -30,7 +28,6 @@ pub async fn read_mapping(path: &PathBuf) -> Result<Mapping> {
.await
.with_context(|| format!("failed to read the file \"{}\"", path.display()))?;
// YAML语法检查
match serde_yaml_ng::from_str::<serde_yaml_ng::Value>(&yaml_str) {
Ok(mut val) => {
val.apply_merge()
@@ -52,13 +49,10 @@ pub async fn read_mapping(path: &PathBuf) -> Result<Mapping> {
}
}
/// read mapping from yaml fix #165
pub async fn read_seq_map(path: &PathBuf) -> Result<SeqMap> {
read_yaml(path).await
}
/// save the data to the file
/// can set `prefix` string to add some comments
pub async fn save_yaml<T: Serialize + Sync>(path: &PathBuf, data: &T, prefix: Option<&str>) -> Result<()> {
let data_str = with_encryption(|| async { serde_yaml_ng::to_string(data) }).await?;
@@ -90,14 +84,11 @@ const ALPHABET: [char; 62] = [
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
];
/// generate the uid
pub fn get_uid(prefix: &str) -> String {
let id = nanoid!(11, &ALPHABET);
format!("{prefix}{id}")
}
/// parse the string
/// xxx=123123; => 123123
pub fn parse_str<T: FromStr>(target: &str, key: &str) -> Option<T> {
target.split(';').map(str::trim).find_map(|s| {
let mut parts = s.splitn(2, '=');
@@ -108,18 +99,13 @@ pub fn parse_str<T: FromStr>(target: &str, key: &str) -> Option<T> {
})
}
/// Mask sensitive parts of a subscription URL for safe logging.
/// Examples:
/// - `https://example.com/api/v1/clash?token=abc123` → `https://example.com/api/v1/clash?token=***`
/// - `https://example.com/abc123def456ghi789/clash` → `https://example.com/***/clash`
/// Masks query values and token-like path segments for logging.
pub fn mask_url(url: &str) -> String {
// Split off query string
let (path_part, query_part) = match url.find('?') {
Some(pos) => (&url[..pos], Some(&url[pos + 1..])),
None => (url, None),
};
// Extract scheme+host prefix (everything up to the first '/' after "://")
let host_end = path_part
.find("://")
.and_then(|scheme_end| {
@@ -134,7 +120,6 @@ pub fn mask_url(url: &str) -> String {
let mut result = scheme_and_host.to_owned();
// Mask path segments that look like tokens (longer than 16 chars)
if !path.is_empty() {
let masked: Vec<&str> = path
.split('/')
@@ -143,7 +128,6 @@ pub fn mask_url(url: &str) -> String {
result.push_str(&masked.join("/"));
}
// Keep query param keys, mask values
if let Some(query) = query_part {
result.push('?');
let masked_query: Vec<String> = query
@@ -159,11 +143,7 @@ pub fn mask_url(url: &str) -> String {
result
}
/// Mask all URLs embedded in an error/log string for safe logging.
///
/// Scans the string for `http://` or `https://` and replaces each URL
/// (terminated by whitespace or `)`, `]`, `"`, `'`) with its masked form.
/// Text between URLs is copied verbatim.
/// Masks every HTTP(S) URL embedded in a log string while preserving surrounding text.
pub fn mask_err(err: &str) -> String {
let mut result = String::with_capacity(err.len());
let mut remaining = err;
@@ -194,9 +174,8 @@ pub fn mask_err(err: &str) -> String {
result
}
/// get the last part of the url, if not found, return empty string
pub fn get_last_part_and_decode(url: &str) -> Option<String> {
let path = url.split('?').next().unwrap_or(""); // Splits URL and takes the path part
let path = url.split('?').next().unwrap_or("");
let segments: Vec<&str> = path.split('/').collect();
let last_segment = segments.last()?;
@@ -207,7 +186,6 @@ pub fn get_last_part_and_decode(url: &str) -> Option<String> {
)
}
/// open file
pub fn open_file(path: PathBuf) -> Result<()> {
open::that_detached(path.as_os_str())?;
Ok(())
@@ -245,7 +223,6 @@ pub fn linux_elevator() -> String {
match Command::new("which").arg("pkexec").output() {
Ok(output) => {
if !output.stdout.is_empty() {
// Convert the output to a string slice
if let Ok(path) = std::str::from_utf8(&output.stdout) {
path.trim().to_string()
} else {
@@ -260,7 +237,6 @@ pub fn linux_elevator() -> String {
}
#[cfg(target_os = "windows")]
/// copy the file to the dist path and return the dist path
pub fn snapshot_path(original_path: &Path) -> Result<PathBuf> {
let temp_dir = original_path
.parent()
+3 -24
View File
@@ -1,4 +1,3 @@
// #[cfg(not(feature = "tracing"))]
use crate::{
config::{Config, IClashTemp, IProfiles, IVerge},
constants,
@@ -43,7 +42,6 @@ async fn delete_snapshot_logs(log_dir: &Path) -> Result<()> {
}
// TODO flexi_logger 提供了最大保留天数,或许我们应该用内置删除log文件
/// 删除log文件
pub async fn delete_log() -> Result<()> {
let log_dir = dirs::app_logs_dir()?;
let service_log_dir = dirs::service_log_dir()?;
@@ -63,7 +61,6 @@ pub async fn delete_log() -> Result<()> {
verge.auto_log_clean.unwrap_or(0)
};
// 1: 1天, 2: 7天, 3: 30天, 4: 90天
let day = match auto_log_clean {
1 => 1,
2 => 7,
@@ -74,7 +71,6 @@ pub async fn delete_log() -> Result<()> {
logging!(info, Type::Setup, "try to delete log files, day: {}", day);
// %Y-%m-%d to NaiveDateTime
let parse_time_str = |s: &str| {
let sa: Vec<&str> = s.split('-').collect();
if sa.len() != 4 {
@@ -320,14 +316,12 @@ async fn migrate_legacy_macos_logs() -> Result<()> {
Ok(())
}
/// 初始化DNS配置文件
pub(super) async fn init_dns_config() -> Result<()> {
use serde_yaml_ng::Value;
// 创建DNS子配置
let dns_config = serde_yaml_ng::Mapping::from_iter([
("enable".into(), Value::Bool(true)),
// 与前端 DNS 默认一致,并供 dns.ipv6 锁定使用
// Must match the frontend default used for authoritative `dns.ipv6`.
("ipv6".into(), Value::Bool(true)),
("listen".into(), Value::String(":53".into())),
("enhanced-mode".into(), Value::String("fake-ip".into())),
@@ -410,13 +404,11 @@ pub(super) async fn init_dns_config() -> Result<()> {
),
]);
// 获取默认DNS和host配置
let default_dns_config = serde_yaml_ng::Mapping::from_iter([
("dns".into(), Value::Mapping(dns_config)),
("hosts".into(), Value::Mapping(serde_yaml_ng::Mapping::new())),
]);
// 检查DNS配置文件是否存在
let app_dir = dirs::app_home_dir()?;
let dns_path = app_dir.join(constants::files::DNS_CONFIG);
@@ -428,7 +420,6 @@ pub(super) async fn init_dns_config() -> Result<()> {
Ok(())
}
/// 确保目录结构存在
async fn ensure_directories() -> Result<()> {
let directories = [
("app_home", dirs::app_home_dir()?),
@@ -449,7 +440,6 @@ async fn ensure_directories() -> Result<()> {
Ok(())
}
/// 初始化配置文件
async fn initialize_config_files() -> Result<()> {
if let Ok(path) = dirs::clash_path()
&& !path.exists()
@@ -481,7 +471,6 @@ async fn initialize_config_files() -> Result<()> {
logging!(info, Type::Setup, "Created profiles config at {:?}", path);
}
// 验证并修正verge配置
IVerge::validate_and_fix_config()
.await
.map_err(|e| anyhow::anyhow!("Failed to validate verge config: {}", e))?;
@@ -489,14 +478,8 @@ async fn initialize_config_files() -> Result<()> {
Ok(())
}
/// Initialize all the config files
/// before tauri setup
/// Initializes configuration required before Tauri setup.
pub async fn init_config() -> Result<()> {
// We do not need init_portable_flag here anymore due to lib.rs will to the things
// let _ = dirs::init_portable_flag();
// We do not need init_log here anymore due to resolve will to the things
#[cfg(target_os = "macos")]
migrate_legacy_macos_logs().await?;
@@ -514,8 +497,7 @@ pub async fn init_config() -> Result<()> {
Ok(())
}
/// initialize app resources
/// after tauri setup
/// Initializes application resources after Tauri setup.
pub async fn init_resources() -> Result<()> {
let app_dir = dirs::app_home_dir()?;
let res_dir = dirs::app_resources_dir()?;
@@ -529,8 +511,6 @@ pub async fn init_resources() -> Result<()> {
let file_list = ["Country.mmdb", "geoip.dat", "geosite.dat"];
// copy the resource file
// if the source file is newer than the destination file, copy it over
for file in file_list.iter() {
let src_path = res_dir.join(file);
let dest_path = app_dir.join(file);
@@ -559,7 +539,6 @@ pub async fn init_resources() -> Result<()> {
Ok(())
}
/// initialize url scheme
#[cfg(target_os = "windows")]
pub fn init_scheme() -> Result<()> {
use tauri::utils::platform::current_exe;
+9 -37
View File
@@ -4,17 +4,15 @@ use tauri::webview::PageLoadEvent;
use tauri::{Theme, WebviewWindow};
use crate::{config::Config, core::handle, utils::resolve::window_script::build_window_initial_script};
use clash_verge_logging::{Type, logging_error};
// logging 仅在 macOS 的渲染进程恢复逻辑中使用
#[cfg(target_os = "macos")]
use clash_verge_logging::logging;
use clash_verge_logging::{Type, logging_error};
const DARK_BACKGROUND_COLOR: Color = Color(46, 48, 61, 255); // #2E303D
const LIGHT_BACKGROUND_COLOR: Color = Color(245, 245, 245, 255); // #F5F5F5
const DARK_BACKGROUND_HEX: &str = "#2E303D";
const LIGHT_BACKGROUND_HEX: &str = "#F5F5F5";
// 定义默认窗口尺寸常量
const DEFAULT_WIDTH: f64 = 940.0;
const DEFAULT_HEIGHT: f64 = 700.0;
@@ -46,7 +44,6 @@ fn restore_default_size_if_needed(window: &WebviewWindow) {
logging_error!(Type::Window, window.center());
}
/// 构建新的 WebView 窗口
pub async fn build_new_window() -> Result<WebviewWindow, String> {
let app_handle = handle::Handle::app_handle();
@@ -112,7 +109,7 @@ pub async fn build_new_window() -> Result<WebviewWindow, String> {
Ok(window) => {
logging_error!(Type::Window, window.set_background_color(Some(background_color)));
restore_default_size_if_needed(&window);
// 全新窗口的页面即为最新状态,丢弃旧窗口遗留的待重载标记,避免多余 reload
// A new page supersedes any reload marker left by the old window.
#[cfg(target_os = "macos")]
take_webview_needs_reload();
Ok(window)
@@ -121,40 +118,19 @@ pub async fn build_new_window() -> Result<WebviewWindow, String> {
}
}
/// 渲染进程死亡、页面待重载标记(macOS)
///
/// 渲染进程在窗口不可见时被系统终止后置位,由下次激活窗口的路径取走并执行 reload。
/// Defers recovery of a terminated hidden main webview until its next activation.
#[cfg(target_os = "macos")]
static WEBVIEW_NEEDS_RELOAD: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// 取出并清除"页面待重载"标记
///
/// # Returns
/// * `bool` - 渲染进程是否曾在窗口不可见时被系统终止、页面需要 reload
#[cfg(target_os = "macos")]
pub fn take_webview_needs_reload() -> bool {
WEBVIEW_NEEDS_RELOAD.swap(false, std::sync::atomic::Ordering::SeqCst)
}
/// WebView 渲染进程被系统终止后的恢复处理(macOS)
///
/// macOS 在内存压力下可能杀掉 WKWebView 的 WebContent 渲染进程:
/// 1. 页面内容层消失,窗口打开后表现为白屏;
/// 2. 前端 JS 状态随之丢失,无法调用 `ws_disconnect` 清理 Mihomo WebSocket 订阅,
/// 孤儿订阅持续把大于 1KB 的 payload(如 `/connections` 全量快照)塞进 tauri 的
/// `ChannelDataIpcQueue`,且没有存活的页面来取走,导致主进程内存无限增长。
///
/// 恢复策略:
/// * 窗口可见(前台被杀的罕见场景)——立即 reload 恢复页面;
/// * 窗口隐藏/最小化(托盘常驻的常见场景)——只置位待重载标记,等用户下次打开
/// 窗口时再 reload。系统正是因内存压力才杀掉不可见窗口的渲染进程,此时立即
/// 重建渲染进程既浪费内存,也可能形成"系统杀→拉起→再杀"的循环。
///
/// 注意:应用层注册 `on_web_content_process_terminate` 后会覆盖 tauri-runtime-wry
/// 的默认自动 reload 行为,因此页面死亡状态会一直保持到我们主动 reload。
///
/// # Arguments
/// * `webview` - 渲染进程被终止的 WebView
/// macOS may kill hidden WebContent under memory pressure. Clean orphaned Mihomo subscriptions,
/// reload visible webviews immediately, and defer a hidden main-window reload until activation.
/// Registering this callback replaces Tauri's default automatic reload.
#[cfg(target_os = "macos")]
pub fn on_web_content_process_terminated(webview: &tauri::Webview) {
if handle::Handle::global().is_exiting() {
@@ -171,18 +147,16 @@ pub fn on_web_content_process_terminated(webview: &tauri::Webview) {
let window = webview.window();
let is_user_visible = window.is_visible().unwrap_or(false) && !window.is_minimized().unwrap_or(false);
// 懒重载标记仅供主窗口;其它 webview(update-splash)无消费通道,不可见时直接 reload 兜底
// Only the main window has a path that consumes the deferred marker.
let is_main_window = webview.label() == "main";
let reload_now = is_user_visible || !is_main_window;
if !reload_now {
// 主窗口不可见:置标记,延迟到下次 activate_window / reload_main_window_if_needed 再 reload
WEBVIEW_NEEDS_RELOAD.store(true, std::sync::atomic::Ordering::SeqCst);
logging!(info, Type::Window, "窗口不可见,页面将在下次打开窗口时重载");
}
// 清理全部 Mihomo WS 订阅,阻断 ChannelDataIpcQueue 泄漏(托盘速率任务约 1s 后自重连)。
// reload 必须与清理同任务、排在其后,否则清理可能误清重载后新页面的订阅(竞态)。
// Clean before reload so cleanup cannot remove subscriptions created by the new page.
let webview = webview.clone();
crate::process::AsyncHandler::spawn(move || async move {
if let Err(err) = handle::Handle::mihomo().clear_all_ws_connections().await {
@@ -196,9 +170,7 @@ pub fn on_web_content_process_terminated(webview: &tauri::Webview) {
});
}
/// 消费待重载标记并 reload 主窗口(macOS)。
/// 兜底原生取消最小化(Dock 缩略图/调度中心/窗口菜单)只触发 Focused(true)、不走
/// activate_window 的情况。与 activate_window 共用 swap 标记,先取先得、不重复 reload。
/// Consumes the shared marker for native unminimize paths that bypass `activate_window`.
#[cfg(target_os = "macos")]
pub fn reload_main_window_if_needed() {
if !take_webview_needs_reload() {
+1 -16
View File
@@ -1,19 +1,4 @@
/// Macro to generate singleton pattern for structs
///
/// Usage:
/// ```rust,ignore
/// use crate::utils::singleton::singleton;
///
/// struct MyStruct {
/// value: i32,
/// }
/// impl MyStruct {
/// fn new() -> Self {
/// MyStruct { value: 0 }
/// }
/// }
/// singleton!(MyStruct, INSTANCE);
/// ```
/// Implements `global()` using the supplied `OnceCell` static.
#[macro_export]
macro_rules! singleton {
($struct_name:ty, $instance_name:ident) => {
+1 -12
View File
@@ -1,10 +1,6 @@
//! 网络速率格式化工具
/// 速率显示升档阈值:保证显示值不超过三位数(显示层约定,与换算基数无关)
/// Promote units before the rendered value exceeds three digits.
const SPEED_DISPLAY_THRESHOLD: f64 = 1000.0;
/// 速率展示单位顺序
const SPEED_UNITS: [&str; 5] = ["B/s", "K/s", "M/s", "G/s", "T/s"];
/// 预计算 1024 的幂次方,避免运行时重复计算 pow
const SCALES: [f64; 5] = [
1.0,
1024.0,
@@ -13,10 +9,6 @@ const SCALES: [f64; 5] = [
1024.0 * 1024.0 * 1024.0 * 1024.0,
];
/// 将字节/秒格式化为可读速率字符串
///
/// # Arguments
/// * `bytes_per_sec` - 每秒字节数
pub fn format_bytes_per_second(bytes_per_sec: u64) -> String {
if bytes_per_sec < SPEED_DISPLAY_THRESHOLD as u64 {
return format!("{bytes_per_sec}B/s");
@@ -47,7 +39,6 @@ mod tests {
fn format_handles_byte_boundaries() {
assert_eq!(format_bytes_per_second(0), "0B/s");
assert_eq!(format_bytes_per_second(999), "999B/s");
// 1000 >= SPEED_DISPLAY_THRESHOLD,升档为 K/s(保证不超过三位数)
assert_eq!(format_bytes_per_second(1000), "1.0K/s");
assert_eq!(format_bytes_per_second(1024), "1.0K/s");
}
@@ -55,7 +46,6 @@ mod tests {
#[test]
fn format_handles_decimal_and_integer_rules() {
assert_eq!(format_bytes_per_second(9 * 1024), "9.0K/s");
// 9.999 K/s:rounded_1dp = 10.0,不满足 < 10,应显示整数 "10K/s"
assert_eq!(format_bytes_per_second(10 * 1024 - 1), "10K/s");
assert_eq!(format_bytes_per_second(10 * 1024), "10K/s");
assert_eq!(format_bytes_per_second(123 * 1024), "123K/s");
@@ -63,7 +53,6 @@ mod tests {
#[test]
fn format_handles_unit_promotion_after_rounding() {
// 999.5 K/s 四舍五入为 1000,≥ SPEED_DISPLAY_THRESHOLD,升档为 1.0M/s
assert_eq!(format_bytes_per_second(999 * 1024 + 512), "1.0M/s");
assert_eq!(format_bytes_per_second(1024 * 1024), "1.0M/s");
assert_eq!(format_bytes_per_second(1536 * 1024), "1.5M/s");
-8
View File
@@ -1,6 +1,3 @@
//! Some config file template
/// template for new a profile item
pub const ITEM_LOCAL: &str = "# Profile Template for Clash Verge
proxies: []
@@ -10,7 +7,6 @@ proxy-groups: []
rules: []
";
/// enhanced profile
pub const ITEM_MERGE: &str = "# Profile Enhancement Merge Template for Clash Verge
profile:
@@ -21,7 +17,6 @@ pub const ITEM_MERGE_EMPTY: &str = "# Profile Enhancement Merge Template for Cla
";
/// enhanced profile
pub const ITEM_SCRIPT: &str = "// Define main function (script entry)
function main(config, profileName) {
@@ -29,7 +24,6 @@ function main(config, profileName) {
}
";
/// enhanced profile
pub const ITEM_RULES: &str = "# Profile Enhancement Rules Template for Clash Verge
prepend: []
@@ -39,7 +33,6 @@ append: []
delete: []
";
/// enhanced profile
pub const ITEM_PROXIES: &str = "# Profile Enhancement Proxies Template for Clash Verge
prepend: []
@@ -49,7 +42,6 @@ append: []
delete: []
";
/// enhanced profile
pub const ITEM_GROUPS: &str = "# Profile Enhancement Groups Template for Clash Verge
prepend: []
+5 -46
View File
@@ -1,7 +1,4 @@
//! macOS 托盘速率富文本渲染模块
//!
//! 通过 objc2 调用 NSAttributedString 实现托盘速率的富文本显示,
//! 支持等宽字体、跟随菜单栏外观配色、两行定宽布局。
//! macOS tray speed rendering via `NSAttributedString`.
use std::cell::RefCell;
@@ -17,36 +14,24 @@ use objc2_app_kit::{
};
use objc2_foundation::{NSAttributedString, NSDictionary, NSNumber, NSString};
/// 富文本渲染使用的字号(适配两行在托盘栏的高度)
const TRAY_FONT_SIZE: f64 = 9.5;
/// 两行文本的固定行高,避免不同菜单栏高度/缩放下使用系统默认行高导致裁剪
/// Fixed metrics prevent clipping across menu-bar scales.
const TRAY_LINE_HEIGHT: f64 = 10.0;
/// 两行文本的行间距
const TRAY_LINE_SPACING: f64 = 0.0;
/// 两行文本整体行高倍数(用于进一步压缩文本块高度)
const TRAY_LINE_HEIGHT_MULTIPLE: f64 = 1.00;
/// 文本块段前偏移(用于将两行文本整体下移)
const TRAY_PARAGRAPH_SPACING_BEFORE: f64 = 0.0;
/// 基线基准位移按字体上沿比例生成(避免硬编码常量)
const TRAY_BASELINE_OFFSET_GLYPH_HEIGHT_RATIO: f64 = 3.0;
/// Tauri tray-icon 将图标缩放为 18pt;这里额外预留图标、图文间距与系统内边距
/// Tauri uses an 18pt icon; reserve room for the icon and system padding.
const TRAY_STATUS_ITEM_EXTRA_WIDTH: f64 = 30.0;
/// 典型 6 字符速率文本的最小宽度,避免 0B/s 等短文本让状态项反复收缩
/// Avoid status-item width jitter for short values such as `0B/s`.
const TRAY_STATUS_ITEM_MIN_LENGTH: f64 = 58.0;
/// AppKit 的 NSVariableStatusItemLength。清空速率标题后恢复系统按图标自适应
const NS_VARIABLE_STATUS_ITEM_LENGTH: f64 = -1.0;
thread_local! {
static LAST_DISPLAY_STR: RefCell<String> = const { RefCell::new(String::new()) };
}
/// 将上行/下行速率格式化为两行定宽文本
///
/// # Arguments
/// * `up` - 上行速率(字节/秒)
/// * `down` - 下行速率(字节/秒)
fn format_tray_speed(up: u64, down: u64) -> String {
// 上行放在第一行,下行放在第二行;通过上下布局表达方向,不再显示箭头字符。
let up_str = format_bytes_per_second(up);
let down_str = format_bytes_per_second(down);
format!("{:>6}\n{:>6}", up_str, down_str)
@@ -54,11 +39,8 @@ fn format_tray_speed(up: u64, down: u64) -> String {
fn build_attributes(button_height: f64) -> Retained<NSDictionary<NSString, AnyObject>> {
unsafe {
// 等宽系统字体,确保数字不跳动
let font = NSFont::monospacedSystemFontOfSize_weight(TRAY_FONT_SIZE, NSFontWeightRegular);
// 与 NSStatusBarButton 原生标题一致,并按每块屏幕的菜单栏外观动态解析颜色
let color = NSColor::controlTextColor();
// 段落样式:右对齐,保证定宽视觉一致
let para_style = NSMutableParagraphStyle::new();
para_style.setAlignment(NSTextAlignment::Right);
para_style.setLineBreakMode(NSLineBreakMode::ByClipping);
@@ -83,11 +65,6 @@ fn build_attributes(button_height: f64) -> Retained<NSDictionary<NSString, AnyOb
}
}
/// 创建带属性的富文本
///
/// # Arguments
/// * `text` - 富文本字符串内容
/// * `attrs` - 可选富文本属性字典(None 表示用默认属性)
fn create_attributed_string(
text: &NSString,
attrs: Option<&NSDictionary<NSString, AnyObject>>,
@@ -111,15 +88,7 @@ fn sync_click_target_frame(button: &NSStatusBarButton) {
}
}
/// 在主线程下设置 NSStatusItem 按钮的标题内容
///
/// 依赖 Tauri `with_inner_tray_icon` 保证回调在主线程执行;
/// 若意外在非主线程调用,`MainThreadMarker::new()` 返回 `None` 并记录警告。
///
/// # Arguments
/// * `status_item` - macOS 托盘 NSStatusItem 引用
/// * `text` - 标题字符串内容
/// * `show_speed` - 是否以速率富文本样式绘制;false 时清空为普通空标题
/// `with_inner_tray_icon` should run this on the main thread; fail safely if it does not.
fn apply_status_item_attributed_title(status_item: &NSStatusItem, text: &NSString, show_speed: bool) {
let Some(mtm) = MainThreadMarker::new() else {
logging!(warn, Type::Tray, "托盘速率富文本设置跳过:非主线程调用");
@@ -141,12 +110,6 @@ fn apply_status_item_attributed_title(status_item: &NSStatusItem, text: &NSStrin
sync_click_target_frame(&button);
}
/// 将速率以富文本形式设置到 NSStatusItem 的按钮上
///
/// # Arguments
/// * `status_item` - macOS 托盘 NSStatusItem 引用
/// * `up` - 上行速率(字节/秒)
/// * `down` - 下行速率(字节/秒)
pub fn set_speed_attributed_title(status_item: &NSStatusItem, up: u64, down: u64) {
let speed_text = format_tray_speed(up, down);
LAST_DISPLAY_STR.with(|last| {
@@ -160,10 +123,6 @@ pub fn set_speed_attributed_title(status_item: &NSStatusItem, up: u64, down: u64
});
}
/// 清除 NSStatusItem 按钮上的富文本速率显示
///
/// # Arguments
/// * `status_item` - macOS 托盘 NSStatusItem 引用
pub fn clear_speed_attributed_title(status_item: &NSStatusItem) {
LAST_DISPLAY_STR.with(|last| {
last.borrow_mut().clear();
+2 -37
View File
@@ -6,39 +6,25 @@ use std::pin::Pin;
use std::time::Duration;
use tauri::{Manager as _, WebviewWindow, Wry};
/// 窗口操作结果
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum WindowOperationResult {
/// 窗口已显示并获得焦点
Shown,
/// 窗口已隐藏
Hidden,
/// 创建了新窗口
Created,
/// 摧毁了窗口
Destroyed,
/// 操作失败
Failed,
/// 无需操作
NoAction,
}
/// 窗口状态
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum WindowState {
/// 窗口可见且有焦点
VisibleFocused,
/// 窗口可见但无焦点
VisibleUnfocused,
/// 窗口最小化
Minimized,
/// 窗口隐藏
Hidden,
/// 窗口不存在
NotExist,
}
// 窗口操作防抖机制
const WINDOW_OPERATION_DEBOUNCE_MS: u64 = 625;
static WINDOW_OPERATION_LIMITER: Lazy<Limiter> = Lazy::new(|| {
Limiter::new(
@@ -55,7 +41,6 @@ fn should_handle_window_operation() -> bool {
allow
}
/// 统一的窗口管理器
pub struct WindowManager;
impl WindowManager {
@@ -112,15 +97,12 @@ impl WindowManager {
}
}
/// 获取主窗口实例
pub fn get_main_window() -> Option<WebviewWindow<Wry>> {
let app_handle = handle::Handle::app_handle();
app_handle.get_webview_window("main")
}
/// 智能显示主窗口
pub async fn show_main_window() -> WindowOperationResult {
// 防抖检查
if !should_handle_window_operation() {
return WindowOperationResult::NoAction;
}
@@ -161,7 +143,6 @@ impl WindowManager {
}
}
/// 切换主窗口显示状态(显示/隐藏)
pub async fn toggle_main_window() -> WindowOperationResult {
if !should_handle_window_operation() {
return WindowOperationResult::NoAction;
@@ -178,10 +159,8 @@ impl WindowManager {
}
}
// 窗口不存在时创建新窗口
async fn handle_not_exist_toggle() -> WindowOperationResult {
logging!(info, Type::Window, "窗口不存在,将创建新窗口");
// 由于已经有防抖保护,直接调用内部方法
if Self::create_window(true).await {
WindowOperationResult::Created
} else {
@@ -189,7 +168,6 @@ impl WindowManager {
}
}
// 隐藏主窗口
fn hide_main_window(window: Option<&WebviewWindow<Wry>>) -> WindowOperationResult {
logging!(info, Type::Window, "窗口可见,将隐藏窗口");
if let Some(window) = window {
@@ -209,7 +187,6 @@ impl WindowManager {
}
}
// 激活已存在的主窗口
fn activate_existing_main_window(window: Option<&WebviewWindow<Wry>>) -> WindowOperationResult {
logging!(info, Type::Window, "窗口存在但被隐藏或最小化,将激活窗口");
if let Some(window) = window {
@@ -220,14 +197,12 @@ impl WindowManager {
}
}
/// 激活窗口(取消最小化、显示、设置焦点)
fn activate_window(window: &WebviewWindow<Wry>) -> WindowOperationResult {
logging!(info, Type::Window, "开始激活窗口");
#[cfg(target_os = "macos")]
Self::set_macos_activation_policy_regular();
// 渲染进程曾被系统终止:先 reload,并把 show+focus 交给 on_page_load(Finished),
// 内容就绪再显示,避免白屏闪烁。reload 成功才 defer,失败则走下方直接显示。
// After renderer termination, defer show/focus until reload finishes to avoid a white flash.
#[allow(unused_mut)]
let mut defer_show_to_page_load = false;
#[cfg(target_os = "macos")]
@@ -241,7 +216,6 @@ impl WindowManager {
let mut operations_successful = true;
// 1. 如果窗口最小化,先取消最小化
if window.is_minimized().unwrap_or(false) {
logging!(info, Type::Window, "窗口已最小化,正在取消最小化");
if let Err(e) = window.unminimize() {
@@ -250,7 +224,6 @@ impl WindowManager {
}
}
// 2/3. 显示 + 焦点(reload 分支跳过,交给 on_page_load)
if !defer_show_to_page_load {
if let Err(e) = window.show() {
logging!(warn, Type::Window, "显示窗口失败: {}", e);
@@ -264,11 +237,9 @@ impl WindowManager {
#[cfg(target_os = "windows")]
{
// Windows 尝试额外的激活方法
if let Err(e) = window.set_always_on_top(true) {
logging!(debug, Type::Window, "设置置顶失败(非关键错误): {}", e);
}
// 立即取消置顶
if let Err(e) = window.set_always_on_top(false) {
logging!(debug, Type::Window, "取消置顶失败(非关键错误): {}", e);
}
@@ -283,23 +254,19 @@ impl WindowManager {
}
}
/// 检查窗口是否可见
pub fn is_main_window_visible(window: Option<&WebviewWindow<Wry>>) -> bool {
window.map(|w| w.is_visible().unwrap_or(false)).unwrap_or(false)
}
/// 检查窗口是否有焦点
pub fn is_main_window_focused(window: Option<&WebviewWindow<Wry>>) -> bool {
window.map(|w| w.is_focused().unwrap_or(false)).unwrap_or(false)
}
/// 检查窗口是否最小化
pub fn is_main_window_minimized(window: Option<&WebviewWindow<Wry>>) -> bool {
window.map(|w| w.is_minimized().unwrap_or(false)).unwrap_or(false)
}
/// 创建新窗口,防抖避免重复调用
/// 窗口创建后保持隐藏,由前端 index.html 在 overlay 渲染后调用 show,避免主题闪烁
/// Keep new windows hidden until the frontend overlay renders, avoiding a theme flash.
pub fn create_window(should_create: bool) -> Pin<Box<dyn Future<Output = bool> + Send>> {
Box::pin(async move {
logging!(info, Type::Window, "开始创建主窗口, should_create={}", should_create);
@@ -325,7 +292,6 @@ impl WindowManager {
})
}
/// 摧毁窗口
pub fn destroy_main_window() -> WindowOperationResult {
if let Some(window) = Self::get_main_window() {
let _ = window.destroy();
@@ -340,7 +306,6 @@ impl WindowManager {
WindowOperationResult::Failed
}
/// 获取详细的窗口状态信息
fn get_window_status_info() -> String {
let (window, state) = Self::get_main_window_with_state();
let is_visible = Self::is_main_window_visible(window.as_ref());