diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index d3848e33..c5b93187 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -4982,8 +4982,8 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "tw-api" -version = "0.18.0" -source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.18.0#fbf7858760e8507c73318f515b832f4bada67554" +version = "0.19.0" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.19.0#a299c4a03c2110c26c18f31f2145e45c36f78135" dependencies = [ "serde", "serde_json", @@ -4992,8 +4992,8 @@ dependencies = [ [[package]] name = "tw-types" -version = "0.18.0" -source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.18.0#fbf7858760e8507c73318f515b832f4bada67554" +version = "0.19.0" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.19.0#a299c4a03c2110c26c18f31f2145e45c36f78135" dependencies = [ "serde", "serde_json", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 7f8dadc3..68e629a2 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.18.0" } +tw-api = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.19.0" } anyhow = "1" libc = "0.2" diff --git a/src-tauri/src/control.rs b/src-tauri/src/control.rs index f4c68a0b..0d8c05df 100644 --- a/src-tauri/src/control.rs +++ b/src-tauri/src/control.rs @@ -441,6 +441,25 @@ impl ControlClient { .await } + /// 一条内置规则在拦截档下做什么。只有工具调用审查的规则有这一项 + pub async fn set_security_rule_action( + &self, + guard: &str, + id: &str, + req: &tw_api::ActionSave, + ) -> Result { + self.send_json( + hyper::Method::PUT, + &format!( + "/security/{}/builtin/{}/action", + segment(guard), + segment(id) + ), + req, + ) + .await + } + pub async fn create_security_rule( &self, guard: &str, diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 25164960..e7a24402 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1539,6 +1539,7 @@ pub fn run() { security::security_events, security::set_security_mode, security::toggle_security_rule, + security::set_security_rule_action, security::create_security_rule, security::update_security_rule, security::delete_security_rule, diff --git a/src-tauri/src/security.rs b/src-tauri/src/security.rs index 90f60418..2c265c1c 100644 --- a/src-tauri/src/security.rs +++ b/src-tauri/src/security.rs @@ -64,6 +64,20 @@ pub async fn toggle_security_rule( .map_err(text) } +#[tauri::command] +pub async fn set_security_rule_action( + state: tauri::State<'_, AppState>, + guard: String, + id: String, + save: tw_api::ActionSave, +) -> Out { + state + .control + .set_security_rule_action(&guard, &id, &save) + .await + .map_err(text) +} + #[tauri::command] pub async fn create_security_rule( state: tauri::State<'_, AppState>, diff --git a/src/Dashboard.tsx b/src/Dashboard.tsx index a632da94..1679192b 100644 --- a/src/Dashboard.tsx +++ b/src/Dashboard.tsx @@ -10,7 +10,7 @@ import { compact, densify } from "./format"; import { usd, type Dashboard as Data, type Guard, type LatencyView, type Overview } from "./types"; import { Alert, AlertDescription } from "@/ui/alert"; import { RangePicker, bucketFor, useRange, windowStart, type Range } from "@/ui/range"; -import { ToggleGroup, ToggleGroupItem } from "@/ui/toggle-group"; +import { Segmented } from "@/ui/segmented"; import { Skeleton } from "@/ui/skeleton"; import { LIVE_BUCKET_MS, LIVE_REACH_MS, liveRate, useLive } from "./useLive"; import { useCountUp } from "./useCountUp"; @@ -362,16 +362,14 @@ export default function Dashboard({ * 补一条 `request_priced`,所以金额也是推过来的,只比用量晚一拍。 */ const metric = ( - value={by} - onValueChange={(v) => v && setBy(v as "token" | "cost")} - > - {t.byTokens} - {t.byCost} - + options={[ + { id: "token", label: t.byTokens }, + { id: "cost", label: t.byCost }, + ]} + onChange={setBy} + /> ); if (error) { diff --git a/src/routing/ProbesTab.tsx b/src/routing/ProbesTab.tsx index f6b70fce..dd00456e 100644 --- a/src/routing/ProbesTab.tsx +++ b/src/routing/ProbesTab.tsx @@ -1,6 +1,6 @@ import { useState } from "react"; import { toast } from "sonner"; -import { ToggleGroup, ToggleGroupItem } from "@/ui/toggle-group"; +import { Segmented } from "@/ui/segmented"; import { useText } from "@/i18n"; import { errorText } from "@/i18n/core.i18n"; import { PROBES, probeLabel } from "@/labels"; @@ -65,22 +65,15 @@ export function ProbesTab({
  • {kind?.label ?? probeLabel(p.id)} - v && void set(p.id, v)} - > - {modes.map((m) => ( - - {m.label} - - ))} - +
    + + label={kind?.label ?? probeLabel(p.id)} + value={p.mode} + disabled={busy === p.id} + options={modes.map((m) => ({ id: m.id, label: m.label }))} + onChange={(v) => v !== p.mode && void set(p.id, v)} + /> +
    {kind &&

    {kind.what}

    }

    diff --git a/src/security/GuardTab.tsx b/src/security/GuardTab.tsx index db9f9eb6..f46af768 100644 --- a/src/security/GuardTab.tsx +++ b/src/security/GuardTab.tsx @@ -5,7 +5,7 @@ import { Button } from "@/ui/button"; import { RowMenu, RowMenuButton, type MenuItems } from "@/ui/row-menu"; import { Switch } from "@/ui/switch"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/ui/table"; -import { ToggleGroup, ToggleGroupItem } from "@/ui/toggle-group"; +import { Segmented } from "@/ui/segmented"; import { useText } from "@/i18n"; import { commonText } from "@/i18n/common.i18n"; import type { Guard, GuardDetail, SecurityRuleView } from "@/types"; @@ -61,21 +61,13 @@ export function GuardTab({

    {copy.lead}

    - + label={t.modeFor(lt.guards[guard])} value={mode} disabled={busy} - aria-label={t.modeFor(lt.guards[guard])} - onValueChange={(v) => v && v !== mode && actions.mode(v as Mode)} - > - {MODES.map((m) => ( - - {lt.modes[m]} - - ))} - + options={MODES.map((m) => ({ id: m, label: lt.modes[m] }))} + onChange={(v) => v !== mode && actions.mode(v)} + />

    {copy.now[mode]}

    @@ -136,13 +128,14 @@ function useMenu(guard: Guard, actions: RuleActions) { { kind: "sep" }, { kind: "item", label: common.delete, onSelect: () => actions.remove(r), danger: true }, ] - : [ - { kind: "item", label: t.view, onSelect: () => actions.open(r) }, - // 出站脱敏的内置规则不是一条正则,复制过去写不出等价的写法 - ...(guard === "inspect_tools" - ? [{ kind: "item" as const, label: t.copyAsCustom, onSelect: () => actions.copy(r) }] - : []), - ]; + : guard === "inspect_tools" + ? [ + // 内置的工具调用规则能改拦截时的处置,所以是「编辑」不是「查看」 + { kind: "item", label: common.edit, onSelect: () => actions.open(r) }, + { kind: "item", label: t.copyAsCustom, onSelect: () => actions.copy(r) }, + ] + : // 出站脱敏的内置规则只能启停,也不是一条正则,复制不出等价的写法 + [{ kind: "item", label: t.view, onSelect: () => actions.open(r) }]; } /** 按顺序把规则分成几段,每段一个标题。core 给的顺序就是界面的顺序 */ diff --git a/src/security/LogTab.tsx b/src/security/LogTab.tsx index ee930824..7ee81c94 100644 --- a/src/security/LogTab.tsx +++ b/src/security/LogTab.tsx @@ -7,7 +7,7 @@ import { Skeleton } from "@/ui/skeleton"; import { Spinner } from "@/ui/spinner"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/ui/table"; import { Tip } from "@/ui/tip"; -import { ToggleGroup, ToggleGroupItem } from "@/ui/toggle-group"; +import { Segmented } from "@/ui/segmented"; import { windowStart, type Range } from "@/ui/range"; import { when } from "@/format"; import { useText } from "@/i18n"; @@ -153,17 +153,16 @@ export function LogTab({ return (

    - + label={t.type} value={guard} - onValueChange={(v) => v && onGuard(v as LogGuard)} - > - {t.all} - {lt.guardShort.redact} - {lt.guardShort.inspect_tools} - + options={[ + { id: "all", label: t.all }, + { id: "redact", label: lt.guardShort.redact }, + { id: "inspect_tools", label: lt.guardShort.inspect_tools }, + ]} + onChange={onGuard} + />
    {rows && rows.length > 0 && ( {t.count(rows.length, more)} diff --git a/src/security/RuleDialog.i18n.ts b/src/security/RuleDialog.i18n.ts index 1a382be5..7d1bab80 100644 --- a/src/security/RuleDialog.i18n.ts +++ b/src/security/RuleDialog.i18n.ts @@ -24,9 +24,11 @@ export const ruleDialogText = messages( match: "匹配", whenEnforced: "拦截时", cut: "切断", - cutHint: "调用不会完整到达客户端,无法执行", record: "仅记录", - recordHint: "调用照常返回", + cutWhat: "命中的调用不会完整到达客户端,因而无法执行。", + recordWhat: "命中的调用照常返回,只记入日志。", + /** 内置规则改过处置时补的一句 */ + factory: (what: string) => `出厂设置为「${what}」。`, state: "状态", on: "已启用", off: "已停用", @@ -80,9 +82,10 @@ export const ruleDialogText = messages( match: "Match", whenEnforced: "On enforce", cut: "Cut off", - cutHint: "The call never fully reaches the client and cannot run", record: "Record only", - recordHint: "The call is returned as usual", + cutWhat: "A matching call never fully reaches the client, so it cannot run. ", + recordWhat: "A matching call is returned as usual and recorded in the log. ", + factory: (what: string) => `The factory setting is “${what}”.`, state: "State", on: "On", off: "Off", diff --git a/src/security/RuleDialog.tsx b/src/security/RuleDialog.tsx index 8d93dd4f..0c1e41a8 100644 --- a/src/security/RuleDialog.tsx +++ b/src/security/RuleDialog.tsx @@ -13,6 +13,7 @@ import { import { Input } from "@/ui/input"; import { Spinner } from "@/ui/spinner"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/ui/table"; +import { Segmented } from "@/ui/segmented"; import { Textarea } from "@/ui/textarea"; import { useText } from "@/i18n"; import { commonText } from "@/i18n/common.i18n"; @@ -61,6 +62,40 @@ function Field({ ); } +/** + * 拦截档下做什么:切断,还是只记录。**自定义规则和内置规则用同一个** —— + * 内置规则提供的只是一条正则,命中之后怎么处置和自定义规则一样由用户定。 + */ +function ActionField({ + value, + onChange, + factory, +}: { + value: Action; + onChange: (a: Action) => void; + /** 内置规则出厂时的处置。改过的话在下面说一句 */ + factory?: Action | null; +}) { + const t = useText(ruleDialogText); + const what = value === "cut" ? t.cutWhat : t.recordWhat; + return ( + + + label={t.whenEnforced} + value={value} + options={[ + { id: "cut", label: t.cut }, + { id: "record", label: t.record }, + ]} + onChange={onChange} + /> + + ); +} + /** * 试出来的结果:命中几处,在原文里标出来。 * @@ -209,24 +244,7 @@ export function RuleDialog({ /> - {guard === "inspect_tools" && ( -
    - {t.whenEnforced} - {(["cut", "record"] as const).map((a) => ( - - ))} -
    - )} + {guard === "inspect_tools" && }