mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-06-16 13:34:04 +08:00
- Remove global proxy auto-start flag from config and UI. - Simplify per-app takeover start/stop and stop server when the last takeover is disabled. - Restore live takeover detection used for crash recovery. - Keep proxy_config.enabled column but always write 0 for compatibility. - Tests: not run (not requested).
180 lines
4.9 KiB
Rust
180 lines
4.9 KiB
Rust
//! 代理服务相关的 Tauri 命令
|
|
//!
|
|
//! 提供前端调用的 API 接口
|
|
|
|
use crate::proxy::types::*;
|
|
use crate::proxy::{CircuitBreakerConfig, CircuitBreakerStats};
|
|
use crate::store::AppState;
|
|
|
|
/// 启动代理服务器(仅启动服务,不接管 Live 配置)
|
|
#[tauri::command]
|
|
pub async fn start_proxy_server(
|
|
state: tauri::State<'_, AppState>,
|
|
) -> Result<ProxyServerInfo, String> {
|
|
state.proxy_service.start().await
|
|
}
|
|
|
|
/// 启动代理服务器(带 Live 配置接管)
|
|
#[tauri::command]
|
|
pub async fn start_proxy_with_takeover(
|
|
state: tauri::State<'_, AppState>,
|
|
) -> Result<ProxyServerInfo, String> {
|
|
state.proxy_service.start_with_takeover().await
|
|
}
|
|
|
|
/// 停止代理服务器(恢复 Live 配置)
|
|
#[tauri::command]
|
|
pub async fn stop_proxy_with_restore(state: tauri::State<'_, AppState>) -> Result<(), String> {
|
|
state.proxy_service.stop_with_restore().await
|
|
}
|
|
|
|
/// 获取各应用接管状态
|
|
#[tauri::command]
|
|
pub async fn get_proxy_takeover_status(
|
|
state: tauri::State<'_, AppState>,
|
|
) -> Result<ProxyTakeoverStatus, String> {
|
|
state.proxy_service.get_takeover_status().await
|
|
}
|
|
|
|
/// 为指定应用开启/关闭接管
|
|
#[tauri::command]
|
|
pub async fn set_proxy_takeover_for_app(
|
|
state: tauri::State<'_, AppState>,
|
|
app_type: String,
|
|
enabled: bool,
|
|
) -> Result<(), String> {
|
|
state
|
|
.proxy_service
|
|
.set_takeover_for_app(&app_type, enabled)
|
|
.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)
|
|
}
|
|
|
|
/// 检查是否处于 Live 接管模式
|
|
#[tauri::command]
|
|
pub async fn is_live_takeover_active(state: tauri::State<'_, AppState>) -> Result<bool, String> {
|
|
state.proxy_service.is_takeover_active().await
|
|
}
|
|
|
|
/// 代理模式下切换供应商(热切换)
|
|
#[tauri::command]
|
|
pub async fn switch_proxy_provider(
|
|
state: tauri::State<'_, AppState>,
|
|
app_type: String,
|
|
provider_id: String,
|
|
) -> Result<(), String> {
|
|
state
|
|
.proxy_service
|
|
.switch_proxy_target(&app_type, &provider_id)
|
|
.await
|
|
}
|
|
|
|
// ==================== 故障转移相关命令 ====================
|
|
|
|
/// 获取供应商健康状态
|
|
#[tauri::command]
|
|
pub async fn get_provider_health(
|
|
state: tauri::State<'_, AppState>,
|
|
provider_id: String,
|
|
app_type: String,
|
|
) -> Result<ProviderHealth, String> {
|
|
let db = &state.db;
|
|
db.get_provider_health(&provider_id, &app_type)
|
|
.await
|
|
.map_err(|e| e.to_string())
|
|
}
|
|
|
|
/// 重置熔断器
|
|
#[tauri::command]
|
|
pub async fn reset_circuit_breaker(
|
|
state: tauri::State<'_, AppState>,
|
|
provider_id: String,
|
|
app_type: String,
|
|
) -> Result<(), String> {
|
|
// 1. 重置数据库健康状态
|
|
let db = &state.db;
|
|
db.update_provider_health(&provider_id, &app_type, true, None)
|
|
.await
|
|
.map_err(|e| e.to_string())?;
|
|
|
|
// 2. 如果代理正在运行,重置内存中的熔断器状态
|
|
state
|
|
.proxy_service
|
|
.reset_provider_circuit_breaker(&provider_id, &app_type)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// 获取熔断器配置
|
|
#[tauri::command]
|
|
pub async fn get_circuit_breaker_config(
|
|
state: tauri::State<'_, AppState>,
|
|
) -> Result<CircuitBreakerConfig, String> {
|
|
let db = &state.db;
|
|
db.get_circuit_breaker_config()
|
|
.await
|
|
.map_err(|e| e.to_string())
|
|
}
|
|
|
|
/// 更新熔断器配置
|
|
#[tauri::command]
|
|
pub async fn update_circuit_breaker_config(
|
|
state: tauri::State<'_, AppState>,
|
|
config: CircuitBreakerConfig,
|
|
) -> Result<(), String> {
|
|
let db = &state.db;
|
|
|
|
// 1. 更新数据库配置
|
|
db.update_circuit_breaker_config(&config)
|
|
.await
|
|
.map_err(|e| e.to_string())?;
|
|
|
|
// 2. 如果代理正在运行,热更新内存中的熔断器配置
|
|
state
|
|
.proxy_service
|
|
.update_circuit_breaker_configs(config)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// 获取熔断器统计信息(仅当代理服务器运行时)
|
|
#[tauri::command]
|
|
pub async fn get_circuit_breaker_stats(
|
|
state: tauri::State<'_, AppState>,
|
|
provider_id: String,
|
|
app_type: String,
|
|
) -> Result<Option<CircuitBreakerStats>, String> {
|
|
// 这个功能需要访问运行中的代理服务器的内存状态
|
|
// 目前先返回 None,后续可以通过 ProxyService 暴露接口来实现
|
|
let _ = (state, provider_id, app_type);
|
|
Ok(None)
|
|
}
|