mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 00:35:38 +08:00
style: clean up whitespace and improve code formatting across multiple files
This commit is contained in:
@@ -72,7 +72,10 @@ impl IClashTemp {
|
||||
map.insert("allow-lan".into(), false.into());
|
||||
map.insert("ipv6".into(), true.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)]
|
||||
map.insert(
|
||||
"external-controller-unix".into(),
|
||||
@@ -85,7 +88,8 @@ impl IClashTemp {
|
||||
);
|
||||
map.insert("tun".into(), tun_config.into());
|
||||
cors_map.insert("allow-private-network".into(), true.into());
|
||||
cors_map.insert("allow-origins".into(),
|
||||
cors_map.insert(
|
||||
"allow-origins".into(),
|
||||
vec![
|
||||
"tauri://localhost",
|
||||
"http://tauri.localhost",
|
||||
|
||||
@@ -173,8 +173,7 @@ impl Config {
|
||||
return Ok::<(), BackoffError<anyhow::Error>>(());
|
||||
}
|
||||
|
||||
Config::generate().await
|
||||
.map_err(BackoffError::transient)
|
||||
Config::generate().await.map_err(BackoffError::transient)
|
||||
};
|
||||
|
||||
if let Err(e) = backoff::future::retry(backoff_strategy, operation).await {
|
||||
|
||||
@@ -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>";
|
||||
|
||||
#[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")]
|
||||
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"];
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{APP_HANDLE, singleton, constants::timing};
|
||||
use crate::{APP_HANDLE, constants::timing, singleton};
|
||||
use parking_lot::RwLock;
|
||||
use std::{sync::Arc, thread};
|
||||
use tauri::{AppHandle, Manager, WebviewWindow};
|
||||
@@ -209,4 +209,3 @@ impl Handle {
|
||||
let _ = self.set_activation_policy(tauri::ActivationPolicy::Accessory);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,9 @@ impl CoreManager {
|
||||
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"))?;
|
||||
|
||||
self.perform_config_update().await
|
||||
@@ -117,7 +119,10 @@ impl CoreManager {
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -149,4 +154,3 @@ impl CoreManager {
|
||||
CONNECTION_ERRORS.iter().any(|p| text.contains(p))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use super::{CoreManager, RunningMode};
|
||||
use crate::{
|
||||
core::{logger::ClashLogger, service::{ServiceStatus, SERVICE_MANAGER}},
|
||||
core::{
|
||||
logger::ClashLogger,
|
||||
service::{SERVICE_MANAGER, ServiceStatus},
|
||||
},
|
||||
logging,
|
||||
utils::logging::Type,
|
||||
};
|
||||
@@ -40,7 +43,8 @@ impl CoreManager {
|
||||
pub async fn change_core(&self, clash_core: Option<String>) -> Result<(), String> {
|
||||
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())?;
|
||||
|
||||
if !IVerge::VALID_CLASH_CORES.contains(&core.as_str()) {
|
||||
@@ -53,7 +57,8 @@ impl CoreManager {
|
||||
let verge_data = Config::verge().await.latest_ref().clone();
|
||||
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())?;
|
||||
|
||||
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 backoff::{Error as BackoffError, ExponentialBackoff};
|
||||
|
||||
let needs_service = Config::verge().await
|
||||
let needs_service = Config::verge()
|
||||
.await
|
||||
.latest_ref()
|
||||
.enable_tun_mode
|
||||
.unwrap_or(false);
|
||||
@@ -107,7 +113,9 @@ impl CoreManager {
|
||||
if matches!(manager.current(), ServiceStatus::Ready) {
|
||||
Ok(())
|
||||
} 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"))]
|
||||
async fn wait_for_service_if_needed(&self) {}
|
||||
}
|
||||
|
||||
|
||||
@@ -77,4 +77,3 @@ impl CoreManager {
|
||||
}
|
||||
|
||||
singleton_lazy!(CoreManager, CORE_MANAGER, CoreManager::default);
|
||||
|
||||
|
||||
@@ -11,7 +11,12 @@ impl CoreManager {
|
||||
pub async fn cleanup_orphaned_processes(&self) -> Result<()> {
|
||||
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 process_futures = target_processes.iter().map(|&name| {
|
||||
@@ -46,19 +51,31 @@ impl CoreManager {
|
||||
.count();
|
||||
|
||||
if killed_count > 0 {
|
||||
logging!(info, Type::Core, "Cleaned {} orphaned processes", killed_count);
|
||||
logging!(
|
||||
info,
|
||||
Type::Core,
|
||||
"Cleaned {} orphaned processes",
|
||||
killed_count
|
||||
);
|
||||
}
|
||||
|
||||
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)]
|
||||
{
|
||||
use std::mem;
|
||||
use winapi::um::{
|
||||
handleapi::CloseHandle,
|
||||
tlhelp32::{CreateToolhelp32Snapshot, PROCESSENTRY32W, Process32FirstW, Process32NextW, TH32CS_SNAPPROCESS},
|
||||
tlhelp32::{
|
||||
CreateToolhelp32Snapshot, PROCESSENTRY32W, Process32FirstW, Process32NextW,
|
||||
TH32CS_SNAPPROCESS,
|
||||
},
|
||||
};
|
||||
|
||||
let process_name_clone = process_name.clone();
|
||||
@@ -76,7 +93,10 @@ impl CoreManager {
|
||||
|
||||
if Process32FirstW(snapshot, &mut pe32) != 0 {
|
||||
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());
|
||||
|
||||
if end_pos > 0 {
|
||||
@@ -96,14 +116,19 @@ impl CoreManager {
|
||||
}
|
||||
|
||||
Ok(pids)
|
||||
}).await??;
|
||||
})
|
||||
.await??;
|
||||
|
||||
Ok((pids, process_name))
|
||||
}
|
||||
|
||||
#[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)
|
||||
.arg(&process_name)
|
||||
.output()
|
||||
@@ -140,7 +165,9 @@ impl CoreManager {
|
||||
let result = TerminateProcess(handle, 1) != 0;
|
||||
CloseHandle(handle);
|
||||
result
|
||||
}).await.unwrap_or(false)
|
||||
})
|
||||
.await
|
||||
.unwrap_or(false)
|
||||
};
|
||||
|
||||
#[cfg(not(windows))]
|
||||
@@ -158,10 +185,22 @@ impl CoreManager {
|
||||
tokio::time::sleep(timing::PROCESS_VERIFY_DELAY).await;
|
||||
|
||||
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
|
||||
} else {
|
||||
logging!(info, Type::Core, "Terminated process {} (PID: {})", process_name, pid);
|
||||
logging!(
|
||||
info,
|
||||
Type::Core,
|
||||
"Terminated process {} (PID: {})",
|
||||
process_name,
|
||||
pid
|
||||
);
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -187,7 +226,8 @@ impl CoreManager {
|
||||
let result = GetExitCodeProcess(handle, &mut exit_code);
|
||||
CloseHandle(handle);
|
||||
Ok(result != 0 && exit_code == 259)
|
||||
}).await?
|
||||
})
|
||||
.await?
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
@@ -201,4 +241,3 @@ impl CoreManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,7 @@ use super::{CoreManager, RunningMode};
|
||||
use crate::{
|
||||
AsyncHandler,
|
||||
config::Config,
|
||||
core::{
|
||||
handle,
|
||||
logger::ClashLogger,
|
||||
service,
|
||||
},
|
||||
core::{handle, logger::ClashLogger, service},
|
||||
logging,
|
||||
process::CommandChildGuard,
|
||||
utils::{
|
||||
@@ -59,7 +55,8 @@ impl CoreManager {
|
||||
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 {
|
||||
while let Some(event) = rx.recv().await {
|
||||
@@ -122,4 +119,3 @@ impl CoreManager {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,4 @@ pub mod tray;
|
||||
pub mod validate;
|
||||
pub mod win_uwp;
|
||||
|
||||
pub use self::{
|
||||
event_driven_proxy::EventDrivenProxyManager,
|
||||
manager::CoreManager,
|
||||
timer::Timer,
|
||||
};
|
||||
pub use self::{event_driven_proxy::EventDrivenProxyManager, manager::CoreManager, timer::Timer};
|
||||
|
||||
@@ -80,7 +80,12 @@ impl NotificationSystem {
|
||||
|
||||
match result {
|
||||
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;
|
||||
|
||||
match event {
|
||||
FrontendEvent::RefreshClash => ("verge://refresh-clash-config", Ok(json!("yes"))),
|
||||
FrontendEvent::RefreshVerge => ("verge://refresh-verge-config", Ok(json!("yes"))),
|
||||
FrontendEvent::NoticeMessage { status, message } => {
|
||||
("verge://notice-message", serde_json::to_value((status, message)))
|
||||
}
|
||||
FrontendEvent::NoticeMessage { status, message } => (
|
||||
"verge://notice-message",
|
||||
serde_json::to_value((status, message)),
|
||||
),
|
||||
FrontendEvent::ProfileChanged { 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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -200,4 +214,3 @@ impl NotificationSystem {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user