- vanilla nginx + Rust 动态模块(取代 OpenResty+Lua),共享内存为运行期真源 - 三域名分流 18098 ssl:mdm 管理台 / lm LLM 代理 / *.dm 企业转发,共用 *.mayishangshu.cn 通配符证书(证书不入库) - LLM 统一出口:同 Key 鉴权、上游 Key 替换、白名单、限流配额、SSE - 回归夹具 tests/(网关 55 + LLM 34 = 89 断言,夹具 cp 覆盖重置) - 文档:readme(16 章)+ CLAUDE.md + conf/tls/README(acme.sh 证书管理、hairpin 环回链路)
623 lines
22 KiB
Rust
623 lines
22 KiB
Rust
//! API 统一分发器。
|
||
//!
|
||
//! 对应旧版 `lua/api.lua`。两类调用方、两套鉴权:
|
||
//!
|
||
//! ```text
|
||
//! /api/v1/* 账号态:请求头 X-API-Key(用户随机 Key)
|
||
//! /api/admin/* 管理员态:登录会话 Cookie(除 login 外均需登录且 role=admin)
|
||
//! ```
|
||
//!
|
||
//! 所有写操作完成后都会同步共享内存(`domains::sync_shm`),做到
|
||
//! 「审核一通过、立刻可访问」——注意新版**没有**回源读文件这条兜底路径,
|
||
//! 所以写穿不是优化而是正确性要求。
|
||
|
||
use std::collections::HashMap;
|
||
|
||
use ngx::core::Status;
|
||
use ngx::ffi::ngx_http_request_t;
|
||
use ngx::http::HTTPStatus;
|
||
use serde_json::{Value, json};
|
||
|
||
use crate::config;
|
||
use crate::domains;
|
||
use crate::http;
|
||
use crate::models::User;
|
||
use crate::session;
|
||
use crate::state;
|
||
use crate::upstreams;
|
||
use crate::users;
|
||
use crate::util;
|
||
|
||
// ===========================================================================
|
||
// 路由
|
||
// ===========================================================================
|
||
|
||
/// 鉴权后传给处理器的身份
|
||
pub enum Actor {
|
||
/// 公开接口,无身份
|
||
None,
|
||
/// 账号态(X-API-Key)
|
||
User(User),
|
||
/// 管理员态(登录会话)
|
||
Admin(User),
|
||
}
|
||
|
||
impl Actor {
|
||
/// 操作者名称:审核记录 `reviewed_by`、审计日志都用它
|
||
fn admin_name(&self) -> &str {
|
||
match self {
|
||
Actor::Admin(u) => &u.username,
|
||
Actor::User(u) => &u.username,
|
||
Actor::None => "",
|
||
}
|
||
}
|
||
}
|
||
|
||
#[derive(Clone, Copy, PartialEq)]
|
||
enum Guard {
|
||
Public,
|
||
User,
|
||
Admin,
|
||
}
|
||
|
||
type Handler = fn(*mut ngx_http_request_t, Actor) -> Status;
|
||
|
||
/// 路由表。刻意用精确匹配而非正则——接口路径是固定的,
|
||
/// 正则只会引入「写错锚点导致前缀误匹配」的风险。
|
||
fn route(method: &str, uri: &str) -> Option<(Guard, Handler)> {
|
||
let entry: (Guard, Handler) = match (method, uri) {
|
||
("POST", "/api/admin/login") => (Guard::Public, h_login),
|
||
("POST", "/api/admin/logout") => (Guard::Admin, h_logout),
|
||
("GET", "/api/admin/session") => (Guard::Admin, h_session),
|
||
("GET", "/api/admin/users") => (Guard::Admin, h_admin_users),
|
||
("POST", "/api/admin/users/create") => (Guard::Admin, h_admin_user_create),
|
||
("POST", "/api/admin/users/key") => (Guard::Admin, h_admin_user_key),
|
||
("POST", "/api/admin/users/grant") => (Guard::Admin, h_admin_user_grant),
|
||
("POST", "/api/admin/users/password") => (Guard::Admin, h_admin_user_password),
|
||
("POST", "/api/admin/users/delete") => (Guard::Admin, h_admin_user_delete),
|
||
("POST", "/api/admin/users/models") => (Guard::Admin, h_admin_user_models),
|
||
("POST", "/api/admin/users/limits") => (Guard::Admin, h_admin_user_limits),
|
||
("GET", "/api/admin/domains") => (Guard::Admin, h_admin_domains),
|
||
("POST", "/api/admin/domains/review") => (Guard::Admin, h_admin_domain_review),
|
||
("POST", "/api/admin/domains/create") => (Guard::Admin, h_admin_domain_create),
|
||
("POST", "/api/admin/domains/delete") => (Guard::Admin, h_admin_domain_delete),
|
||
("POST", "/api/admin/purge") => (Guard::Admin, h_admin_purge),
|
||
("GET", "/api/admin/upstreams") => (Guard::Admin, h_admin_upstreams),
|
||
("POST", "/api/admin/upstreams/save") => (Guard::Admin, h_admin_upstream_save),
|
||
("POST", "/api/admin/upstreams/delete") => (Guard::Admin, h_admin_upstream_delete),
|
||
("GET", "/api/v1/me") => (Guard::User, h_me),
|
||
("GET", "/api/v1/upstreams") => (Guard::User, h_my_upstreams),
|
||
("GET", "/api/v1/domains") => (Guard::User, h_my_domains),
|
||
("POST", "/api/v1/domains/apply") => (Guard::User, h_apply),
|
||
("POST", "/api/v1/domains/delete") => (Guard::User, h_my_delete),
|
||
_ => return None,
|
||
};
|
||
Some(entry)
|
||
}
|
||
|
||
/// 一次请求的完整处理:路由 → 鉴权 → 执行
|
||
pub fn dispatch(r: *mut ngx_http_request_t) -> Status {
|
||
let method = http::method_str(r);
|
||
let uri = http::uri(r);
|
||
|
||
let Some((guard, handler)) = route(&method, &uri) else {
|
||
return http::fail(
|
||
r,
|
||
HTTPStatus::NOT_FOUND,
|
||
&format!("接口不存在:{method} {uri}"),
|
||
);
|
||
};
|
||
|
||
let actor = match guard {
|
||
Guard::Public => Actor::None,
|
||
|
||
Guard::User => {
|
||
let key = http::header(r, "x-api-key").unwrap_or_default();
|
||
match users::find_by_key(&key) {
|
||
Some(u) => Actor::User(u),
|
||
None => {
|
||
util::log_warn(&format!(
|
||
"[api] API Key 校验失败 client={}",
|
||
http::remote_addr(r)
|
||
));
|
||
return http::fail(
|
||
r,
|
||
HTTPStatus::UNAUTHORIZED,
|
||
"API Key 无效或缺失,请在请求头携带 X-API-Key",
|
||
);
|
||
}
|
||
}
|
||
}
|
||
|
||
Guard::Admin => {
|
||
let cookie = http::header(r, "cookie");
|
||
match session::authenticate(cookie.as_deref()).and_then(|name| users::get(&name)) {
|
||
Some(u) if u.is_admin() => Actor::Admin(u),
|
||
// 会话有效但非管理员:403 而不是 401,便于前端区分
|
||
Some(_) => return http::fail(r, HTTPStatus::FORBIDDEN, "需要管理员权限"),
|
||
None => {
|
||
return http::fail(
|
||
r,
|
||
HTTPStatus::UNAUTHORIZED,
|
||
"未登录或会话已过期,请重新登录",
|
||
);
|
||
}
|
||
}
|
||
}
|
||
};
|
||
|
||
handler(r, actor)
|
||
}
|
||
|
||
// ===========================================================================
|
||
// 管理员:登录 / 会话
|
||
// ===========================================================================
|
||
|
||
fn h_login(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
let ip = http::remote_addr(r);
|
||
let now = util::now();
|
||
|
||
if state::get_login_fail(&ip, now) >= config::LOGIN_MAX_FAIL {
|
||
return http::fail(r, HTTPStatus::TOO_MANY_REQUESTS, "登录失败次数过多,请 5 分钟后再试");
|
||
}
|
||
|
||
let p = http::params(r);
|
||
let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default();
|
||
let password = p.get("password").cloned().unwrap_or_default();
|
||
|
||
if username.is_empty() || password.is_empty() {
|
||
return http::fail(r, HTTPStatus::BAD_REQUEST, "请输入账号和密码");
|
||
}
|
||
|
||
let Some(user) = users::get(&username) else {
|
||
state::bump_login_fail(&ip, now);
|
||
util::log_warn(&format!("[api] 登录失败 user={username} client={ip}"));
|
||
return http::fail(r, HTTPStatus::UNAUTHORIZED, "账号或密码错误");
|
||
};
|
||
|
||
if !util::verify_password(&password, &user.salt, &user.pwd_hash) {
|
||
state::bump_login_fail(&ip, now);
|
||
util::log_warn(&format!("[api] 登录失败 user={username} client={ip}"));
|
||
return http::fail(r, HTTPStatus::UNAUTHORIZED, "账号或密码错误");
|
||
}
|
||
|
||
if !user.is_admin() {
|
||
return http::fail(r, HTTPStatus::FORBIDDEN, "该账号不是管理员,无法登录管理台");
|
||
}
|
||
|
||
state::clear_login_fail(&ip);
|
||
let token = session::create(&username);
|
||
let cookie = session::set_cookie(&token);
|
||
util::log_notice(&format!("[api] 管理员登录 user={username} client={ip}"));
|
||
|
||
http::ok_with(r, users::view(&user), "登录成功", &[("Set-Cookie", &cookie)])
|
||
}
|
||
|
||
fn h_logout(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
let cookie = http::header(r, "cookie");
|
||
session::destroy(cookie.as_deref());
|
||
let cleared = session::clear_cookie();
|
||
http::ok_with(r, json!({}), "已退出登录", &[("Set-Cookie", &cleared)])
|
||
}
|
||
|
||
fn h_session(r: *mut ngx_http_request_t, actor: Actor) -> Status {
|
||
let Actor::Admin(user) = actor else {
|
||
return http::fail(r, HTTPStatus::UNAUTHORIZED, "未登录或会话已过期,请重新登录");
|
||
};
|
||
http::ok(r, users::view(&user), "ok")
|
||
}
|
||
|
||
// ===========================================================================
|
||
// 管理员:账号管理
|
||
// ===========================================================================
|
||
|
||
fn h_admin_users(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
let mut counts: HashMap<String, usize> = HashMap::new();
|
||
for d in domains::all() {
|
||
*counts.entry(d.owner).or_insert(0) += 1;
|
||
}
|
||
|
||
let items: Vec<Value> = users::all()
|
||
.iter()
|
||
.map(|u| {
|
||
let mut v = users::view(u);
|
||
if let Value::Object(ref mut m) = v {
|
||
m.insert(
|
||
"domain_count".to_owned(),
|
||
json!(counts.get(&u.username).copied().unwrap_or(0)),
|
||
);
|
||
// 今日 LLM 用量:读 shm 的日窗计数(与热路径同一数据源)
|
||
m.insert(
|
||
"llm_today".to_owned(),
|
||
json!(state::window_count(
|
||
config::KEY_ST_PREFIX,
|
||
&u.username,
|
||
util::day_id(util::now())
|
||
)),
|
||
);
|
||
}
|
||
v
|
||
})
|
||
.collect();
|
||
|
||
http::ok(r, json!({ "items": items }), "ok")
|
||
}
|
||
|
||
fn h_admin_user_create(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
let p = http::params(r);
|
||
let grant_temp = util::to_bool(p.get("grant_temp"));
|
||
|
||
match users::create(
|
||
p.get("username").map(String::as_str).unwrap_or(""),
|
||
p.get("password").map(String::as_str).unwrap_or(""),
|
||
p.get("role").map(String::as_str).unwrap_or("user"),
|
||
grant_temp,
|
||
) {
|
||
Ok(user) => {
|
||
util::log_notice(&format!(
|
||
"[api] 创建账号 {} role={} temp_grant={grant_temp}",
|
||
user.username, user.role
|
||
));
|
||
http::ok(r, users::view(&user), "账号创建成功")
|
||
}
|
||
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
}
|
||
}
|
||
|
||
fn h_admin_user_key(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
let p = http::params(r);
|
||
let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default();
|
||
|
||
match users::regenerate_key(&username) {
|
||
Ok(key) => http::ok(r, json!({ "username": username, "api_key": key }), "已重置 API Key"),
|
||
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
}
|
||
}
|
||
|
||
fn h_admin_user_grant(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
let p = http::params(r);
|
||
let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default();
|
||
|
||
if util::to_bool(p.get("revoke")) {
|
||
if let Err(e) = users::revoke_temp(&username) {
|
||
return http::fail(r, HTTPStatus::BAD_REQUEST, &e);
|
||
}
|
||
let view = users::get(&username).map(|u| users::view(&u)).unwrap_or(Value::Null);
|
||
return http::ok(r, view, "已取消临时授权权限");
|
||
}
|
||
|
||
match users::grant_temp(&username, p.get("hours")) {
|
||
Ok(_) => {
|
||
let view = users::get(&username).map(|u| users::view(&u)).unwrap_or(Value::Null);
|
||
http::ok(r, view, "已授予临时授权权限")
|
||
}
|
||
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
}
|
||
}
|
||
|
||
fn h_admin_user_password(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
let p = http::params(r);
|
||
let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default();
|
||
let password = p.get("password").map(String::as_str).unwrap_or("");
|
||
|
||
match users::set_password(&username, password) {
|
||
Ok(()) => http::ok(r, json!({}), "密码已重置"),
|
||
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
}
|
||
}
|
||
|
||
fn h_admin_user_delete(r: *mut ngx_http_request_t, actor: Actor) -> Status {
|
||
let p = http::params(r);
|
||
let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default();
|
||
|
||
if let Err(e) = users::delete(&username, actor.admin_name()) {
|
||
return http::fail(r, HTTPStatus::BAD_REQUEST, &e);
|
||
}
|
||
|
||
// 连带回收该账号名下的域名,避免出现无主映射
|
||
let removed = domains::remove_by_owner(&username);
|
||
util::log_notice(&format!("[api] 删除账号 {username},连带域名 {removed} 条"));
|
||
http::ok(r, json!({ "removed_domains": removed }), "账号已删除")
|
||
}
|
||
|
||
// ===========================================================================
|
||
// 管理员:域名审核
|
||
// ===========================================================================
|
||
|
||
fn h_admin_domains(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
http::ok(
|
||
r,
|
||
json!({
|
||
"items": domains::view_all(&domains::all()),
|
||
"pending": domains::count_pending(),
|
||
}),
|
||
"ok",
|
||
)
|
||
}
|
||
|
||
fn h_admin_domain_review(r: *mut ngx_http_request_t, actor: Actor) -> Status {
|
||
let p = http::params(r);
|
||
let id = p.get("id").map(String::as_str).unwrap_or("");
|
||
let action = p.get("action").map(String::as_str).unwrap_or("");
|
||
match domains::review(id, action, actor.admin_name()) {
|
||
Ok(entry) => {
|
||
let msg = if action == "approve" { "已通过审核" } else { "已驳回" };
|
||
http::ok(r, domains::view(&entry), msg)
|
||
}
|
||
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
}
|
||
}
|
||
|
||
fn h_admin_domain_create(r: *mut ngx_http_request_t, actor: Actor) -> Status {
|
||
let p = http::params(r);
|
||
|
||
match domains::create_by_admin(
|
||
p.get("subdomain").map(String::as_str).unwrap_or(""),
|
||
p.get("backend").map(String::as_str).unwrap_or(""),
|
||
p.get("type"),
|
||
p.get("owner"),
|
||
actor.admin_name(),
|
||
) {
|
||
Ok(entry) => http::ok(r, domains::view(&entry), "已创建并直接放行"),
|
||
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
}
|
||
}
|
||
|
||
fn h_admin_domain_delete(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
let p = http::params(r);
|
||
let id = p.get("id").map(String::as_str).unwrap_or("");
|
||
|
||
match domains::remove(id) {
|
||
Ok(sub) => http::ok(r, json!({ "subdomain": sub }), "已删除"),
|
||
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
}
|
||
}
|
||
|
||
fn h_admin_purge(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
match domains::purge() {
|
||
Ok(removed) => http::ok(
|
||
r,
|
||
json!({ "removed": removed }),
|
||
&format!("已清理 {removed} 条过期/陈旧记录"),
|
||
),
|
||
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
}
|
||
}
|
||
|
||
// ===========================================================================
|
||
// 账号态 API
|
||
// ===========================================================================
|
||
|
||
fn h_me(r: *mut ngx_http_request_t, actor: Actor) -> Status {
|
||
let Actor::User(user) = actor else {
|
||
return http::fail(r, HTTPStatus::UNAUTHORIZED, "API Key 无效或缺失,请在请求头携带 X-API-Key");
|
||
};
|
||
|
||
let mut v = users::view(&user);
|
||
if let Value::Object(ref mut m) = v {
|
||
m.insert(
|
||
"domains".to_owned(),
|
||
json!(domains::view_all(&domains::by_owner(&user.username))),
|
||
);
|
||
m.insert(
|
||
"policy".to_owned(),
|
||
json!({
|
||
"temp_grant_ttl_hours": config::TEMP_GRANT_TTL / 3600,
|
||
"temp_domain_ttl_hours": config::TEMP_DOMAIN_TTL / 3600,
|
||
}),
|
||
);
|
||
}
|
||
http::ok(r, v, "ok")
|
||
}
|
||
|
||
fn h_my_domains(r: *mut ngx_http_request_t, actor: Actor) -> Status {
|
||
let Actor::User(user) = actor else {
|
||
return http::fail(r, HTTPStatus::UNAUTHORIZED, "API Key 无效或缺失,请在请求头携带 X-API-Key");
|
||
};
|
||
http::ok(
|
||
r,
|
||
json!({ "items": domains::view_all(&domains::by_owner(&user.username)) }),
|
||
"ok",
|
||
)
|
||
}
|
||
|
||
fn h_apply(r: *mut ngx_http_request_t, actor: Actor) -> Status {
|
||
let Actor::User(user) = actor else {
|
||
return http::fail(r, HTTPStatus::UNAUTHORIZED, "API Key 无效或缺失,请在请求头携带 X-API-Key");
|
||
};
|
||
|
||
let p = http::params(r);
|
||
let entry = match domains::apply(
|
||
&user.username,
|
||
p.get("subdomain").map(String::as_str).unwrap_or(""),
|
||
p.get("backend").map(String::as_str).unwrap_or(""),
|
||
p.get("type"),
|
||
) {
|
||
Ok(e) => e,
|
||
Err(e) => return http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
};
|
||
|
||
let view = domains::view(&entry);
|
||
util::log_notice(&format!(
|
||
"[api] {} 申请 {} type={} status={}",
|
||
user.username,
|
||
entry.subdomain,
|
||
entry.dtype,
|
||
entry.effective_status(util::now())
|
||
));
|
||
|
||
let msg = if entry.status == "approved" {
|
||
format!(
|
||
"已直接放行(临时授权权限生效),{} 小时后自动失效",
|
||
config::TEMP_DOMAIN_TTL / 3600
|
||
)
|
||
} else {
|
||
"已提交申请,等待管理员审核".to_owned()
|
||
};
|
||
http::ok(r, view, &msg)
|
||
}
|
||
|
||
fn h_my_delete(r: *mut ngx_http_request_t, actor: Actor) -> Status {
|
||
let Actor::User(user) = actor else {
|
||
return http::fail(r, HTTPStatus::UNAUTHORIZED, "API Key 无效或缺失,请在请求头携带 X-API-Key");
|
||
};
|
||
|
||
let p = http::params(r);
|
||
let id = p.get("id").map(String::as_str).unwrap_or("");
|
||
|
||
match domains::remove_own(id, &user.username) {
|
||
Ok(sub) => http::ok(r, json!({ "subdomain": sub }), "已删除"),
|
||
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
}
|
||
}
|
||
|
||
// ===========================================================================
|
||
// 管理员:LLM 上游
|
||
// ===========================================================================
|
||
|
||
fn h_admin_upstreams(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
http::ok(
|
||
r,
|
||
json!({ "items": upstreams::view_all(&upstreams::all()) }),
|
||
"ok",
|
||
)
|
||
}
|
||
|
||
fn h_admin_upstream_save(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
let p = http::params(r);
|
||
|
||
// 表单没提交 enabled 字段时视为「启用」——否则改个备注就会把上游改停,
|
||
// 这种误伤在管理台上极难自查
|
||
let enabled = match p.get("enabled") {
|
||
None => true,
|
||
Some(v) => util::to_bool(Some(v)),
|
||
};
|
||
|
||
let slot_raw = p.get("slot").map(String::as_str).unwrap_or("");
|
||
|
||
match upstreams::upsert(
|
||
slot_raw,
|
||
p.get("base").map(String::as_str).unwrap_or(""),
|
||
p.get("auth"),
|
||
enabled,
|
||
p.get("note"),
|
||
) {
|
||
Ok(up) => {
|
||
// 用校验后的 slot 回显;直接拿原始入参会把大小写差异带进响应
|
||
let slot = upstreams::validate_slot(slot_raw).unwrap_or_default();
|
||
util::log_notice(&format!("[api] 保存 LLM 上游 {slot} enabled={enabled}"));
|
||
http::ok(r, upstreams::view(&slot, &up), "已保存")
|
||
}
|
||
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
}
|
||
}
|
||
|
||
fn h_admin_upstream_delete(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
let p = http::params(r);
|
||
match upstreams::remove(p.get("slot").map(String::as_str).unwrap_or("")) {
|
||
Ok(slot) => {
|
||
util::log_notice(&format!("[api] 删除 LLM 上游 {slot}"));
|
||
http::ok(r, json!({ "slot": slot }), "已删除")
|
||
}
|
||
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
}
|
||
}
|
||
|
||
/// 设置某账号可用的 LLM 上游白名单。`models` 传空串 = 解除限制。
|
||
fn h_admin_user_models(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
let p = http::params(r);
|
||
let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default();
|
||
|
||
let slots: Vec<String> = p
|
||
.get("models")
|
||
.map(|s| {
|
||
s.split(',')
|
||
.map(|x| x.trim().to_owned())
|
||
.filter(|x| !x.is_empty())
|
||
.collect()
|
||
})
|
||
.unwrap_or_default();
|
||
|
||
// 上游必须先存在:否则白名单里会存进永远匹配不上的死名字,
|
||
// 表现为「账号被授权了却一直 403」,排查起来很绕
|
||
for slot in &slots {
|
||
if upstreams::get(slot).is_none() {
|
||
return http::fail(r, HTTPStatus::BAD_REQUEST, &format!("上游 {slot} 不存在"));
|
||
}
|
||
}
|
||
|
||
match users::set_allowed_models(&username, slots) {
|
||
Ok(u) => http::ok(r, users::view(&u), "已更新可用上游"),
|
||
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
}
|
||
}
|
||
|
||
/// 设置账号的 LLM 限流参数。字段缺省 = 保持现值,`0` = 不限。
|
||
fn h_admin_user_limits(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||
let p = http::params(r);
|
||
let username = p.get("username").map(|s| util::trim(s).to_owned()).unwrap_or_default();
|
||
|
||
let parse_limit = |key: &str| -> Result<Option<i64>, String> {
|
||
match p.get(key) {
|
||
None => Ok(None),
|
||
Some(v) if util::trim(v).is_empty() => Ok(None),
|
||
Some(v) => util::trim(v)
|
||
.parse::<i64>()
|
||
.map(Some)
|
||
.map_err(|_| format!("{key} 必须是非负整数(0 表示不限)")),
|
||
}
|
||
};
|
||
|
||
let rpm = match parse_limit("rpm") {
|
||
Ok(v) => v,
|
||
Err(e) => return http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
};
|
||
let daily = match parse_limit("daily") {
|
||
Ok(v) => v,
|
||
Err(e) => return http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
};
|
||
if rpm.is_none() && daily.is_none() {
|
||
return http::fail(r, HTTPStatus::BAD_REQUEST, "至少提供 rpm 或 daily 之一");
|
||
}
|
||
|
||
util::log_notice(&format!(
|
||
"[api] 设置账号 {username} LLM 限流 rpm={rpm:?} daily={daily:?}"
|
||
));
|
||
match users::set_llm_limits(&username, rpm, daily) {
|
||
Ok(u) => http::ok(r, users::view(&u), "已更新限流参数"),
|
||
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
|
||
}
|
||
}
|
||
|
||
/// 账号态:看看自己能用哪些上游。
|
||
///
|
||
/// 刻意只回显 slot 与拼接好的 base_url——**不回显任何密钥**(连脱敏都不给,
|
||
/// 上游密钥与调用方无关,没有知道的必要)。
|
||
fn h_my_upstreams(r: *mut ngx_http_request_t, actor: Actor) -> Status {
|
||
let Actor::User(user) = actor else {
|
||
return http::fail(
|
||
r,
|
||
HTTPStatus::UNAUTHORIZED,
|
||
"API Key 无效或缺失,请在请求头携带 X-API-Key",
|
||
);
|
||
};
|
||
|
||
let items: Vec<Value> = upstreams::all()
|
||
.into_iter()
|
||
.filter(|(slot, up)| {
|
||
up.enabled
|
||
&& (user.allowed_models.is_empty()
|
||
|| user.allowed_models.iter().any(|s| s == slot))
|
||
})
|
||
.map(|(slot, up)| {
|
||
json!({
|
||
"slot": slot,
|
||
"base_url": format!("https://llm.yourdomain.com/{slot}"),
|
||
"note": up.note,
|
||
})
|
||
})
|
||
.collect();
|
||
|
||
http::ok(r, json!({ "items": items }), "ok")
|
||
}
|