12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- const axios = require('axios');
- const { HttpsProxyAgent } = require('https-proxy-agent');
- const { SocksProxyAgent } = require('socks-proxy-agent');
- async function testDirectConnection() {
- console.log('🔍 测试直接连接...');
- try {
- const response = await axios.get('https://httpbin.org/ip', {
- timeout: 10000
- });
- console.log('✅ 直接连接成功:', response.data);
- return true;
- } catch (error) {
- console.log('❌ 直接连接失败:', error.message);
- return false;
- }
- }
- async function testProxyConnection(proxyType, host, port, username = null, password = null) {
- console.log(`🔍 测试${proxyType}代理: ${host}:${port}`);
-
- try {
- let agent;
- let proxyUrl;
-
- if (proxyType === 'http' || proxyType === 'https') {
- proxyUrl = `${proxyType}://${host}:${port}`;
- if (username && password) {
- proxyUrl = `${proxyType}://${username}:${password}@${host}:${port}`;
- }
- agent = new HttpsProxyAgent(proxyUrl);
- } else if (proxyType === 'socks5') {
- proxyUrl = `socks5://${host}:${port}`;
- if (username && password) {
- proxyUrl = `socks5://${username}:${password}@${host}:${port}`;
- }
- agent = new SocksProxyAgent(proxyUrl);
- } else {
- console.log(`❌ 不支持的代理类型: ${proxyType}`);
- return false;
- }
-
- console.log(`使用代理URL: ${proxyUrl}`);
-
- const response = await axios.get('https://httpbin.org/ip', {
- httpsAgent: agent,
- httpAgent: agent,
- timeout: 10000,
- headers: {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
- }
- });
-
- console.log('✅ 代理连接成功:', response.data);
- return true;
- } catch (error) {
- console.log('❌ 代理连接失败:', error.message);
- return false;
- }
- }
- async function main() {
- console.log('🚀 开始代理连接测试...\n');
-
- // 测试直接连接
- await testDirectConnection();
- console.log('');
-
- // 测试HTTP代理(如果有的话)
- // 这里可以添加你的代理服务器信息
- // await testProxyConnection('http', 'proxy.example.com', 8080);
-
- // 测试SOCKS5代理(如果有的话)
- // await testProxyConnection('socks5', 'socks.example.com', 1080);
-
- console.log('✅ 测试完成');
- }
- main().catch(console.error);
|