style: clean up whitespace and improve code formatting across multiple files

This commit is contained in:
xmk23333
2025-10-21 17:53:02 +08:00
parent 0e933597f5
commit ef3f8e1839
12 changed files with 141 additions and 85 deletions

View File

@@ -72,7 +72,10 @@ impl IClashTemp {
map.insert("allow-lan".into(), false.into()); map.insert("allow-lan".into(), false.into());
map.insert("ipv6".into(), true.into()); map.insert("ipv6".into(), true.into());
map.insert("mode".into(), "rule".into()); map.insert("mode".into(), "rule".into());
map.insert("external-controller".into(), network::DEFAULT_EXTERNAL_CONTROLLER.into()); map.insert(
"external-controller".into(),
network::DEFAULT_EXTERNAL_CONTROLLER.into(),
);
#[cfg(unix)] #[cfg(unix)]
map.insert( map.insert(
"external-controller-unix".into(), "external-controller-unix".into(),
@@ -85,7 +88,8 @@ impl IClashTemp {
); );
map.insert("tun".into(), tun_config.into()); map.insert("tun".into(), tun_config.into());
cors_map.insert("allow-private-network".into(), true.into()); cors_map.insert("allow-private-network".into(), true.into());
cors_map.insert("allow-origins".into(), cors_map.insert(
"allow-origins".into(),
vec![ vec![
"tauri://localhost", "tauri://localhost",
"http://tauri.localhost", "http://tauri.localhost",

View File

@@ -173,8 +173,7 @@ impl Config {
return Ok::<(), BackoffError<anyhow::Error>>(()); return Ok::<(), BackoffError<anyhow::Error>>(());
} }
Config::generate().await Config::generate().await.map_err(BackoffError::transient)
.map_err(BackoffError::transient)
}; };
if let Err(e) = backoff::future::retry(backoff_strategy, operation).await { if let Err(e) = backoff::future::retry(backoff_strategy, operation).await {

View File

@@ -27,7 +27,8 @@ pub mod bypass {
pub const DEFAULT: &str = "localhost;127.*;192.168.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;<local>"; pub const DEFAULT: &str = "localhost;127.*;192.168.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;<local>";
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub const DEFAULT: &str = "localhost,127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,172.29.0.0/16,::1"; pub const DEFAULT: &str =
"localhost,127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,172.29.0.0/16,::1";
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub const DEFAULT: &str = "127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,172.29.0.0/16,localhost,*.local,*.crashlytics.com,<local>"; pub const DEFAULT: &str = "127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,172.29.0.0/16,localhost,*.local,*.crashlytics.com,<local>";
@@ -104,4 +105,3 @@ pub mod tun {
pub const DNS_HIJACK: &[&str] = &["any:53"]; pub const DNS_HIJACK: &[&str] = &["any:53"];
} }

View File

@@ -1,4 +1,4 @@
use crate::{APP_HANDLE, singleton, constants::timing}; use crate::{APP_HANDLE, constants::timing, singleton};
use parking_lot::RwLock; use parking_lot::RwLock;
use std::{sync::Arc, thread}; use std::{sync::Arc, thread};
use tauri::{AppHandle, Manager, WebviewWindow}; use tauri::{AppHandle, Manager, WebviewWindow};
@@ -209,4 +209,3 @@ impl Handle {
let _ = self.set_activation_policy(tauri::ActivationPolicy::Accessory); let _ = self.set_activation_policy(tauri::ActivationPolicy::Accessory);
} }
} }

View File

@@ -38,7 +38,9 @@ impl CoreManager {
return Ok((true, String::new())); return Ok((true, String::new()));
} }
let _permit = self.update_semaphore.try_acquire() let _permit = self
.update_semaphore
.try_acquire()
.map_err(|_| anyhow!("Config update already in progress"))?; .map_err(|_| anyhow!("Config update already in progress"))?;
self.perform_config_update().await self.perform_config_update().await
@@ -117,7 +119,10 @@ impl CoreManager {
} }
async fn reload_config(&self, path: &str) -> Result<(), MihomoError> { async fn reload_config(&self, path: &str) -> Result<(), MihomoError> {
handle::Handle::mihomo().await.reload_config(true, path).await handle::Handle::mihomo()
.await
.reload_config(true, path)
.await
} }
fn should_restart_on_error(err: &MihomoError) -> bool { fn should_restart_on_error(err: &MihomoError) -> bool {
@@ -149,4 +154,3 @@ impl CoreManager {
CONNECTION_ERRORS.iter().any(|p| text.contains(p)) CONNECTION_ERRORS.iter().any(|p| text.contains(p))
} }
} }

