test_proxy_websocket_raw.cjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const WebSocket = require('ws');
  2. const { HttpsProxyAgent } = require('https-proxy-agent');
  3. /**
  4. * 原生WebSocket代理测试
  5. * 测试IPRoyal代理是否支持WebSocket连接
  6. */
  7. async function testProxyWebSocket() {
  8. console.log('🚀 开始原生WebSocket代理测试...\n');
  9. // 生成动态代理密码
  10. const sessionId = Math.random().toString(36).substr(2, 8);
  11. const proxyPassword = `test123456_country-jp,sg_session-${sessionId}_lifetime-59m`;
  12. const proxyUrl = `http://tuxla:${proxyPassword}@geo.iproyal.com:12321`;
  13. console.log('🔐 代理配置:');
  14. console.log(` 代理URL: http://tuxla:***@geo.iproyal.com:12321`);
  15. console.log(` 会话ID: ${sessionId}`);
  16. // 测试1: 不使用代理的WebSocket连接
  17. console.log('\n📡 测试1: 直接WebSocket连接(无代理)...');
  18. try {
  19. await testWebSocketConnection('wss://ws.pacifica.fi/ws', null);
  20. } catch (error) {
  21. console.log(`❌ 直接连接失败: ${error.message}`);
  22. }
  23. // 测试2: 使用HTTP代理的WebSocket连接
  24. console.log('\n🌐 测试2: 通过IPRoyal HTTP代理的WebSocket连接...');
  25. try {
  26. const proxyAgent = new HttpsProxyAgent(proxyUrl);
  27. await testWebSocketConnection('wss://ws.pacifica.fi/ws', proxyAgent);
  28. } catch (error) {
  29. console.log(`❌ 代理连接失败: ${error.message}`);
  30. }
  31. console.log('\n✅ WebSocket代理测试完成!');
  32. }
  33. function testWebSocketConnection(url, agent) {
  34. return new Promise((resolve, reject) => {
  35. console.log(` 🔗 连接到: ${url}`);
  36. console.log(` 🌐 代理: ${agent ? '启用' : '禁用'}`);
  37. const wsOptions = {};
  38. if (agent) {
  39. wsOptions.agent = agent;
  40. }
  41. const ws = new WebSocket(url, wsOptions);
  42. const startTime = Date.now();
  43. // 设置超时
  44. const timeout = setTimeout(() => {
  45. ws.terminate();
  46. reject(new Error('连接超时 (15秒)'));
  47. }, 15000);
  48. ws.on('open', () => {
  49. const duration = Date.now() - startTime;
  50. console.log(` ✅ WebSocket连接成功! 耗时: ${duration}ms`);
  51. // 发送一个测试消息
  52. const testMessage = JSON.stringify({
  53. id: `test-${Date.now()}`,
  54. method: 'ping'
  55. });
  56. console.log(` 📤 发送测试消息: ${testMessage}`);
  57. ws.send(testMessage);
  58. // 等待响应
  59. setTimeout(() => {
  60. clearTimeout(timeout);
  61. ws.close();
  62. resolve();
  63. }, 3000);
  64. });
  65. ws.on('message', (data) => {
  66. console.log(` 📥 收到消息: ${data.toString()}`);
  67. });
  68. ws.on('close', (code, reason) => {
  69. console.log(` 🔒 连接关闭: ${code} ${reason}`);
  70. });
  71. ws.on('error', (error) => {
  72. console.log(` ❌ WebSocket错误: ${error.message}`);
  73. clearTimeout(timeout);
  74. reject(error);
  75. });
  76. });
  77. }
  78. // 运行测试
  79. if (require.main === module) {
  80. testProxyWebSocket().catch(error => {
  81. console.error('🚨 测试失败:', error);
  82. process.exit(1);
  83. });
  84. }
  85. module.exports = { testProxyWebSocket };