fix(llm): base_url 端口跟随调用方 Host(修正 .11 上打不通的回显地址)

问题:账号态回的 base_url 不带端口(https://lm.mayishangshu.cn/deepseek/v1),
而本网关在宿主 88 上。客户端照它拼请求会落到 443 —— 实测那是**出口 SSL 设备**,
返回 HTTP 200 + 一个 HTML 登录页:状态码正常、内容完全不对,最难察觉的一种失败。
也不能写死 :88:挂在 443 反代后面时又必须不带端口。

改法:新增 http::host_port()(从 Host 头取 ":88"/""),base_url / view / view_all
接收端口参数,调用方从当前请求取。88 部署给 :88,443 反代给无端口,两种都对。

本轮 .11 实机交付验证:
- 镜像 ipass-1.0.0(digest 046a9dd7…)更新完成:端口 88:18098、网络 ipass_default、
  数据目录 /home/train11/ipass/data 全部复刻,3 账号 / 1 域名 / 1 上游无损;worker 可写。
- 给 deepseek 登记 deepseek-chat / deepseek-reasoner(保存时不提交 auth 字段 → 密钥未被动)。
- 两把 Key 实调真实 DeepSeek 均 200,含此前 404 的 /deepseek/v1/chat/completions(去重修复生效)。
- 用量统计按账号/按模型正确累加;账号态自查可用。

文档:§15 对外端口由 :18098 更正为宿主 :88;§16 新增条目;CLAUDE.md 补 base_url 端口约定。
回归:网关 78 + LLM 77 = 155 条全绿(新增 2 条端口自适应断言)。
This commit is contained in:
admin committed 2026-09-12 10:43:11 +08:00
1 parent 5e8c38aef4
commit 6a5387b8ce
8 files changed
+94 -30

No files matched your search

+10 -5
View File
@@ -504,7 +504,7 @@ fn h_my_delete(r: *mut ngx_http_request_t, actor: Actor) -> Status {
fn h_admin_upstreams(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
http::ok(
r,
json!({ "items": upstreams::view_all(&upstreams::all()) }),
json!({ "items": upstreams::view_all(&upstreams::all(), &http::host_port(r)) }),
"ok",
)
}
@@ -540,7 +540,8 @@ fn h_admin_upstream_save(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
"[api] 保存 LLM 上游 {slot} enabled={enabled} models={}",
up.models.len()
));
http::ok(r, upstreams::view(&slot, &up), "已保存")
// base_url 带「调用方正在用的端口」:见 upstreams::base_url 的说明
http::ok(r, upstreams::view(&slot, &up, &http::host_port(r)), "已保存")
}
Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e),
}
@@ -661,6 +662,8 @@ fn user_or_401(r: *mut ngx_http_request_t, actor: Actor) -> Result<User, Status>
/// 刻意只回显 slot / base_url / **模型清单**——**不回显任何密钥**(连脱敏都不给,
/// 上游密钥与调用方无关,没有知道的必要)。
fn h_my_upstreams(r: *mut ngx_http_request_t, actor: Actor) -> Status {
// 回显的 base_url 必须带「调用方正在用的端口」,否则客户端照着拼会打不通
let port = http::host_port(r);
let user = match user_or_401(r, actor) {
Ok(u) => u,
Err(e) => return e,
@@ -671,7 +674,7 @@ fn h_my_upstreams(r: *mut ngx_http_request_t, actor: Actor) -> Status {
.map(|(slot, up)| {
json!({
"slot": slot,
"base_url": upstreams::base_url(&slot, &up),
"base_url": upstreams::base_url(&slot, &up, &port),
"models": up.models,
"note": up.note,
})
@@ -687,6 +690,8 @@ fn h_my_upstreams(r: *mut ngx_http_request_t, actor: Actor) -> Status {
/// (形状 `{object:"list",data:[{id,...}]}`),这边给管理/自研客户端用
/// (形状 `{code,msg,data}`,带 base_url 便于直接拼请求)。
fn h_my_models(r: *mut ngx_http_request_t, actor: Actor) -> Status {
// 同 h_my_upstreams:base_url 跟随调用方所用的端口
let port = http::host_port(r);
let user = match user_or_401(r, actor) {
Ok(u) => u,
Err(e) => return e,
@@ -699,7 +704,7 @@ fn h_my_models(r: *mut ngx_http_request_t, actor: Actor) -> Status {
items.push(json!({
"model": model,
"slot": slot,
"base_url": upstreams::base_url(slot, up),
"base_url": upstreams::base_url(slot, up, &port),
"note": up.note,
}));
}
@@ -713,7 +718,7 @@ fn h_my_models(r: *mut ngx_http_request_t, actor: Actor) -> Status {
.iter()
.map(|(slot, up)| json!({
"slot": slot,
"base_url": upstreams::base_url(slot, up),
"base_url": upstreams::base_url(slot, up, &port),
"models": up.models,
}))
.collect::<Vec<_>>(),
+18
View File
@@ -47,6 +47,24 @@ pub fn header(r: *mut ngx_http_request_t, name: &str) -> Option<String> {
None
}
/// 请求 Host 里的端口后缀(`:88` / 空串)。
///
/// 对外三个域名(`mdm` 管理台 / `lm` LLM / `*.dm` 企业)**共用同一个端口**,
/// 所以「管理接口从哪个端口进来,LLM 就该从哪个端口出去」。回显用的 `base_url`
/// 用它拼:写死端口在换端口部署时失效,完全不写则在非 443 部署时指向别处。
pub fn host_port(r: *mut ngx_http_request_t) -> String {
let host = match header(r, "Host") {
Some(h) => h,
None => return String::new(),
};
// IPv6 字面量形如 `[::1]:88`,rsplit 后 `[::1]` / `88` 仍然正确;
// 纯 `[::1]` 会切出非全数字的尾巴,被下面判掉。
match host.rsplit_once(':') {
Some((_, p)) if !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit()) => format!(":{p}"),
_ => String::new(),
}
}
/// 客户端地址(登录限速的分组键)
pub fn remote_addr(r: *mut ngx_http_request_t) -> String {
// SAFETY: r 有效;connection 在请求生命周期内必然存在
+12 -8
View File
@@ -188,20 +188,24 @@ pub fn available(allowed: &[String]) -> Vec<(String, Upstream)> {
/// 这样它才真的能和厂商文档里的 base_url 逐个字符替换:
///
/// ```text
/// https://api.deepseek.com/v1 → https://lm.mayishangshu.cn/deepseek/v1
/// https://api.openai.com → https://lm.mayishangshu.cn/openai
/// https://api.deepseek.com/v1 → https://lm.mayishangshu.cn:88/deepseek/v1
/// https://api.openai.com → https://lm.mayishangshu.cn:88/openai
/// ```
///
/// `port` 由调用方从请求 Host 头取(`http::host_port`),**不要写死**:
/// 部署在 88 时客户端必须带 `:88` 才连得到,而放在 443 反代后面时又必须不带端口。
/// 写死任何一种,都会在另一种部署里给出一个「看着对、实际打不通」的地址。
///
/// 反过来(不管上游 base 的路径、一律拼 `/v1`)会让「base 自带 /v1」的上游
/// 变成 `/v1/v1/chat/completions` → 上游 404,且客户端拿到的是空响应体,
/// 排查起来完全看不出是网关拼错了。**实测踩过,别再改回去。**
pub fn base_url(slot: &str, up: &Upstream) -> String {
pub fn base_url(slot: &str, up: &Upstream, port: &str) -> String {
let rest = up.base.split("://").nth(1).unwrap_or("");
let path = match rest.find('/') {
Some(i) => &rest[i..],
None => "",
};
format!("https://{LLM_PUBLIC_HOST}/{slot}{path}")
format!("https://{LLM_PUBLIC_HOST}{port}/{slot}{path}")
}
/// 模型发现:把可用上游挂载的模型摊平成 `(模型名, slot)` 列表。
@@ -299,11 +303,11 @@ pub fn remove(slot_raw: &str) -> Result<String, String> {
///
/// **绝不回显完整 auth**——管理台页面会被截图、被浏览器插件读取,
/// 回显上游真 Key 等于把公司所有厂商密钥摆在一个 HTML 里。
pub fn view(slot: &str, up: &Upstream) -> Value {
pub fn view(slot: &str, up: &Upstream, port: &str) -> Value {
json!({
"slot": slot,
"base": up.base,
"base_url": base_url(slot, up),
"base_url": base_url(slot, up, port),
"models": up.models,
"model_count": up.models.len(),
"auth_set": !up.auth.is_empty(),
@@ -315,6 +319,6 @@ pub fn view(slot: &str, up: &Upstream) -> Value {
})
}
pub fn view_all(list: &[(String, Upstream)]) -> Vec<Value> {
list.iter().map(|(slot, up)| view(slot, up)).collect()
pub fn view_all(list: &[(String, Upstream)], port: &str) -> Vec<Value> {
list.iter().map(|(slot, up)| view(slot, up, port)).collect()
}