File size: 14,210 Bytes
a21c316 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | //! HTTP API Module
//! Provides local HTTP interfaces for external programs (e.g., VS Code extension) to call.
use axum::{
extract::{Path, Query, State},
http::StatusCode,
response::IntoResponse,
routing::{get, post},
Json, Router,
};
use serde::{Deserialize, Serialize};
// 预留 HTTP API 模块,当前未在主流程中启用
use std::sync::Arc;
use tokio::sync::RwLock;
use tower_http::cors::{Any, CorsLayer};
use crate::modules::{account, logger, proxy_db};
/// Default port for HTTP API server
pub const DEFAULT_PORT: u16 = 19527;
// ============================================================================
// Settings
// ============================================================================
/// HTTP API Settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HttpApiSettings {
/// Whether to enable HTTP API service
#[serde(default = "default_enabled")]
pub enabled: bool,
/// Listening port
#[serde(default = "default_port")]
pub port: u16,
}
fn default_enabled() -> bool {
true
}
fn default_port() -> u16 {
DEFAULT_PORT
}
impl Default for HttpApiSettings {
fn default() -> Self {
Self {
enabled: true,
port: DEFAULT_PORT,
}
}
}
/// Load HTTP API settings
pub fn load_settings() -> Result<HttpApiSettings, String> {
let data_dir = crate::modules::account::get_data_dir()
.map_err(|e| format!("Failed to get data dir: {}", e))?;
let settings_path = data_dir.join("http_api_settings.json");
if !settings_path.exists() {
return Ok(HttpApiSettings::default());
}
let content = std::fs::read_to_string(&settings_path)
.map_err(|e| format!("Failed to read settings file: {}", e))?;
serde_json::from_str(&content)
.map_err(|e| format!("Failed to parse settings: {}", e))
}
/// Save HTTP API settings
pub fn save_settings(settings: &HttpApiSettings) -> Result<(), String> {
let data_dir = crate::modules::account::get_data_dir()
.map_err(|e| format!("Failed to get data dir: {}", e))?;
let settings_path = data_dir.join("http_api_settings.json");
let content = serde_json::to_string_pretty(settings)
.map_err(|e| format!("Failed to serialize settings: {}", e))?;
std::fs::write(&settings_path, content)
.map_err(|e| format!("Failed to write settings file: {}", e))
}
/// Server State
#[derive(Clone)]
pub struct ApiState {
/// Whether there is a switch operation currently in progress
pub switching: Arc<RwLock<bool>>,
pub integration: crate::modules::integration::SystemManager,
}
impl ApiState {
pub fn new(integration: crate::modules::integration::SystemManager) -> Self {
Self {
switching: Arc::new(RwLock::new(false)),
integration,
}
}
}
// ============================================================================
// Response Types
// ============================================================================
#[derive(Serialize)]
struct HealthResponse {
status: String,
version: String,
}
#[derive(Serialize)]
struct AccountResponse {
id: String,
email: String,
name: Option<String>,
is_current: bool,
disabled: bool,
quota: Option<QuotaResponse>,
device_bound: bool,
last_used: i64,
}
#[derive(Serialize)]
struct QuotaResponse {
models: Vec<ModelQuota>,
updated_at: Option<i64>,
subscription_tier: Option<String>,
}
#[derive(Serialize)]
struct ModelQuota {
name: String,
percentage: i32,
reset_time: String,
}
#[derive(Serialize)]
struct AccountListResponse {
accounts: Vec<AccountResponse>,
current_account_id: Option<String>,
}
#[derive(Serialize)]
struct CurrentAccountResponse {
account: Option<AccountResponse>,
}
#[derive(Serialize)]
struct SwitchResponse {
success: bool,
message: String,
}
#[derive(Serialize)]
struct RefreshResponse {
success: bool,
message: String,
refreshed_count: usize,
}
#[derive(Serialize)]
struct BindDeviceResponse {
success: bool,
message: String,
device_profile: Option<DeviceProfileResponse>,
}
#[derive(Serialize)]
struct DeviceProfileResponse {
machine_id: String,
mac_machine_id: String,
dev_device_id: String,
sqm_id: String,
}
#[derive(Serialize)]
struct ErrorResponse {
error: String,
}
#[derive(Serialize)]
struct LogsResponse {
total: u64,
logs: Vec<crate::proxy::monitor::ProxyRequestLog>,
}
// ============================================================================
// Request Types
// ============================================================================
#[derive(Deserialize)]
struct SwitchRequest {
account_id: String,
}
#[derive(Deserialize)]
struct BindDeviceRequest {
#[serde(default = "default_bind_mode")]
mode: String,
}
fn default_bind_mode() -> String {
"generate".to_string()
}
#[derive(Deserialize)]
struct LogsRequest {
#[serde(default)]
limit: usize,
#[serde(default)]
offset: usize,
#[serde(default)]
filter: String,
#[serde(default)]
errors_only: bool,
}
// ============================================================================
// Handlers
// ============================================================================
/// GET /health - Health check
async fn health() -> impl IntoResponse {
Json(HealthResponse {
status: "ok".to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
})
}
/// GET /accounts - Get all accounts
async fn list_accounts() -> Result<impl IntoResponse, (StatusCode, Json<ErrorResponse>)> {
let accounts = account::list_accounts().map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse { error: e }),
)
})?;
let current_id = account::get_current_account_id()
.ok()
.flatten();
let account_responses: Vec<AccountResponse> = accounts
.into_iter()
.map(|acc| {
let is_current = current_id.as_ref().map(|id| id == &acc.id).unwrap_or(false);
let quota = acc.quota.map(|q| QuotaResponse {
models: q.models.into_iter().map(|m| ModelQuota {
name: m.name,
percentage: m.percentage,
reset_time: m.reset_time,
}).collect(),
updated_at: Some(q.last_updated),
subscription_tier: q.subscription_tier,
});
AccountResponse {
id: acc.id,
email: acc.email,
name: acc.name,
is_current,
disabled: acc.disabled,
quota,
device_bound: acc.device_profile.is_some(),
last_used: acc.last_used,
}
})
.collect();
Ok(Json(AccountListResponse {
current_account_id: current_id,
accounts: account_responses,
}))
}
/// GET /accounts/current - Get current account
async fn get_current_account() -> Result<impl IntoResponse, (StatusCode, Json<ErrorResponse>)> {
let current = account::get_current_account().map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse { error: e }),
)
})?;
let response = current.map(|acc| {
let quota = acc.quota.map(|q| QuotaResponse {
models: q.models.into_iter().map(|m| ModelQuota {
name: m.name,
percentage: m.percentage,
reset_time: m.reset_time,
}).collect(),
updated_at: Some(q.last_updated),
subscription_tier: q.subscription_tier,
});
AccountResponse {
id: acc.id,
email: acc.email,
name: acc.name,
is_current: true,
disabled: acc.disabled,
quota,
device_bound: acc.device_profile.is_some(),
last_used: acc.last_used,
}
});
Ok(Json(CurrentAccountResponse { account: response }))
}
/// POST /accounts/switch - Switch account
async fn switch_account(
State(state): State<ApiState>,
Json(payload): Json<SwitchRequest>,
) -> Result<impl IntoResponse, (StatusCode, Json<ErrorResponse>)> {
// Check if another switch operation is already in progress
{
let switching = state.switching.read().await;
if *switching {
return Err((
StatusCode::CONFLICT,
Json(ErrorResponse {
error: "Another switch operation is already in progress".to_string(),
}),
));
}
}
// Mark switch started
{
let mut switching = state.switching.write().await;
*switching = true;
}
let account_id = payload.account_id.clone();
let state_clone = state.clone();
// Execute switch asynchronously (non-blocking response)
tokio::spawn(async move {
logger::log_info(&format!("[HTTP API] Starting account switch: {}", account_id));
match account::switch_account(&account_id, &state_clone.integration).await {
Ok(()) => {
logger::log_info(&format!("[HTTP API] Account switch successful: {}", account_id));
}
Err(e) => {
logger::log_error(&format!("[HTTP API] Account switch failed: {}", e));
}
}
// Mark switch ended
let mut switching = state_clone.switching.write().await;
*switching = false;
});
// Immediately return 202 Accepted
Ok((
StatusCode::ACCEPTED,
Json(SwitchResponse {
success: true,
message: format!("Account switch task started: {}", payload.account_id),
}),
))
}
/// POST /accounts/refresh - Refresh all quotas
async fn refresh_all_quotas() -> Result<impl IntoResponse, (StatusCode, Json<ErrorResponse>)> {
logger::log_info("[HTTP API] Starting refresh of all account quotas");
// Execute refresh asynchronously
tokio::spawn(async {
match account::refresh_all_quotas_logic().await {
Ok(stats) => {
logger::log_info(&format!(
"[HTTP API] Quota refresh completed, successful {}/{} accounts",
stats.success, stats.total
));
}
Err(e) => {
logger::log_error(&format!("[HTTP API] Quota refresh failed: {}", e));
}
}
});
Ok((
StatusCode::ACCEPTED,
Json(RefreshResponse {
success: true,
message: "Quota refresh task started".to_string(),
refreshed_count: 0,
}),
))
}
/// POST /accounts/:id/bind-device - Bind device fingerprint
async fn bind_device(
Path(account_id): Path<String>,
Json(payload): Json<BindDeviceRequest>,
) -> Result<impl IntoResponse, (StatusCode, Json<ErrorResponse>)> {
logger::log_info(&format!(
"[HTTP API] Binding device fingerprint: account={}, mode={}",
account_id, payload.mode
));
let result = account::bind_device_profile(&account_id, &payload.mode).map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse { error: e }),
)
})?;
Ok(Json(BindDeviceResponse {
success: true,
message: "Device fingerprint bound successfully".to_string(),
device_profile: Some(DeviceProfileResponse {
machine_id: result.machine_id,
mac_machine_id: result.mac_machine_id,
dev_device_id: result.dev_device_id,
sqm_id: result.sqm_id,
}),
}))
}
/// GET /logs - Get proxy logs
async fn get_logs(
Query(params): Query<LogsRequest>,
) -> Result<impl IntoResponse, (StatusCode, Json<ErrorResponse>)> {
let limit = if params.limit == 0 { 50 } else { params.limit };
let total = proxy_db::get_logs_count_filtered(¶ms.filter, params.errors_only)
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: e })))?;
let logs = proxy_db::get_logs_filtered(¶ms.filter, params.errors_only, limit, params.offset)
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: e })))?;
Ok(Json(LogsResponse {
total,
logs,
}))
}
// ============================================================================
// Server
// ============================================================================
/// Start HTTP API server
pub async fn start_server(port: u16, integration: crate::modules::integration::SystemManager) -> Result<(), String> {
let state = ApiState::new(integration);
// CORS config - allow local calls
let cors = CorsLayer::new()
.allow_origin(Any)
.allow_methods(Any)
.allow_headers(Any);
let app = Router::new()
.route("/health", get(health))
.route("/accounts", get(list_accounts))
.route("/accounts/current", get(get_current_account))
.route("/accounts/switch", post(switch_account))
.route("/accounts/refresh", post(refresh_all_quotas))
.route("/accounts/{id}/bind-device", post(bind_device))
.route("/logs", get(get_logs))
.layer(cors)
.with_state(state);
let addr = format!("127.0.0.1:{}", port);
logger::log_info(&format!("[HTTP API] Starting server: http://{}", addr));
let listener = tokio::net::TcpListener::bind(&addr)
.await
.map_err(|e| format!("failed_to_bind_port: {}", e))?;
axum::serve(listener, app)
.await
.map_err(|e| format!("failed_to_run_server: {}", e))?;
Ok(())
}
/// Start HTTP API server in background (non-blocking)
pub fn spawn_server(port: u16, integration: crate::modules::integration::SystemManager) {
// Use tauri::async_runtime::spawn to ensure running within Tauri's runtime
tauri::async_runtime::spawn(async move {
if let Err(e) = start_server(port, integration).await {
logger::log_error(&format!("[HTTP API] Failed to start server: {}", e));
}
});
}
|