Skip to content

Commit

Permalink
update waf api (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
uubulb authored Jan 4, 2025
1 parent 231f483 commit 2d7c41a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 114 deletions.
104 changes: 0 additions & 104 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 15 additions & 10 deletions src/routes/waf.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -41,10 +45,11 @@ export default function WAFPage() {
// 计算 offset
const offset = (page - 1) * pageSize

const { data, mutate, error, isLoading } = useSWR<ModelWAFApiMock>(
`/api/v1/waf?offset=${offset}&limit=${pageSize}`,
swrFetcher,
)
const { data, mutate, error, isLoading } =
useSWR<GithubComNezhahqNezhaModelValueArrayModelWAFApiMock>(
`/api/v1/waf?offset=${offset}&limit=${pageSize}`,
swrFetcher,
)

const isAdmin = profile?.role === 0

Expand All @@ -56,7 +61,7 @@ export default function WAFPage() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [error])

let columns: ColumnDef<ModelWAF>[] = [
let columns: ColumnDef<ModelWAFApiMock>[] = [
{
id: "select",
header: ({ table }) => (
Expand All @@ -82,7 +87,7 @@ export default function WAFPage() {
{
header: "IP",
accessorKey: "ip",
accessorFn: (row) => ip16Str(row.ip ?? ""),
accessorFn: (row) => row.ip,
},
{
header: t("Count"),
Expand Down Expand Up @@ -122,7 +127,7 @@ export default function WAFPage() {
className="flex gap-2"
delete={{
fn: deleteWAF,
id: ip16Str(s.ip ?? ""),
id: s.ip || "",
mutate: mutate,
}}
>
Expand Down Expand Up @@ -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,
}}
>
Expand Down

0 comments on commit 2d7c41a

Please sign in to comment.