refactored IP detection and added zashboard redirect URL (#3510)

* Fixed the language display size issue and updated the translation synchronously!

* Fixed Russian language display issue

* refactored IP detection and added zashboard redirect URL

---------

Co-authored-by: Ahao <108321411+xuanyuan0408@users.noreply.github.com>
This commit is contained in:
Ahao
2025-05-14 20:32:45 +08:00
committed by GitHub
parent c100b9c54d
commit 4f86f3c0ec
3 changed files with 98 additions and 35 deletions

View File

@@ -44,11 +44,11 @@
- 支持手动卸载服务模式,回退到 Sidecar 模式 - 支持手动卸载服务模式,回退到 Sidecar 模式
- 添加了土耳其语,日本语,德语,西班牙语,繁体中文的支持 - 添加了土耳其语,日本语,德语,西班牙语,繁体中文的支持
- 卸载服务的按钮 - 卸载服务的按钮
- 添加了Zashboard的一键跳转URL
#### 优化了: #### 优化了:
- 系统代理 Bypass 设置 - 系统代理 Bypass 设置
- Windows 下使用 Startup 文件夹的方式实现开机自启,解决管理员模式下开机自启的各种问题 - Windows 下使用 Startup 文件夹的方式实现开机自启,解决管理员模式下开机自启的各种问题
- 增加 IP 信息请求重试机制,减少 Network Error 发生的情况
- 切换到规则页面时自动刷新规则数据 - 切换到规则页面时自动刷新规则数据
- 重构更新失败回退机制,使用后端完成更新失败后回退到使用 Clash 代理再次尝试更新 - 重构更新失败回退机制,使用后端完成更新失败后回退到使用 Clash 代理再次尝试更新
- 编辑非激活订阅的时候不在触发当前订阅配置重载 - 编辑非激活订阅的时候不在触发当前订阅配置重载
@@ -63,6 +63,7 @@
- 优化了其他语言的翻译问题 - 优化了其他语言的翻译问题
- Mihomo 内核默认日志等级为 warn - Mihomo 内核默认日志等级为 warn
- Clash Verge Rev 应用默认日志等级为 warn - Clash Verge Rev 应用默认日志等级为 warn
- 重构了原来的IP 信息请求重试机制,采用轮询检测,解决了 Network Error 和 超时问题
## v2.2.3 ## v2.2.3

View File

@@ -26,6 +26,7 @@ export const WebUIViewer = forwardRef<DialogRef>((props, ref) => {
const webUIList = verge?.web_ui_list || [ const webUIList = verge?.web_ui_list || [
"https://metacubex.github.io/metacubexd/#/setup?http=true&hostname=%host&port=%port&secret=%secret", "https://metacubex.github.io/metacubexd/#/setup?http=true&hostname=%host&port=%port&secret=%secret",
"https://yacd.metacubex.one/?hostname=%host&port=%port&secret=%secret", "https://yacd.metacubex.one/?hostname=%host&port=%port&secret=%secret",
"https://board.zash.run.place/#/setup?http=true&hostname=%host&port=%port&secret=%secret",
]; ];
const handleAdd = useLockFn(async (value: string) => { const handleAdd = useLockFn(async (value: string) => {

View File

@@ -319,42 +319,103 @@ export const gc = async () => {
} }
}; };
// Get current IP and geolocation information // Get current IP and geolocation information refactored IP detection
export const getIpInfo = async () => { interface IpInfo {
// 添加重试机制 ip: string;
const maxRetries = 3; country_code: string;
const retryDelay = 1500; country: string;
const timeout = 5000; region: string;
city: string;
organization: string;
asn: number;
asn_organization: string;
longitude: number;
latitude: number;
timezone: string;
}
let lastError; // 可用的IP检测服务列表
const IP_CHECK_SERVICES = [
for (let attempt = 0; attempt < maxRetries; attempt++) { "https://api.ip.sb/geoip",
try { "https://ipapi.co/json",
// 使用axios直接请求IP.sb的API不通过clash代理 "http://ip-api.com/json",
const response = await axios.get("https://api.ip.sb/geoip", { timeout }); "https://ipinfo.io/json",
return response.data as { "https://ifconfig.co/json",
ip: string; ];
country_code: string;
country: string; // 随机打乱服务列表顺序
region: string; function shuffleServices() {
city: string; return [...IP_CHECK_SERVICES].sort(() => Math.random() - 0.5);
organization: string; }
asn: number;
asn_organization: string; // 获取当前IP和地理位置信息优化版本
longitude: number; export const getIpInfo = async (): Promise<IpInfo> => {
latitude: number; // 配置参数
timezone: string; const maxRetries = 3;
}; const serviceTimeout = 5000;
} catch (error) { const overallTimeout = 15000;
console.log(`获取IP信息失败尝试 ${attempt + 1}/${maxRetries}`, error);
lastError = error; const overallTimeoutController = new AbortController();
const overallTimeoutId = setTimeout(() => {
overallTimeoutController.abort();
}, overallTimeout);
try {
const shuffledServices = shuffleServices();
let lastError: Error | null = null;
for (const serviceUrl of shuffledServices) {
console.log(`尝试IP检测服务: ${serviceUrl}`);
// 如果不是最后一次尝试,则等待后重试 for (let attempt = 0; attempt < maxRetries; attempt++) {
if (attempt < maxRetries - 1) { let timeoutId: ReturnType<typeof setTimeout> | null = null;
await new Promise(resolve => setTimeout(resolve, retryDelay));
try {
const timeoutController = new AbortController();
timeoutId = setTimeout(() => {
timeoutController.abort();
}, serviceTimeout);
const response = await axios.get<IpInfo>(serviceUrl, {
signal: timeoutController.signal,
timeout: serviceTimeout,
headers: { "User-Agent": "Mozilla/5.0" },
});
if (timeoutId) clearTimeout(timeoutId);
if (response.data && response.data.ip) {
console.log(`IP检测成功使用服务: ${serviceUrl}`);
return response.data;
} else {
throw new Error(`无效的响应格式 from ${serviceUrl}`);
}
} catch (error: any) {
if (timeoutId) clearTimeout(timeoutId);
lastError = error;
console.log(
`尝试 ${attempt + 1}/${maxRetries} 失败 (${serviceUrl}):`,
error.message
);
if (error.name === "AbortError") {
throw error;
}
if (attempt < maxRetries - 1) {
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
} }
} }
}
throw lastError; if (lastError) {
}; throw new Error(`所有IP检测服务都失败: ${lastError.message}`);
} else {
throw new Error('没有可用的IP检测服务');
}
} finally {
clearTimeout(overallTimeoutId);
}
};