From 495b41d3936ee9e6f0890ce67ad2661814402a93 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Tue, 22 Sep 2026 11:36:54 +0800 Subject: [PATCH] feat(traffic): say which key, app and machine sent a request The traffic page, the request drawer and the security log showed the gateway key's name as "client", which with keys named after apps read as "Codex sent this". A key can be called anything, apps can share one, and on a network-exposed gateway machines can share one too. The key is now called a key and reads as on the keys page, masked with its name (tw-re...wb4e (claude-code)); the mask is the one core took when the request arrived. Beside it, muted, the app the headers point to (a guess) and, for another machine, where the request came from. The traffic page shows the key column when keys, apps or sources differ, and search matches the app and source. Pins core v0.21.0. Co-Authored-By: Claude Opus 5 --- src-tauri/Cargo.lock | 8 ++++---- src-tauri/Cargo.toml | 2 +- src/App.i18n.tsx | 19 +++++++++++-------- src/App.tsx | 11 +++++++++-- src/KeyLabel.i18n.ts | 14 ++++++++++++++ src/KeyLabel.tsx | 27 +++++++++++++++++++++++++++ src/RequestDrawer.i18n.tsx | 13 +++++++++++-- src/RequestDrawer.tsx | 19 +++++++++++++++++-- src/labels.ts | 22 ++++++++++++++++++++++ src/requestTable.ts | 6 ++++-- src/security/LogTab.i18n.ts | 2 ++ src/security/LogTab.tsx | 16 ++++++++++++---- src/traffic/RequestTable.tsx | 16 ++++++++++++---- src/types.ts | 26 ++++++++++++++++++++++++-- src/useRequests.ts | 6 ++++++ 15 files changed, 176 insertions(+), 31 deletions(-) create mode 100644 src/KeyLabel.i18n.ts create mode 100644 src/KeyLabel.tsx diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index c5b93187..f41e28f1 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -4982,8 +4982,8 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "tw-api" -version = "0.19.0" -source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.19.0#a299c4a03c2110c26c18f31f2145e45c36f78135" +version = "0.21.0" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.21.0#c2596a982baad23d1d2876243ec3dc280bebe0fe" dependencies = [ "serde", "serde_json", @@ -4992,8 +4992,8 @@ dependencies = [ [[package]] name = "tw-types" -version = "0.19.0" -source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.19.0#a299c4a03c2110c26c18f31f2145e45c36f78135" +version = "0.21.0" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.21.0#c2596a982baad23d1d2876243ec3dc280bebe0fe" dependencies = [ "serde", "serde_json", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 68e629a2..a5f272b6 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -31,7 +31,7 @@ tauri-plugin-deep-link = "2" # 编出来的东西不一样;而更要紧的是,打进 `.app` 的那个 twcore 二进制 # 必须和这里编译进去的协议镜像来自同一个 core 版本 —— 打包脚本正是从 # 这个 tag 去取二进制的(见 scripts/fetch-core.sh)。 -tw-api = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.19.0" } +tw-api = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.21.0" } anyhow = "1" libc = "0.2" diff --git a/src/App.i18n.tsx b/src/App.i18n.tsx index 1dc3cd6e..7c247e68 100644 --- a/src/App.i18n.tsx +++ b/src/App.i18n.tsx @@ -63,14 +63,14 @@ export const appText = messages( loadingConfig: "读取配置中…", // 过滤条 - search: "搜索路径、客户端、上游、错误… ⌘F", + search: "搜索路径、密钥、上游、错误… ⌘F", failedOnly: "仅显示失败", groupBySession: "按会话归组", unpricedOnly: "仅显示无法计价", expandSession: "展开这次会话", collapseSession: "收起这次会话", turnCount: (n: number) => `${n} 轮`, - allClients: "全部客户端", + allClients: "全部密钥", allUpstreams: "全部上游", shownOf: (shown: number, total: number) => `${shown} / ${total} 条`, total: (n: number) => `${n} 条`, @@ -94,7 +94,9 @@ export const appText = messages( // 表头 status: "状态", time: "时间", - client: "客户端", + /** 请求带的是哪把网关密钥。**不是哪个应用** —— 应用写在后面,是按请求头推测的 */ + client: "密钥", + fromPeer: (ip: string) => `来自 ${ip}`, model: "模型", upstream: "上游", latency: "延迟", @@ -104,7 +106,7 @@ export const appText = messages( // 行的右键菜单 openDetails: "打开详情", onlyUpstream: (name: string) => `仅显示上游 ${name}`, - onlyClient: (name: string) => `仅显示客户端 ${name}`, + onlyClient: (name: string) => `仅显示密钥 ${name}`, copyId: "复制请求 ID", copyRow: "复制此行", @@ -191,14 +193,14 @@ export const appText = messages( loadingConfig: "Loading config…", // 输入框 256px 宽,放得下的文字约 234px;带上「Search」就放不下 ⌘F 了 - search: "Path, client, upstream, error… ⌘F", + search: "Path, key, upstream, error… ⌘F", failedOnly: "Failed only", groupBySession: "Group by session", unpricedOnly: "Unpriced only", expandSession: "Expand this session", collapseSession: "Collapse this session", turnCount: (n: number) => (n === 1 ? "1 turn" : `${n} turns`), - allClients: "All clients", + allClients: "All keys", allUpstreams: "All upstreams", shownOf: (shown: number, total: number) => `${shown} / ${count(total, "request", "requests")}`, total: (n: number) => count(n, "request", "requests"), @@ -232,7 +234,8 @@ export const appText = messages( status: "Status", time: "Time", - client: "Client", + client: "Key", + fromPeer: (ip: string) => `from ${ip}`, model: "Model", upstream: "Upstream", latency: "Latency", @@ -241,7 +244,7 @@ export const appText = messages( openDetails: "Open details", onlyUpstream: (name: string) => `Show only upstream ${name}`, - onlyClient: (name: string) => `Show only client ${name}`, + onlyClient: (name: string) => `Show only key ${name}`, copyId: "Copy request ID", copyRow: "Copy row", diff --git a/src/App.tsx b/src/App.tsx index 22574b4c..c150eed9 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -303,13 +303,20 @@ export default function App() { : gs; }, [grouped, rows, sessions, sortKey, sortDir]); /** - * 客户端这一列只在真的分得开的时候才出现。 + * 「密钥」这一列只在真的分得开的时候才出现。 * * 目标用户「一个 key 就够」,那时整列二十五行是同一个值 —— 占着宽度 * 却零信息,而那点宽度给模型名用正好。**按实际出现过的算,不按配置里 * 有几个算**:配了两个而只有一个在发请求时,这一列同样是常量。 + * + * 格子里除了密钥还写推测出的应用和非本机的来源,所以**这两样分得开也 + * 算**:一把密钥几个应用共用、或者局域网里另一台机器也在用,这一列就 + * 有话可说。 */ - const showClient = facet.clients.length > 1; + const showClient = + facet.clients.length > 1 || + new Set(allRows.map((r) => r.hint ?? "")).size > 1 || + allRows.some((r) => r.peer); const searchRef = useRef(null); useEffect(() => { if (openSession === null) { diff --git a/src/KeyLabel.i18n.ts b/src/KeyLabel.i18n.ts new file mode 100644 index 00000000..74c31cb8 --- /dev/null +++ b/src/KeyLabel.i18n.ts @@ -0,0 +1,14 @@ +import { messages } from "@/i18n"; + +export const keyLabelText = messages( + { + /** 打码的密钥后面括号里的名字 */ + named: (name: string) => `(${name})`, + /** 老记录没有打码的值,只有名字 */ + bare: (name: string) => `密钥 ${name}`, + }, + { + named: (name: string) => ` (${name})`, + bare: (name: string) => `Key ${name}`, + }, +); diff --git a/src/KeyLabel.tsx b/src/KeyLabel.tsx new file mode 100644 index 00000000..3b597f8e --- /dev/null +++ b/src/KeyLabel.tsx @@ -0,0 +1,27 @@ +import { textOf, useText } from "@/i18n"; +import { keyLabelText } from "./KeyLabel.i18n"; + +/** + * 一把网关密钥:打码后的值,括号里是名字 —— `tw-re…wb4e(claude-code)`, + * 和密钥页的写法一样。 + * + * **打码的值是 core 在请求那一刻记下的**,不是按名字去查现在的密钥:名字 + * 随便起、密钥会更换,按名字查的话,更换之后老记录上会是新密钥的尾巴。 + * 老记录没有这个值,退回「密钥 名字」—— 只写名字的话,读起来像是哪个应用。 + */ +export function KeyLabel({ name, masked }: { name: string; masked?: string | null }) { + const t = useText(keyLabelText); + if (!masked) return <>{t.bare(name)}; + return ( + <> + {masked} + {t.named(name)} + + ); +} + +/** 同上,写成一句话。悬停说明里用 */ +export function keyText(name: string, masked?: string | null): string { + const t = textOf(keyLabelText); + return masked ? masked + t.named(name) : t.bare(name); +} diff --git a/src/RequestDrawer.i18n.tsx b/src/RequestDrawer.i18n.tsx index 4cb1ed49..2050dbaa 100644 --- a/src/RequestDrawer.i18n.tsx +++ b/src/RequestDrawer.i18n.tsx @@ -33,7 +33,13 @@ export const requestDrawerText = messages( generationTime: "生成用时", upstream: "上游", answeredLocally: "本地应答", - client: "客户端", + /** 请求带的是哪把网关密钥 —— 身份 */ + client: "密钥", + /** 按请求头推测的应用 —— 旁证 */ + app: "应用", + guessed: "(按请求头推测)", + /** 非本机来的请求从哪台机器来 */ + peer: "来源", path: "路径", conversion: "格式转换", dropped: "丢弃字段", @@ -123,7 +129,10 @@ export const requestDrawerText = messages( generationTime: "Generation", upstream: "Upstream", answeredLocally: "Answered locally", - client: "Client", + client: "Key", + app: "App", + guessed: " (guessed from the request headers)", + peer: "Source", path: "Path", conversion: "Conversion", dropped: "Dropped", diff --git a/src/RequestDrawer.tsx b/src/RequestDrawer.tsx index 1e4978c9..bd483911 100644 --- a/src/RequestDrawer.tsx +++ b/src/RequestDrawer.tsx @@ -19,7 +19,8 @@ import { NativeSelect, NativeSelectOption } from "@/ui/native-select"; import { Collapsible, CollapsibleTrigger } from "@/ui/collapsible"; import { XIcon } from "lucide-react"; import { priceSourceDetail } from "./upstreams/labels"; -import { attemptText, formatLabel, quoteText, targetLabel } from "./labels"; +import { appLabel, attemptText, formatLabel, quoteText, targetLabel } from "./labels"; +import { KeyLabel } from "./KeyLabel"; import { useText } from "@/i18n"; import { commonText } from "@/i18n/common.i18n"; import { requestDrawerText } from "./RequestDrawer.i18n"; @@ -284,7 +285,21 @@ export default function RequestDrawer({ } /> - + {/* 密钥是身份;应用是按请求头推测的,能伪造;来源是这条连接对面的 + 地址,只有非本机来的才有 */} + } /> + {r.client_hint && ( + + {appLabel(r.client_hint)} + {t.guessed} + + } + /> + )} + {r.peer && {r.peer}} />} {r.path}} /> {/* **转了就要看得见,丢了字段更要看得见** —— 「扩展思考开了却没 生效」在客户端那头无从查起 */} diff --git a/src/labels.ts b/src/labels.ts index ca130ad5..ed46b821 100644 --- a/src/labels.ts +++ b/src/labels.ts @@ -244,3 +244,25 @@ export function takesEffectText(t: TakesEffect): string { export function fieldsOnlyText(): string { return textOf(labelsText).fieldsOnly; } + +/** + * 按请求头认出来的应用叫什么。**这是旁证,不是身份** —— core 按 + * User-Agent 之类猜的,能被伪造,只用来显示;身份是请求带的那把密钥。 + * + * 名字是产品名,中英文一样。认不出的原样显示。 + */ +const APPS: Record = { + "claude-code": "Claude Code", + "claude-desktop": "Claude Desktop", + codex: "Codex CLI", + cursor: "Cursor", + opencode: "opencode", + aider: "Aider", + zed: "Zed", + continue: "Continue", + "gemini-cli": "Gemini CLI", +}; + +export function appLabel(hint: string): string { + return APPS[hint] ?? hint; +} diff --git a/src/requestTable.ts b/src/requestTable.ts index 965f901a..5e010153 100644 --- a/src/requestTable.ts +++ b/src/requestTable.ts @@ -108,11 +108,13 @@ export function filterRows(rows: RequestRow[], f: Filter): RequestRow[] { if (f.client && r.client !== f.client) return false; if (f.provider && r.provider !== f.provider) return false; if (!q) return true; - // 路径、客户端、上游、模型、错误信息都算 —— 排查时你记得住的往往是 - // 错误里的那半句话,而不是哪个字段装着它。 + // 路径、密钥、应用、来源、上游、模型、错误信息都算 —— 排查时记得住的 + // 往往是错误里的那半句话,而不是哪个字段装着它。 return ( r.path.toLowerCase().includes(q) || r.client.toLowerCase().includes(q) || + (r.hint ?? "").toLowerCase().includes(q) || + (r.peer ?? "").includes(q) || r.provider.toLowerCase().includes(q) || (r.model ?? "").toLowerCase().includes(q) || coreText(r.error).toLowerCase().includes(q) diff --git a/src/security/LogTab.i18n.ts b/src/security/LogTab.i18n.ts index 9a5153c1..4a6d9737 100644 --- a/src/security/LogTab.i18n.ts +++ b/src/security/LogTab.i18n.ts @@ -8,6 +8,7 @@ export const logTabText = messages( hit: "命中", action: "处置", request: "请求", + from: (ip: string) => `来自 ${ip}`, /** 还有更多没读进来时,说的是「前 N 条」 */ count: (n: number, more: boolean) => (more ? `前 ${n} 条` : `${n} 条`), /** 同一个值在一个请求里出现了几次 */ @@ -28,6 +29,7 @@ export const logTabText = messages( hit: "Match", action: "Action", request: "Request", + from: (ip: string) => `from ${ip}`, count: (n: number, more: boolean) => more ? `First ${n}` : n === 1 ? "1 entry" : `${n} entries`, times: (n: number) => `${n} times`, diff --git a/src/security/LogTab.tsx b/src/security/LogTab.tsx index 7ee81c94..101c7b99 100644 --- a/src/security/LogTab.tsx +++ b/src/security/LogTab.tsx @@ -10,6 +10,8 @@ import { Tip } from "@/ui/tip"; import { Segmented } from "@/ui/segmented"; import { windowStart, type Range } from "@/ui/range"; import { when } from "@/format"; +import { appLabel } from "@/labels"; +import { KeyLabel } from "@/KeyLabel"; import { useText } from "@/i18n"; import { errorText } from "@/i18n/core.i18n"; import RequestDrawer from "@/RequestDrawer"; @@ -184,13 +186,13 @@ export function LogTab({ ) : ( <> {/* 列宽是定死的:窗口再窄,「命中」一列也要留出能读的宽度,放不下就横向滚 */} - +
- + @@ -232,12 +234,18 @@ export function LogTab({ + {/* 密钥是身份:打码的值加名字。名字是随便起的,不代表是哪个应用 */}
- {e.client || "—"} + → {e.provider || "—"}
- {e.model &&
{e.model}
} + {/* 模型、推测出的应用、非本机的来源 */} +
+ {[e.model, e.client_hint && appLabel(e.client_hint), e.peer && t.from(e.peer)] + .filter(Boolean) + .join(" · ")} +
ev.stopPropagation()}> diff --git a/src/traffic/RequestTable.tsx b/src/traffic/RequestTable.tsx index b4364f49..1a8b7743 100644 --- a/src/traffic/RequestTable.tsx +++ b/src/traffic/RequestTable.tsx @@ -5,7 +5,8 @@ import { coreText } from "@/i18n/core.i18n"; import { cn } from "@/lib/utils"; import { latency, money, statusTone, tokens, when } from "@/format"; import { Tip } from "@/ui/tip"; -import { translatedText } from "@/labels"; +import { appLabel, translatedText } from "@/labels"; +import { KeyLabel, keyText } from "@/KeyLabel"; import { ruleName } from "@/security/labels"; import { Button } from "@/ui/button"; import { RowMenu } from "@/ui/row-menu"; @@ -503,15 +504,22 @@ function Row({ {/* 和上一行相同就淡化 —— 眼睛要找的是变化的那一行 */} + {/* 密钥是身份;后面是推测出的应用和非本机的来源,淡一档 */} {showClient && ( x.client) + "max-w-56 truncate " + + (same((x) => `${x.client}|${x.keyMasked ?? ""}|${x.hint ?? ""}|${x.peer ?? ""}`) ? "text-neutral-400/50" - : "" + : "") } + title={[keyText(r.client, r.keyMasked), r.hint && appLabel(r.hint), r.peer && t.fromPeer(r.peer)] + .filter(Boolean) + .join(" · ")} > - {r.client} + + {r.hint && · {appLabel(r.hint)}} + {r.peer && · {t.fromPeer(r.peer)}} )} {/* diff --git a/src/types.ts b/src/types.ts index 299dcafb..15a77ef8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -33,7 +33,7 @@ export type CoreEvent = * 长出一批对不上任何会话的影子组。界面因此不读它:归组只认落库之后 * 的 `session`。 */ - | { kind: "request_started"; id: number; client: string; provider: string; model: string; method: string; path: string; at_ms: number; session_fp?: string | null } + | { kind: "request_started"; id: number; client: string; client_hint?: string | null; peer?: string | null; key_masked?: string | null; provider: string; model: string; method: string; path: string; at_ms: number; session_fp?: string | null } | { kind: "request_headers"; id: number; status: number; ttfb_ms: number } /** * 三种结局(结束、失败、取消)都带着 `model`,和开始事件里的是同一个。 @@ -82,7 +82,7 @@ export type CoreEvent = * 会让那两个数字都变得没意义。它单独计数。 */ /** `probe` 和 `ProbeView.id` 是同一个词表 */ - | { kind: "locally_answered"; id: number; client: string; probe: string; at_ms: number } + | { kind: "locally_answered"; id: number; client: string; client_hint?: string | null; peer?: string | null; probe: string; at_ms: number } /** 配置换了一份新的进去,已经生效。界面靠它知道自己手里那份过期了。 */ | { kind: "config_reloaded"; id: number; version: string; origin: ConfigOrigin; at_ms: number } /** @@ -256,7 +256,14 @@ export interface CoreStatus { /** 一行请求,由四类事件缝出来。 */ export interface RequestRow { id: number; + /** 请求带的网关密钥叫什么。**是身份,不是应用** —— 一把密钥可以几个应用共用 */ client: string; + /** 按请求头推测是哪个应用发的(`claude-code`、`codex`…)。可以伪造,只用来显示 */ + hint?: string; + /** 非本机来的请求的来源地址。本机来的没有 */ + peer?: string; + /** 请求带的那把密钥打码后的样子(`tw-re…wb4e`),请求那一刻的 */ + keyMasked?: string; provider: string; /** 哪个模型。**决定这次多贵、多慢的就是它** */ model?: string; @@ -313,6 +320,9 @@ export function applyEvent(rows: Map, ev: CoreEvent): void { rows.set(ev.id, { id: ev.id, client: ev.client, + hint: ev.client_hint ?? undefined, + peer: ev.peer ?? undefined, + keyMasked: ev.key_masked ?? undefined, provider: ev.provider, // 老记录里没有这个字段,空串当作「不知道」 model: ev.model || undefined, @@ -671,6 +681,12 @@ export interface HistoryRow { session?: string | null; /** 两项防护在这条请求上留下的记录。没命中的没有 */ security?: SecurityEventView[]; + /** 按请求头推测是哪个应用发的。可以伪造,只用来显示 */ + client_hint?: string | null; + /** 非本机来的请求的来源地址。本机来的没有 */ + peer?: string | null; + /** 请求带的那把密钥打码后的样子,请求那一刻的 */ + key_masked?: string | null; } /** 一次请求做过的格式转换 */ @@ -1320,6 +1336,12 @@ export interface SecurityEventView { /** 出站脱敏是打码后的值;工具调用审查是命中的那一小段 */ excerpt: string; count: number; + /** 按请求头推测是哪个应用发的 */ + client_hint?: string | null; + /** 非本机来的请求的来源地址 */ + peer?: string | null; + /** 请求带的那把密钥打码后的样子,请求那一刻的 */ + key_masked?: string | null; } export interface SecurityEventsPage { diff --git a/src/useRequests.ts b/src/useRequests.ts index be341fa4..9b23da11 100644 --- a/src/useRequests.ts +++ b/src/useRequests.ts @@ -171,6 +171,9 @@ export function useRequests() { const marks = marksFromEvents(h.security); cur.secrets ??= marks.secrets; cur.flagged ??= marks.flagged; + cur.hint ??= h.client_hint ?? undefined; + cur.peer ??= h.peer ?? undefined; + cur.keyMasked ??= h.key_masked ?? undefined; continue; } store.current.set(h.id, { @@ -192,6 +195,9 @@ export function useRequests() { error: h.error ?? undefined, translated: h.translated ?? undefined, session: h.session ?? undefined, + hint: h.client_hint ?? undefined, + peer: h.peer ?? undefined, + keyMasked: h.key_masked ?? undefined, ...marksFromEvents(h.security), }); }