mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-13 04:29:50 +08:00
refactor: 大规模模块拆分与重组,新增 aether-admin crate
- 新建独立 aether-admin crate 承载 admin 相关共享契约与纯辅助函数 - 拆分 ai_pipeline 下 kiro/private_envelope/conversion/planner 等大文件为子模块目录 - 重组 admin handlers 各业务域(billing/oauth/provider/system/users 等)为目录结构,移除 shared.rs/builders.rs 等反模式 - 移除 ai_pipeline runtime adapters 旧实现(claude/openai/gemini/kiro/vertex/antigravity 等),改由 provider transport 统一承载 - 移除 control_facade/execution_facade/auth_snapshot_facade 等冗余 facade 层 - 拆分 query/billing 与 query/monitoring 模块、state/runtime/payments 与 security 模块 - 扩展架构测试覆盖 admin_billing/admin_model/admin_users 等新模块 - 删除 docs/architecture/refactor-execution-plan.md 已完成的执行计划文档
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
use aether_admin::provider::verify as admin_provider_verify_pure;
|
||||
use reqwest::header::HeaderMap;
|
||||
use serde_json::Value;
|
||||
|
||||
pub(super) fn admin_provider_ops_verify_headers(
|
||||
architecture_id: &str,
|
||||
config: &serde_json::Map<String, Value>,
|
||||
credentials: &serde_json::Map<String, Value>,
|
||||
) -> Result<HeaderMap, String> {
|
||||
admin_provider_verify_pure::admin_provider_ops_verify_headers(
|
||||
architecture_id,
|
||||
config,
|
||||
credentials,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
use crate::handlers::admin::request::AdminAppState;
|
||||
use aether_admin::provider::verify as admin_provider_verify_pure;
|
||||
use regex::Regex;
|
||||
use serde_json::{json, Map, Value};
|
||||
|
||||
pub(super) fn admin_provider_ops_normalized_verify_architecture_id(architecture_id: &str) -> &str {
|
||||
admin_provider_verify_pure::admin_provider_ops_normalized_verify_architecture_id(
|
||||
architecture_id,
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_extract_cookie_value(cookie_input: &str, key: &str) -> String {
|
||||
admin_provider_verify_pure::admin_provider_ops_extract_cookie_value(cookie_input, key)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_yescode_cookie_header(cookie_input: &str) -> String {
|
||||
admin_provider_verify_pure::admin_provider_ops_yescode_cookie_header(cookie_input)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_anyrouter_compute_acw_sc_v2(arg1: &str) -> Option<String> {
|
||||
admin_provider_verify_pure::admin_provider_ops_anyrouter_compute_acw_sc_v2(arg1)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_anyrouter_parse_session_user_id(
|
||||
cookie_input: &str,
|
||||
) -> Option<String> {
|
||||
admin_provider_verify_pure::admin_provider_ops_anyrouter_parse_session_user_id(cookie_input)
|
||||
}
|
||||
|
||||
pub(super) async fn admin_provider_ops_anyrouter_acw_cookie(
|
||||
state: &AdminAppState<'_>,
|
||||
base_url: &str,
|
||||
) -> Option<String> {
|
||||
let response = state
|
||||
.http_client()
|
||||
.get(base_url.trim_end_matches('/'))
|
||||
.header(
|
||||
reqwest::header::USER_AGENT,
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||
)
|
||||
.send()
|
||||
.await
|
||||
.ok()?;
|
||||
let body = response.text().await.ok()?;
|
||||
let compiled = Regex::new(r"var\s+arg1\s*=\s*'([0-9a-fA-F]{40})'").ok()?;
|
||||
let captures = compiled.captures(&body)?;
|
||||
let arg1 = captures.get(1)?.as_str();
|
||||
admin_provider_ops_anyrouter_compute_acw_sc_v2(arg1).map(|value| format!("acw_sc__v2={value}"))
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_verify_failure(message: impl Into<String>) -> Value {
|
||||
admin_provider_verify_pure::admin_provider_ops_verify_failure(message)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_verify_success(
|
||||
data: Value,
|
||||
updated_credentials: Option<Map<String, Value>>,
|
||||
) -> Value {
|
||||
admin_provider_verify_pure::admin_provider_ops_verify_success(data, updated_credentials)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_verify_user_payload(
|
||||
username: Option<String>,
|
||||
display_name: Option<String>,
|
||||
email: Option<String>,
|
||||
quota: Option<f64>,
|
||||
extra: Option<Map<String, Value>>,
|
||||
) -> Value {
|
||||
admin_provider_verify_pure::admin_provider_ops_verify_user_payload(
|
||||
username,
|
||||
display_name,
|
||||
email,
|
||||
quota,
|
||||
extra,
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_value_as_f64(value: Option<&Value>) -> Option<f64> {
|
||||
admin_provider_verify_pure::admin_provider_ops_value_as_f64(value)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_json_object(
|
||||
value: &Value,
|
||||
) -> Option<&serde_json::Map<String, Value>> {
|
||||
admin_provider_verify_pure::admin_provider_ops_json_object(value)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_frontend_updated_credentials(
|
||||
credentials: Map<String, Value>,
|
||||
) -> Option<Map<String, Value>> {
|
||||
admin_provider_verify_pure::admin_provider_ops_frontend_updated_credentials(credentials)
|
||||
}
|
||||
|
||||
pub(super) async fn admin_provider_ops_sub2api_exchange_token(
|
||||
state: &AdminAppState<'_>,
|
||||
base_url: &str,
|
||||
credentials: &Map<String, Value>,
|
||||
) -> Result<(String, Option<Map<String, Value>>), String> {
|
||||
let email = credentials
|
||||
.get("email")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.unwrap_or_default();
|
||||
let password = credentials
|
||||
.get("password")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.unwrap_or_default();
|
||||
let refresh_token = credentials
|
||||
.get("refresh_token")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.unwrap_or_default();
|
||||
|
||||
let (path, body, default_error, previous_refresh_token) =
|
||||
if !email.is_empty() && !password.is_empty() {
|
||||
(
|
||||
"/api/v1/auth/login",
|
||||
json!({ "email": email, "password": password }),
|
||||
"登录失败",
|
||||
None,
|
||||
)
|
||||
} else if !refresh_token.is_empty() {
|
||||
(
|
||||
"/api/v1/auth/refresh",
|
||||
json!({ "refresh_token": refresh_token }),
|
||||
"Refresh Token 无效或已过期",
|
||||
Some(refresh_token),
|
||||
)
|
||||
} else {
|
||||
return Err("请填写账号密码或 Refresh Token".to_string());
|
||||
};
|
||||
|
||||
let response = match state
|
||||
.http_client()
|
||||
.post(format!("{}{path}", base_url.trim_end_matches('/')))
|
||||
.json(&body)
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(response) => response,
|
||||
Err(err) if err.is_timeout() => return Err("连接超时".to_string()),
|
||||
Err(err) if err.is_connect() => return Err(format!("连接失败: {err}")),
|
||||
Err(err) => return Err(format!("验证失败: {err}")),
|
||||
};
|
||||
|
||||
let status = response.status();
|
||||
let response_json = match response.bytes().await {
|
||||
Ok(bytes) => serde_json::from_slice::<Value>(&bytes).unwrap_or_else(|_| json!({})),
|
||||
Err(_) => json!({}),
|
||||
};
|
||||
let payload = response_json.as_object().cloned().unwrap_or_default();
|
||||
if status != http::StatusCode::OK
|
||||
|| payload.get("code").and_then(Value::as_i64).unwrap_or(-1) != 0
|
||||
{
|
||||
let message = payload
|
||||
.get("message")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or(default_error);
|
||||
return Err(message.to_string());
|
||||
}
|
||||
|
||||
let Some(token_data) = payload.get("data").and_then(Value::as_object) else {
|
||||
return Err("响应格式无效".to_string());
|
||||
};
|
||||
let access_token = token_data
|
||||
.get("access_token")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.ok_or_else(|| "响应格式无效".to_string())?;
|
||||
|
||||
let mut updated_credentials = Map::new();
|
||||
if let Some(new_refresh_token) = token_data
|
||||
.get("refresh_token")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
{
|
||||
if previous_refresh_token != Some(new_refresh_token) {
|
||||
updated_credentials.insert(
|
||||
"refresh_token".to_string(),
|
||||
Value::String(new_refresh_token.to_string()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Ok((
|
||||
access_token.to_string(),
|
||||
admin_provider_ops_frontend_updated_credentials(updated_credentials),
|
||||
))
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
mod headers;
|
||||
mod helpers;
|
||||
mod payload;
|
||||
|
||||
use crate::handlers::admin::provider::ops::providers::support::ADMIN_PROVIDER_OPS_VERIFY_RUST_ONLY_MESSAGE;
|
||||
use crate::handlers::admin::request::AdminAppState;
|
||||
use axum::{
|
||||
body::Body,
|
||||
http,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use serde_json::json;
|
||||
|
||||
pub(super) fn admin_provider_ops_verify_headers(
|
||||
architecture_id: &str,
|
||||
config: &serde_json::Map<String, serde_json::Value>,
|
||||
credentials: &serde_json::Map<String, serde_json::Value>,
|
||||
) -> Result<reqwest::header::HeaderMap, String> {
|
||||
headers::admin_provider_ops_verify_headers(architecture_id, config, credentials)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_normalized_verify_architecture_id(architecture_id: &str) -> &str {
|
||||
helpers::admin_provider_ops_normalized_verify_architecture_id(architecture_id)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_verify_failure(message: impl Into<String>) -> serde_json::Value {
|
||||
helpers::admin_provider_ops_verify_failure(message)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_value_as_f64(value: Option<&serde_json::Value>) -> Option<f64> {
|
||||
helpers::admin_provider_ops_value_as_f64(value)
|
||||
}
|
||||
|
||||
pub(super) async fn admin_provider_ops_local_verify_response(
|
||||
state: &AdminAppState<'_>,
|
||||
base_url: &str,
|
||||
architecture_id: &str,
|
||||
config: &serde_json::Map<String, serde_json::Value>,
|
||||
credentials: &serde_json::Map<String, serde_json::Value>,
|
||||
) -> serde_json::Value {
|
||||
if architecture_id == "sub2api" {
|
||||
return payload::admin_provider_ops_local_sub2api_verify_response(
|
||||
state,
|
||||
base_url,
|
||||
credentials,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
let mut resolved_config = config.clone();
|
||||
if architecture_id == "anyrouter" {
|
||||
if let Some(acw_cookie) =
|
||||
helpers::admin_provider_ops_anyrouter_acw_cookie(state, base_url).await
|
||||
{
|
||||
resolved_config.insert(
|
||||
"acw_cookie".to_string(),
|
||||
serde_json::Value::String(acw_cookie),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let verify_path = match architecture_id {
|
||||
"anyrouter" => "/api/user/self",
|
||||
"cubence" => "/api/v1/dashboard/overview",
|
||||
"yescode" => "/api/v1/auth/profile",
|
||||
"nekocode" => "/api/user/self",
|
||||
"new_api" | "generic_api" => "/api/user/self",
|
||||
_ => {
|
||||
return helpers::admin_provider_ops_verify_failure(
|
||||
ADMIN_PROVIDER_OPS_VERIFY_RUST_ONLY_MESSAGE,
|
||||
)
|
||||
}
|
||||
};
|
||||
let base_url = base_url.trim().trim_end_matches('/');
|
||||
if base_url.is_empty() {
|
||||
return helpers::admin_provider_ops_verify_failure("请提供 API 地址");
|
||||
}
|
||||
|
||||
let headers = match headers::admin_provider_ops_verify_headers(
|
||||
architecture_id,
|
||||
&resolved_config,
|
||||
credentials,
|
||||
) {
|
||||
Ok(headers) => headers,
|
||||
Err(message) => return helpers::admin_provider_ops_verify_failure(message),
|
||||
};
|
||||
|
||||
let response = match state
|
||||
.http_client()
|
||||
.get(format!("{base_url}{verify_path}"))
|
||||
.headers(headers)
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(response) => response,
|
||||
Err(err) if err.is_timeout() => {
|
||||
return helpers::admin_provider_ops_verify_failure("连接超时")
|
||||
}
|
||||
Err(err) if err.is_connect() => {
|
||||
return helpers::admin_provider_ops_verify_failure(format!("连接失败: {err}"))
|
||||
}
|
||||
Err(err) => return helpers::admin_provider_ops_verify_failure(format!("验证失败: {err}")),
|
||||
};
|
||||
|
||||
let status = response.status();
|
||||
let response_json = match response.bytes().await {
|
||||
Ok(bytes) => {
|
||||
serde_json::from_slice::<serde_json::Value>(&bytes).unwrap_or_else(|_| json!({}))
|
||||
}
|
||||
Err(_) => json!({}),
|
||||
};
|
||||
|
||||
match architecture_id {
|
||||
"cubence" => payload::admin_provider_ops_cubence_verify_payload(status, &response_json),
|
||||
"yescode" => payload::admin_provider_ops_yescode_verify_payload(status, &response_json),
|
||||
"nekocode" => payload::admin_provider_ops_nekocode_verify_payload(status, &response_json),
|
||||
_ => payload::admin_provider_ops_generic_verify_payload(status, &response_json),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
use super::helpers::{
|
||||
admin_provider_ops_frontend_updated_credentials, admin_provider_ops_json_object,
|
||||
admin_provider_ops_sub2api_exchange_token, admin_provider_ops_value_as_f64,
|
||||
admin_provider_ops_verify_failure, admin_provider_ops_verify_success,
|
||||
admin_provider_ops_verify_user_payload,
|
||||
};
|
||||
use crate::handlers::admin::request::AdminAppState;
|
||||
use aether_admin::provider::verify as admin_provider_verify_pure;
|
||||
use http;
|
||||
use serde_json::{json, Map, Value};
|
||||
|
||||
pub(super) fn admin_provider_ops_generic_verify_payload(
|
||||
status: http::StatusCode,
|
||||
response_json: &Value,
|
||||
) -> Value {
|
||||
admin_provider_verify_pure::admin_provider_ops_generic_verify_payload(status, response_json)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_cubence_verify_payload(
|
||||
status: http::StatusCode,
|
||||
response_json: &Value,
|
||||
) -> Value {
|
||||
admin_provider_verify_pure::admin_provider_ops_cubence_verify_payload(status, response_json)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_yescode_verify_payload(
|
||||
status: http::StatusCode,
|
||||
response_json: &Value,
|
||||
) -> Value {
|
||||
admin_provider_verify_pure::admin_provider_ops_yescode_verify_payload(status, response_json)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_nekocode_verify_payload(
|
||||
status: http::StatusCode,
|
||||
response_json: &Value,
|
||||
) -> Value {
|
||||
admin_provider_verify_pure::admin_provider_ops_nekocode_verify_payload(status, response_json)
|
||||
}
|
||||
|
||||
pub(super) fn admin_provider_ops_sub2api_verify_payload(
|
||||
status: http::StatusCode,
|
||||
response_json: &Value,
|
||||
updated_credentials: Option<Map<String, Value>>,
|
||||
) -> Value {
|
||||
admin_provider_verify_pure::admin_provider_ops_sub2api_verify_payload(
|
||||
status,
|
||||
response_json,
|
||||
updated_credentials,
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) async fn admin_provider_ops_local_sub2api_verify_response(
|
||||
state: &AdminAppState<'_>,
|
||||
base_url: &str,
|
||||
credentials: &Map<String, Value>,
|
||||
) -> Value {
|
||||
let base_url = base_url.trim().trim_end_matches('/');
|
||||
if base_url.is_empty() {
|
||||
return admin_provider_ops_verify_failure("请提供 API 地址");
|
||||
}
|
||||
|
||||
let (access_token, updated_credentials) =
|
||||
match admin_provider_ops_sub2api_exchange_token(state, base_url, credentials).await {
|
||||
Ok(value) => value,
|
||||
Err(message) => return admin_provider_ops_verify_failure(message),
|
||||
};
|
||||
|
||||
let response = match state
|
||||
.http_client()
|
||||
.get(format!("{base_url}/api/v1/auth/me?timezone=Asia/Shanghai"))
|
||||
.bearer_auth(access_token)
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(response) => response,
|
||||
Err(err) if err.is_timeout() => return admin_provider_ops_verify_failure("连接超时"),
|
||||
Err(err) if err.is_connect() => {
|
||||
return admin_provider_ops_verify_failure(format!("连接失败: {err}"));
|
||||
}
|
||||
Err(err) => return admin_provider_ops_verify_failure(format!("验证失败: {err}")),
|
||||
};
|
||||
|
||||
let status = response.status();
|
||||
let response_json = match response.bytes().await {
|
||||
Ok(bytes) => serde_json::from_slice::<Value>(&bytes).unwrap_or_else(|_| json!({})),
|
||||
Err(_) => json!({}),
|
||||
};
|
||||
admin_provider_ops_sub2api_verify_payload(status, &response_json, updated_credentials)
|
||||
}
|
||||
Reference in New Issue
Block a user