123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- const axios = require('axios');
- console.log('🔍 简单连通性测试\n');
- // 测试直连
- async function testDirect() {
- console.log('1. 测试直连...');
-
- const urls = [
- 'https://httpbin.org/get',
- 'https://www.google.com',
- 'https://www.github.com'
- ];
-
- for (const url of urls) {
- try {
- console.log(` 尝试: ${url}`);
- const start = Date.now();
- const response = await axios.get(url, {
- timeout: 10000,
- headers: {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
- }
- });
- const latency = Date.now() - start;
-
- console.log(` ✅ 成功: ${latency}ms (状态码: ${response.status})`);
- return true;
- } catch (error) {
- console.log(` ❌ 失败: ${error.message}`);
- }
- }
-
- console.log(' ❌ 所有直连测试都失败了');
- return false;
- }
- // 测试本地代理端口
- async function testLocalProxy() {
- console.log('\n2. 测试本地代理端口...');
-
- const net = require('net');
- const ports = [8080, 1080, 7890, 7891, 8118, 3128];
-
- for (const port of ports) {
- try {
- const socket = new net.Socket();
- const result = await new Promise((resolve) => {
- socket.setTimeout(3000);
- socket.on('connect', () => {
- socket.destroy();
- resolve(true);
- });
- socket.on('timeout', () => {
- socket.destroy();
- resolve(false);
- });
- socket.on('error', () => {
- socket.destroy();
- resolve(false);
- });
- socket.connect(port, '127.0.0.1');
- });
-
- if (result) {
- console.log(` ✅ 端口 ${port} 可连接`);
- } else {
- console.log(` ❌ 端口 ${port} 不可连接`);
- }
- } catch (error) {
- console.log(` ❌ 端口 ${port} 测试失败: ${error.message}`);
- }
- }
- }
- // 测试HTTP代理
- async function testHttpProxy() {
- console.log('\n3. 测试HTTP代理...');
-
- try {
- const { HttpsProxyAgent } = require('https-proxy-agent');
- const proxyUrl = 'http://127.0.0.1:7897';
- const agent = new HttpsProxyAgent(proxyUrl);
-
- const response = await axios.get('https://httpbin.org/get', {
- httpsAgent: agent,
- timeout: 10000,
- headers: {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
- }
- });
-
- console.log(` ✅ HTTP代理测试成功: ${response.status}`);
- console.log(` IP: ${response.data.origin}`);
- } catch (error) {
- console.log(` ❌ HTTP代理测试失败: ${error.message}`);
- }
- }
- // 测试SOCKS5代理
- async function testSocks5Proxy() {
- console.log('\n4. 测试SOCKS5代理...');
-
- try {
- const { SocksProxyAgent } = require('socks-proxy-agent');
- const proxyUrl = 'socks5://127.0.0.1:7897';
- const agent = new SocksProxyAgent(proxyUrl);
-
- const response = await axios.get('https://httpbin.org/get', {
- httpsAgent: agent,
- timeout: 10000,
- headers: {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
- }
- });
-
- console.log(` ✅ SOCKS5代理测试成功: ${response.status}`);
- console.log(` IP: ${response.data.origin}`);
- } catch (error) {
- console.log(` ❌ SOCKS5代理测试失败: ${error.message}`);
- }
- }
- // 主函数
- async function main() {
- const directSuccess = await testDirect();
-
- if (!directSuccess) {
- console.log('\n⚠️ 直连测试失败,请检查网络连接');
- console.log('可能的原因:');
- console.log('- 网络连接问题');
- console.log('- 防火墙阻止');
- console.log('- DNS解析问题');
- return;
- }
-
- await testLocalProxy();
- await testHttpProxy();
- await testSocks5Proxy();
-
- console.log('\n📋 测试完成');
- console.log('如果直连成功但代理测试失败,说明:');
- console.log('1. 代理服务未运行');
- console.log('2. 代理端口配置错误');
- console.log('3. 代理配置有问题');
- }
- main().catch(console.error);
|