Чекер проксей на NodeJS работает меньше секунды. Где косяк?



  • Запускаю чекинг фрии проксей на ноде 18.
    Вот какие npm пакеты стоят :

    er.png

    После запуска кубика NodeJS . кубик работает меньше секунды и в переменой [[SAVED_CONTENT_GOOD_PROXYS]] по итогу unefined. Ну не может же прокси чекер на 300 проксей отработать меньше секунды, значит он и не начинает проверять прокси) .

    Хоть прокся чекеру скармливаю через список [[SAVED_CONTENT]] , хоть в виде списка прям в коде - эффект один , кубик работает меньше секунды! Даже ошибки ни какие в логе не выдаёт.

    Проксей для примера здесь мало в коде отобразил, в по факту их более 300 шт.
    Где косяк?

    Код чекера на NodeJS 18

    const http = require('http');
    const HttpProxyAgent = require('http-proxy-agent');
    const axios = require('axios');
    
    // const SAVED_CONTENT = [[SAVED_CONTENT]];
    const SAVED_CONTENT = ["190.5.77.211:80","142.11.222.22:80","45.136.58.22:8888","72.170.220.17:8080","47.244.32.96:80"];
    
    const testUrl = 'http://www.google.com'; // Replace with the URL you want to test
    const timeout = 1000; // Timeout for the proxy check in milliseconds
    const goodProxys = [];
    
    async function checkProxies(proxies) {
      const promises = proxies.map(proxy => {
        return new Promise(async resolve => {
          try {
            const response = await axios.get(testUrl, {
              proxy: {
                host: proxy.split(':')[0],
                port: parseInt(proxy.split(':')[1], 10)
              },
              timeout: timeout
            });
    
            if (response.status === 200) {
            //   console.log(`Good proxy: ${proxy}`);
              goodProxys.push(proxy);
            }
          } catch (error) {
            // console.log(`Bad proxy: ${proxy}`);
          }
    
          resolve();
        });
      });
    
      await Promise.all(promises);
    //   console.log('\nAll proxies checked.');
    //   console.log('Good proxies:', goodProxys);
      [[SAVED_CONTENT_GOOD_PROXYS]] = goodProxys;
    }
    
    checkProxies(SAVED_CONTENT);
      
    


  • @Nikolas

    Сделай свой npm пакет, напиши к нему небольшой тест, где проверяешь, что прокси действительно проверяются.

    Что-то вроде:

    const assert = require('assert');
    const sinon = require('sinon');
    const axios = require('axios');
    
    const checkProxies = require('./your-file-name.js');
    
    describe('checkProxies', () => {
      let axiosGetStub;
      beforeEach(() => {
        axiosGetStub = sinon.stub(axios, 'get');
      });
    
      afterEach(() => {
        axiosGetStub.restore();
      });
    
      it('should add good proxies to the goodProxys array', async () => {
        axiosGetStub.resolves({ status: 200 });
    
        const proxies = ['1.2.3.4:80', '5.6.7.8:8080', '9.10.11.12:8888'];
        const goodProxys = [];
        await checkProxies(proxies, goodProxys);
    
        assert.deepStrictEqual(goodProxys, proxies);
      });
    
      it('should not add bad proxies to the goodProxys array', async () => {
        axiosGetStub.rejects(new Error('Connection error'));
    
        const proxies = ['1.2.3.4:80', '5.6.7.8:8080', '9.10.11.12:8888'];
        const goodProxys = [];
        await checkProxies(proxies, goodProxys);
    
        assert.deepStrictEqual(goodProxys, []);
      });
    });
    
    

Log in to reply