View File

@@ -1,6 +1,9 @@
use super::{CoreManager, RunningMode}; use super::{CoreManager, RunningMode};
use crate::{ use crate::{
core::{logger::ClashLogger, service::{ServiceStatus, SERVICE_MANAGER}}, core::{
logger::ClashLogger,
service::{SERVICE_MANAGER, ServiceStatus},
},
logging, logging,
utils::logging::Type, utils::logging::Type,
}; };
@@ -40,7 +43,8 @@ impl CoreManager {
pub async fn change_core(&self, clash_core: Option<String>) -> Result<(), String> { pub async fn change_core(&self, clash_core: Option<String>) -> Result<(), String> {
use crate::config::{Config, ConfigType, IVerge}; use crate::config::{Config, ConfigType, IVerge};
let core = clash_core.as_ref() let core = clash_core
.as_ref()
.ok_or_else(|| "Clash core cannot be None".to_string())?; .ok_or_else(|| "Clash core cannot be None".to_string())?;
if !IVerge::VALID_CLASH_CORES.contains(&core.as_str()) { if !IVerge::VALID_CLASH_CORES.contains(&core.as_str()) {
@@ -53,7 +57,8 @@ impl CoreManager {
let verge_data = Config::verge().await.latest_ref().clone(); let verge_data = Config::verge().await.latest_ref().clone();
verge_data.save_file().await.map_err(|e| e.to_string())?; verge_data.save_file().await.map_err(|e| e.to_string())?;
let run_path = Config::generate_file(ConfigType::Run).await let run_path = Config::generate_file(ConfigType::Run)
.await
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
self.apply_config(run_path).await.map_err(|e| e.to_string()) self.apply_config(run_path).await.map_err(|e| e.to_string())
@@ -76,7 +81,8 @@ impl CoreManager {
use crate::{config::Config, constants::timing}; use crate::{config::Config, constants::timing};
use backoff::{Error as BackoffError, ExponentialBackoff}; use backoff::{Error as BackoffError, ExponentialBackoff};
let needs_service = Config::verge().await let needs_service = Config::verge()
.await
.latest_ref() .latest_ref()
.enable_tun_mode .enable_tun_mode
.unwrap_or(false); .unwrap_or(false);
@@ -107,7 +113,9 @@ impl CoreManager {
if matches!(manager.current(), ServiceStatus::Ready) { if matches!(manager.current(), ServiceStatus::Ready) {
Ok(()) Ok(())
} else { } else {
Err(BackoffError::transient(anyhow::anyhow!("Service not ready"))) Err(BackoffError::transient(anyhow::anyhow!(
"Service not ready"
)))
} }
}; };
@@ -117,4 +125,3 @@ impl CoreManager {
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
async fn wait_for_service_if_needed(&self) {} async fn wait_for_service_if_needed(&self) {}
} }

View File

@@ -77,4 +77,3 @@ impl CoreManager {
} }
singleton_lazy!(CoreManager, CORE_MANAGER, CoreManager::default); singleton_lazy!(CoreManager, CORE_MANAGER, CoreManager::default);

View File

@@ -11,7 +11,12 @@ impl CoreManager {
pub async fn cleanup_orphaned_processes(&self) -> Result<()> { pub async fn cleanup_orphaned_processes(&self) -> Result<()> {
logging!(info, Type::Core, "Cleaning orphaned mihomo processes"); logging!(info, Type::Core, "Cleaning orphaned mihomo processes");
let current_pid = self.state.lock().child_sidecar.as_ref().and_then(|c| c.pid()); let current_pid = self
.state
.lock()
.child_sidecar
.as_ref()
.and_then(|c| c.pid());
let target_processes = process::process_names(); let target_processes = process::process_names();
let process_futures = target_processes.iter().map(|&name| { let process_futures = target_processes.iter().map(|&name| {
@@ -46,19 +51,31 @@ impl CoreManager {
.count(); .count();
if killed_count > 0 { if killed_count > 0 {
logging!(info, Type::Core, "Cleaned {} orphaned processes", killed_count); logging!(
info,
Type::Core,
"Cleaned {} orphaned processes",
killed_count
);
} }
Ok(()) Ok(())
} }
async fn find_processes_by_name(&self, process_name: String, _target: &str) -> Result<(Vec<u32>, String)> { async fn find_processes_by_name(
&self,
process_name: String,
_target: &str,
) -> Result<(Vec<u32>, String)> {
#[cfg(windows)] #[cfg(windows)]
{ {
use std::mem; use std::mem;
use winapi::um::{ use winapi::um::{
handleapi::CloseHandle, handleapi::CloseHandle,
tlhelp32::{CreateToolhelp32Snapshot, PROCESSENTRY32W, Process32FirstW, Process32NextW, TH32CS_SNAPPROCESS}, tlhelp32::{
CreateToolhelp32Snapshot, PROCESSENTRY32W, Process32FirstW, Process32NextW,
TH32CS_SNAPPROCESS,
},
}; };
let process_name_clone = process_name.clone(); let process_name_clone = process_name.clone();
@@ -76,7 +93,10 @@ impl CoreManager {
if Process32FirstW(snapshot, &mut pe32) != 0 { if Process32FirstW(snapshot, &mut pe32) != 0 {
loop { loop {
let end_pos = pe32.szExeFile.iter().position(|&x| x == 0) let end_pos = pe32
.szExeFile
.iter()
.position(|&x| x == 0)
.unwrap_or(pe32.szExeFile.len()); .unwrap_or(pe32.szExeFile.len());
if end_pos > 0 { if end_pos > 0 {
@@ -96,14 +116,19 @@ impl CoreManager {
} }
Ok(pids) Ok(pids)
}).await??; })
.await??;
Ok((pids, process_name)) Ok((pids, process_name))
} }
#[cfg(not(windows))] #[cfg(not(windows))]
{ {
let cmd = if cfg!(target_os = "macos") { "pgrep" } else { "pidof" }; let cmd = if cfg!(target_os = "macos") {
"pgrep"
} else {
"pidof"
};
let output = tokio::process::Command::new(cmd) let output = tokio::process::Command::new(cmd)
.arg(&process_name) .arg(&process_name)
.output() .output()
@@ -140,7 +165,9 @@ impl CoreManager {
let result = TerminateProcess(handle, 1) != 0; let result = TerminateProcess(handle, 1) != 0;
CloseHandle(handle); CloseHandle(handle);
result result
}).await.unwrap_or(false) })
.await
.unwrap_or(false)
}; };
#[cfg(not(windows))] #[cfg(not(windows))]
@@ -158,10 +185,22 @@ impl CoreManager {
tokio::time::sleep(timing::PROCESS_VERIFY_DELAY).await; tokio::time::sleep(timing::PROCESS_VERIFY_DELAY).await;
if self.is_process_running(pid).await.unwrap_or(false) { if self.is_process_running(pid).await.unwrap_or(false) {
logging!(warn, Type::Core, "Process {} (PID: {}) still running after termination", process_name, pid); logging!(
warn,
Type::Core,
"Process {} (PID: {}) still running after termination",
process_name,
pid
);
false false
} else { } else {
logging!(info, Type::Core, "Terminated process {} (PID: {})", process_name, pid); logging!(
info,
Type::Core,
"Terminated process {} (PID: {})",
process_name,
pid
);
true true
} }
} }
@@ -187,7 +226,8 @@ impl CoreManager {
let result = GetExitCodeProcess(handle, &mut exit_code); let result = GetExitCodeProcess(handle, &mut exit_code);
CloseHandle(handle); CloseHandle(handle);
Ok(result != 0 && exit_code == 259) Ok(result != 0 && exit_code == 259)
}).await? })
.await?
} }
#[cfg(not(windows))] #[cfg(not(windows))]
@@ -201,4 +241,3 @@ impl CoreManager {
} }
} }
} }

