networkTest.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // 网络测试相关功能
  2. // 创建networkTest对象
  3. const networkTest = {
  4. // 初始化函数
  5. init: function() {
  6. console.log('初始化网络测试模块...');
  7. this.initNetworkTest();
  8. return Promise.resolve();
  9. },
  10. // 初始化网络测试界面
  11. initNetworkTest: function() {
  12. const domainSelect = document.getElementById('domainSelect');
  13. const testType = document.getElementById('testType');
  14. // 填充域名选择器
  15. if (domainSelect) {
  16. domainSelect.innerHTML = `
  17. <option value="">选择预定义域名</option>
  18. <option value="gcr.io">gcr.io</option>
  19. <option value="ghcr.io">ghcr.io</option>
  20. <option value="quay.io">quay.io</option>
  21. <option value="k8s.gcr.io">k8s.gcr.io</option>
  22. <option value="registry.k8s.io">registry.k8s.io</option>
  23. <option value="mcr.microsoft.com">mcr.microsoft.com</option>
  24. <option value="docker.elastic.co">docker.elastic.co</option>
  25. <option value="registry-1.docker.io">registry-1.docker.io</option>
  26. `;
  27. }
  28. // 填充测试类型选择器
  29. if (testType) {
  30. testType.innerHTML = `
  31. <option value="ping">Ping</option>
  32. <option value="traceroute">Traceroute</option>
  33. `;
  34. }
  35. // 绑定测试按钮点击事件
  36. const testButton = document.querySelector('#network-test button');
  37. if (testButton) {
  38. testButton.addEventListener('click', this.runNetworkTest);
  39. }
  40. },
  41. // 运行网络测试
  42. runNetworkTest: function() {
  43. const domain = document.getElementById('domainSelect').value;
  44. const testType = document.getElementById('testType').value;
  45. const resultsDiv = document.getElementById('testResults');
  46. // 验证选择了域名
  47. if (!domain) {
  48. core.showAlert('请选择目标域名', 'error');
  49. return;
  50. }
  51. resultsDiv.innerHTML = '测试中,请稍候...';
  52. const controller = new AbortController();
  53. const timeoutId = setTimeout(() => controller.abort(), 60000); // 60秒超时
  54. fetch('/api/network-test', {
  55. method: 'POST',
  56. headers: {
  57. 'Content-Type': 'application/json',
  58. },
  59. body: JSON.stringify({ domain, type: testType }),
  60. signal: controller.signal
  61. })
  62. .then(response => {
  63. clearTimeout(timeoutId);
  64. if (!response.ok) {
  65. throw new Error('网络测试失败');
  66. }
  67. return response.text();
  68. })
  69. .then(result => {
  70. resultsDiv.textContent = result;
  71. })
  72. .catch(error => {
  73. console.error('网络测试出错:', error);
  74. if (error.name === 'AbortError') {
  75. resultsDiv.textContent = '测试超时,请稍后再试';
  76. } else {
  77. resultsDiv.textContent = '测试失败: ' + error.message;
  78. }
  79. });
  80. }
  81. };
  82. // 全局公开网络测试模块
  83. window.networkTest = networkTest;