feat: update ISP check

This commit is contained in:
thehrz 2024-08-20 21:13:46 +08:00
parent 75c1187b54
commit 1adb529d2e
Signed by: thehrz
GPG Key ID: C84CBCE7D5F88855
3 changed files with 39 additions and 17 deletions

View File

@ -31,12 +31,15 @@ export default function CheckItem({
id, id,
name, name,
fetcher, fetcher,
enabled,
content, content,
}: CheckItemProps) { }: CheckItemProps) {
const { data, error, isLoading } = useQuery({ const { data, error, isLoading } = useQuery({
queryKey: [id], queryKey: [id],
queryFn: fetcher, queryFn: fetcher,
}) enabled: !enabled ? undefined : enabled()
},
)
return ( return (
<> <>

View File

@ -1,22 +1,39 @@
"use client" "use client"
import CheckItem from "./CheckItem" import CheckItem from "./CheckItem"
import { CheckItemType } from "@/types/CheckItem" import { CheckItemType } from "@/types/CheckItem"
import { useRef } from "react" import { useRef, useEffect, useState } from "react"
import { QueryClient, QueryClientProvider } from "@tanstack/react-query" import {
QueryClient,
QueryClientProvider,
useQueryClient,
} from "@tanstack/react-query"
const queryClient = new QueryClient()
export default function Checker() { export default function Checker() {
const IPv4 = useRef("") // const IPv4 = useRef(null)
const IPv6 = useRef("") const [IPv6, setIPv6] = useState(null)
useEffect(() => {
if (IPv6) {
queryClient.invalidateQueries(
{
queryKey: ['ISP'],
refetchType: 'all',
},
)
}
}, [IPv6])
const checkList: CheckItemType[] = [ const checkList: CheckItemType[] = [
{ {
id: "IPv6", id: "IPv6",
name: "公网 IPv6 地址", name: "公网 IPv6 地址",
fetcher: () => fetch("https://test.ipv6test.online/ip/myip").then((res) => res.json()), fetcher: () => fetch("/api/ip/myip").then((res) => res.json()),
content: (data) => { content: (data) => {
const success = data?.version == 6 const success = data?.version == 6
if (success && data.address != null) { if (success && data.address != null) {
IPv6.current = data.address setIPv6(data.address)
} }
return { return {
state: success ? "ok" : "error", state: success ? "ok" : "error",
@ -27,13 +44,14 @@ export default function Checker() {
{ {
id: "IPv4", id: "IPv4",
name: "公网 IPv4 地址", name: "公网 IPv4 地址",
fetcher: () => fetch("https://test.ipv6test.online/ip/myip").then((res) => res.json()), fetcher: () =>
fetch("https://test.ipv6test.online/ip/myip").then((res) => res.json()),
content: (data) => { content: (data) => {
const success = data?.version == 4 const success = data?.version == 4
if (success && data.address != null) { // if (success && data.address != null) {
IPv4.current = data.address // IPv4.current = data.address
} // }
return { return {
state: success ? "ok" : "error", state: success ? "ok" : "error",
@ -44,7 +62,7 @@ export default function Checker() {
{ {
id: "ipv4andipv6", id: "ipv4andipv6",
name: "双协议栈测试", name: "双协议栈测试",
fetcher: () => fetch("https://test.ipv6test.online/ip/myip").then((res) => res.json()), fetcher: () => fetch("/api/ip/myip").then((res) => res.json()),
content: (data) => { content: (data) => {
const isIPv6 = data?.version == 6 const isIPv6 = data?.version == 6
const isIPv4 = data?.version == 4 const isIPv4 = data?.version == 4
@ -62,20 +80,19 @@ export default function Checker() {
fetch("https://test.ipv6test.online/ip/isp", { fetch("https://test.ipv6test.online/ip/isp", {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
address: IPv6.current, address: IPv6,
}), }),
}).then((res) => res.json()), }).then((res) => res.json()),
enabled: () => !!IPv6, // 确保在检测到 IPv6 地址时也进行 ISP 检查
content: (data) => { content: (data) => {
return { return {
text: data.isp, state: data.isp ? "ok" : "error", // 确保返回 state 字段
text: data.isp || "未检测到",
} }
}, },
}, },
] ]
const queryClient = new QueryClient();
return ( return (
<QueryClientProvider client={queryClient}> <QueryClientProvider client={queryClient}>
{checkList.map((item, i) => { {checkList.map((item, i) => {
@ -85,6 +102,7 @@ export default function Checker() {
id={item.id} id={item.id}
name={item.name} name={item.name}
fetcher={item.fetcher} fetcher={item.fetcher}
enabled={item.enabled}
content={item.content} content={item.content}
/> />
) )

View File

@ -2,6 +2,7 @@ export interface CheckItemType {
id: string id: string
name: string name: string
fetcher: () => Promise<any> fetcher: () => Promise<any>
enabled?: () => boolean
content: (data: any) => {state?: CheckItemIconState, text: string} content: (data: any) => {state?: CheckItemIconState, text: string}
} }