Files
clash-verge-rev/src-tauri/src/core/hotkey.rs
T
wonfen 22947632c0 refactor: Rewrite and improve sysproxy logic (#7744)
* fix(cmd): preserve anyhow context in coded errors

Use alternate error formatting so coded failures retain the full anyhow context chain without changing their wire format.

* fix(macos): skip redundant proxy disable writes

When both proxy modes are off, read macOS state first and skip networksetup if the OS is already clean. Update the guard target only after a successful write.

* feat(proxy): classify system proxy failures

Attach stable, actionable proxy failure codes to anyhow chains while preserving the original diagnostic causes.

* feat(cmd): return structured command failures

Replace encoded error strings with serializable code and detail fields, and update frontend notice consumers to handle the structured shape.

* fix(scripts): use the js-yaml namespace import

Import js-yaml through its ESM namespace so the i18n cleanup script can access load and dump.

* feat(proxy): localize classified proxy failures

Map privilege and direct-fallback codes to localized explanations, and let mapped causes outrank generic operation keys in notices.

* fix(ui): bound notice text and render failure details

Limit resolved notice text without truncating clipboard content, and normalize structured failures embedded in translation parameters.

* feat(proxy): persist unseen proxy failures

Store unresolved classified failures by code, notify the frontend when the table changes, and show each failure sequence once when a visible window can receive it.

* feat(proxy): report tray and hotkey toggle failures

Record classified failures from tray and hotkey proxy toggles so they appear when a visible window is available.

* fix(ui): restore the proxy switch after failed toggles

Refresh frontend proxy state after a failed toggle so the switch reflects the rolled-back configuration.

* feat(proxy): stop the guard after repeated failures

Report guard outcomes, stop after the failure threshold, and persist the classified failure for the frontend.

* fix(macos): preserve proxy during service handoff

Stop a sidecar being handed to the service without clearing proxy state that the new owner must inherit.

* feat(macos): classify misplaced sidecar state

Distinguish a refused sidecar proxy write from a machine that still needs the privileged service installed.

* feat(ui): recover misplaced sidecar notices

Offer a restart action for macOS proxy failures caused by a ready service while the core still runs as a sidecar.

* feat(ui): install the service from proxy notices

Let privilege notices install the macOS service, restart the core, and report the runtime mode that was actually reached.

* feat(cmd): attach requested proxy operations

Carry the requested proxy direction with structured command failures when the originating command knows it.

* feat(sysproxy): drain guards before OS access

Wait for the local proxy guard to stop before OS reads, service handoff, or final writes, and refuse transitions when it does not drain.

* feat(sysproxy): recover partial proxy writes

Use write progress to choose whether to restore the old guard or compensate with a disabled proxy state, while preserving actionable errors.

* feat(sysproxy): skip already-applied proxy writes

Compare macOS proxy snapshots with global, PAC, and disabled targets so matching state avoids redundant privileged writes.

* feat(proxy): report background proxy failures

Persist and surface classified proxy failures from lifecycle paths that have no direct caller awaiting the result.

* feat(ui): move proxy recovery into a dialog

Guide users through installing or switching to the service after a refused proxy write, and suppress duplicate toast reports.

* fix(ui): close proxy dialog after service recovery

Dismiss the recovery dialog once the core reaches service mode, even when the original proxy request remains pending.

* feat(ui): retry proxy request after service recovery

Replay the requested proxy direction after the core reaches service mode, while leaving incidental restores unchanged.

* fix(proxy): refresh state after successful apply

Publish the proxy refresh from the shared apply path after releasing the lifecycle lock, and remove the tray-specific duplicate.

* feat(ui): show proxy recovery progress

Display installation, restart, and proxy-apply stages while keeping the primary action spinner visible during recovery.

* feat(ui): route unavailable TUN through service recovery

Let the TUN switch request service recovery and restore its requested state after the core restarts in service mode.

* fix(ui): avoid duplicate TUN recovery errors

Roll back deferred TUN toggles without reporting an error, and publish configuration refreshes from the transaction commit path.

* chore: update changelog & pinlock

* fix(core): resolve platform-specific clippy warnings

* fix(core): detect Windows wildcard TCP conflicts

Windows permits a specific exclusive bind beside an existing non-exclusive wildcard listener. Consult the system TCP listener table so the probe reports that overlap as a conflict.
2026-08-14 23:52:37 +08:00

478 lines
17 KiB
Rust
Executable File

use crate::process::AsyncHandler;
use crate::singleton;
use crate::utils::notification::{NotificationEvent, notify_event};
use crate::utils::window_manager::WindowManager;
use crate::{config::Config, core::handle, feat, module::lightweight::entry_lightweight_mode};
use anyhow::{Result, bail};
use arc_swap::ArcSwap;
use clash_verge_logging::{Type, logging};
use smartstring::alias::String;
use std::{
collections::HashMap,
fmt,
str::FromStr,
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
};
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt as _, ShortcutState};
/// Enum representing all available hotkey functions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HotkeyFunction {
OpenOrCloseDashboard,
ClashModeRule,
ClashModeGlobal,
ClashModeDirect,
ToggleSystemProxy,
ToggleTunMode,
EntryLightweightMode,
ReactivateProfiles,
Quit,
#[cfg(target_os = "macos")]
Hide,
}
impl fmt::Display for HotkeyFunction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::OpenOrCloseDashboard => "open_or_close_dashboard",
Self::ClashModeRule => "clash_mode_rule",
Self::ClashModeGlobal => "clash_mode_global",
Self::ClashModeDirect => "clash_mode_direct",
Self::ToggleSystemProxy => "toggle_system_proxy",
Self::ToggleTunMode => "toggle_tun_mode",
Self::EntryLightweightMode => "entry_lightweight_mode",
Self::ReactivateProfiles => "reactivate_profiles",
Self::Quit => "quit",
#[cfg(target_os = "macos")]
Self::Hide => "hide",
};
write!(f, "{s}")
}
}
impl FromStr for HotkeyFunction {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim() {
"open_or_close_dashboard" => Ok(Self::OpenOrCloseDashboard),
"clash_mode_rule" => Ok(Self::ClashModeRule),
"clash_mode_global" => Ok(Self::ClashModeGlobal),
"clash_mode_direct" => Ok(Self::ClashModeDirect),
"toggle_system_proxy" => Ok(Self::ToggleSystemProxy),
"toggle_tun_mode" => Ok(Self::ToggleTunMode),
"entry_lightweight_mode" => Ok(Self::EntryLightweightMode),
"reactivate_profiles" => Ok(Self::ReactivateProfiles),
"quit" => Ok(Self::Quit),
#[cfg(target_os = "macos")]
"hide" => Ok(Self::Hide),
_ => bail!("invalid hotkey function: {}", s),
}
}
}
#[cfg(target_os = "macos")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// Enum representing predefined system hotkeys
pub enum SystemHotkey {
CmdQ,
CmdW,
}
#[cfg(target_os = "macos")]
impl fmt::Display for SystemHotkey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::CmdQ => "CMD+Q",
Self::CmdW => "CMD+W",
};
write!(f, "{s}")
}
}
#[cfg(target_os = "macos")]
impl SystemHotkey {
pub const fn function(self) -> HotkeyFunction {
match self {
Self::CmdQ => HotkeyFunction::Quit,
Self::CmdW => HotkeyFunction::Hide,
}
}
}
pub struct Hotkey {
current: ArcSwap<Vec<String>>,
}
impl Hotkey {
fn new() -> Self {
Self {
current: ArcSwap::new(Arc::new(Vec::new())),
}
}
/// Execute the function associated with a hotkey function enum
fn execute_function(function: HotkeyFunction) {
match function {
HotkeyFunction::OpenOrCloseDashboard => {
AsyncHandler::spawn(async move || {
crate::feat::open_or_close_dashboard().await;
notify_event(NotificationEvent::DashboardToggled).await;
});
}
HotkeyFunction::ClashModeRule => {
AsyncHandler::spawn(async move || {
// 错误已在 change_clash_mode 内部记录,此处显式忽略返回值
let _ = feat::change_clash_mode("rule".into()).await;
notify_event(NotificationEvent::ClashModeChanged { mode: "Rule" }).await;
});
}
HotkeyFunction::ClashModeGlobal => {
AsyncHandler::spawn(async move || {
// 错误已在 change_clash_mode 内部记录,此处显式忽略返回值
let _ = feat::change_clash_mode("global".into()).await;
notify_event(NotificationEvent::ClashModeChanged { mode: "Global" }).await;
});
}
HotkeyFunction::ClashModeDirect => {
AsyncHandler::spawn(async move || {
// 错误已在 change_clash_mode 内部记录,此处显式忽略返回值
let _ = feat::change_clash_mode("direct".into()).await;
notify_event(NotificationEvent::ClashModeChanged { mode: "Direct" }).await;
});
}
HotkeyFunction::ToggleSystemProxy => {
AsyncHandler::spawn(async move || {
if let Some(is_proxy_enabled) = feat::toggle_system_proxy().await {
notify_event(NotificationEvent::SystemProxyToggled(is_proxy_enabled)).await;
}
});
}
HotkeyFunction::ToggleTunMode => {
AsyncHandler::spawn(async move || {
let is_tun_enable = feat::toggle_tun_mode(None).await;
notify_event(NotificationEvent::TunModeToggled(is_tun_enable)).await;
});
}
HotkeyFunction::EntryLightweightMode => {
AsyncHandler::spawn(async move || {
entry_lightweight_mode().await;
notify_event(NotificationEvent::LightweightModeEntered).await;
});
}
HotkeyFunction::ReactivateProfiles => {
AsyncHandler::spawn(async move || match feat::enhance_profiles().await {
Ok(outcome) if outcome.is_valid() => {
handle::Handle::refresh_clash();
notify_event(NotificationEvent::ProfilesReactivated).await;
}
Ok(outcome) => {
let message = outcome.to_string();
logging!(
warn,
Type::Hotkey,
"Hotkey profile reactivation failed validation: {}",
message.as_str()
);
handle::Handle::notice_message("reactivate_profiles::error", message);
}
Err(err) => {
logging!(
error,
Type::Hotkey,
"Failed to reactivate subscriptions via hotkey: {}",
err
);
handle::Handle::notice_message("reactivate_profiles::error", err.to_string());
}
});
}
HotkeyFunction::Quit => {
AsyncHandler::spawn(async move || {
notify_event(NotificationEvent::AppQuit).await;
feat::quit().await;
});
}
#[cfg(target_os = "macos")]
HotkeyFunction::Hide => {
AsyncHandler::spawn(async move || {
feat::hide().await;
notify_event(NotificationEvent::AppHidden).await;
});
}
}
}
#[cfg(target_os = "macos")]
/// Register a system hotkey using enum
pub async fn register_system_hotkey(&self, hotkey: SystemHotkey) -> Result<()> {
let hotkey_str = hotkey.to_string();
let function = hotkey.function();
self.register_hotkey_with_function(&hotkey_str, function).await
}
#[cfg(target_os = "macos")]
/// Unregister a system hotkey using enum
pub fn unregister_system_hotkey(&self, hotkey: SystemHotkey) -> Result<()> {
let hotkey_str = hotkey.to_string();
self.unregister(&hotkey_str)
}
/// Register a hotkey with function enum
#[allow(clippy::unused_async)]
pub async fn register_hotkey_with_function(&self, hotkey: &str, function: HotkeyFunction) -> Result<()> {
let app_handle = handle::Handle::app_handle();
let manager = app_handle.global_shortcut();
logging!(
debug,
Type::Hotkey,
"Attempting to register hotkey: {} for function: {}",
hotkey,
function
);
if manager.is_registered(hotkey) {
logging!(
debug,
Type::Hotkey,
"Hotkey {} was already registered, unregistering first",
hotkey
);
manager.unregister(hotkey)?;
}
let is_quit = matches!(function, HotkeyFunction::Quit);
let pressed = AtomicBool::new(false);
manager.on_shortcut(hotkey, move |_app_handle, hotkey_event, event| match event.state {
ShortcutState::Released => {
pressed.store(false, Ordering::Relaxed);
}
ShortcutState::Pressed => {
if pressed.swap(true, Ordering::Relaxed) {
logging!(
debug,
Type::Hotkey,
"Ignoring repeated hotkey press: {:?}",
hotkey_event
);
return;
}
logging!(debug, Type::Hotkey, "Hotkey pressed: {:?}", hotkey_event);
let hotkey = hotkey_event.key;
if hotkey == Code::KeyQ && is_quit {
if let Some(window) = WindowManager::get_main_window()
&& window.is_focused().unwrap_or(false)
{
logging!(debug, Type::Hotkey, "Executing quit function");
Self::execute_function(function);
}
} else {
AsyncHandler::spawn(move || async move {
logging!(debug, Type::Hotkey, "Executing function directly");
let is_enable_global_hotkey =
Config::verge().await.data_arc().enable_global_hotkey.unwrap_or(true);
if is_enable_global_hotkey {
Self::execute_function(function);
} else {
use crate::utils::window_manager::WindowManager;
let window = WindowManager::get_main_window();
let is_visible = WindowManager::is_main_window_visible(window.as_ref());
let is_focused = WindowManager::is_main_window_focused(window.as_ref());
if is_focused && is_visible {
Self::execute_function(function);
}
}
});
}
}
})?;
logging!(
debug,
Type::Hotkey,
"Successfully registered hotkey {} for {}",
hotkey,
function
);
Ok(())
}
}
singleton!(Hotkey, INSTANCE);
impl Hotkey {
pub async fn init(&self, skip: bool) -> Result<()> {
if skip {
logging!(debug, Type::Hotkey, "skip register all hotkeys");
return Ok(());
}
let verge = Config::verge().await;
let enable_global_hotkey = verge.latest_arc().enable_global_hotkey.unwrap_or(true);
logging!(
debug,
Type::Hotkey,
"Initializing global hotkeys: {}",
enable_global_hotkey
);
// Extract hotkeys data before async operations
let hotkeys = verge.latest_arc().hotkeys.clone();
if let Some(hotkeys) = hotkeys {
logging!(debug, Type::Hotkey, "Has {} hotkeys need to register", hotkeys.len());
for hotkey in hotkeys.iter() {
let mut iter = hotkey.split(',');
let func = iter.next();
let key = iter.next();
match (key, func) {
(Some(key), Some(func)) => {
logging!(debug, Type::Hotkey, "Registering hotkey: {} -> {}", key, func);
if let Err(e) = self.register(key, func).await {
logging!(
error,
Type::Hotkey,
"Failed to register hotkey {} -> {}: {:?}",
key,
func,
e
);
} else {
logging!(
debug,
Type::Hotkey,
"Successfully registered hotkey {} -> {}",
key,
func
);
}
}
_ => {
let key = key.unwrap_or("None");
let func = func.unwrap_or("None");
logging!(
error,
Type::Hotkey,
"Invalid hotkey configuration: `{}`:`{}`",
key,
func
);
}
}
}
self.current.store(Arc::new(hotkeys));
} else {
logging!(debug, Type::Hotkey, "No hotkeys configured");
}
Ok(())
}
pub fn reset(&self) -> Result<()> {
let app_handle = handle::Handle::app_handle();
let manager = app_handle.global_shortcut();
manager.unregister_all()?;
Ok(())
}
/// Register a hotkey with string-based function (backward compatibility)
pub async fn register(&self, hotkey: &str, func: &str) -> Result<()> {
let function = HotkeyFunction::from_str(func)?;
self.register_hotkey_with_function(hotkey, function).await
}
pub fn unregister(&self, hotkey: &str) -> Result<()> {
let app_handle = handle::Handle::app_handle();
let manager = app_handle.global_shortcut();
manager.unregister(hotkey)?;
logging!(debug, Type::Hotkey, "Unregister hotkey {}", hotkey);
Ok(())
}
pub async fn update(&self, new_hotkeys: Vec<String>) -> Result<()> {
// Extract current hotkeys before async operations
let current_hotkeys = &*self.current.load();
let old_map = Self::get_map_from_vec(current_hotkeys);
let new_map = Self::get_map_from_vec(&new_hotkeys);
let (del, add) = Self::get_diff(old_map, new_map);
del.iter().for_each(|key| {
let _ = self.unregister(key);
});
for (key, func) in add.iter() {
self.register(key, func).await?;
}
// Update the current hotkeys after all async operations
self.current.store(Arc::new(new_hotkeys));
Ok(())
}
fn get_map_from_vec(hotkeys: &[String]) -> HashMap<&str, &str> {
let mut map = HashMap::new();
hotkeys.iter().for_each(|hotkey| {
let mut iter = hotkey.split(',');
let func = iter.next();
let key = iter.next();
if let (Some(func), Some(key)) = (func, key) {
let func = func.trim();
let key = key.trim();
map.insert(key, func);
}
});
map
}
fn get_diff<'a>(
old_map: HashMap<&'a str, &'a str>,
new_map: HashMap<&'a str, &'a str>,
) -> (Vec<&'a str>, Vec<(&'a str, &'a str)>) {
let mut del_list = vec![];
let mut add_list = vec![];
old_map.iter().for_each(|(&key, func)| {
match new_map.get(key) {
Some(new_func) => {
if new_func != func {
del_list.push(key);
add_list.push((key, *new_func));
}
}
None => del_list.push(key),
};
});
new_map.iter().for_each(|(&key, &func)| {
if !old_map.contains_key(key) {
add_list.push((key, func));
}
});
(del_list, add_list)
}
}
impl Drop for Hotkey {
fn drop(&mut self) {
let app_handle = handle::Handle::app_handle();
if let Err(e) = app_handle.global_shortcut().unregister_all() {
logging!(error, Type::Hotkey, "Error unregistering all hotkeys: {:?}", e);
}
}
}