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
8 changes: 4 additions & 4 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions src-tauri/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,25 @@ impl ControlClient {
.await
}

/// 一条内置规则在拦截档下做什么。只有工具调用审查的规则有这一项
pub async fn set_security_rule_action(
&self,
guard: &str,
id: &str,
req: &tw_api::ActionSave,
) -> Result<tw_api::ConfigWritten> {
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,
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions src-tauri/src/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<tw_api::ConfigWritten> {
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>,
Expand Down
18 changes: 8 additions & 10 deletions src/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -362,16 +362,14 @@ export default function Dashboard({
* 补一条 `request_priced`,所以金额也是推过来的,只比用量晚一拍。
*/
const metric = (
<ToggleGroup
type="single"
variant="outline"
size="sm"
<Segmented<"token" | "cost">
value={by}
onValueChange={(v) => v && setBy(v as "token" | "cost")}
>
<ToggleGroupItem value="token">{t.byTokens}</ToggleGroupItem>
<ToggleGroupItem value="cost">{t.byCost}</ToggleGroupItem>
</ToggleGroup>
options={[
{ id: "token", label: t.byTokens },
{ id: "cost", label: t.byCost },
]}
onChange={setBy}
/>
);

if (error) {
Expand Down
27 changes: 10 additions & 17 deletions src/routing/ProbesTab.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -65,22 +65,15 @@ export function ProbesTab({
<li key={p.id} className="rounded-md border border-border px-3 py-2">
<div className="flex items-baseline gap-3">
<span className="tw-body font-medium">{kind?.label ?? probeLabel(p.id)}</span>
<ToggleGroup
type="single"
variant="outline"
size="sm"
className="ml-auto"
value={p.mode}
disabled={busy === p.id}
/* 择一,不许择空 —— 空了等于没有模式 */
onValueChange={(v) => v && void set(p.id, v)}
>
{modes.map((m) => (
<ToggleGroupItem key={m.id} value={m.id}>
{m.label}
</ToggleGroupItem>
))}
</ToggleGroup>
<div className="ml-auto">
<Segmented<string>
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)}
/>
</div>
</div>
{kind && <p className="mt-1 tw-body text-muted-foreground">{kind.what}</p>}
<p className="mt-0.5 tw-label text-muted-foreground">
Expand Down
35 changes: 14 additions & 21 deletions src/security/GuardTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -61,21 +61,13 @@ export function GuardTab({
<div className="flex flex-wrap items-center gap-3">
<p className="tw-body font-medium">{copy.lead}</p>
<div className="flex-1" />
<ToggleGroup
type="single"
variant="outline"
size="sm"
<Segmented<Mode>
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) => (
<ToggleGroupItem key={m} value={m}>
{lt.modes[m]}
</ToggleGroupItem>
))}
</ToggleGroup>
options={MODES.map((m) => ({ id: m, label: lt.modes[m] }))}
onChange={(v) => v !== mode && actions.mode(v)}
/>
</div>
<p className="mt-2 tw-body">{copy.now[mode]}</p>
<p className="mt-0.5 tw-label text-muted-foreground">
Expand Down Expand Up @@ -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 给的顺序就是界面的顺序 */
Expand Down
21 changes: 10 additions & 11 deletions src/security/LogTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -153,17 +153,16 @@ export function LogTab({
return (
<div className="flex flex-col gap-3">
<div className="flex flex-wrap items-center gap-2">
<ToggleGroup
type="single"
variant="outline"
size="sm"
<Segmented<LogGuard>
label={t.type}
value={guard}
onValueChange={(v) => v && onGuard(v as LogGuard)}
>
<ToggleGroupItem value="all">{t.all}</ToggleGroupItem>
<ToggleGroupItem value="redact">{lt.guardShort.redact}</ToggleGroupItem>
<ToggleGroupItem value="inspect_tools">{lt.guardShort.inspect_tools}</ToggleGroupItem>
</ToggleGroup>
options={[
{ id: "all", label: t.all },
{ id: "redact", label: lt.guardShort.redact },
{ id: "inspect_tools", label: lt.guardShort.inspect_tools },
]}
onChange={onGuard}
/>
<div className="flex-1" />
{rows && rows.length > 0 && (
<span className="tw-label tabular-nums text-muted-foreground">{t.count(rows.length, more)}</span>
Expand Down
11 changes: 7 additions & 4 deletions src/security/RuleDialog.i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ export const ruleDialogText = messages(
match: "匹配",
whenEnforced: "拦截时",
cut: "切断",
cutHint: "调用不会完整到达客户端,无法执行",
record: "仅记录",
recordHint: "调用照常返回",
cutWhat: "命中的调用不会完整到达客户端,因而无法执行。",
recordWhat: "命中的调用照常返回,只记入日志。",
/** 内置规则改过处置时补的一句 */
factory: (what: string) => `出厂设置为「${what}」。`,
state: "状态",
on: "已启用",
off: "已停用",
Expand Down Expand Up @@ -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",
Expand Down
Loading
Loading