test-simple.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const axios = require('axios');
  2. console.log('🔍 简单连通性测试\n');
  3. // 测试直连
  4. async function testDirect() {
  5. console.log('1. 测试直连...');
  6. const urls = [
  7. 'https://httpbin.org/get',
  8. 'https://www.google.com',
  9. 'https://www.github.com'
  10. ];
  11. for (const url of urls) {
  12. try {
  13. console.log(` 尝试: ${url}`);
  14. const start = Date.now();
  15. const response = await axios.get(url, {
  16. timeout: 10000,
  17. headers: {
  18. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
  19. }
  20. });
  21. const latency = Date.now() - start;
  22. console.log(` ✅ 成功: ${latency}ms (状态码: ${response.status})`);
  23. return true;
  24. } catch (error) {
  25. console.log(` ❌ 失败: ${error.message}`);
  26. }
  27. }
  28. console.log(' ❌ 所有直连测试都失败了');
  29. return false;
  30. }
  31. // 测试本地代理端口
  32. async function testLocalProxy() {
  33. console.log('\n2. 测试本地代理端口...');
  34. const net = require('net');
  35. const ports = [8080, 1080, 7890, 7891, 8118, 3128];
  36. for (const port of ports) {
  37. try {
  38. const socket = new net.Socket();
  39. const result = await new Promise((resolve) => {
  40. socket.setTimeout(3000);
  41. socket.on('connect', () => {
  42. socket.destroy();
  43. resolve(true);
  44. });
  45. socket.on('timeout', () => {
  46. socket.destroy();
  47. resolve(false);
  48. });
  49. socket.on('error', () => {
  50. socket.destroy();
  51. resolve(false);
  52. });
  53. socket.connect(port, '127.0.0.1');
  54. });
  55. if (result) {
  56. console.log(` ✅ 端口 ${port} 可连接`);
  57. } else {
  58. console.log(` ❌ 端口 ${port} 不可连接`);
  59. }
  60. } catch (error) {
  61. console.log(` ❌ 端口 ${port} 测试失败: ${error.message}`);
  62. }
  63. }
  64. }
  65. // 测试HTTP代理
  66. async function testHttpProxy() {
  67. console.log('\n3. 测试HTTP代理...');
  68. try {
  69. const { HttpsProxyAgent } = require('https-proxy-agent');
  70. const proxyUrl = 'http://127.0.0.1:7897';
  71. const agent = new HttpsProxyAgent(proxyUrl);
  72. const response = await axios.get('https://httpbin.org/get', {
  73. httpsAgent: agent,
  74. timeout: 10000,
  75. headers: {
  76. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
  77. }
  78. });
  79. console.log(` ✅ HTTP代理测试成功: ${response.status}`);
  80. console.log(` IP: ${response.data.origin}`);
  81. } catch (error) {
  82. console.log(` ❌ HTTP代理测试失败: ${error.message}`);
  83. }
  84. }
  85. // 测试SOCKS5代理
  86. async function testSocks5Proxy() {
  87. console.log('\n4. 测试SOCKS5代理...');
  88. try {
  89. const { SocksProxyAgent } = require('socks-proxy-agent');
  90. const proxyUrl = 'socks5://127.0.0.1:7897';
  91. const agent = new SocksProxyAgent(proxyUrl);
  92. const response = await axios.get('https://httpbin.org/get', {
  93. httpsAgent: agent,
  94. timeout: 10000,
  95. headers: {
  96. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
  97. }
  98. });
  99. console.log(` ✅ SOCKS5代理测试成功: ${response.status}`);
  100. console.log(` IP: ${response.data.origin}`);
  101. } catch (error) {
  102. console.log(` ❌ SOCKS5代理测试失败: ${error.message}`);
  103. }
  104. }
  105. // 主函数
  106. async function main() {
  107. const directSuccess = await testDirect();
  108. if (!directSuccess) {
  109. console.log('\n⚠️ 直连测试失败,请检查网络连接');
  110. console.log('可能的原因:');
  111. console.log('- 网络连接问题');
  112. console.log('- 防火墙阻止');
  113. console.log('- DNS解析问题');
  114. return;
  115. }
  116. await testLocalProxy();
  117. await testHttpProxy();
  118. await testSocks5Proxy();
  119. console.log('\n📋 测试完成');
  120. console.log('如果直连成功但代理测试失败,说明:');
  121. console.log('1. 代理服务未运行');
  122. console.log('2. 代理端口配置错误');
  123. console.log('3. 代理配置有问题');
  124. }
  125. main().catch(console.error);