View File

@@ -2,11 +2,7 @@ use super::{CoreManager, RunningMode};
use crate::{ use crate::{
AsyncHandler, AsyncHandler,
config::Config, config::Config,
core::{ core::{handle, logger::ClashLogger, service},
handle,
logger::ClashLogger,
service,
},
logging, logging,
process::CommandChildGuard, process::CommandChildGuard,
utils::{ utils::{
@@ -59,7 +55,8 @@ impl CoreManager {
state.running_mode = RunningMode::Sidecar; state.running_mode = RunningMode::Sidecar;
} }
let shared_writer: SharedWriter = std::sync::Arc::new(tokio::sync::Mutex::new(sidecar_writer().await?)); let shared_writer: SharedWriter =
std::sync::Arc::new(tokio::sync::Mutex::new(sidecar_writer().await?));
AsyncHandler::spawn(|| async move { AsyncHandler::spawn(|| async move {
while let Some(event) = rx.recv().await { while let Some(event) = rx.recv().await {
@@ -122,4 +119,3 @@ impl CoreManager {
Ok(()) Ok(())
} }
} }

View File

@@ -13,8 +13,4 @@ pub mod tray;
pub mod validate; pub mod validate;
pub mod win_uwp; pub mod win_uwp;
pub use self::{ pub use self::{event_driven_proxy::EventDrivenProxyManager, manager::CoreManager, timer::Timer};
event_driven_proxy::EventDrivenProxyManager,
manager::CoreManager,
timer::Timer,
};

View File

@@ -80,7 +80,12 @@ impl NotificationSystem {
match result { match result {
Ok(handle) => self.worker_handle = Some(handle), Ok(handle) => self.worker_handle = Some(handle),
Err(e) => logging!(error, Type::System, "Failed to start notification worker: {}", e), Err(e) => logging!(
error,
Type::System,
"Failed to start notification worker: {}",
e
),
} }
} }
@@ -141,15 +146,19 @@ impl NotificationSystem {
} }
} }
fn serialize_event(&self, event: FrontendEvent) -> (&'static str, Result<serde_json::Value, serde_json::Error>) { fn serialize_event(
&self,
event: FrontendEvent,
) -> (&'static str, Result<serde_json::Value, serde_json::Error>) {
use serde_json::json; use serde_json::json;
match event { match event {
FrontendEvent::RefreshClash => ("verge://refresh-clash-config", Ok(json!("yes"))), FrontendEvent::RefreshClash => ("verge://refresh-clash-config", Ok(json!("yes"))),
FrontendEvent::RefreshVerge => ("verge://refresh-verge-config", Ok(json!("yes"))), FrontendEvent::RefreshVerge => ("verge://refresh-verge-config", Ok(json!("yes"))),
FrontendEvent::NoticeMessage { status, message } => { FrontendEvent::NoticeMessage { status, message } => (
("verge://notice-message", serde_json::to_value((status, message))) "verge://notice-message",
} serde_json::to_value((status, message)),
),
FrontendEvent::ProfileChanged { current_profile_id } => { FrontendEvent::ProfileChanged { current_profile_id } => {
("profile-changed", Ok(json!(current_profile_id))) ("profile-changed", Ok(json!(current_profile_id)))
} }
@@ -171,7 +180,12 @@ impl NotificationSystem {
let errors = self.stats.total_errors.load(Ordering::Relaxed); let errors = self.stats.total_errors.load(Ordering::Relaxed);
if errors > retry::EVENT_EMIT_THRESHOLD && !*self.emergency_mode.read() { if errors > retry::EVENT_EMIT_THRESHOLD && !*self.emergency_mode.read() {
logging!(warn, Type::Frontend, "Entering emergency mode after {} errors", errors); logging!(
warn,
Type::Frontend,
"Entering emergency mode after {} errors",
errors
);
*self.emergency_mode.write() = true; *self.emergency_mode.write() = true;
} }
} }
@@ -200,4 +214,3 @@ impl NotificationSystem {
} }
} }
} }