From 3dc6da8ea91fc49ed60f07ac1a200b2b1e225496 Mon Sep 17 00:00:00 2001 From: naiba Date: Sat, 30 Nov 2024 21:31:55 +0800 Subject: [PATCH] fix: waf page --- src/api/waf.ts | 6 +++--- src/lib/utils.ts | 14 ++++++++++++-- src/locales/en/translation.json | 1 + src/locales/it/translation.json | 1 + src/locales/zh-CN/translation.json | 1 + src/locales/zh-TW/translation.json | 1 + src/routes/settings.tsx | 28 +++++++++++++++++++++------- src/routes/waf.tsx | 14 +++++++------- src/types/api.ts | 9 +++++---- 9 files changed, 52 insertions(+), 23 deletions(-) diff --git a/src/api/waf.ts b/src/api/waf.ts index 9e34f73..d71b23b 100644 --- a/src/api/waf.ts +++ b/src/api/waf.ts @@ -1,10 +1,10 @@ -import { ModelWAF } from "@/types" +import { ModelWAFApiMock } from "@/types" import { fetcher, FetcherMethod } from "./api" export const deleteWAF = async (ip: string[]): Promise => { return fetcher(FetcherMethod.POST, '/api/v1/batch-delete/waf', ip); } -export const getWAFList = async (): Promise => { - return fetcher(FetcherMethod.GET, '/api/v1/waf', null); +export const getWAFList = async (): Promise => { + return fetcher(FetcherMethod.GET, '/api/v1/waf', null); } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 20537ee..aec650e 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -141,8 +141,18 @@ export function joinIP(p?: ModelIP) { return ''; } -export function ip16Str(p: number[]) { - const buf = new Uint8Array(p); +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('.'); diff --git a/src/locales/en/translation.json b/src/locales/en/translation.json index 12a3ef6..13e2687 100644 --- a/src/locales/en/translation.json +++ b/src/locales/en/translation.json @@ -134,6 +134,7 @@ "CustomCodesDashboard": "Custom Codes for Dashboard", "CustomPublicDNSNameserversforDDNS": "Custom Public DNS Nameservers for DDNS", "RealIPHeader": "Real IP request header", + "UseDirectConnectingIP": "Use direct connection IP", "IPChangeNotification": "IP Change notification", "FullIPNotification": "Show Full IP Address in Notification Messages", "EditService": "Editing services", diff --git a/src/locales/it/translation.json b/src/locales/it/translation.json index 9734b23..d7349fa 100644 --- a/src/locales/it/translation.json +++ b/src/locales/it/translation.json @@ -141,6 +141,7 @@ "DashboardOriginalHost": "Nome di dominio/IP del server Dashboard (no CDN)", "CustomPublicDNSNameserversforDDNS": "Server dei nomi DNS pubblici personalizzati per DDNS", "RealIPHeader": "Intestazione della richiesta IP reale", + "UseDirectConnectingIP": "Utilizzare l'IP di connessione diretta", "IPChangeNotification": "Notifica di modifica IP", "FullIPNotification": "Mostra l'indirizzo IP completo nei messaggi di notifica", "LoginFailed": "Accesso non riuscito", diff --git a/src/locales/zh-CN/translation.json b/src/locales/zh-CN/translation.json index 966ba39..45582af 100644 --- a/src/locales/zh-CN/translation.json +++ b/src/locales/zh-CN/translation.json @@ -140,6 +140,7 @@ "DashboardOriginalHost": "仪表板服务器域名/IP(无 CDN)", "CustomPublicDNSNameserversforDDNS": "DDNS 的自定义公共 DNS 名称服务器", "RealIPHeader": "真实IP请求头", + "UseDirectConnectingIP": "使用直连 IP", "IPChangeNotification": "IP变更通知", "FullIPNotification": "在通知消息中显示完整的 IP 地址", "LoginFailed": "登录失败", diff --git a/src/locales/zh-TW/translation.json b/src/locales/zh-TW/translation.json index 99d7a7c..eef24fb 100644 --- a/src/locales/zh-TW/translation.json +++ b/src/locales/zh-TW/translation.json @@ -140,6 +140,7 @@ "DashboardOriginalHost": "儀表板伺服器網域/IP(無 CDN)", "CustomPublicDNSNameserversforDDNS": "DDNS 的自訂公共 DNS 名稱伺服器", "RealIPHeader": "真實IP請求頭", + "UseDirectConnectingIP": "使用直連 IP", "IPChangeNotification": "IP變更通知", "FullIPNotification": "在通知訊息中顯示完整的 IP 位址", "LoginFailed": "登入失敗", diff --git a/src/routes/settings.tsx b/src/routes/settings.tsx index 2e2540a..20371c4 100644 --- a/src/routes/settings.tsx +++ b/src/routes/settings.tsx @@ -48,7 +48,7 @@ const settingFormSchema = z.object({ }); export default function SettingsPage() { - const { t , i18n} = useTranslation(); + const { t, i18n } = useTranslation(); const [config, setConfig] = useState(); const [error, setError] = useState(); @@ -57,7 +57,7 @@ export default function SettingsPage() { toast(t("Error"), { description: t("Results.ErrorFetchingResource", { error: error.message }), }); - // eslint-disable-next-line react-hooks/exhaustive-deps + // eslint-disable-next-line react-hooks/exhaustive-deps }, [error]); useEffect(() => { @@ -106,9 +106,9 @@ export default function SettingsPage() { return; } finally { toast(t("Success")); - if (values.language!= "auto"){ + if (values.language != "auto") { i18n.changeLanguage(values.language) - }else{ + } else { i18n.changeLanguage(i18n.services.languageDetector.detect()); } } @@ -204,7 +204,7 @@ export default function SettingsPage() { render={({ field }) => ( - {t("CustomPublicDNSNameserversforDDNS")+" " + t("SeparateWithComma")} + {t("CustomPublicDNSNameserversforDDNS") + " " + t("SeparateWithComma")} @@ -220,7 +220,21 @@ export default function SettingsPage() { {t("RealIPHeader")} - +
+ + { + if (checked) { + field.disabled = true; + form.setValue("real_ip_header", "NZ::Use-Peer-IP"); + } else { + field.disabled = false; + form.setValue("real_ip_header", ""); + } + }} /> + + {t("UseDirectConnectingIP")} + +
@@ -260,7 +274,7 @@ export default function SettingsPage() { name="ignored_ip_notification" render={({ field }) => ( - {t("SpecificServers")+" " + t("SeparateWithComma")} + {t("SpecificServers") + " " + t("SeparateWithComma")} diff --git a/src/routes/waf.tsx b/src/routes/waf.tsx index cdc64fd..f7d8bb0 100644 --- a/src/routes/waf.tsx +++ b/src/routes/waf.tsx @@ -14,7 +14,7 @@ import { useEffect, useMemo } from "react"; import { ActionButtonGroup } from "@/components/action-button-group"; import { HeaderButtonGroup } from "@/components/header-button-group"; import { toast } from "sonner"; -import { ModelWAF, wafBlockReasons } from "@/types"; +import { ModelWAFApiMock, wafBlockReasons } from "@/types"; import { deleteWAF } from "@/api/waf"; import { ip16Str } from "@/lib/utils"; import { SettingsTab } from "@/components/settings-tab"; @@ -23,7 +23,7 @@ import { useTranslation } from "react-i18next"; export default function WAFPage() { const { t } = useTranslation(); - const { data, mutate, error, isLoading } = useSWR("/api/v1/waf", swrFetcher); + const { data, mutate, error, isLoading } = useSWR("/api/v1/waf", swrFetcher); useEffect(() => { if (error) @@ -33,7 +33,7 @@ export default function WAFPage() { // eslint-disable-next-line react-hooks/exhaustive-deps }, [error]); - const columns: ColumnDef[] = [ + const columns: ColumnDef[] = [ { id: "select", header: ({ table }) => ( @@ -59,7 +59,7 @@ export default function WAFPage() { { header: "IP", accessorKey: "ip", - accessorFn: (row) => ip16Str(row.ip ?? []), + accessorFn: (row) => ip16Str(row.ip ?? ""), }, { header: t("Count"), @@ -78,7 +78,7 @@ export default function WAFPage() { accessorFn: (row) => row.last_block_timestamp, cell: ({ row }) => { const s = row.original; - const date = new Date(s.last_block_timestamp || 0); + const date = new Date((s.last_block_timestamp || 0)*1000); return {date.toISOString()}; }, }, @@ -92,7 +92,7 @@ export default function WAFPage() { className="flex gap-2" delete={{ fn: deleteWAF, - id: ip16Str(s.ip ?? []), + id: ip16Str(s.ip ?? ""), mutate: mutate, }} > @@ -123,7 +123,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) => ip16Str(r.original.ip ?? "")), mutate: mutate, }} > diff --git a/src/types/api.ts b/src/types/api.ts index c65e9e6..fba2d68 100644 --- a/src/types/api.ts +++ b/src/types/api.ts @@ -75,8 +75,8 @@ export interface GithubComNezhahqNezhaModelCommonResponseArrayModelUser { success?: boolean; } -export interface GithubComNezhahqNezhaModelCommonResponseArrayModelWAF { - data?: ModelWAF[]; +export interface GithubComNezhahqNezhaModelCommonResponseArrayModelWAFApiMock { + data?: ModelWAFApiMock[]; error?: string; success?: boolean; } @@ -425,6 +425,7 @@ export interface ModelProfile { export interface ModelProfileForm { new_password?: string; + new_username?: string; original_password?: string; } @@ -647,9 +648,9 @@ export interface ModelUserForm { username?: string; } -export interface ModelWAF { +export interface ModelWAFApiMock { count?: number; - ip?: number[]; + ip?: string; last_block_reason?: number; last_block_timestamp?: number; }