From 2d7c41a5b2c87af14acb05c3e13d3c0c76089387 Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Sat, 4 Jan 2025 20:47:14 +0800 Subject: [PATCH] update waf api (#91) --- src/lib/utils.ts | 104 --------------------------------------------- src/routes/waf.tsx | 25 ++++++----- 2 files changed, 15 insertions(+), 114 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 6adeb78..5b07416 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -145,110 +145,6 @@ export function joinIP(p?: ModelIP) { return "" } -function base64toUint8Array(base64str: string) { - const binary = atob(base64str) - const len = binary.length - const buf = new Uint8Array(len) - for (let i = 0; i < len; i++) { - buf[i] = binary.charCodeAt(i) - } - return buf -} - -export function ip16Str(base64str: string) { - const buf = base64toUint8Array(base64str) - const ip4 = buf.slice(-6) - if (ip4[0] === 255 && ip4[1] === 255) { - return ip4.slice(2).join(".") - } - return ipv6BinaryToString(buf) -} - -const digits = "0123456789abcdef" - -function appendHex(b: string[], x: number): void { - if (x >= 0x1000) { - b.push(digits[(x >> 12) & 0xf]) - } - if (x >= 0x100) { - b.push(digits[(x >> 8) & 0xf]) - } - if (x >= 0x10) { - b.push(digits[(x >> 4) & 0xf]) - } - b.push(digits[x & 0xf]) -} - -function ipv6BinaryToString(ip: Uint8Array): string { - let ipBytes: Uint8Array - - if (ip.length !== 16) { - ipBytes = new Uint8Array(16) - const len = Math.min(ip.length, 16) - ipBytes.set(ip.subarray(0, len)) - } else { - ipBytes = ip - } - - const hextets: number[] = [] - for (let i = 0; i < 16; i += 2) { - hextets.push((ipBytes[i] << 8) | ipBytes[i + 1]) - } - - let zeroStart = -1 - let zeroLength = 0 - - for (let i = 0; i <= hextets.length; ) { - let j = i - while (j < hextets.length && hextets[j] === 0) { - j++ - } - const length = j - i - if (length >= 2 && length > zeroLength) { - zeroStart = i - zeroLength = length - } - if (j === i) { - i++ - } else { - i = j - } - } - - const parts: string[] = [] - for (let i = 0; i < hextets.length; i++) { - if (zeroLength > 0 && i === zeroStart) { - parts.push("") - i += zeroLength - 1 - continue - } - - if (parts.length > 0) { - parts.push(":") - } - - const b: string[] = [] - appendHex(b, hextets[i]) - parts.push(b.join("")) - } - - let ipv6 = parts.join("") - - if (ipv6.startsWith("::")) { - } else if (ipv6.startsWith(":")) { - ipv6 = ":" + ipv6 - } - if (ipv6.endsWith("::")) { - } else if (ipv6.endsWith(":")) { - ipv6 = ipv6 + ":" - } - if (ipv6 === "") { - ipv6 = "::" - } - - return ipv6 -} - export async function copyToClipboard(text: string) { try { return await navigator.clipboard.writeText(text) diff --git a/src/routes/waf.tsx b/src/routes/waf.tsx index 3463f6f..c547ded 100644 --- a/src/routes/waf.tsx +++ b/src/routes/waf.tsx @@ -22,8 +22,12 @@ import { TableRow, } from "@/components/ui/table" import { useAuth } from "@/hooks/useAuth" -import { ip16Str } from "@/lib/utils" -import { ModelWAF, ModelWAFApiMock, wafBlockIdentifiers, wafBlockReasons } from "@/types" +import { + GithubComNezhahqNezhaModelValueArrayModelWAFApiMock, + ModelWAFApiMock, + wafBlockIdentifiers, + wafBlockReasons, +} from "@/types" import { ColumnDef, flexRender, getCoreRowModel, useReactTable } from "@tanstack/react-table" import { useEffect, useMemo } from "react" import { useTranslation } from "react-i18next" @@ -41,10 +45,11 @@ export default function WAFPage() { // 计算 offset const offset = (page - 1) * pageSize - const { data, mutate, error, isLoading } = useSWR( - `/api/v1/waf?offset=${offset}&limit=${pageSize}`, - swrFetcher, - ) + const { data, mutate, error, isLoading } = + useSWR( + `/api/v1/waf?offset=${offset}&limit=${pageSize}`, + swrFetcher, + ) const isAdmin = profile?.role === 0 @@ -56,7 +61,7 @@ export default function WAFPage() { // eslint-disable-next-line react-hooks/exhaustive-deps }, [error]) - let columns: ColumnDef[] = [ + let columns: ColumnDef[] = [ { id: "select", header: ({ table }) => ( @@ -82,7 +87,7 @@ export default function WAFPage() { { header: "IP", accessorKey: "ip", - accessorFn: (row) => ip16Str(row.ip ?? ""), + accessorFn: (row) => row.ip, }, { header: t("Count"), @@ -122,7 +127,7 @@ export default function WAFPage() { className="flex gap-2" delete={{ fn: deleteWAF, - id: ip16Str(s.ip ?? ""), + id: s.ip || "", mutate: mutate, }} > @@ -260,7 +265,7 @@ export default function WAFPage() { className="flex-2 flex gap-2 ml-auto" delete={{ fn: deleteWAF, - id: selectedRows.map((r) => ip16Str(r.original.ip ?? "")), + id: selectedRows.map((r) => r.original.ip || ""), mutate: mutate, }} >