feat: enhance latency test logging and error handling

This commit is contained in:
wonfen
2025-03-09 04:22:34 +08:00
parent f18202a3a4
commit e1905aced4
5 changed files with 219 additions and 51 deletions

View File

@@ -161,12 +161,42 @@ export async function cmdGetProxyDelay(
timeout: number,
url?: string,
) {
name = encodeURIComponent(name);
return invoke<{ delay: number }>("clash_api_get_proxy_delay", {
name,
url,
timeout,
});
// 确保URL不为空
const testUrl = url || "http://cp.cloudflare.com/generate_204";
console.log(
`[API] 调用延迟测试API代理: ${name}, 超时: ${timeout}ms, URL: ${testUrl}`,
);
try {
name = encodeURIComponent(name);
const result = await invoke<{ delay: number }>(
"clash_api_get_proxy_delay",
{
name,
url: testUrl, // 传递经过验证的URL
timeout,
},
);
// 验证返回结果中是否有delay字段并且值是一个有效的数字
if (result && typeof result.delay === "number") {
console.log(
`[API] 延迟测试API调用成功代理: ${name}, 延迟: ${result.delay}ms`,
);
return result;
} else {
console.error(
`[API] 延迟测试API返回无效结果代理: ${name}, 结果:`,
result,
);
// 返回一个有效的结果对象,但标记为超时
return { delay: 1e6 };
}
} catch (error) {
console.error(`[API] 延迟测试API调用失败代理: ${name}`, error);
// 返回一个有效的结果对象,但标记为错误
return { delay: 1e6 };
}
}
export async function cmdTestDelay(url: string) {