refactor: invock mihomo api by use tauri-plugin-mihomo (#4926)

* feat: add tauri-plugin-mihomo

* refactor: invock mihomo api by use tauri-plugin-mihomo

* chore: todo

* chore: update

* chore: update

* chore: update

* chore: update

* fix: incorrect delay status and update pretty config

* chore: update

* chore: remove cache

* chore: update

* chore: update

* fix: app freezed when change group proxy

* chore: update

* chore: update

* chore: add rustfmt.toml to tauri-plugin-mihomo

* chore: happy clippy

* refactor: connect mihomo websocket

* chore: update

* chore: update

* fix: parse bigint to number

* chore: update

* Revert "fix: parse bigint to number"

This reverts commit 74c006522e23aa52cf8979a8fb47d2b1ae0bb043.

* chore: use number instead of bigint

* chore: cleanup

* fix: rule data not refresh when switch profile

* chore: update

* chore: cleanup

* chore: update

* fix: traffic graph data display

* feat: add ipc connection pool

* chore: update

* chore: clippy

* fix: incorrect delay status

* fix: typo

* fix: empty proxies tray menu

* chore: clippy

* chore: import tauri-plugin-mihomo by using git repo

* chore: cleanup

* fix: mihomo api

* fix: incorrect delay status

* chore: update tauri-plugin-mihomo dep

chore: update
This commit is contained in:
oomeow
2025-10-08 12:32:40 +08:00
committed by GitHub
parent 72aa56007c
commit 7fc238c27b
85 changed files with 1780 additions and 3344 deletions

View File

@@ -1,14 +1,11 @@
use crate::{
config::Config,
core::{CoreManager, handle, tray},
ipc::IpcManager,
logging_error,
process::AsyncHandler,
utils::{logging::Type, resolve},
};
use serde_yaml_ng::{Mapping, Value};
use std::env;
use std::process::{Command, exit};
/// Restart the Clash core
pub async fn restart_clash_core() {
@@ -35,55 +32,57 @@ pub async fn restart_app() {
return;
}
handle::Handle::notice_message("restart_app::info", "Restarting application...");
let app_handle = handle::Handle::app_handle();
app_handle.restart();
// TODO: PR Ref: https://github.com/clash-verge-rev/clash-verge-rev/pull/4960
// handle::Handle::notice_message("restart_app::info", "Restarting application...");
// Use the manual restart method consistently to ensure reliability across platforms
// This addresses the issue where app_handle.restart() doesn't work properly on Windows
let current_exe = match env::current_exe() {
Ok(path) => path,
Err(_) => {
// If we can't get the current executable path, try to use the fallback method
if let Some(app_handle) = handle::Handle::global().app_handle() {
app_handle.restart();
}
exit(1); // If we reach here, either app_handle was None or restart() failed to restart
}
};
// // Use the manual restart method consistently to ensure reliability across platforms
// // This addresses the issue where app_handle.restart() doesn't work properly on Windows
// let current_exe = match env::current_exe() {
// Ok(path) => path,
// Err(_) => {
// // If we can't get the current executable path, try to use the fallback method
// if let Some(app_handle) = handle::Handle::global().app_handle() {
// app_handle.restart();
// }
// exit(1); // If we reach here, either app_handle was None or restart() failed to restart
// }
// };
let mut cmd = Command::new(current_exe);
cmd.args(env::args().skip(1));
// let mut cmd = Command::new(current_exe);
// cmd.args(env::args().skip(1));
match cmd.spawn() {
Ok(child) => {
log::info!(target: "app", "New application instance started with PID: {}", child.id());
// Successfully started new process, now exit current process
if let Some(app_handle) = handle::Handle::global().app_handle() {
app_handle.exit(0);
} else {
exit(0);
}
}
Err(e) => {
log::error!(target: "app", "Failed to start new application instance: {}", e);
// If manual spawn fails, try the original restart method as a last resort
if let Some(app_handle) = handle::Handle::global().app_handle() {
app_handle.restart();
} else {
exit(1);
}
}
}
// match cmd.spawn() {
// Ok(child) => {
// log::info!(target: "app", "New application instance started with PID: {}", child.id());
// // Successfully started new process, now exit current process
// if let Some(app_handle) = handle::Handle::global().app_handle() {
// app_handle.exit(0);
// } else {
// exit(0);
// }
// }
// Err(e) => {
// log::error!(target: "app", "Failed to start new application instance: {}", e);
// // If manual spawn fails, try the original restart method as a last resort
// if let Some(app_handle) = handle::Handle::global().app_handle() {
// app_handle.restart();
// } else {
// exit(1);
// }
// }
// }
}
fn after_change_clash_mode() {
AsyncHandler::spawn(move || async {
match IpcManager::global().get_connections().await {
let mihomo = handle::Handle::mihomo().await;
match mihomo.get_connections().await {
Ok(connections) => {
if let Some(connections_array) = connections["connections"].as_array() {
if let Some(connections_array) = connections.connections {
for connection in connections_array {
if let Some(id) = connection["id"].as_str() {
let _ = IpcManager::global().delete_connection(id).await;
}
let _ = mihomo.close_connection(&connection.id).await;
}
}
}
@@ -103,7 +102,11 @@ pub async fn change_clash_mode(mode: String) {
"mode": mode
});
log::debug!(target: "app", "change clash mode to {mode}");
match IpcManager::global().patch_configs(json_value).await {
match handle::Handle::mihomo()
.await
.patch_base_config(&json_value)
.await
{
Ok(_) => {
// 更新订阅
Config::clash().await.data_mut().patch_config(mapping);
@@ -113,11 +116,7 @@ pub async fn change_clash_mode(mode: String) {
if clash_data.save_config().await.is_ok() {
handle::Handle::refresh_clash();
logging_error!(Type::Tray, true, tray::Tray::global().update_menu().await);
logging_error!(
Type::Tray,
true,
tray::Tray::global().update_icon(None).await
);
logging_error!(Type::Tray, true, tray::Tray::global().update_icon().await);
}
let is_auto_close_connection = Config::verge()

View File

@@ -23,11 +23,7 @@ pub async fn patch_clash(patch: Mapping) -> Result<()> {
} else {
if patch.get("mode").is_some() {
logging_error!(Type::Tray, true, tray::Tray::global().update_menu().await);
logging_error!(
Type::Tray,
true,
tray::Tray::global().update_icon(None).await
);
logging_error!(Type::Tray, true, tray::Tray::global().update_icon().await);
}
Config::runtime().await.draft_mut().patch_config(patch);
CoreManager::global().update_config().await?;
@@ -211,7 +207,7 @@ pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> {
tray::Tray::global().update_menu().await?;
}
if (update_flags & (UpdateFlags::SystrayIcon as i32)) != 0 {
tray::Tray::global().update_icon(None).await?;
tray::Tray::global().update_icon().await?;
}
if (update_flags & (UpdateFlags::SystrayTooltip as i32)) != 0 {
tray::Tray::global().update_tooltip().await?;

View File

@@ -143,15 +143,15 @@ pub async fn update_profile(
Ok(_) => {
logging!(info, Type::Config, true, "[订阅更新] 更新成功");
handle::Handle::refresh_clash();
if let Err(err) = cmd::proxy::force_refresh_proxies().await {
logging!(
error,
Type::Config,
true,
"[订阅更新] 代理组刷新失败: {}",
err
);
}
// if let Err(err) = cmd::proxy::force_refresh_proxies().await {
// logging!(
// error,
// Type::Config,
// true,
// "[订阅更新] 代理组刷新失败: {}",
// err
// );
// }
}
Err(err) => {
logging!(error, Type::Config, true, "[订阅更新] 更新失败: {}", err);

View File

@@ -1,9 +1,6 @@
use crate::{
config::{Config, IVerge},
core::handle,
ipc::IpcManager,
logging,
utils::logging::Type,
};
use std::env;
use tauri_plugin_clipboard_manager::ClipboardExt;
@@ -26,7 +23,7 @@ pub async fn toggle_system_proxy() {
// 如果当前系统代理即将关闭且自动关闭连接设置为true则关闭所有连接
if enable
&& auto_close_connection
&& let Err(err) = IpcManager::global().close_all_connections().await
&& let Err(err) = handle::Handle::mihomo().await.close_all_connections().await
{
log::error!(target: "app", "Failed to close all connections: {err}");
}
@@ -78,14 +75,7 @@ pub async fn copy_clash_env() {
.unwrap_or_else(|| "127.0.0.1".to_string()),
};
let Some(app_handle) = handle::Handle::global().app_handle() else {
logging!(
error,
Type::System,
"Failed to get app handle for proxy operation"
);
return;
};
let app_handle = handle::Handle::app_handle();
let port = {
Config::verge()
.await

View File

@@ -2,7 +2,6 @@ use crate::utils::window_manager::WindowManager;
use crate::{
config::Config,
core::{CoreManager, handle, sysopt},
ipc::IpcManager,
logging,
module::lightweight,
utils::logging::Type,
@@ -23,17 +22,12 @@ async fn open_or_close_dashboard_internal() {
pub async fn quit() {
logging!(debug, Type::System, true, "启动退出流程");
let Some(app_handle) = handle::Handle::global().app_handle() else {
logging!(
error,
Type::System,
"Failed to get app handle for quit operation"
);
return;
};
// 获取应用句柄并设置退出标志
let app_handle = handle::Handle::app_handle();
handle::Handle::global().set_is_exiting();
if let Some(window) = handle::Handle::global().get_window() {
// 优先关闭窗口,提供立即反馈
if let Some(window) = handle::Handle::get_window() {
let _ = window.hide();
log::info!(target: "app", "窗口已隐藏");
}
@@ -69,7 +63,14 @@ async fn clean_async() -> bool {
#[cfg(not(target_os = "windows"))]
let tun_timeout = Duration::from_secs(2);
match timeout(tun_timeout, IpcManager::global().patch_configs(disable_tun)).await {
match timeout(
tun_timeout,
handle::Handle::mihomo()
.await
.patch_base_config(&disable_tun),
)
.await
{
Ok(Ok(_)) => {
log::info!(target: "app", "TUN模式已禁用");
tokio::time::sleep(Duration::from_millis(300)).await;
@@ -314,7 +315,7 @@ pub async fn hide() {
add_light_weight_timer().await;
}
if let Some(window) = handle::Handle::global().get_window()
if let Some(window) = handle::Handle::get_window()
&& window.is_visible().unwrap_or(false)
{
let _ = window.hide();