Files
agent/app/server/public-files.js
T
kicer 3f4ab51ea5 公网临时链接改为不透明短 ID(/p/<22字符>),不再暴露文件名/会话名/签名
- public-files.js(新): 内存表 id → (会话, 相对路径, 过期时间);mint 顺手清过期项并兜底上限
- routes/public-files.js(新): GET /p/<id> 匿名取用(查表 → resolveInTask 复核 → sendDownload),未命中/过期一律 404
- routes/public-link.js: 不再签 HMAC,改为 mint 短 ID,返回 http://<IP>:8090/p/<id>(约 50 字符、纯 ASCII)
  动因:原 URL 把文件名写了两遍再挂 64 位签名,共 300+ 字符;它会被模型抄进工具参数、并长期留在会话历史每轮重发
- download-send.js(新): 从 files.js 抽出 sendDownload/downloadDisposition,供浏览器下载与公网取用共用
- server.js: /p 挂在 auth.middleware 之前(匿名可达);浏览器下载那套签名链接保持不动
- docs: DEPLOY.md 4.1 / ARCHITECTURE.md(文件树、路由表、约束)/ README.md 同步
2026-09-15 21:27:43 +08:00

57 lines
2.0 KiB
JavaScript
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.
// 公网临时文件:短 ID 能力链接。
//
// 为什么不用签名 URL:外部服务(如智谱)那一跳只需要「随机不可猜 + 会过期」,而签名 URL 必须把
// 会话名、文件名、过期时间、64 位签名全塞进 URL —— 又长又满是 %XX,模型抄进工具参数容易抄错,
// 而且这条 URL 会留在会话历史里每轮重发,白烧 token。这里只发一个 22 字符的随机 ID:
// http://<IP>:8090/p/9fQ2mZ7xK4tR1vBn
//
// 状态放本进程内存:重启即全部失效 —— 与签名方案语义一致(签名密钥本来就是进程内随机的),
// 所以不是退步,而是把「不可猜」从 HMAC 换成了随机 ID 本身。
//
// 边界:只有内网签发入口(routes/public-link.js,不映射宿主机)能 mint;
// 取用侧(routes/public-files.js)只按 ID 查表,查不到/过期一律 404。
'use strict';
const crypto = require('crypto');
const DEFAULT_TTL_MS = 30 * 60 * 1000;
const MAX_ENTRIES = 1000; // 兜底上限:内网签发,正常远达不到
const entries = new Map(); // id -> { sessionId, rel, expires }
function sweep(now) {
for (const [id, e] of entries) {
if (e.expires <= now) entries.delete(id);
}
}
function mint({ sessionId, rel, ttlMs }) {
const now = Date.now();
sweep(now);
while (entries.size >= MAX_ENTRIES) {
// 满了就丢最老的(Map 保持插入顺序)
entries.delete(entries.keys().next().value);
}
const id = crypto.randomBytes(16).toString('base64url'); // 22 字符,URL 安全
entries.set(id, {
sessionId,
rel,
expires: now + (Number.isFinite(ttlMs) && ttlMs > 0 ? ttlMs : DEFAULT_TTL_MS),
});
return id;
}
// 命中且未过期才返回 {sessionId, rel, expires};过期即顺手删掉
function resolve(id) {
const key = String(id || '');
const e = entries.get(key);
if (!e) return null;
if (e.expires <= Date.now()) {
entries.delete(key);
return null;
}
return e;
}
module.exports = { mint, resolve };