156 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			156 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| const net = require('net');
 | ||
| 
 | ||
| function testProxyConnection(host, port, username, password) {
 | ||
|   return new Promise((resolve) => {
 | ||
|     console.log(`\n🔍 测试代理连接:`);
 | ||
|     console.log(`   主机: ${host}`);
 | ||
|     console.log(`   端口: ${port}`);
 | ||
|     console.log(`   用户名: ${username}`);
 | ||
|     console.log(`   密码: ${password ? '***' : '无'}`);
 | ||
|     console.log(`${'='.repeat(50)}`);
 | ||
| 
 | ||
|     // 1. 基本TCP连接测试
 | ||
|     console.log('\n📡 测试1: TCP连接测试');
 | ||
|     const socket = new net.Socket();
 | ||
|     const timeout = setTimeout(() => {
 | ||
|       socket.destroy();
 | ||
|       console.log('   ❌ TCP连接超时 (10秒)');
 | ||
|       testHttpProxy();
 | ||
|     }, 10000);
 | ||
| 
 | ||
|     socket.connect(port, host, () => {
 | ||
|       clearTimeout(timeout);
 | ||
|       console.log('   ✅ TCP连接成功');
 | ||
|       socket.destroy();
 | ||
|       testHttpProxy();
 | ||
|     });
 | ||
| 
 | ||
|     socket.on('error', (err) => {
 | ||
|       clearTimeout(timeout);
 | ||
|       console.log(`   ❌ TCP连接失败: ${err.code || err.message}`);
 | ||
|       
 | ||
|       // 错误分析
 | ||
|       if (err.code === 'ECONNREFUSED') {
 | ||
|         console.log('      💡 连接被拒绝 - 端口可能未开放或服务未运行');
 | ||
|       } else if (err.code === 'ENOTFOUND') {
 | ||
|         console.log('      💡 主机未找到 - 请检查IP地址');
 | ||
|       } else if (err.code === 'ETIMEDOUT') {
 | ||
|         console.log('      💡 连接超时 - 主机不可达或网络问题');
 | ||
|       }
 | ||
|       
 | ||
|       testHttpProxy();
 | ||
|     });
 | ||
| 
 | ||
|     // 2. HTTP代理测试
 | ||
|     function testHttpProxy() {
 | ||
|       console.log('\n📡 测试2: HTTP代理协议测试');
 | ||
|       
 | ||
|       const http = require('http');
 | ||
|       
 | ||
|       // 构建HTTP CONNECT请求
 | ||
|       const auth = username && password ? 
 | ||
|         Buffer.from(`${username}:${password}`).toString('base64') : null;
 | ||
|       
 | ||
|       const options = {
 | ||
|         hostname: host,
 | ||
|         port: port,
 | ||
|         method: 'CONNECT',
 | ||
|         path: 'httpbin.org:80',
 | ||
|         headers: auth ? {
 | ||
|           'Proxy-Authorization': `Basic ${auth}`
 | ||
|         } : {}
 | ||
|       };
 | ||
| 
 | ||
|       const req = http.request(options);
 | ||
|       
 | ||
|       req.on('connect', (res, socket, head) => {
 | ||
|         console.log('   ✅ HTTP代理连接成功');
 | ||
|         console.log(`      状态码: ${res.statusCode}`);
 | ||
|         socket.end();
 | ||
|         testSocksProxy();
 | ||
|       });
 | ||
| 
 | ||
|       req.on('error', (err) => {
 | ||
|         console.log(`   ❌ HTTP代理连接失败: ${err.code || err.message}`);
 | ||
|         testSocksProxy();
 | ||
|       });
 | ||
| 
 | ||
|       req.setTimeout(10000, () => {
 | ||
|         console.log('   ❌ HTTP代理连接超时');
 | ||
|         req.destroy();
 | ||
|         testSocksProxy();
 | ||
|       });
 | ||
| 
 | ||
|       req.end();
 | ||
|     }
 | ||
| 
 | ||
|     // 3. SOCKS代理测试
 | ||
|     function testSocksProxy() {
 | ||
|       console.log('\n📡 测试3: SOCKS代理协议测试');
 | ||
|       
 | ||
|       const socksSocket = new net.Socket();
 | ||
|       const socksTimeout = setTimeout(() => {
 | ||
|         socksSocket.destroy();
 | ||
|         console.log('   ❌ SOCKS连接超时');
 | ||
|         printSummary();
 | ||
|       }, 10000);
 | ||
| 
 | ||
|       socksSocket.connect(port, host, () => {
 | ||
|         clearTimeout(socksTimeout);
 | ||
|         
 | ||
|         // 发送SOCKS5握手
 | ||
|         const handshake = Buffer.from([0x05, 0x01, 0x00]); // SOCKS5, 1 method, no auth
 | ||
|         socksSocket.write(handshake);
 | ||
|         
 | ||
|         socksSocket.once('data', (data) => {
 | ||
|           if (data.length >= 2 && data[0] === 0x05) {
 | ||
|             console.log('   ✅ SOCKS5代理响应正常');
 | ||
|             console.log(`      认证方法: ${data[1] === 0x00 ? '无需认证' : data[1] === 0x02 ? '用户名密码' : '其他'}`);
 | ||
|           } else {
 | ||
|             console.log('   ❌ SOCKS5代理响应异常');
 | ||
|           }
 | ||
|           socksSocket.destroy();
 | ||
|           printSummary();
 | ||
|         });
 | ||
|       });
 | ||
| 
 | ||
|       socksSocket.on('error', (err) => {
 | ||
|         clearTimeout(socksTimeout);
 | ||
|         console.log(`   ❌ SOCKS连接失败: ${err.code || err.message}`);
 | ||
|         printSummary();
 | ||
|       });
 | ||
|     }
 | ||
| 
 | ||
|     // 4. 总结
 | ||
|     function printSummary() {
 | ||
|       console.log('\n📋 测试总结:');
 | ||
|       console.log('   🔍 端口3306通常是MySQL数据库端口,不是代理端口');
 | ||
|       console.log('   💡 常见代理端口:');
 | ||
|       console.log('      - HTTP代理: 8080, 3128, 8888, 80');
 | ||
|       console.log('      - SOCKS5代理: 1080, 1085');
 | ||
|       console.log('   📞 建议联系代理服务商确认正确的端口和协议类型');
 | ||
|       
 | ||
|       resolve();
 | ||
|     }
 | ||
|   });
 | ||
| }
 | ||
| 
 | ||
| // 测试您提供的代理配置
 | ||
| const proxyConfig = {
 | ||
|   host: '143.20.228.192',
 | ||
|   port: 3306,
 | ||
|   username: 'mNrz1aEg',
 | ||
|   password: '3xV3dBYB'
 | ||
| };
 | ||
| 
 | ||
| console.log('🚀 开始代理连接测试...');
 | ||
| testProxyConnection(proxyConfig.host, proxyConfig.port, proxyConfig.username, proxyConfig.password)
 | ||
|   .then(() => {
 | ||
|     console.log('\n✨ 测试完成!');
 | ||
|     process.exit(0);
 | ||
|   })
 | ||
|   .catch(error => {
 | ||
|     console.error('\n💥 测试过程中发生错误:', error.message);
 | ||
|     process.exit(1);
 | ||
|   });
 | 
