update waf api (#91)

This commit is contained in:
UUBulb
2025-01-04 20:47:14 +08:00
committed by GitHub
parent 231f483876
commit 2d7c41a5b2
2 changed files with 15 additions and 114 deletions

View File

@@ -145,110 +145,6 @@ export function joinIP(p?: ModelIP) {
return "" 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) { export async function copyToClipboard(text: string) {
try { try {
return await navigator.clipboard.writeText(text) return await navigator.clipboard.writeText(text)

View File

@@ -22,8 +22,12 @@ import {
TableRow, TableRow,
} from "@/components/ui/table" } from "@/components/ui/table"
import { useAuth } from "@/hooks/useAuth" import { useAuth } from "@/hooks/useAuth"
import { ip16Str } from "@/lib/utils" import {
import { ModelWAF, ModelWAFApiMock, wafBlockIdentifiers, wafBlockReasons } from "@/types" GithubComNezhahqNezhaModelValueArrayModelWAFApiMock,
ModelWAFApiMock,
wafBlockIdentifiers,
wafBlockReasons,
} from "@/types"
import { ColumnDef, flexRender, getCoreRowModel, useReactTable } from "@tanstack/react-table" import { ColumnDef, flexRender, getCoreRowModel, useReactTable } from "@tanstack/react-table"
import { useEffect, useMemo } from "react" import { useEffect, useMemo } from "react"
import { useTranslation } from "react-i18next" import { useTranslation } from "react-i18next"
@@ -41,7 +45,8 @@ export default function WAFPage() {
// 计算 offset // 计算 offset
const offset = (page - 1) * pageSize const offset = (page - 1) * pageSize
const { data, mutate, error, isLoading } = useSWR<ModelWAFApiMock>( const { data, mutate, error, isLoading } =
useSWR<GithubComNezhahqNezhaModelValueArrayModelWAFApiMock>(
`/api/v1/waf?offset=${offset}&limit=${pageSize}`, `/api/v1/waf?offset=${offset}&limit=${pageSize}`,
swrFetcher, swrFetcher,
) )
@@ -56,7 +61,7 @@ export default function WAFPage() {
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [error]) }, [error])
let columns: ColumnDef<ModelWAF>[] = [ let columns: ColumnDef<ModelWAFApiMock>[] = [
{ {
id: "select", id: "select",
header: ({ table }) => ( header: ({ table }) => (
@@ -82,7 +87,7 @@ export default function WAFPage() {
{ {
header: "IP", header: "IP",
accessorKey: "ip", accessorKey: "ip",
accessorFn: (row) => ip16Str(row.ip ?? ""), accessorFn: (row) => row.ip,
}, },
{ {
header: t("Count"), header: t("Count"),
@@ -122,7 +127,7 @@ export default function WAFPage() {
className="flex gap-2" className="flex gap-2"
delete={{ delete={{
fn: deleteWAF, fn: deleteWAF,
id: ip16Str(s.ip ?? ""), id: s.ip || "",
mutate: mutate, mutate: mutate,
}} }}
> >
@@ -260,7 +265,7 @@ export default function WAFPage() {
className="flex-2 flex gap-2 ml-auto" className="flex-2 flex gap-2 ml-auto"
delete={{ delete={{
fn: deleteWAF, fn: deleteWAF,
id: selectedRows.map((r) => ip16Str(r.original.ip ?? "")), id: selectedRows.map((r) => r.original.ip || ""),
mutate: mutate, mutate: mutate,
}} }}
> >