mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-29 09:37:37 +08:00
e7badb1a24
* refactor(ui): simplify UpdateBadge to minimal dot indicator * feat(provider): add individual test and proxy config for providers Add support for provider-specific model test and proxy configurations: - Add ProviderTestConfig and ProviderProxyConfig types in Rust and TypeScript - Create ProviderAdvancedConfig component with collapsible panels - Update stream_check service to merge provider config with global config - Proxy config UI follows global proxy style (single URL input) Provider-level configs stored in meta field, no database schema changes needed. * feat(ui): add failover toggle and improve proxy controls - Add FailoverToggle component with slide animation - Simplify ProxyToggle style to match FailoverToggle - Add usage statistics button when proxy is active - Fix i18n parameter passing for failover messages - Add missing failover translation keys (inQueue, addQueue, priority) - Replace AboutSection icon with app logo * fix(proxy): support system proxy fallback and provider-level proxy config - Remove no_proxy() calls in http_client.rs to allow system proxy fallback - Add get_for_provider() to build HTTP client with provider-specific proxy - Update forwarder.rs and stream_check.rs to use provider proxy config - Fix EditProviderDialog.tsx to include provider.meta in useMemo deps - Add useEffect in ProviderAdvancedConfig.tsx to sync expand state Fixes #636 Fixes #583 * fix(ui): sync toast theme with app setting * feat(settings): add log config management Fixes #612 Fixes #514 * fix(proxy): increase request body size limit to 200MB Fixes #666 * docs(proxy): update timeout config descriptions and defaults Fixes #612 * fix(proxy): filter x-goog-api-key header to prevent duplication * fix(proxy): prevent proxy recursion when system proxy points to localhost Detect if HTTP_PROXY, HTTPS_PROXY, or ALL_PROXY environment variables point to loopback addresses (localhost, 127.0.0.1), and bypass system proxy in such cases to avoid infinite request loops. * fix(i18n): add providerAdvanced i18n keys and fix failover toast parameter - Add providerAdvanced.* i18n keys to en.json, zh.json, and ja.json - Fix failover toggleFailed toast to pass detail parameter - Remove Chinese fallback text from UI for English/Japanese users * fix(tray): restore tray-provider events and enable Auto failover properly - Emit provider-switched event on tray provider click (backward compatibility) - Auto button now: starts proxy, takes over live config, enables failover * fix(log): enable dynamic log level and single file mode - Initialize log at Trace level for dynamic adjustment - Change rotation strategy to KeepSome(1) for single file - Set max file size to 1GB - Delete old log file on startup for clean start * fix(tray): fix clippy uninlined format args warning Use inline format arguments: {app_type_str} instead of {} * fix(provider): allow typing :// in endpoint URL inputs Change input type from "url" to "text" to prevent browser URL validation from blocking :// input. Closes #681 * fix(stream-check): use Gemini native streaming API format - Change endpoint from OpenAI-compatible to native streamGenerateContent - Add alt=sse parameter for SSE format response - Use x-goog-api-key header instead of Bearer token - Convert request body to Gemini contents/parts format * feat(proxy): add request logging for debugging Add debug logs for outgoing requests including URL and body content with byte size, matching the existing response logging format. * fix(log): prevent usize underflow in KeepSome rotation strategy KeepSome(n) internally computes n-2, so n=1 causes underflow. Use KeepSome(2) as the minimum safe value.
208 lines
7.3 KiB
Rust
208 lines
7.3 KiB
Rust
//! 通用设置数据访问对象
|
||
//!
|
||
//! 提供键值对形式的通用设置存储。
|
||
|
||
use crate::database::{lock_conn, Database};
|
||
use crate::error::AppError;
|
||
use rusqlite::params;
|
||
|
||
impl Database {
|
||
/// 获取设置值
|
||
pub fn get_setting(&self, key: &str) -> Result<Option<String>, AppError> {
|
||
let conn = lock_conn!(self.conn);
|
||
let mut stmt = conn
|
||
.prepare("SELECT value FROM settings WHERE key = ?1")
|
||
.map_err(|e| AppError::Database(e.to_string()))?;
|
||
|
||
let mut rows = stmt
|
||
.query(params![key])
|
||
.map_err(|e| AppError::Database(e.to_string()))?;
|
||
|
||
if let Some(row) = rows.next().map_err(|e| AppError::Database(e.to_string()))? {
|
||
Ok(Some(
|
||
row.get(0).map_err(|e| AppError::Database(e.to_string()))?,
|
||
))
|
||
} else {
|
||
Ok(None)
|
||
}
|
||
}
|
||
|
||
/// 设置值
|
||
pub fn set_setting(&self, key: &str, value: &str) -> Result<(), AppError> {
|
||
let conn = lock_conn!(self.conn);
|
||
conn.execute(
|
||
"INSERT OR REPLACE INTO settings (key, value) VALUES (?1, ?2)",
|
||
params![key, value],
|
||
)
|
||
.map_err(|e| AppError::Database(e.to_string()))?;
|
||
Ok(())
|
||
}
|
||
|
||
// --- Config Snippets 辅助方法 ---
|
||
|
||
/// 获取通用配置片段
|
||
pub fn get_config_snippet(&self, app_type: &str) -> Result<Option<String>, AppError> {
|
||
self.get_setting(&format!("common_config_{app_type}"))
|
||
}
|
||
|
||
/// 设置通用配置片段
|
||
pub fn set_config_snippet(
|
||
&self,
|
||
app_type: &str,
|
||
snippet: Option<String>,
|
||
) -> Result<(), AppError> {
|
||
let key = format!("common_config_{app_type}");
|
||
if let Some(value) = snippet {
|
||
self.set_setting(&key, &value)
|
||
} else {
|
||
// 如果为 None 则删除
|
||
let conn = lock_conn!(self.conn);
|
||
conn.execute("DELETE FROM settings WHERE key = ?1", params![key])
|
||
.map_err(|e| AppError::Database(e.to_string()))?;
|
||
Ok(())
|
||
}
|
||
}
|
||
|
||
// --- 全局出站代理 ---
|
||
|
||
/// 全局代理 URL 的存储键名
|
||
const GLOBAL_PROXY_URL_KEY: &'static str = "global_proxy_url";
|
||
|
||
/// 获取全局出站代理 URL
|
||
///
|
||
/// 返回 None 表示未配置或已清除代理(直连)
|
||
/// 返回 Some(url) 表示已配置代理
|
||
pub fn get_global_proxy_url(&self) -> Result<Option<String>, AppError> {
|
||
self.get_setting(Self::GLOBAL_PROXY_URL_KEY)
|
||
}
|
||
|
||
/// 设置全局出站代理 URL
|
||
///
|
||
/// - 传入非空字符串:启用代理
|
||
/// - 传入空字符串或 None:清除代理设置(直连)
|
||
pub fn set_global_proxy_url(&self, url: Option<&str>) -> Result<(), AppError> {
|
||
match url {
|
||
Some(u) if !u.trim().is_empty() => {
|
||
self.set_setting(Self::GLOBAL_PROXY_URL_KEY, u.trim())
|
||
}
|
||
_ => {
|
||
// 清除代理设置
|
||
let conn = lock_conn!(self.conn);
|
||
conn.execute(
|
||
"DELETE FROM settings WHERE key = ?1",
|
||
params![Self::GLOBAL_PROXY_URL_KEY],
|
||
)
|
||
.map_err(|e| AppError::Database(e.to_string()))?;
|
||
Ok(())
|
||
}
|
||
}
|
||
}
|
||
|
||
// --- 代理接管状态管理(已废弃,使用 proxy_config.enabled 替代)---
|
||
|
||
/// 获取指定应用的代理接管状态
|
||
///
|
||
/// **已废弃**: 请使用 `proxy_config.enabled` 字段替代
|
||
/// 此方法仅用于数据库迁移时读取旧数据
|
||
#[deprecated(since = "3.9.0", note = "使用 get_proxy_config_for_app().enabled 替代")]
|
||
pub fn get_proxy_takeover_enabled(&self, app_type: &str) -> Result<bool, AppError> {
|
||
let key = format!("proxy_takeover_{app_type}");
|
||
match self.get_setting(&key)? {
|
||
Some(value) => Ok(value == "true"),
|
||
None => Ok(false),
|
||
}
|
||
}
|
||
|
||
/// 设置指定应用的代理接管状态
|
||
///
|
||
/// **已废弃**: 请使用 `proxy_config.enabled` 字段替代
|
||
#[deprecated(
|
||
since = "3.9.0",
|
||
note = "使用 update_proxy_config_for_app() 修改 enabled 字段"
|
||
)]
|
||
pub fn set_proxy_takeover_enabled(
|
||
&self,
|
||
app_type: &str,
|
||
enabled: bool,
|
||
) -> Result<(), AppError> {
|
||
let key = format!("proxy_takeover_{app_type}");
|
||
let value = if enabled { "true" } else { "false" };
|
||
self.set_setting(&key, value)
|
||
}
|
||
|
||
/// 检查是否有任一应用开启了代理接管
|
||
///
|
||
/// **已废弃**: 请使用 `is_live_takeover_active()` 替代
|
||
#[deprecated(since = "3.9.0", note = "使用 is_live_takeover_active() 替代")]
|
||
pub fn has_any_proxy_takeover(&self) -> Result<bool, AppError> {
|
||
let conn = lock_conn!(self.conn);
|
||
let count: i64 = conn
|
||
.query_row(
|
||
"SELECT COUNT(*) FROM settings WHERE key LIKE 'proxy_takeover_%' AND value = 'true'",
|
||
[],
|
||
|row| row.get(0),
|
||
)
|
||
.map_err(|e| AppError::Database(e.to_string()))?;
|
||
Ok(count > 0)
|
||
}
|
||
|
||
/// 清除所有代理接管状态(将所有 proxy_takeover_* 设置为 false)
|
||
///
|
||
/// **已废弃**: settings 表不再用于存储代理状态
|
||
#[deprecated(
|
||
since = "3.9.0",
|
||
note = "使用 update_proxy_config_for_app() 清除各应用的 enabled 字段"
|
||
)]
|
||
pub fn clear_all_proxy_takeover(&self) -> Result<(), AppError> {
|
||
let conn = lock_conn!(self.conn);
|
||
conn.execute(
|
||
"UPDATE settings SET value = 'false' WHERE key LIKE 'proxy_takeover_%'",
|
||
[],
|
||
)
|
||
.map_err(|e| AppError::Database(e.to_string()))?;
|
||
log::info!("已清除所有代理接管状态");
|
||
Ok(())
|
||
}
|
||
|
||
// --- 整流器配置 ---
|
||
|
||
/// 获取整流器配置
|
||
///
|
||
/// 返回整流器配置,如果不存在则返回默认值(全部启用)
|
||
pub fn get_rectifier_config(&self) -> Result<crate::proxy::types::RectifierConfig, AppError> {
|
||
match self.get_setting("rectifier_config")? {
|
||
Some(json) => serde_json::from_str(&json)
|
||
.map_err(|e| AppError::Database(format!("解析整流器配置失败: {e}"))),
|
||
None => Ok(crate::proxy::types::RectifierConfig::default()),
|
||
}
|
||
}
|
||
|
||
/// 更新整流器配置
|
||
pub fn set_rectifier_config(
|
||
&self,
|
||
config: &crate::proxy::types::RectifierConfig,
|
||
) -> Result<(), AppError> {
|
||
let json = serde_json::to_string(config)
|
||
.map_err(|e| AppError::Database(format!("序列化整流器配置失败: {e}")))?;
|
||
self.set_setting("rectifier_config", &json)
|
||
}
|
||
|
||
// --- 日志配置 ---
|
||
|
||
/// 获取日志配置
|
||
pub fn get_log_config(&self) -> Result<crate::proxy::types::LogConfig, AppError> {
|
||
match self.get_setting("log_config")? {
|
||
Some(json) => serde_json::from_str(&json)
|
||
.map_err(|e| AppError::Database(format!("解析日志配置失败: {e}"))),
|
||
None => Ok(crate::proxy::types::LogConfig::default()),
|
||
}
|
||
}
|
||
|
||
/// 更新日志配置
|
||
pub fn set_log_config(&self, config: &crate::proxy::types::LogConfig) -> Result<(), AppError> {
|
||
let json = serde_json::to_string(config)
|
||
.map_err(|e| AppError::Database(format!("序列化日志配置失败: {e}")))?;
|
||
self.set_setting("log_config", &json)
|
||
}
|
||
}
|