Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/security/LogTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,22 @@ function LogTable({
const lt = useText(securityLabelsText);
const shown = usePresentList(events, (e) => e.id);

// 按天分组。列表本来就是倒序,同一天的挨在一起
/*
按天分组。列表本来就是倒序,同一天的挨在一起 —— 除了淡出的那一小会儿:
一下子来了一整页新记录时,读回来的全是新的,旧的几行留在原处淡出,新行
排在它们后面,同一天会被隔成两段。按天归拢,一天只有一个标题。
*/
const days: { key: string; at: number; rows: typeof shown }[] = [];
const byKey = new Map<string, (typeof days)[number]>();
for (const s of shown) {
const key = dayKey(s.item.at_ms);
const last = days[days.length - 1];
if (last && last.key === key) last.rows.push(s);
else days.push({ key, at: s.item.at_ms, rows: [s] });
const day = byKey.get(key);
if (day) day.rows.push(s);
else {
const next = { key, at: s.item.at_ms, rows: [s] };
byKey.set(key, next);
days.push(next);
}
}

/**
Expand Down
24 changes: 19 additions & 5 deletions src/security/SecurityPage.i18n.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ReactNode } from "react";
import { messages } from "@/i18n";
import type { SecurityOutcome } from "@/types";

/** 英文句中的「Since 9/8」要小写开头 */
const lower = (s: string) => s.charAt(0).toLowerCase() + s.slice(1);
Expand All @@ -16,14 +17,21 @@ export const securityPageText = messages(
off: "项关闭",
},
/**
* 页头上日志那段时间里命中了几次。`n` 是画好的数字;读到的是前若干条时
* 带「+」。自定义区间的名字本身是「9/8 至今」,不再加「内」
* 页头上日志那段时间里一共命中了几次。`count` 是条数(英文按它分单复数),
* `n` 是画好的数字。自定义区间的名字本身是「9/8 至今」,不再加「内」
*/
hits: (n: ReactNode, range: string, custom: boolean) => (
hits: (_count: number, n: ReactNode, range: string, custom: boolean) => (
<>
{custom ? range : `${range}内`} {n} 次命中
</>
),
/** 页头上各做了什么:「1 已切断」。和日志「处置」一栏同一组词 */
outcomes: {
cut: "已切断",
blocked: "已拒绝",
replaced: "已替换",
recorded: "仅记录",
} satisfies Record<SecurityOutcome, string>,
/** 标签上的名字。和源列表、规则表里的名字一致 */
tabs: {
redact: "出站脱敏",
Expand All @@ -47,11 +55,17 @@ export const securityPageText = messages(
observe: "observing",
off: "off",
},
hits: (n: ReactNode, range: string, custom: boolean) => (
hits: (count: number, n: ReactNode, range: string, custom: boolean) => (
<>
{n} {custom ? `matches ${lower(range)}` : `matches in the last ${range}`}
{n} {count === 1 ? "match" : "matches"} {custom ? lower(range) : `in the last ${range}`}
</>
),
outcomes: {
cut: "cut off",
blocked: "refused",
replaced: "replaced",
recorded: "recorded",
},
// 英文名长,标签用短名;全名在每一项的档位那一块上
tabs: {
redact: "Redaction",
Expand Down
59 changes: 37 additions & 22 deletions src/security/SecurityPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from "@/types";
import { api, hasAction, hasCustom, type CustomGuard, type RuleSave } from "./api";
import { GuardSkeleton, GuardTab, type RuleActions } from "./GuardTab";
import { modeTone, viewName } from "./labels";
import { modeTone, OUTCOMES, outcomeTone, viewName } from "./labels";
import { securityLabelsText } from "./labels.i18n";
import { LogTab, type LogActions } from "./LogTab";
import { OutputLimitTab } from "./OutputLimitTab";
Expand Down Expand Up @@ -70,8 +70,8 @@ const ruleKey = (guard: RuleGuard, r: SecurityRuleView) => `${guard}/${r.custom
* MCP 页,不在这里:这一页只管经过网关的请求。
*
* 页头一行是全貌:几项在拦截、几项在观察、几项关着(状态点和标签上的同色),
* 以及日志那段时间里命中了几次。日志在第一个标签:开着一项防护却不知道它查到
* 了什么,等于没开。
* 以及日志那段时间里一共命中了几次、各做了什么。日志在第一个标签:开着一项防护
* 却不知道它查到了什么,等于没开。
*
* **改动先画出来再写**:启停规则、换档都是先改界面,写完给一个带「撤销」的
* 提示;几处连着改时一个接一个写(每一次写都要带上一次写完的版本号)。
Expand All @@ -93,7 +93,7 @@ export default function SecurityPage({
const lt = useText(securityLabelsText);
const [tab, setTab] = useState<SecurityTab>("log");
const detail = useResource<SecurityDetail>("security-detail", () => api.detail(), { deps: [configVersion] });
// 日志的区间放在这一层:页头的命中次数和日志是同一份
// 日志的区间放在这一层:页头的命中次数和日志是同一次读取
const [range, setRange] = useRange("tw-security-range", "1d");
const log = useSecurityLog(range, tick);
const [dialog, setDialog] = useState<DialogState>(null);
Expand Down Expand Up @@ -352,8 +352,12 @@ export default function SecurityPage({
}

/**
* 页头的一行:几项防护各在哪一档(和标签上的点同色),日志那段时间里命中了
* 几次。命中次数就是日志读到的条数;读到的是前若干条时写「100+」,不估。
* 页头的一行:几项防护各在哪一档(和标签上的点同色);日志那段时间里一共命中了
* 几次、各做了什么(和日志「处置」一栏同色)。
*
* **条数是 core 按整段时间数的**(`total`、`by_outcome`),不是日志读到了几条:
* 日志往下翻多少页,这几个数都不变;来了新记录、日志重读时跟着走。换了区间
* 直接落到新值,不从旧区间的数滚过去(`scope`)。
*/
function Summary({
detail,
Expand All @@ -371,6 +375,31 @@ function Summary({
if (!detail && loading) return <Skeleton className="my-1 h-3 w-56 rounded-sm" />;
const counts: Record<GuardMode, number> = { enforce: 0, observe: 0, off: 0 };
if (detail) for (const g of GUARDS) counts[detail[g].mode] += 1;
// 日志还在读(开页、换了没读过的区间):先占住数字的位置;读不到时日志那里说
const hits = page ? (
// 总数和各做了什么是一句话:窗口窄、一行放不下时整句换到下一行,不从中间断开
<span className="inline-flex min-w-0 flex-wrap items-center gap-x-3 gap-y-0.5">
{/* 不能是 flex:flex 会吞掉数字两边的空格 */}
<span className="whitespace-nowrap">
{t.hits(
page.total,
<AnimatedNumber value={page.total} scope={log.scope} className="font-medium text-foreground" />,
range.label,
range.custom === true,
)}
</span>
{OUTCOMES.filter((o) => page.by_outcome[o] > 0).map((o) => (
<SummaryItem
key={o}
lead={<StatusDot tone={outcomeTone(o)} />}
value={<AnimatedNumber value={page.by_outcome[o]} scope={log.scope} />}
label={t.outcomes[o]}
/>
))}
</span>
) : log.r.loading ? (
<Skeleton className="my-1 h-3 w-40 rounded-sm" />
) : null;
return (
<>
{detail &&
Expand All @@ -379,22 +408,8 @@ function Summary({
.map((m) => (
<SummaryItem key={m} lead={<StatusDot tone={modeTone(m)} />} value={counts[m]} label={t.modes[m]} />
))}
{detail && page && <span aria-hidden className="h-3 w-px bg-border" />}
{page && (
// 不能是 flex:flex 会吞掉数字两边的空格
<span className="whitespace-nowrap">
{t.hits(
<AnimatedNumber
value={page.events.length}
scope={log.scope}
format={(n) => `${Math.round(n)}${page.more ? "+" : ""}`}
className="font-medium text-foreground"
/>,
range.label,
range.custom === true,
)}
</span>
)}
{detail && hits && <span aria-hidden className="h-3 w-px bg-border" />}
{hits}
</>
);
}
10 changes: 9 additions & 1 deletion src/security/labels.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
import { setLang } from "@/i18n";
import { clock, dayHead, dayKey, modeTone, outcomeTone } from "./labels";
import type { SecurityOutcomeCounts } from "@/types";
import { clock, dayHead, dayKey, modeTone, OUTCOMES, outcomeTone } from "./labels";

/** 2026-09-25(周五)16:42:07,本地时区 */
const NOW = new Date(2026, 8, 25, 16, 42, 7).getTime();
Expand Down Expand Up @@ -61,4 +62,11 @@ describe("状态的颜色", () => {
expect(outcomeTone("replaced")).toBe("ok");
expect(outcomeTone("recorded")).toBe("warn");
});

/** 页头按这个先后列各做法的条数:四种各一次,红的在前 */
it("处置的先后", () => {
const all: SecurityOutcomeCounts = { recorded: 0, replaced: 0, cut: 0, blocked: 0 };
expect([...OUTCOMES].sort()).toEqual(Object.keys(all).sort());
expect(OUTCOMES.map(outcomeTone)).toEqual(["error", "error", "ok", "warn"]);
});
});
6 changes: 6 additions & 0 deletions src/security/labels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export function outcomeTone(action: SecurityOutcome): StatusTone {
return action === "cut" || action === "blocked" ? "error" : action === "replaced" ? "ok" : "warn";
}

/**
* 几种处置一起列时的先后(页头):先说改变了请求结局的切断、拒绝,再说替换、
* 仅记录。和规则「拦截时」的选项同一个先后。
*/
export const OUTCOMES: readonly SecurityOutcome[] = ["cut", "blocked", "replaced", "recorded"];

/**
* 一条规则叫什么。
*
Expand Down
5 changes: 3 additions & 2 deletions src/security/useSecurityLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ export interface SecurityLog {
/**
* 安全日志:一段时间里的命中,按时间倒序,一页一页往下翻。
*
* **页头和日志用同一份。**页头的「N 次命中」就是日志里读出来的条数,两处
* 不会对不上;换页再回来先画缓存(`useResource`),后台再读。
* **页头和日志用同一次读取。**每一页都带着整段时间的总数和各做法的条数
* (`total`、`by_outcome`),页头说的是这两样;日志列的是其中最新的若干条,
* 往下翻才读更早的。换页再回来先画缓存(`useResource`),后台再读。
*
* **换了区间从第一页读起;只是来了新记录,就把已经翻出来的几页一起重读。**
* 用户往下翻了三页正看着,一条新命中进来就把列表弹回第一页,等于把他翻过的
Expand Down
Loading