MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / runSpeedtest

Function runSpeedtest

speedtest/speedtest.ts:637–790  ·  view source on GitHub ↗
(serverId?: number, retryCount: number = 0, useSystem: boolean = false)

Source from the content-addressed store, hash-verified

635 if (useSystem) {
636 return await runSystemSpeedtest(serverId, retryCount);
637 }
638
639 // 检查并诊断内置可执行文件
640 if (!fs.existsSync(SPEEDTEST_PATH)) {
641 console.log("Speedtest executable not found, downloading...");
642 await downloadCli();
643 }
644
645 // 只在第一次尝试时进行诊断,避免重复诊断
646 if (retryCount === 0) {
647 const diagnosis = await diagnoseSpeedtestExecutable();
648 if (!diagnosis.canRun) {
649 console.log(`Speedtest executable issue detected: ${diagnosis.error}`);
650 if (diagnosis.needsReinstall) {
651 console.log("Attempting auto-fix...");
652 await autoFixSpeedtest();
653 }
654 }
655 }
656
657 const serverArg = serverId ? ` -s ${serverId}` : "";
658 const command = `"${SPEEDTEST_PATH}" --accept-license --accept-gdpr -f json${serverArg}`;
659
660 const { stdout, stderr } = await execAsync(command, {
661 timeout: 120000 // 120秒超时
662 });
663
664 if (stderr) {
665 console.log("Speedtest stderr:", stderr);
666 if (stderr.includes("NoServersException")) {
667 // 如果指定服务器不可用,尝试自动选择
668 if (serverId) {
669 console.log(`Server ${serverId} not available, trying auto selection...`);
670 return await runSpeedtest(undefined, retryCount, useSystem); // 递归调用,不指定服务器ID,保持重试计数
671 }
672 throw new Error("指定的服务器不可用,请尝试其他服务器或使用自动选择");
673 }
674 if (stderr.includes("Timeout occurred")) {
675 throw new Error("网络连接超时,请检查网络状况或稍后重试");
676 }
677 if (stderr.includes("Cannot read from socket")) {
678 throw new Error("网络连接中断,可能是网络不稳定或防火墙阻止");
679 }
680 }
681
682 // 尝试解析JSON结果,处理可能的部分失败情况
683 let result: any;
684 try {
685 result = JSON.parse(stdout);
686
687 // 检查JSON中是否包含错误信息
688 if (result.error) {
689 if (result.error.includes("Cannot read")) {
690 throw new Error(`网络连接错误: ${result.error}\n\n这是网络环境问题,不是程序问题。建议:\n1. 检查网络连接稳定性\n2. 尝试其他测试服务器\n3. 稍后重试`);
691 }
692 throw new Error(`测试失败: ${result.error}`);
693 }
694 } catch (parseError) {

Callers 2

runSystemSpeedtestFunction · 0.85
speedtestFunction · 0.85

Calls 5

runSystemSpeedtestFunction · 0.85
autoFixSpeedtestFunction · 0.85
errorMethod · 0.80
downloadCliFunction · 0.70

Tested by

no test coverage detected