mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-04-13 05:20:28 +08:00
refactor(ip-info-card): make ip info card much better (#6226)
* perf(ip-info-card): make ip info card much better * fix(ip-info-card): remove unused useEffect deps * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * refactor(ip-info-card): use `async-retry`, bail out non-2XX resp * feat(ip-info-card): add new backend * feat(ip-info-card): only revalidate when window is visible * perf(ip-info-card): reduce re-renders when window is hidden * fix(ip-info-card): remove `mutate` from `useEffect` arg * Update src/components/home/ip-info-card.tsx Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix: drop AbortSignal.timeout for old safati compat --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
parent
b3a1fb8d23
commit
90e193099f
@ -58,6 +58,7 @@
|
|||||||
"axios": "^1.13.3",
|
"axios": "^1.13.3",
|
||||||
"dayjs": "1.11.19",
|
"dayjs": "1.11.19",
|
||||||
"foxact": "^0.2.52",
|
"foxact": "^0.2.52",
|
||||||
|
"foxts": "^5.2.1",
|
||||||
"i18next": "^25.8.0",
|
"i18next": "^25.8.0",
|
||||||
"ipaddr.js": "^2.3.0",
|
"ipaddr.js": "^2.3.0",
|
||||||
"js-yaml": "^4.1.1",
|
"js-yaml": "^4.1.1",
|
||||||
|
|||||||
8
pnpm-lock.yaml
generated
8
pnpm-lock.yaml
generated
@ -80,6 +80,9 @@ importers:
|
|||||||
foxact:
|
foxact:
|
||||||
specifier: ^0.2.52
|
specifier: ^0.2.52
|
||||||
version: 0.2.52(react-dom@19.2.3(react@19.2.3))(react@19.2.3)
|
version: 0.2.52(react-dom@19.2.3(react@19.2.3))(react@19.2.3)
|
||||||
|
foxts:
|
||||||
|
specifier: ^5.2.1
|
||||||
|
version: 5.2.1
|
||||||
i18next:
|
i18next:
|
||||||
specifier: ^25.8.0
|
specifier: ^25.8.0
|
||||||
version: 25.8.0(typescript@5.9.3)
|
version: 25.8.0(typescript@5.9.3)
|
||||||
@ -2649,6 +2652,9 @@ packages:
|
|||||||
react-dom:
|
react-dom:
|
||||||
optional: true
|
optional: true
|
||||||
|
|
||||||
|
foxts@5.2.1:
|
||||||
|
resolution: {integrity: sha512-EsvV1QDTp8leo7RXluZbiIc1IjO9m06G6ePeX8P8VwJmKodSviS++2AKSXUh1GBMliFl57oH8ZV2fCJWEbh2rw==}
|
||||||
|
|
||||||
fsevents@2.3.3:
|
fsevents@2.3.3:
|
||||||
resolution: {integrity: sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==}
|
resolution: {integrity: sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==}
|
||||||
engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0}
|
engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0}
|
||||||
@ -6340,6 +6346,8 @@ snapshots:
|
|||||||
react: 19.2.3
|
react: 19.2.3
|
||||||
react-dom: 19.2.3(react@19.2.3)
|
react-dom: 19.2.3(react@19.2.3)
|
||||||
|
|
||||||
|
foxts@5.2.1: {}
|
||||||
|
|
||||||
fsevents@2.3.3:
|
fsevents@2.3.3:
|
||||||
optional: true
|
optional: true
|
||||||
|
|
||||||
|
|||||||
@ -5,10 +5,12 @@ import {
|
|||||||
VisibilityOutlined,
|
VisibilityOutlined,
|
||||||
} from "@mui/icons-material";
|
} from "@mui/icons-material";
|
||||||
import { Box, Button, IconButton, Skeleton, Typography } from "@mui/material";
|
import { Box, Button, IconButton, Skeleton, Typography } from "@mui/material";
|
||||||
import { memo, useCallback, useEffect, useRef, useState } from "react";
|
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
|
||||||
|
import { useEffect } from "foxact/use-abortable-effect";
|
||||||
|
import { memo, useCallback, useState, useEffectEvent, useMemo } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
import useSWR from "swr";
|
||||||
|
|
||||||
import { useAppData } from "@/providers/app-data-context";
|
|
||||||
import { getIpInfo } from "@/services/api";
|
import { getIpInfo } from "@/services/api";
|
||||||
|
|
||||||
import { EnhancedCard } from "./enhanced-card";
|
import { EnhancedCard } from "./enhanced-card";
|
||||||
@ -17,8 +19,7 @@ import { EnhancedCard } from "./enhanced-card";
|
|||||||
const IP_REFRESH_SECONDS = 300;
|
const IP_REFRESH_SECONDS = 300;
|
||||||
const IP_INFO_CACHE_KEY = "cv_ip_info_cache";
|
const IP_INFO_CACHE_KEY = "cv_ip_info_cache";
|
||||||
|
|
||||||
// 提取InfoItem子组件并使用memo优化
|
const InfoItem = memo(({ label, value }: { label: string; value?: string }) => (
|
||||||
const InfoItem = memo(({ label, value }: { label: string; value: string }) => (
|
|
||||||
<Box sx={{ mb: 0.7, display: "flex", alignItems: "flex-start" }}>
|
<Box sx={{ mb: 0.7, display: "flex", alignItems: "flex-start" }}>
|
||||||
<Typography
|
<Typography
|
||||||
variant="body2"
|
variant="body2"
|
||||||
@ -44,7 +45,7 @@ const InfoItem = memo(({ label, value }: { label: string; value: string }) => (
|
|||||||
));
|
));
|
||||||
|
|
||||||
// 获取国旗表情
|
// 获取国旗表情
|
||||||
const getCountryFlag = (countryCode: string) => {
|
const getCountryFlag = (countryCode: string | undefined) => {
|
||||||
if (!countryCode) return "";
|
if (!countryCode) return "";
|
||||||
const codePoints = countryCode
|
const codePoints = countryCode
|
||||||
.toUpperCase()
|
.toUpperCase()
|
||||||
@ -56,149 +57,71 @@ const getCountryFlag = (countryCode: string) => {
|
|||||||
// IP信息卡片组件
|
// IP信息卡片组件
|
||||||
export const IpInfoCard = () => {
|
export const IpInfoCard = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { clashConfig } = useAppData();
|
|
||||||
const [ipInfo, setIpInfo] = useState<any>(null);
|
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const [error, setError] = useState("");
|
|
||||||
const [showIp, setShowIp] = useState(false);
|
const [showIp, setShowIp] = useState(false);
|
||||||
|
const appWindow = useMemo(() => getCurrentWebviewWindow(), []);
|
||||||
|
|
||||||
const [countdown, setCountdown] = useState(IP_REFRESH_SECONDS);
|
const [countdown, setCountdown] = useState(IP_REFRESH_SECONDS);
|
||||||
const lastFetchRef = useRef<number | null>(null);
|
|
||||||
|
|
||||||
const fetchIpInfo = useCallback(
|
const {
|
||||||
async (force = false) => {
|
data: ipInfo,
|
||||||
setError("");
|
error,
|
||||||
|
isLoading,
|
||||||
|
mutate,
|
||||||
|
} = useSWR(IP_INFO_CACHE_KEY, getIpInfo, {
|
||||||
|
refreshInterval: 0,
|
||||||
|
refreshWhenOffline: false,
|
||||||
|
revalidateOnFocus: true,
|
||||||
|
shouldRetryOnError: true,
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
// function useEffectEvent
|
||||||
if (!force && typeof window !== "undefined" && window.sessionStorage) {
|
const onCountdownTick = useEffectEvent(async () => {
|
||||||
const raw = window.sessionStorage.getItem(IP_INFO_CACHE_KEY);
|
const now = Date.now();
|
||||||
if (raw) {
|
const ts = ipInfo?.lastFetchTs;
|
||||||
const parsed = JSON.parse(raw);
|
if (!ts) {
|
||||||
const now = Date.now();
|
return;
|
||||||
if (
|
}
|
||||||
parsed?.ts &&
|
|
||||||
parsed?.data &&
|
|
||||||
now - parsed.ts < IP_REFRESH_SECONDS * 1000
|
|
||||||
) {
|
|
||||||
setIpInfo(parsed.data);
|
|
||||||
lastFetchRef.current = parsed.ts;
|
|
||||||
const elapsed = Math.floor((now - parsed.ts) / 1000);
|
|
||||||
setCountdown(Math.max(IP_REFRESH_SECONDS - elapsed, 0));
|
|
||||||
setLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.warn("Failed to read IP info from sessionStorage:", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof navigator !== "undefined" && !navigator.onLine) {
|
const elapsed = Math.floor((now - ts) / 1000);
|
||||||
setLoading(false);
|
const remaining = IP_REFRESH_SECONDS - elapsed;
|
||||||
lastFetchRef.current = Date.now();
|
|
||||||
|
if (remaining <= 0) {
|
||||||
|
if (navigator.onLine && (await appWindow.isVisible())) {
|
||||||
|
mutate();
|
||||||
setCountdown(IP_REFRESH_SECONDS);
|
setCountdown(IP_REFRESH_SECONDS);
|
||||||
return;
|
} else {
|
||||||
|
// do nothing. we even skip "setCountdown" to reduce re-renders
|
||||||
|
//
|
||||||
|
// but the remaining time still <= 0, and setInterval is not stopped, this
|
||||||
|
// callback will still be regularly triggered, as soon as the window is visible
|
||||||
|
// or network online again, we mutate() immediately in the following tick.
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
setCountdown(remaining);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (!clashConfig) {
|
// Countdown / refresh scheduler — updates UI every 1s and triggers immediate revalidation when expired
|
||||||
setLoading(false);
|
|
||||||
lastFetchRef.current = Date.now();
|
|
||||||
setCountdown(IP_REFRESH_SECONDS);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
setLoading(true);
|
|
||||||
const data = await getIpInfo();
|
|
||||||
setIpInfo(data);
|
|
||||||
const ts = Date.now();
|
|
||||||
lastFetchRef.current = ts;
|
|
||||||
try {
|
|
||||||
if (typeof window !== "undefined" && window.sessionStorage) {
|
|
||||||
window.sessionStorage.setItem(
|
|
||||||
IP_INFO_CACHE_KEY,
|
|
||||||
JSON.stringify({ data, ts }),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.warn("Failed to write IP info to sessionStorage:", e);
|
|
||||||
}
|
|
||||||
setCountdown(IP_REFRESH_SECONDS);
|
|
||||||
} catch (err) {
|
|
||||||
setError(
|
|
||||||
err instanceof Error
|
|
||||||
? err.message
|
|
||||||
: t("home.components.ipInfo.errors.load"),
|
|
||||||
);
|
|
||||||
lastFetchRef.current = Date.now();
|
|
||||||
setCountdown(IP_REFRESH_SECONDS);
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[t, clashConfig],
|
|
||||||
);
|
|
||||||
|
|
||||||
// 组件加载时获取IP信息并启动基于上次请求时间的倒计时
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchIpInfo();
|
const timer: number | null = window.setInterval(onCountdownTick, 1000);
|
||||||
|
|
||||||
let timer: number | null = null;
|
|
||||||
|
|
||||||
const startCountdown = () => {
|
|
||||||
timer = window.setInterval(() => {
|
|
||||||
const now = Date.now();
|
|
||||||
let ts = lastFetchRef.current;
|
|
||||||
try {
|
|
||||||
if (!ts && typeof window !== "undefined" && window.sessionStorage) {
|
|
||||||
const raw = window.sessionStorage.getItem(IP_INFO_CACHE_KEY);
|
|
||||||
if (raw) {
|
|
||||||
const parsed = JSON.parse(raw);
|
|
||||||
ts = parsed?.ts || null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.warn("Failed to read IP info from sessionStorage:", e);
|
|
||||||
ts = ts || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const elapsed = ts ? Math.floor((now - ts) / 1000) : 0;
|
|
||||||
let remaining = IP_REFRESH_SECONDS - elapsed;
|
|
||||||
|
|
||||||
if (remaining <= 0) {
|
|
||||||
fetchIpInfo();
|
|
||||||
remaining = IP_REFRESH_SECONDS;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 每5秒或倒计时结束时才更新UI
|
|
||||||
if (remaining % 5 === 0 || remaining <= 0) {
|
|
||||||
setCountdown(remaining);
|
|
||||||
}
|
|
||||||
}, 1000);
|
|
||||||
};
|
|
||||||
|
|
||||||
startCountdown();
|
|
||||||
return () => {
|
return () => {
|
||||||
if (timer) clearInterval(timer);
|
if (timer != null) clearInterval(timer);
|
||||||
};
|
};
|
||||||
}, [fetchIpInfo]);
|
}, []);
|
||||||
|
|
||||||
const toggleShowIp = useCallback(() => {
|
const toggleShowIp = useCallback(() => {
|
||||||
setShowIp((prev) => !prev);
|
setShowIp((prev) => !prev);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// 渲染加载状态
|
// Loading
|
||||||
if (loading) {
|
if (isLoading) {
|
||||||
return (
|
return (
|
||||||
<EnhancedCard
|
<EnhancedCard
|
||||||
title={t("home.components.ipInfo.title")}
|
title={t("home.components.ipInfo.title")}
|
||||||
icon={<LocationOnOutlined />}
|
icon={<LocationOnOutlined />}
|
||||||
iconColor="info"
|
iconColor="info"
|
||||||
action={
|
action={
|
||||||
<IconButton
|
<IconButton size="small" onClick={() => mutate()} disabled>
|
||||||
size="small"
|
|
||||||
onClick={() => fetchIpInfo(true)}
|
|
||||||
disabled={true}
|
|
||||||
>
|
|
||||||
<RefreshOutlined />
|
<RefreshOutlined />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
}
|
}
|
||||||
@ -213,7 +136,7 @@ export const IpInfoCard = () => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 渲染错误状态
|
// Error
|
||||||
if (error) {
|
if (error) {
|
||||||
return (
|
return (
|
||||||
<EnhancedCard
|
<EnhancedCard
|
||||||
@ -221,7 +144,7 @@ export const IpInfoCard = () => {
|
|||||||
icon={<LocationOnOutlined />}
|
icon={<LocationOnOutlined />}
|
||||||
iconColor="info"
|
iconColor="info"
|
||||||
action={
|
action={
|
||||||
<IconButton size="small" onClick={() => fetchIpInfo(true)}>
|
<IconButton size="small" onClick={() => mutate()}>
|
||||||
<RefreshOutlined />
|
<RefreshOutlined />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
}
|
}
|
||||||
@ -237,9 +160,11 @@ export const IpInfoCard = () => {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Typography variant="body1" color="error">
|
<Typography variant="body1" color="error">
|
||||||
{error}
|
{error instanceof Error
|
||||||
|
? error.message
|
||||||
|
: t("home.components.ipInfo.errors.load")}
|
||||||
</Typography>
|
</Typography>
|
||||||
<Button onClick={() => fetchIpInfo(true)} sx={{ mt: 2 }}>
|
<Button onClick={() => mutate()} sx={{ mt: 2 }}>
|
||||||
{t("shared.actions.retry")}
|
{t("shared.actions.retry")}
|
||||||
</Button>
|
</Button>
|
||||||
</Box>
|
</Box>
|
||||||
@ -247,14 +172,14 @@ export const IpInfoCard = () => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 渲染正常数据
|
// Normal render
|
||||||
return (
|
return (
|
||||||
<EnhancedCard
|
<EnhancedCard
|
||||||
title={t("home.components.ipInfo.title")}
|
title={t("home.components.ipInfo.title")}
|
||||||
icon={<LocationOnOutlined />}
|
icon={<LocationOnOutlined />}
|
||||||
iconColor="info"
|
iconColor="info"
|
||||||
action={
|
action={
|
||||||
<IconButton size="small" onClick={() => fetchIpInfo(true)}>
|
<IconButton size="small" onClick={() => mutate()}>
|
||||||
<RefreshOutlined />
|
<RefreshOutlined />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
}
|
}
|
||||||
@ -355,7 +280,7 @@ export const IpInfoCard = () => {
|
|||||||
<Box sx={{ width: "60%", overflow: "auto" }}>
|
<Box sx={{ width: "60%", overflow: "auto" }}>
|
||||||
<InfoItem
|
<InfoItem
|
||||||
label={t("home.components.ipInfo.labels.isp")}
|
label={t("home.components.ipInfo.labels.isp")}
|
||||||
value={ipInfo?.isp}
|
value={ipInfo?.organization}
|
||||||
/>
|
/>
|
||||||
<InfoItem
|
<InfoItem
|
||||||
label={t("home.components.ipInfo.labels.org")}
|
label={t("home.components.ipInfo.labels.org")}
|
||||||
@ -396,8 +321,7 @@ export const IpInfoCard = () => {
|
|||||||
whiteSpace: "nowrap",
|
whiteSpace: "nowrap",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{ipInfo?.country_code}, {ipInfo?.longitude?.toFixed(2)},{" "}
|
{`${ipInfo?.country_code ?? "N/A"}, ${ipInfo?.longitude?.toFixed(2) ?? "N/A"}, ${ipInfo?.latitude?.toFixed(2) ?? "N/A"}`}
|
||||||
{ipInfo?.latitude?.toFixed(2)}
|
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
import { fetch } from "@tauri-apps/plugin-http";
|
import { fetch } from "@tauri-apps/plugin-http";
|
||||||
|
import { asyncRetry } from "foxts/async-retry";
|
||||||
|
import { extractErrorMessage } from "foxts/extract-error-message";
|
||||||
|
|
||||||
import { debugLog } from "@/utils/debug";
|
import { debugLog } from "@/utils/debug";
|
||||||
|
|
||||||
@ -90,6 +92,38 @@ const IP_CHECK_SERVICES: ServiceConfig[] = [
|
|||||||
timezone: data.timezone?.id || "",
|
timezone: data.timezone?.id || "",
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
url: "https://ip.api.skk.moe/cf-geoip",
|
||||||
|
mapping: (data) => ({
|
||||||
|
ip: data.ip || "",
|
||||||
|
country_code: data.country || "",
|
||||||
|
country: data.country || "",
|
||||||
|
region: data.region || "",
|
||||||
|
city: data.city || "",
|
||||||
|
organization: data.asOrg || "",
|
||||||
|
asn: data.asn || 0,
|
||||||
|
asn_organization: data.asOrg || "",
|
||||||
|
longitude: data.longitude || 0,
|
||||||
|
latitude: data.latitude || 0,
|
||||||
|
timezone: data.timezone || "",
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: "https://get.geojs.io/v1/ip/geo.json",
|
||||||
|
mapping: (data) => ({
|
||||||
|
ip: data.ip || "",
|
||||||
|
country_code: data.country_code || "",
|
||||||
|
country: data.country || "",
|
||||||
|
region: data.region || "",
|
||||||
|
city: data.city || "",
|
||||||
|
organization: data.organization_name || "",
|
||||||
|
asn: data.asn || 0,
|
||||||
|
asn_organization: data.organization_name || "",
|
||||||
|
longitude: Number(data.longitude) || 0,
|
||||||
|
latitude: Number(data.latitude) || 0,
|
||||||
|
timezone: data.timezone || "",
|
||||||
|
}),
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
// 随机性服务列表洗牌函数
|
// 随机性服务列表洗牌函数
|
||||||
@ -149,76 +183,74 @@ function createPrng(seed: number): () => number {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取当前IP和地理位置信息
|
// 获取当前IP和地理位置信息
|
||||||
export const getIpInfo = async (): Promise<IpInfo> => {
|
export const getIpInfo = async (): Promise<
|
||||||
|
IpInfo & { lastFetchTs: number }
|
||||||
|
> => {
|
||||||
|
const lastFetchTs = Date.now();
|
||||||
|
|
||||||
// 配置参数
|
// 配置参数
|
||||||
const maxRetries = 3;
|
const maxRetries = 3;
|
||||||
const serviceTimeout = 5000;
|
const serviceTimeout = 5000;
|
||||||
const overallTimeout = 20000; // 增加总超时时间以容纳延迟
|
|
||||||
|
|
||||||
const overallTimeoutController = new AbortController();
|
const shuffledServices = shuffleServices();
|
||||||
const overallTimeoutId = setTimeout(() => {
|
let lastError: unknown | null = null;
|
||||||
overallTimeoutController.abort();
|
|
||||||
}, overallTimeout);
|
|
||||||
|
|
||||||
try {
|
for (const service of shuffledServices) {
|
||||||
const shuffledServices = shuffleServices();
|
debugLog(`尝试IP检测服务: ${service.url}`);
|
||||||
let lastError: Error | null = null;
|
|
||||||
|
|
||||||
for (const service of shuffledServices) {
|
const timeoutController = new AbortController();
|
||||||
debugLog(`尝试IP检测服务: ${service.url}`);
|
const timeoutId = setTimeout(() => {
|
||||||
|
timeoutController.abort();
|
||||||
|
}, service.timeout || serviceTimeout);
|
||||||
|
|
||||||
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
try {
|
||||||
let timeoutId: ReturnType<typeof setTimeout> | null = null;
|
return await asyncRetry(
|
||||||
|
async (bail) => {
|
||||||
try {
|
|
||||||
const timeoutController = new AbortController();
|
|
||||||
timeoutId = setTimeout(() => {
|
|
||||||
timeoutController.abort();
|
|
||||||
}, service.timeout || serviceTimeout);
|
|
||||||
console.debug("Fetching IP information...");
|
console.debug("Fetching IP information...");
|
||||||
|
|
||||||
const response = await fetch(service.url, {
|
const response = await fetch(service.url, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
signal: timeoutController.signal,
|
signal: timeoutController.signal, // AbortSignal.timeout(service.timeout || serviceTimeout),
|
||||||
connectTimeout: service.timeout || serviceTimeout,
|
connectTimeout: service.timeout || serviceTimeout,
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await response.json();
|
if (!response.ok) {
|
||||||
|
return bail(
|
||||||
|
new Error(
|
||||||
|
`IP 检测服务出错,状态码: ${response.status} from ${service.url}`,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (timeoutId) clearTimeout(timeoutId);
|
const data = await response.json();
|
||||||
|
|
||||||
if (data && data.ip) {
|
if (data && data.ip) {
|
||||||
debugLog(`IP检测成功,使用服务: ${service.url}`);
|
debugLog(`IP检测成功,使用服务: ${service.url}`);
|
||||||
return service.mapping(data);
|
return Object.assign(service.mapping(data), { lastFetchTs });
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`无效的响应格式 from ${service.url}`);
|
throw new Error(`无效的响应格式 from ${service.url}`);
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
},
|
||||||
if (timeoutId) clearTimeout(timeoutId);
|
{
|
||||||
|
retries: maxRetries,
|
||||||
lastError = error;
|
minTimeout: 500,
|
||||||
console.warn(
|
maxTimeout: 2000,
|
||||||
`尝试 ${attempt + 1}/${maxRetries} 失败 (${service.url}):`,
|
randomize: true,
|
||||||
error,
|
},
|
||||||
);
|
);
|
||||||
|
} catch (error) {
|
||||||
if (error.name === "AbortError") {
|
debugLog(`IP检测服务失败: ${service.url}`, error);
|
||||||
throw error;
|
lastError = error;
|
||||||
}
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
if (attempt < maxRetries - 1) {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (lastError) {
|
if (lastError) {
|
||||||
throw new Error(`所有IP检测服务都失败: ${lastError.message}`);
|
throw new Error(
|
||||||
} else {
|
`所有IP检测服务都失败: ${extractErrorMessage(lastError) || "未知错误"}`,
|
||||||
throw new Error("没有可用的IP检测服务");
|
);
|
||||||
}
|
} else {
|
||||||
} finally {
|
throw new Error("没有可用的IP检测服务");
|
||||||
clearTimeout(overallTimeoutId);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user