mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-24 21:30:17 +08:00
4c94e70f97
Add a complete HTTP proxy server implementation built on Axum framework, enabling local API request forwarding with automatic provider failover and load balancing capabilities. Backend Implementation (Rust): - Add proxy server module with 7 core components: * server.rs: Axum HTTP server lifecycle management (start/stop/status) * router.rs: API routing configuration for Claude/OpenAI/Gemini endpoints * handlers.rs: Request/response handling and transformation * forwarder.rs: Upstream forwarding logic with retry mechanism (652 lines) * error.rs: Comprehensive error handling and HTTP status mapping * types.rs: Shared types (ProxyConfig, ProxyStatus, ProxyServerInfo) * health.rs: Provider health check infrastructure Service Layer: - Add ProxyService (services/proxy.rs, 157 lines): * Manage proxy server lifecycle * Handle configuration updates * Track runtime status and metrics Database Layer: - Add proxy configuration DAO (dao/proxy.rs, 242 lines): * Persist proxy settings (listen address, port, timeout) * Store provider priority and availability flags - Update schema with proxy_config table (schema.rs): * Support runtime configuration persistence Tauri Commands: - Add 6 command endpoints (commands/proxy.rs): * start_proxy_server: Launch proxy server * stop_proxy_server: Gracefully shutdown server * get_proxy_status: Query runtime status * get_proxy_config: Retrieve current configuration * update_proxy_config: Modify settings without restart * is_proxy_running: Check server state Frontend Implementation (React + TypeScript): - Add ProxyPanel component (222 lines): * Real-time server status display * Start/stop controls * Provider availability monitoring - Add ProxySettingsDialog component (420 lines): * Configuration editor (address, port, timeout) * Provider priority management * Settings validation - Add React hooks: * useProxyConfig: Manage proxy configuration state * useProxyStatus: Poll and display server status - Add TypeScript types (types/proxy.ts): * Define ProxyConfig, ProxyStatus interfaces Provider Integration: - Extend Provider model with availability field (providers.rs): * Track provider health for failover logic - Update ProviderCard UI to display proxy status - Integrate proxy controls in Settings page Dependencies: - Add Axum 0.7 (async web framework) - Add Tower 0.4 (middleware and service abstractions) - Add Tower-HTTP (CORS layer) - Add Tokio sync primitives (oneshot, RwLock) Technical Details: - Graceful shutdown via oneshot channel - Shared state with Arc<RwLock<T>> for thread-safe config updates - CORS enabled for cross-origin frontend access - Request/response streaming support - Automatic retry with exponential backoff (forwarder) - API key extraction from multiple config formats (Claude/Codex/Gemini) File Statistics: - 41 files changed - 3491 insertions(+), 41 deletions(-) - Core modules: 1393 lines (server + forwarder + handlers) - Frontend UI: 642 lines (ProxyPanel + ProxySettingsDialog) - Database/DAO: 326 lines This implementation provides the foundation for advanced features like: - Multi-provider load balancing - Automatic failover on provider errors - Request logging and analytics - Usage tracking and cost monitoring
48 lines
1.3 KiB
Rust
48 lines
1.3 KiB
Rust
//! 代理服务相关的 Tauri 命令
|
|
//!
|
|
//! 提供前端调用的 API 接口
|
|
|
|
use crate::proxy::types::*;
|
|
use crate::store::AppState;
|
|
|
|
/// 启动代理服务器
|
|
#[tauri::command]
|
|
pub async fn start_proxy_server(
|
|
state: tauri::State<'_, AppState>,
|
|
) -> Result<ProxyServerInfo, String> {
|
|
state.proxy_service.start().await
|
|
}
|
|
|
|
/// 停止代理服务器
|
|
#[tauri::command]
|
|
pub async fn stop_proxy_server(state: tauri::State<'_, AppState>) -> Result<(), String> {
|
|
state.proxy_service.stop().await
|
|
}
|
|
|
|
/// 获取代理服务器状态
|
|
#[tauri::command]
|
|
pub async fn get_proxy_status(state: tauri::State<'_, AppState>) -> Result<ProxyStatus, String> {
|
|
state.proxy_service.get_status().await
|
|
}
|
|
|
|
/// 获取代理配置
|
|
#[tauri::command]
|
|
pub async fn get_proxy_config(state: tauri::State<'_, AppState>) -> Result<ProxyConfig, String> {
|
|
state.proxy_service.get_config().await
|
|
}
|
|
|
|
/// 更新代理配置
|
|
#[tauri::command]
|
|
pub async fn update_proxy_config(
|
|
state: tauri::State<'_, AppState>,
|
|
config: ProxyConfig,
|
|
) -> Result<(), String> {
|
|
state.proxy_service.update_config(&config).await
|
|
}
|
|
|
|
/// 检查代理服务器是否正在运行
|
|
#[tauri::command]
|
|
pub async fn is_proxy_running(state: tauri::State<'_, AppState>) -> Result<bool, String> {
|
|
Ok(state.proxy_service.is_running().await)
|
|
}
|