Files
ipass__ipass/rust/src/models.rs
T
admin 5c307ba1aa iPass 山海关:Rust 动态模块版全量入库
- 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 环回链路)
2026-09-11 15:37:09 +08:00

150 lines
4.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 持久化数据模型。
//!
//! 字段名与旧版 `data/users.json` / `data/domains.json` **逐字对应**,
//! 以便新版直接接管旧数据文件、且管理台前端无需改动。
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
/// 账号表(`users.json`):以账号名为 key 的对象。
pub type UserTable = BTreeMap<String, User>;
/// 单个账号。
///
/// `temp_grant_until` 是「临时授权权限」的到期时间戳;> now 表示当前拥有
/// 该权限,申请临时域名时可跳过审核直接放行。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
pub username: String,
/// `admin` | `user`
pub role: String,
pub salt: String,
/// 迭代加盐 SHA-256
pub pwd_hash: String,
/// `sk_<48 位 hex>`。
///
/// LLM 代理与域名接口**共用这一把 Key**——这正是「用同一套 Key 验证」
/// 的落点,所以重置 Key 会同时影响两个入口。
pub api_key: String,
/// LLM 代理可用的上游 slot 白名单。空 = 不限。
///
/// 老数据没有该字段,`serde(default)` 反序列化得空表,语义正好是「不限」。
#[serde(default)]
pub allowed_models: Vec<String>,
/// LLM 每分钟请求上限。老数据缺省 → 全局默认(`LLM_RPM_DEFAULT`);0 = 不限
#[serde(default = "default_llm_rpm")]
pub llm_rpm: i64,
/// LLM 每日请求上限。老数据缺省 → 全局默认(`LLM_DAILY_DEFAULT`);0 = 不限
#[serde(default = "default_llm_daily")]
pub llm_daily: i64,
#[serde(default)]
pub temp_grant_until: i64,
#[serde(default)]
pub created_at: i64,
}
impl User {
/// 是否拥有「临时授权权限」
pub fn has_temp_permission(&self, now: i64) -> bool {
self.temp_grant_until > now
}
pub fn is_admin(&self) -> bool {
self.role == "admin"
}
}
/// 上游表(`llm-upstreams.json`):以 slot 名为 key。
///
/// `slot` 就是 LLM 客户端 URL 的第一段路径:`/openai/v1/chat/completions`
/// 里的 `openai`。**刻意不按 body 里的 `model` 字段路由**——请求体要到
/// content 阶段才读得到,而 access 阶段(转发决策点)读不到,
/// 按 model 分流就得放弃 `proxy_pass` 自己实现 upstream,代价差一个数量级。
pub type UpstreamTable = BTreeMap<String, Upstream>;
/// 单个 LLM 上游。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Upstream {
/// 上游基址,含 scheme,如 `https://api.openai.com`(可带端口)
pub base: String,
/// 完整的 Authorization 头值,如 `Bearer sk-xxx`。
/// 留空表示不透传鉴权头(本地免鉴权后端,如 vLLM / Ollama)
#[serde(default)]
pub auth: String,
/// 关闭后该 slot 立即停止转发(保留配置便于恢复)
#[serde(default = "default_true")]
pub enabled: bool,
#[serde(default)]
pub note: String,
#[serde(default)]
pub created_at: i64,
}
fn default_true() -> bool {
true
}
fn default_llm_rpm() -> i64 {
crate::config::LLM_RPM_DEFAULT
}
fn default_llm_daily() -> i64 {
crate::config::LLM_DAILY_DEFAULT
}
/// 域名表(`domains.json`):一条记录 = 一个子域名。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DomainEntry {
pub id: String,
pub subdomain: String,
/// `host:port`
pub backend: String,
/// `temp`(临时,到时自动停止转发) | `long`(长期)
#[serde(rename = "type")]
pub dtype: String,
/// 归属账号
pub owner: String,
/// `pending` | `approved` | `rejected`
pub status: String,
#[serde(default)]
pub created_at: i64,
/// 仅 temp 有意义;long 恒为 0
#[serde(default)]
pub expires_at: i64,
/// `auto` 表示凭临时授权权限免审放行
#[serde(default)]
pub reviewed_by: String,
#[serde(default)]
pub reviewed_at: i64,
}
impl DomainEntry {
/// 实际状态:approved 的临时域名过期后视为 `expired`。
///
/// 这是「到期不等于删除」语义的落点——记录保留,只是不再转发。
pub fn effective_status(&self, now: i64) -> &'static str {
if self.status == "approved" && self.dtype == "temp" {
if self.expires_at > 0 && self.expires_at <= now {
return "expired";
}
}
match self.status.as_str() {
"approved" => "approved",
"rejected" => "rejected",
_ => "pending",
}
}
/// 转发侧判定:返回该域名当前是否可转发(已放行且未过期)
pub fn is_forwardable(&self, now: i64) -> bool {
if self.status != "approved" {
return false;
}
if self.dtype == "temp" {
// 临时域名必须有明确的、尚未到达的到期时刻
return self.expires_at > now;
}
true
}
}