test-simple-proxy.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const axios = require('axios');
  2. const { HttpsProxyAgent } = require('https-proxy-agent');
  3. const { SocksProxyAgent } = require('socks-proxy-agent');
  4. async function testDirectConnection() {
  5. console.log('🔍 测试直接连接...');
  6. try {
  7. const response = await axios.get('https://httpbin.org/ip', {
  8. timeout: 10000
  9. });
  10. console.log('✅ 直接连接成功:', response.data);
  11. return true;
  12. } catch (error) {
  13. console.log('❌ 直接连接失败:', error.message);
  14. return false;
  15. }
  16. }
  17. async function testProxyConnection(proxyType, host, port, username = null, password = null) {
  18. console.log(`🔍 测试${proxyType}代理: ${host}:${port}`);
  19. try {
  20. let agent;
  21. let proxyUrl;
  22. if (proxyType === 'http' || proxyType === 'https') {
  23. proxyUrl = `${proxyType}://${host}:${port}`;
  24. if (username && password) {
  25. proxyUrl = `${proxyType}://${username}:${password}@${host}:${port}`;
  26. }
  27. agent = new HttpsProxyAgent(proxyUrl);
  28. } else if (proxyType === 'socks5') {
  29. proxyUrl = `socks5://${host}:${port}`;
  30. if (username && password) {
  31. proxyUrl = `socks5://${username}:${password}@${host}:${port}`;
  32. }
  33. agent = new SocksProxyAgent(proxyUrl);
  34. } else {
  35. console.log(`❌ 不支持的代理类型: ${proxyType}`);
  36. return false;
  37. }
  38. console.log(`使用代理URL: ${proxyUrl}`);
  39. const response = await axios.get('https://httpbin.org/ip', {
  40. httpsAgent: agent,
  41. httpAgent: agent,
  42. timeout: 10000,
  43. headers: {
  44. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
  45. }
  46. });
  47. console.log('✅ 代理连接成功:', response.data);
  48. return true;
  49. } catch (error) {
  50. console.log('❌ 代理连接失败:', error.message);
  51. return false;
  52. }
  53. }
  54. async function main() {
  55. console.log('🚀 开始代理连接测试...\n');
  56. // 测试直接连接
  57. await testDirectConnection();
  58. console.log('');
  59. // 测试HTTP代理(如果有的话)
  60. // 这里可以添加你的代理服务器信息
  61. // await testProxyConnection('http', 'proxy.example.com', 8080);
  62. // 测试SOCKS5代理(如果有的话)
  63. // await testProxyConnection('socks5', 'socks.example.com', 1080);
  64. console.log('✅ 测试完成');
  65. }
  66. main().catch(console.error);