feat: 采集 LLM token 用量并上报 lpt-be
- 客户端解析上游 usage(含 DeepSeek 缓存命中/未命中),跨重试累加 - 新增计价表(三档单价 + 峰谷时段),费用在结算时冻结 - 新增用量上报与本地 outbox 重试;4xx 判定为确定性拒绝,不无限重试 - 面板补充 Token/费用/调用次数与计价快照 - 顺带修复面板内联脚本转义错误(模板字面量吃掉 \n 与 \',面板一直空白)
This commit is contained in:
+84
-1
@@ -1,6 +1,6 @@
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { checkToken, configuredToken, TOKEN_HEADER } from "./auth.js";
|
||||
import { checkToken, configuredToken, providedToken, queryTokenMatches, readCookie, TOKEN_COOKIE, TOKEN_HEADER } from "./auth.js";
|
||||
|
||||
/** 极简 reply 替身:记录是否写出 401 */
|
||||
const makeReply = () => {
|
||||
@@ -70,4 +70,87 @@ describe("AI 入站令牌", () => {
|
||||
expect(configuredToken()).toBe("");
|
||||
expect(checkToken({ headers: {} }, makeReply())).toBe(true);
|
||||
});
|
||||
|
||||
it("支持 cookie 传令牌:面板 HTML 与它后续的 fetch 是两个独立请求", () => {
|
||||
process.env.LPT_AI_TOKEN = "secret-token";
|
||||
const reply = makeReply();
|
||||
|
||||
expect(
|
||||
checkToken({ headers: { cookie: `${TOKEN_COOKIE}=secret-token` } }, reply),
|
||||
).toBe(true);
|
||||
expect(reply.calls).toEqual([]);
|
||||
});
|
||||
|
||||
it("cookie 与其它 cookie 混在一起时仍能取出令牌", () => {
|
||||
process.env.LPT_AI_TOKEN = "secret-token";
|
||||
const reply = makeReply();
|
||||
|
||||
expect(
|
||||
checkToken(
|
||||
{ headers: { cookie: `a=1; ${TOKEN_COOKIE}=secret-token; b=2` } },
|
||||
reply,
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("header 优先于 cookie,避免旧 cookie 把新令牌挡在门外", () => {
|
||||
process.env.LPT_AI_TOKEN = "new-token";
|
||||
const reply = makeReply();
|
||||
|
||||
expect(
|
||||
checkToken(
|
||||
{ headers: { [TOKEN_HEADER]: "new-token", cookie: `${TOKEN_COOKIE}=old-token` } },
|
||||
reply,
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("readCookie", () => {
|
||||
it("取出 URL 编码的值", () => {
|
||||
expect(readCookie("t=a%20b", "t")).toBe("a b");
|
||||
});
|
||||
|
||||
it("编码非法时退回原值,不抛异常", () => {
|
||||
expect(readCookie("t=%E0%A4%A", "t")).toBe("%E0%A4%A");
|
||||
});
|
||||
|
||||
it("缺失时返回 undefined", () => {
|
||||
expect(readCookie(undefined, "t")).toBeUndefined();
|
||||
expect(readCookie("a=1", "t")).toBeUndefined();
|
||||
expect(readCookie("novalue", "t")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("providedToken", () => {
|
||||
it("按 header → query → cookie 的优先级取值", () => {
|
||||
process.env.LPT_AI_TOKEN = "secret-token";
|
||||
expect(
|
||||
providedToken({
|
||||
headers: { [TOKEN_HEADER]: "from-header", cookie: `${TOKEN_COOKIE}=from-cookie` },
|
||||
query: { token: "from-query" },
|
||||
}),
|
||||
).toBe("from-header");
|
||||
expect(
|
||||
providedToken({ headers: {}, query: { token: "from-query" } }),
|
||||
).toBe("from-query");
|
||||
expect(
|
||||
providedToken({ headers: { cookie: `${TOKEN_COOKIE}=from-cookie` } }),
|
||||
).toBe("from-cookie");
|
||||
expect(providedToken({ headers: {} })).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("queryTokenMatches", () => {
|
||||
it("仅在 query 令牌正确时返回 true,用于决定要不要下发 cookie", () => {
|
||||
process.env.LPT_AI_TOKEN = "secret-token";
|
||||
expect(queryTokenMatches({ query: { token: "secret-token" } })).toBe(true);
|
||||
expect(queryTokenMatches({ query: { token: "wrong" } })).toBe(false);
|
||||
expect(queryTokenMatches({})).toBe(false);
|
||||
});
|
||||
|
||||
it("未配置令牌时不下发 cookie", () => {
|
||||
delete process.env.LPT_AI_TOKEN;
|
||||
expect(queryTokenMatches({ query: { token: "" } })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
+48
-5
@@ -2,14 +2,20 @@
|
||||
* 入站访问令牌(安全-03)。
|
||||
*
|
||||
* 设计取舍:令牌是**可选**的。
|
||||
* - 设置 `LPT_AI_TOKEN` 后,`/ai/*` 与 `/admin` 都要求携带令牌(header `x-api-token` 或 query `token`);
|
||||
* - 设置 `LPT_AI_TOKEN` 后,`/ai/*` 与 `/admin` 都要求携带令牌;
|
||||
* - 未设置时保持可访问,并在启动日志里明确告知,避免开发阶段被迫维护额外凭据。
|
||||
*
|
||||
* 令牌来源按优先级:header `x-api-token` → query `token` → cookie `lpt_ai_token`。
|
||||
* cookie 这一路是给 `/admin` 面板用的:面板 HTML 与它后续的 `fetch` 是两个独立请求,
|
||||
* 只在 URL 上带令牌的话,HTML 能打开、里面的 fetch 全是 401(面板永远空白);
|
||||
* 用 `?token=` 首次访问时换发一个 HttpOnly cookie,后续请求浏览器会自动带上。
|
||||
*
|
||||
* 生产环境只需往 `lpt-secrets` 里加一个 key 即可收紧(部署清单已用 `optional: true` 引用,
|
||||
* 不设置也不会导致 Pod 起不来)。
|
||||
*/
|
||||
|
||||
export const TOKEN_HEADER = "x-api-token";
|
||||
export const TOKEN_COOKIE = "lpt_ai_token";
|
||||
|
||||
/** 当前配置的令牌;空字符串表示未启用 */
|
||||
export function configuredToken(): string {
|
||||
@@ -21,6 +27,33 @@ interface TokenRequest {
|
||||
query?: unknown;
|
||||
}
|
||||
|
||||
/** 极简 cookie 解析:只取需要的那个键,不引入额外依赖 */
|
||||
export function readCookie(cookieHeader: unknown, name: string): string | undefined {
|
||||
if (typeof cookieHeader !== "string" || cookieHeader.length === 0) return undefined;
|
||||
for (const part of cookieHeader.split(";")) {
|
||||
const separator = part.indexOf("=");
|
||||
if (separator === -1) continue;
|
||||
if (part.slice(0, separator).trim() !== name) continue;
|
||||
const raw = part.slice(separator + 1).trim();
|
||||
try {
|
||||
return decodeURIComponent(raw);
|
||||
} catch {
|
||||
// 编码非法时退回原值,宁可让校验失败也不要抛异常
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** 从请求里取出调用方提供的令牌;取不到时返回空串 */
|
||||
export function providedToken(request: TokenRequest): string {
|
||||
const header = request.headers[TOKEN_HEADER];
|
||||
if (typeof header === "string" && header.length > 0) return header;
|
||||
const query = request.query as { token?: string } | undefined;
|
||||
if (typeof query?.token === "string" && query.token.length > 0) return query.token;
|
||||
return readCookie(request.headers["cookie"], TOKEN_COOKIE) ?? "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验请求是否携带了正确的令牌。
|
||||
*
|
||||
@@ -33,11 +66,21 @@ export function checkToken(
|
||||
const token = configuredToken();
|
||||
if (!token) return true;
|
||||
|
||||
const header = request.headers[TOKEN_HEADER];
|
||||
const query = request.query as { token?: string } | undefined;
|
||||
const provided = typeof header === "string" ? header : query?.token;
|
||||
if (provided === token) return true;
|
||||
if (providedToken(request) === token) return true;
|
||||
|
||||
reply.status(401).send({ error: "未授权" });
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断请求是否用 query 提供了正确的令牌。
|
||||
*
|
||||
* `/admin` 据此决定要不要下发 cookie:只在真正用 `?token=` 打开面板时才种,
|
||||
* 避免把令牌写进每个响应头。
|
||||
*/
|
||||
export function queryTokenMatches(request: { query?: unknown }): boolean {
|
||||
const token = configuredToken();
|
||||
if (!token) return false;
|
||||
const query = request.query as { token?: string } | undefined;
|
||||
return query?.token === token;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user