Files
clash-proxy/src-tauri/src/utils/network.rs

312 lines
10 KiB
Rust
Raw Normal View History

2025-05-03 23:17:51 +08:00
use anyhow::Result;
use parking_lot::Mutex;
use reqwest::{Client, ClientBuilder, Proxy, RequestBuilder, Response};
use std::{
sync::{
atomic::{AtomicUsize, Ordering},
Arc, Once,
},
time::{Duration, Instant},
};
use tokio::runtime::{Builder, Runtime};
refactor: optimize singleton macro usage with Default trait implementations (#4279) * refactor: implement DRY principle improvements across backend Major DRY violations identified and addressed: 1. **IPC Stream Monitor Pattern**: - Created `utils/ipc_monitor.rs` with generic `IpcStreamMonitor` trait - Added `IpcMonitorManager` for common async task management patterns - Eliminates duplication across traffic.rs, memory.rs, and logs.rs 2. **Singleton Pattern Duplication**: - Created `utils/singleton.rs` with `singleton\!` and `singleton_with_logging\!` macros - Replaces 16+ duplicate singleton implementations across codebase - Provides consistent, tested patterns for global instances 3. **macOS Activation Policy Refactoring**: - Consolidated 3 duplicate methods into single parameterized `set_activation_policy()` - Eliminated code duplication while maintaining backward compatibility - Reduced maintenance burden for macOS-specific functionality These improvements enhance maintainability, reduce bug potential, and ensure consistent patterns across the backend codebase. * fix: resolve test failures and clippy warnings - Fix doctest in singleton.rs by using rust,ignore syntax and proper code examples - Remove unused time::Instant import from ipc_monitor.rs - Add #[allow(dead_code)] attributes to future-use utility modules - All 11 unit tests now pass successfully - All clippy checks pass with -D warnings strict mode - Documentation tests properly ignore example code that requires full context * refactor: migrate code to use new utility tools (partial) Progress on systematic migration to use created utility tools: 1. **Reorganized IPC Monitor**: - Moved ipc_monitor.rs to src-tauri/src/ipc/monitor.rs for better organization - Updated module structure to emphasize IPC relationship 2. **IpcManager Singleton Migration**: - Replaced manual OnceLock singleton pattern with singleton_with_logging\! macro - Simplified initialization code and added consistent logging - Removed unused imports (OnceLock, logging::Type) 3. **ProxyRequestCache Singleton Migration**: - Migrated from once_cell::sync::OnceCell to singleton\! macro - Cleaner, more maintainable singleton pattern - Consistent with project-wide singleton approach These migrations demonstrate the utility and effectiveness of the created tools: - Less boilerplate code - Consistent patterns across codebase - Easier maintenance and debugging * feat: complete migration to new utility tools - phase 1 Successfully migrated core components to use the created utility tools: - Moved `ipc_monitor.rs` to `src-tauri/src/ipc/monitor.rs` - Better organization emphasizing IPC relationship - Updated module exports and imports - **IpcManager**: Migrated to `singleton_with_logging\!` macro - **ProxyRequestCache**: Migrated to `singleton\!` macro - Eliminated ~30 lines of boilerplate singleton code - Consistent logging and initialization patterns - Removed unused imports (OnceLock, once_cell, logging::Type) - Cleaner, more maintainable code structure - All 11 unit tests pass successfully - Zero compilation warnings - **Lines of code reduced**: ~50+ lines of boilerplate - **Consistency improved**: Unified singleton patterns - **Maintainability enhanced**: Centralized utility functions - **Test coverage maintained**: 100% test pass rate Remaining complex monitors (traffic, memory, logs) will be migrated to use the shared IPC monitoring patterns in the next phase, which requires careful refactoring of their streaming logic. * refactor: complete singleton pattern migration to utility macros Migrate remaining singleton patterns across the backend to use standardized utility macros, achieving significant code reduction and consistency improvements. - **LogsMonitor** (ipc/logs.rs): `OnceLock` → `singleton_with_logging\!` - **Sysopt** (core/sysopt.rs): `OnceCell` → `singleton_lazy\!` - **Tray** (core/tray/mod.rs): Complex `OnceCell` → `singleton_lazy\!` - **Handle** (core/handle.rs): `OnceCell` → `singleton\!` - **CoreManager** (core/core.rs): `OnceCell` → `singleton_lazy\!` - **TrafficMonitor** (ipc/traffic.rs): `OnceLock` → `singleton_lazy_with_logging\!` - **MemoryMonitor** (ipc/memory.rs): `OnceLock` → `singleton_lazy_with_logging\!` - `singleton_lazy\!` - For complex initialization patterns - `singleton_lazy_with_logging\!` - For complex initialization with logging - **Code Reduction**: -33 lines of boilerplate singleton code - **DRY Compliance**: Eliminated duplicate initialization patterns - **Consistency**: Unified singleton approach across codebase - **Maintainability**: Centralized singleton logic in utility macros - **Zero Breaking Changes**: All existing APIs remain compatible All tests pass and clippy warnings resolved. * refactor: optimize singleton macros using Default trait implementation Simplify singleton macro usage by implementing Default trait for complex initialization patterns, significantly improving code readability and maintainability. - **MemoryMonitor**: Move IPC client initialization to Default impl - **TrafficMonitor**: Move IPC client initialization to Default impl - **Sysopt**: Move Arc<Mutex> initialization to Default impl - **Tray**: Move struct field initialization to Default impl - **CoreManager**: Move Arc<Mutex> initialization to Default impl ```rust singleton_lazy_with_logging\!(MemoryMonitor, INSTANCE, "MemoryMonitor", || { let ipc_path_buf = ipc_path().unwrap(); let ipc_path = ipc_path_buf.to_str().unwrap_or_default(); let client = IpcStreamClient::new(ipc_path).unwrap(); MemoryMonitor::new(client) }); ``` ```rust impl Default for MemoryMonitor { /* initialization logic */ } singleton_lazy_with_logging\!(MemoryMonitor, INSTANCE, "MemoryMonitor", MemoryMonitor::default); ``` - **Code Reduction**: -17 lines of macro closure code (80%+ simplification) - **Separation of Concerns**: Initialization logic moved to proper Default impl - **Readability**: Single-line macro calls vs multi-line closures - **Testability**: Default implementations can be tested independently - **Rust Idioms**: Using standard Default trait pattern - **Performance**: Function calls more efficient than closures All tests pass and clippy warnings resolved. * refactor: implement MonitorData and StreamingParser traits for IPC monitors * refactor: add timeout and retry_interval fields to IpcStreamMonitor; update TrafficMonitorState to derive Default * refactor: migrate AppHandleManager to unified singleton control - Replace manual singleton implementation with singleton_with_logging\! macro - Remove std::sync::Once dependency in favor of OnceLock-based pattern - Improve error handling for macOS activation policy methods - Maintain thread safety with parking_lot::Mutex for AppHandle storage - Add proper initialization check to prevent duplicate handle assignment - Enhance logging consistency across AppHandleManager operations * refactor: improve hotkey management with enum-based operations - Add HotkeyFunction enum for type-safe function selection - Add SystemHotkey enum for predefined system shortcuts - Implement Display and FromStr traits for type conversions - Replace string-based hotkey registration with enum methods - Add register_system_hotkey() and unregister_system_hotkey() methods - Maintain backward compatibility with string-based register() method - Migrate singleton pattern to use singleton_with_logging\! macro - Extract hotkey function execution logic into centralized execute_function() - Update lib.rs to use new enum-based SystemHotkey operations - Improve type safety and reduce string manipulation errors Benefits: - Type safety prevents invalid hotkey function names - Centralized function execution reduces code duplication - Enum-based API provides better IDE autocomplete support - Maintains full backward compatibility with existing configurations * fix: resolve LightWeightState initialization order panic - Modify with_lightweight_status() to safely handle unmanaged state using try_state() - Return Option<R> instead of R to gracefully handle state unavailability - Update is_in_lightweight_mode() to use unwrap_or(false) for safe defaults - Add state availability check in auto_lightweight_mode_init() before access - Maintain singleton check priority while preventing early state access panics - Fix clippy warnings for redundant pattern matching Resolves runtime panic: "state() called before manage() for LightWeightState" * refactor: add unreachable patterns for non-macOS in hotkey handling * refactor: simplify SystemHotkey enum by removing redundant cfg attributes * refactor: add macOS conditional compilation for system hotkey registration methods * refactor: streamline hotkey unregistration and error logging for macOS
2025-07-31 14:35:13 +08:00
use crate::{config::Config, logging, singleton_lazy, utils::logging::Type};
2025-05-03 23:17:51 +08:00
// HTTP2 相关
const H2_CONNECTION_WINDOW_SIZE: u32 = 1024 * 1024;
const H2_STREAM_WINDOW_SIZE: u32 = 1024 * 1024;
const H2_MAX_FRAME_SIZE: u32 = 16 * 1024;
const H2_KEEP_ALIVE_INTERVAL: Duration = Duration::from_secs(5);
const H2_KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(5);
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
const POOL_MAX_IDLE_PER_HOST: usize = 5;
const POOL_IDLE_TIMEOUT: Duration = Duration::from_secs(15);
2025-05-03 23:17:51 +08:00
/// 网络管理器
pub struct NetworkManager {
runtime: Arc<Runtime>,
self_proxy_client: Mutex<Option<Client>>,
system_proxy_client: Mutex<Option<Client>>,
no_proxy_client: Mutex<Option<Client>>,
init: Once,
last_connection_error: Mutex<Option<(Instant, String)>>,
connection_error_count: AtomicUsize,
}
refactor: optimize singleton macro usage with Default trait implementations (#4279) * refactor: implement DRY principle improvements across backend Major DRY violations identified and addressed: 1. **IPC Stream Monitor Pattern**: - Created `utils/ipc_monitor.rs` with generic `IpcStreamMonitor` trait - Added `IpcMonitorManager` for common async task management patterns - Eliminates duplication across traffic.rs, memory.rs, and logs.rs 2. **Singleton Pattern Duplication**: - Created `utils/singleton.rs` with `singleton\!` and `singleton_with_logging\!` macros - Replaces 16+ duplicate singleton implementations across codebase - Provides consistent, tested patterns for global instances 3. **macOS Activation Policy Refactoring**: - Consolidated 3 duplicate methods into single parameterized `set_activation_policy()` - Eliminated code duplication while maintaining backward compatibility - Reduced maintenance burden for macOS-specific functionality These improvements enhance maintainability, reduce bug potential, and ensure consistent patterns across the backend codebase. * fix: resolve test failures and clippy warnings - Fix doctest in singleton.rs by using rust,ignore syntax and proper code examples - Remove unused time::Instant import from ipc_monitor.rs - Add #[allow(dead_code)] attributes to future-use utility modules - All 11 unit tests now pass successfully - All clippy checks pass with -D warnings strict mode - Documentation tests properly ignore example code that requires full context * refactor: migrate code to use new utility tools (partial) Progress on systematic migration to use created utility tools: 1. **Reorganized IPC Monitor**: - Moved ipc_monitor.rs to src-tauri/src/ipc/monitor.rs for better organization - Updated module structure to emphasize IPC relationship 2. **IpcManager Singleton Migration**: - Replaced manual OnceLock singleton pattern with singleton_with_logging\! macro - Simplified initialization code and added consistent logging - Removed unused imports (OnceLock, logging::Type) 3. **ProxyRequestCache Singleton Migration**: - Migrated from once_cell::sync::OnceCell to singleton\! macro - Cleaner, more maintainable singleton pattern - Consistent with project-wide singleton approach These migrations demonstrate the utility and effectiveness of the created tools: - Less boilerplate code - Consistent patterns across codebase - Easier maintenance and debugging * feat: complete migration to new utility tools - phase 1 Successfully migrated core components to use the created utility tools: - Moved `ipc_monitor.rs` to `src-tauri/src/ipc/monitor.rs` - Better organization emphasizing IPC relationship - Updated module exports and imports - **IpcManager**: Migrated to `singleton_with_logging\!` macro - **ProxyRequestCache**: Migrated to `singleton\!` macro - Eliminated ~30 lines of boilerplate singleton code - Consistent logging and initialization patterns - Removed unused imports (OnceLock, once_cell, logging::Type) - Cleaner, more maintainable code structure - All 11 unit tests pass successfully - Zero compilation warnings - **Lines of code reduced**: ~50+ lines of boilerplate - **Consistency improved**: Unified singleton patterns - **Maintainability enhanced**: Centralized utility functions - **Test coverage maintained**: 100% test pass rate Remaining complex monitors (traffic, memory, logs) will be migrated to use the shared IPC monitoring patterns in the next phase, which requires careful refactoring of their streaming logic. * refactor: complete singleton pattern migration to utility macros Migrate remaining singleton patterns across the backend to use standardized utility macros, achieving significant code reduction and consistency improvements. - **LogsMonitor** (ipc/logs.rs): `OnceLock` → `singleton_with_logging\!` - **Sysopt** (core/sysopt.rs): `OnceCell` → `singleton_lazy\!` - **Tray** (core/tray/mod.rs): Complex `OnceCell` → `singleton_lazy\!` - **Handle** (core/handle.rs): `OnceCell` → `singleton\!` - **CoreManager** (core/core.rs): `OnceCell` → `singleton_lazy\!` - **TrafficMonitor** (ipc/traffic.rs): `OnceLock` → `singleton_lazy_with_logging\!` - **MemoryMonitor** (ipc/memory.rs): `OnceLock` → `singleton_lazy_with_logging\!` - `singleton_lazy\!` - For complex initialization patterns - `singleton_lazy_with_logging\!` - For complex initialization with logging - **Code Reduction**: -33 lines of boilerplate singleton code - **DRY Compliance**: Eliminated duplicate initialization patterns - **Consistency**: Unified singleton approach across codebase - **Maintainability**: Centralized singleton logic in utility macros - **Zero Breaking Changes**: All existing APIs remain compatible All tests pass and clippy warnings resolved. * refactor: optimize singleton macros using Default trait implementation Simplify singleton macro usage by implementing Default trait for complex initialization patterns, significantly improving code readability and maintainability. - **MemoryMonitor**: Move IPC client initialization to Default impl - **TrafficMonitor**: Move IPC client initialization to Default impl - **Sysopt**: Move Arc<Mutex> initialization to Default impl - **Tray**: Move struct field initialization to Default impl - **CoreManager**: Move Arc<Mutex> initialization to Default impl ```rust singleton_lazy_with_logging\!(MemoryMonitor, INSTANCE, "MemoryMonitor", || { let ipc_path_buf = ipc_path().unwrap(); let ipc_path = ipc_path_buf.to_str().unwrap_or_default(); let client = IpcStreamClient::new(ipc_path).unwrap(); MemoryMonitor::new(client) }); ``` ```rust impl Default for MemoryMonitor { /* initialization logic */ } singleton_lazy_with_logging\!(MemoryMonitor, INSTANCE, "MemoryMonitor", MemoryMonitor::default); ``` - **Code Reduction**: -17 lines of macro closure code (80%+ simplification) - **Separation of Concerns**: Initialization logic moved to proper Default impl - **Readability**: Single-line macro calls vs multi-line closures - **Testability**: Default implementations can be tested independently - **Rust Idioms**: Using standard Default trait pattern - **Performance**: Function calls more efficient than closures All tests pass and clippy warnings resolved. * refactor: implement MonitorData and StreamingParser traits for IPC monitors * refactor: add timeout and retry_interval fields to IpcStreamMonitor; update TrafficMonitorState to derive Default * refactor: migrate AppHandleManager to unified singleton control - Replace manual singleton implementation with singleton_with_logging\! macro - Remove std::sync::Once dependency in favor of OnceLock-based pattern - Improve error handling for macOS activation policy methods - Maintain thread safety with parking_lot::Mutex for AppHandle storage - Add proper initialization check to prevent duplicate handle assignment - Enhance logging consistency across AppHandleManager operations * refactor: improve hotkey management with enum-based operations - Add HotkeyFunction enum for type-safe function selection - Add SystemHotkey enum for predefined system shortcuts - Implement Display and FromStr traits for type conversions - Replace string-based hotkey registration with enum methods - Add register_system_hotkey() and unregister_system_hotkey() methods - Maintain backward compatibility with string-based register() method - Migrate singleton pattern to use singleton_with_logging\! macro - Extract hotkey function execution logic into centralized execute_function() - Update lib.rs to use new enum-based SystemHotkey operations - Improve type safety and reduce string manipulation errors Benefits: - Type safety prevents invalid hotkey function names - Centralized function execution reduces code duplication - Enum-based API provides better IDE autocomplete support - Maintains full backward compatibility with existing configurations * fix: resolve LightWeightState initialization order panic - Modify with_lightweight_status() to safely handle unmanaged state using try_state() - Return Option<R> instead of R to gracefully handle state unavailability - Update is_in_lightweight_mode() to use unwrap_or(false) for safe defaults - Add state availability check in auto_lightweight_mode_init() before access - Maintain singleton check priority while preventing early state access panics - Fix clippy warnings for redundant pattern matching Resolves runtime panic: "state() called before manage() for LightWeightState" * refactor: add unreachable patterns for non-macOS in hotkey handling * refactor: simplify SystemHotkey enum by removing redundant cfg attributes * refactor: add macOS conditional compilation for system hotkey registration methods * refactor: streamline hotkey unregistration and error logging for macOS
2025-07-31 14:35:13 +08:00
// Use singleton_lazy macro to replace lazy_static!
singleton_lazy!(NetworkManager, NETWORK_MANAGER, NetworkManager::new);
impl NetworkManager {
fn new() -> Self {
// 创建专用的异步运行时线程数限制为4个
let runtime = Builder::new_multi_thread()
.worker_threads(4)
.thread_name("clash-verge-network")
.enable_io()
.enable_time()
.build()
.expect("Failed to create network runtime");
NetworkManager {
runtime: Arc::new(runtime),
self_proxy_client: Mutex::new(None),
system_proxy_client: Mutex::new(None),
no_proxy_client: Mutex::new(None),
init: Once::new(),
last_connection_error: Mutex::new(None),
connection_error_count: AtomicUsize::new(0),
}
}
/// 初始化网络客户端
pub fn init(&self) {
self.init.call_once(|| {
self.runtime.spawn(async {
logging!(info, Type::Network, true, "初始化网络管理器");
// 创建无代理客户端
let no_proxy_client = ClientBuilder::new()
.use_rustls_tls()
.no_proxy()
.pool_max_idle_per_host(POOL_MAX_IDLE_PER_HOST)
.pool_idle_timeout(POOL_IDLE_TIMEOUT)
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(30))
.build()
.expect("Failed to build no_proxy client");
refactor: optimize singleton macro usage with Default trait implementations (#4279) * refactor: implement DRY principle improvements across backend Major DRY violations identified and addressed: 1. **IPC Stream Monitor Pattern**: - Created `utils/ipc_monitor.rs` with generic `IpcStreamMonitor` trait - Added `IpcMonitorManager` for common async task management patterns - Eliminates duplication across traffic.rs, memory.rs, and logs.rs 2. **Singleton Pattern Duplication**: - Created `utils/singleton.rs` with `singleton\!` and `singleton_with_logging\!` macros - Replaces 16+ duplicate singleton implementations across codebase - Provides consistent, tested patterns for global instances 3. **macOS Activation Policy Refactoring**: - Consolidated 3 duplicate methods into single parameterized `set_activation_policy()` - Eliminated code duplication while maintaining backward compatibility - Reduced maintenance burden for macOS-specific functionality These improvements enhance maintainability, reduce bug potential, and ensure consistent patterns across the backend codebase. * fix: resolve test failures and clippy warnings - Fix doctest in singleton.rs by using rust,ignore syntax and proper code examples - Remove unused time::Instant import from ipc_monitor.rs - Add #[allow(dead_code)] attributes to future-use utility modules - All 11 unit tests now pass successfully - All clippy checks pass with -D warnings strict mode - Documentation tests properly ignore example code that requires full context * refactor: migrate code to use new utility tools (partial) Progress on systematic migration to use created utility tools: 1. **Reorganized IPC Monitor**: - Moved ipc_monitor.rs to src-tauri/src/ipc/monitor.rs for better organization - Updated module structure to emphasize IPC relationship 2. **IpcManager Singleton Migration**: - Replaced manual OnceLock singleton pattern with singleton_with_logging\! macro - Simplified initialization code and added consistent logging - Removed unused imports (OnceLock, logging::Type) 3. **ProxyRequestCache Singleton Migration**: - Migrated from once_cell::sync::OnceCell to singleton\! macro - Cleaner, more maintainable singleton pattern - Consistent with project-wide singleton approach These migrations demonstrate the utility and effectiveness of the created tools: - Less boilerplate code - Consistent patterns across codebase - Easier maintenance and debugging * feat: complete migration to new utility tools - phase 1 Successfully migrated core components to use the created utility tools: - Moved `ipc_monitor.rs` to `src-tauri/src/ipc/monitor.rs` - Better organization emphasizing IPC relationship - Updated module exports and imports - **IpcManager**: Migrated to `singleton_with_logging\!` macro - **ProxyRequestCache**: Migrated to `singleton\!` macro - Eliminated ~30 lines of boilerplate singleton code - Consistent logging and initialization patterns - Removed unused imports (OnceLock, once_cell, logging::Type) - Cleaner, more maintainable code structure - All 11 unit tests pass successfully - Zero compilation warnings - **Lines of code reduced**: ~50+ lines of boilerplate - **Consistency improved**: Unified singleton patterns - **Maintainability enhanced**: Centralized utility functions - **Test coverage maintained**: 100% test pass rate Remaining complex monitors (traffic, memory, logs) will be migrated to use the shared IPC monitoring patterns in the next phase, which requires careful refactoring of their streaming logic. * refactor: complete singleton pattern migration to utility macros Migrate remaining singleton patterns across the backend to use standardized utility macros, achieving significant code reduction and consistency improvements. - **LogsMonitor** (ipc/logs.rs): `OnceLock` → `singleton_with_logging\!` - **Sysopt** (core/sysopt.rs): `OnceCell` → `singleton_lazy\!` - **Tray** (core/tray/mod.rs): Complex `OnceCell` → `singleton_lazy\!` - **Handle** (core/handle.rs): `OnceCell` → `singleton\!` - **CoreManager** (core/core.rs): `OnceCell` → `singleton_lazy\!` - **TrafficMonitor** (ipc/traffic.rs): `OnceLock` → `singleton_lazy_with_logging\!` - **MemoryMonitor** (ipc/memory.rs): `OnceLock` → `singleton_lazy_with_logging\!` - `singleton_lazy\!` - For complex initialization patterns - `singleton_lazy_with_logging\!` - For complex initialization with logging - **Code Reduction**: -33 lines of boilerplate singleton code - **DRY Compliance**: Eliminated duplicate initialization patterns - **Consistency**: Unified singleton approach across codebase - **Maintainability**: Centralized singleton logic in utility macros - **Zero Breaking Changes**: All existing APIs remain compatible All tests pass and clippy warnings resolved. * refactor: optimize singleton macros using Default trait implementation Simplify singleton macro usage by implementing Default trait for complex initialization patterns, significantly improving code readability and maintainability. - **MemoryMonitor**: Move IPC client initialization to Default impl - **TrafficMonitor**: Move IPC client initialization to Default impl - **Sysopt**: Move Arc<Mutex> initialization to Default impl - **Tray**: Move struct field initialization to Default impl - **CoreManager**: Move Arc<Mutex> initialization to Default impl ```rust singleton_lazy_with_logging\!(MemoryMonitor, INSTANCE, "MemoryMonitor", || { let ipc_path_buf = ipc_path().unwrap(); let ipc_path = ipc_path_buf.to_str().unwrap_or_default(); let client = IpcStreamClient::new(ipc_path).unwrap(); MemoryMonitor::new(client) }); ``` ```rust impl Default for MemoryMonitor { /* initialization logic */ } singleton_lazy_with_logging\!(MemoryMonitor, INSTANCE, "MemoryMonitor", MemoryMonitor::default); ``` - **Code Reduction**: -17 lines of macro closure code (80%+ simplification) - **Separation of Concerns**: Initialization logic moved to proper Default impl - **Readability**: Single-line macro calls vs multi-line closures - **Testability**: Default implementations can be tested independently - **Rust Idioms**: Using standard Default trait pattern - **Performance**: Function calls more efficient than closures All tests pass and clippy warnings resolved. * refactor: implement MonitorData and StreamingParser traits for IPC monitors * refactor: add timeout and retry_interval fields to IpcStreamMonitor; update TrafficMonitorState to derive Default * refactor: migrate AppHandleManager to unified singleton control - Replace manual singleton implementation with singleton_with_logging\! macro - Remove std::sync::Once dependency in favor of OnceLock-based pattern - Improve error handling for macOS activation policy methods - Maintain thread safety with parking_lot::Mutex for AppHandle storage - Add proper initialization check to prevent duplicate handle assignment - Enhance logging consistency across AppHandleManager operations * refactor: improve hotkey management with enum-based operations - Add HotkeyFunction enum for type-safe function selection - Add SystemHotkey enum for predefined system shortcuts - Implement Display and FromStr traits for type conversions - Replace string-based hotkey registration with enum methods - Add register_system_hotkey() and unregister_system_hotkey() methods - Maintain backward compatibility with string-based register() method - Migrate singleton pattern to use singleton_with_logging\! macro - Extract hotkey function execution logic into centralized execute_function() - Update lib.rs to use new enum-based SystemHotkey operations - Improve type safety and reduce string manipulation errors Benefits: - Type safety prevents invalid hotkey function names - Centralized function execution reduces code duplication - Enum-based API provides better IDE autocomplete support - Maintains full backward compatibility with existing configurations * fix: resolve LightWeightState initialization order panic - Modify with_lightweight_status() to safely handle unmanaged state using try_state() - Return Option<R> instead of R to gracefully handle state unavailability - Update is_in_lightweight_mode() to use unwrap_or(false) for safe defaults - Add state availability check in auto_lightweight_mode_init() before access - Maintain singleton check priority while preventing early state access panics - Fix clippy warnings for redundant pattern matching Resolves runtime panic: "state() called before manage() for LightWeightState" * refactor: add unreachable patterns for non-macOS in hotkey handling * refactor: simplify SystemHotkey enum by removing redundant cfg attributes * refactor: add macOS conditional compilation for system hotkey registration methods * refactor: streamline hotkey unregistration and error logging for macOS
2025-07-31 14:35:13 +08:00
let mut no_proxy_guard = NetworkManager::global().no_proxy_client.lock();
*no_proxy_guard = Some(no_proxy_client);
logging!(info, Type::Network, true, "网络管理器初始化完成");
});
});
}
2025-05-03 23:17:51 +08:00
fn record_connection_error(&self, error: &str) {
let mut last_error = self.last_connection_error.lock();
2025-05-03 23:17:51 +08:00
*last_error = Some((Instant::now(), error.to_string()));
self.connection_error_count.fetch_add(1, Ordering::Relaxed);
2025-05-03 23:17:51 +08:00
}
fn should_reset_clients(&self) -> bool {
let error_count = self.connection_error_count.load(Ordering::Relaxed);
let last_error = self.last_connection_error.lock();
2025-05-03 23:17:51 +08:00
if error_count > 5 {
return true;
}
if let Some((time, _)) = *last_error {
if time.elapsed() < Duration::from_secs(30) && error_count > 2 {
return true;
}
}
false
}
pub fn reset_clients(&self) {
2025-05-03 23:17:51 +08:00
logging!(info, Type::Network, true, "正在重置所有HTTP客户端");
{
let mut client = self.self_proxy_client.lock();
2025-05-03 23:17:51 +08:00
*client = None;
}
{
let mut client = self.system_proxy_client.lock();
2025-05-03 23:17:51 +08:00
*client = None;
}
{
let mut client = self.no_proxy_client.lock();
2025-05-03 23:17:51 +08:00
*client = None;
}
self.connection_error_count.store(0, Ordering::Relaxed);
2025-05-03 23:17:51 +08:00
}
/// 创建带有自定义选项的HTTP请求
pub fn create_request(
&self,
url: &str,
proxy_type: ProxyType,
timeout_secs: Option<u64>,
user_agent: Option<String>,
accept_invalid_certs: bool,
) -> RequestBuilder {
2025-05-03 23:17:51 +08:00
if self.should_reset_clients() {
self.reset_clients();
}
let mut builder = ClientBuilder::new()
.use_rustls_tls()
.pool_max_idle_per_host(POOL_MAX_IDLE_PER_HOST)
.pool_idle_timeout(POOL_IDLE_TIMEOUT)
2025-05-03 23:17:51 +08:00
.connect_timeout(DEFAULT_CONNECT_TIMEOUT)
.http2_initial_stream_window_size(H2_STREAM_WINDOW_SIZE)
.http2_initial_connection_window_size(H2_CONNECTION_WINDOW_SIZE)
.http2_adaptive_window(true)
.http2_keep_alive_interval(Some(H2_KEEP_ALIVE_INTERVAL))
.http2_keep_alive_timeout(H2_KEEP_ALIVE_TIMEOUT)
.http2_max_frame_size(H2_MAX_FRAME_SIZE)
.tcp_keepalive(Some(Duration::from_secs(10)))
.http2_max_header_list_size(16 * 1024);
if let Some(timeout) = timeout_secs {
builder = builder.timeout(Duration::from_secs(timeout));
} else {
2025-05-03 23:17:51 +08:00
builder = builder.timeout(DEFAULT_REQUEST_TIMEOUT);
}
match proxy_type {
ProxyType::None => {
builder = builder.no_proxy();
}
ProxyType::Localhost => {
let port = Config::verge()
.latest_ref()
.verge_mixed_port
.unwrap_or(Config::clash().latest_ref().get_mixed_port());
let proxy_scheme = format!("http://127.0.0.1:{port}");
if let Ok(proxy) = Proxy::http(&proxy_scheme) {
builder = builder.proxy(proxy);
}
if let Ok(proxy) = Proxy::https(&proxy_scheme) {
builder = builder.proxy(proxy);
}
if let Ok(proxy) = Proxy::all(&proxy_scheme) {
builder = builder.proxy(proxy);
}
}
ProxyType::System => {
use sysproxy::Sysproxy;
if let Ok(p @ Sysproxy { enable: true, .. }) = Sysproxy::get_system_proxy() {
let proxy_scheme = format!("http://{}:{}", p.host, p.port);
if let Ok(proxy) = Proxy::http(&proxy_scheme) {
builder = builder.proxy(proxy);
}
if let Ok(proxy) = Proxy::https(&proxy_scheme) {
builder = builder.proxy(proxy);
}
if let Ok(proxy) = Proxy::all(&proxy_scheme) {
builder = builder.proxy(proxy);
}
}
}
}
builder = builder.danger_accept_invalid_certs(accept_invalid_certs);
if let Some(ua) = user_agent {
builder = builder.user_agent(ua);
} else {
use crate::utils::resolve::VERSION;
let version = match VERSION.get() {
Some(v) => format!("clash-verge/v{v}"),
None => "clash-verge/unknown".to_string(),
};
builder = builder.user_agent(version);
}
let client = builder.build().expect("Failed to build custom HTTP client");
client.get(url)
}
/* /// 执行GET请求添加错误跟踪
pub async fn get(
&self,
url: &str,
proxy_type: ProxyType,
timeout_secs: Option<u64>,
user_agent: Option<String>,
accept_invalid_certs: bool,
) -> Result<Response> {
2025-05-03 23:17:51 +08:00
let request = self.create_request(
url,
proxy_type,
timeout_secs,
user_agent,
accept_invalid_certs,
2025-05-03 23:17:51 +08:00
);
let timeout_duration = timeout_secs.unwrap_or(30);
match tokio::time::timeout(Duration::from_secs(timeout_duration), request.send()).await {
Ok(result) => match result {
Ok(response) => Ok(response),
Err(e) => {
self.record_connection_error(&e.to_string());
Err(anyhow::anyhow!("Failed to send HTTP request: {}", e))
}
},
Err(_) => {
self.record_connection_error("Request timeout");
Err(anyhow::anyhow!(
"HTTP request timed out after {} seconds",
timeout_duration
))
}
}
} */
2025-05-03 23:17:51 +08:00
pub async fn get_with_interrupt(
&self,
url: &str,
proxy_type: ProxyType,
timeout_secs: Option<u64>,
user_agent: Option<String>,
accept_invalid_certs: bool,
) -> Result<Response> {
2025-05-03 23:17:51 +08:00
let request = self.create_request(
url,
proxy_type,
timeout_secs,
user_agent,
accept_invalid_certs,
);
let timeout_duration = timeout_secs.unwrap_or(20);
let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel::<()>();
2025-05-03 23:17:51 +08:00
let url_clone = url.to_string();
let watchdog = tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(timeout_duration)).await;
let _ = cancel_tx.send(());
logging!(warn, Type::Network, true, "请求超时取消: {}", url_clone);
});
let result = tokio::select! {
result = request.send() => result,
2025-05-03 23:17:51 +08:00
_ = cancel_rx => {
self.record_connection_error(&format!("Request interrupted for: {url}"));
2025-05-03 23:17:51 +08:00
return Err(anyhow::anyhow!("Request interrupted after {} seconds", timeout_duration));
}
2025-05-03 23:17:51 +08:00
};
watchdog.abort();
2025-05-03 23:17:51 +08:00
match result {
Ok(response) => Ok(response),
Err(e) => {
self.record_connection_error(&e.to_string());
Err(anyhow::anyhow!("Failed to send HTTP request: {}", e))
}
}
}
}
/// 代理类型
#[derive(Debug, Clone, Copy)]
pub enum ProxyType {
None,
Localhost,
System,
}