0
votes

I want to use proxy for request a html each query in node js. I found a request module, it allows to use proxies and easy in use.

For me it working good with simple requests, but whith proxy pool - not. It throws me errors:

  • tunneling socket could not be established, cause=connect ECONNREFUSED
  • tunneling socket could not be established, statusCode=400
  • tunneling socket could not be established, cause=connect EHOSTUNREACH 0.0.13.64:80 - Local (192.168.1.174:54491)

How to fix it?

var Promise = require('bluebird');

// Rounting
var koa = require('koa');
var router = require('koa-router')();
var koaRestMongoose = require('koa-rest-mongoose');
var cors = require('koa-cors');

var mongoUrl = '127.0.0.1:27017/db';
var mongoose = require('mongoose');
    mongoose.connect(mongoUrl);

// PROXY
// This insert proxy list to the db/proxy
var proxy = require("./proxy")(mongoUrl, 'proxy');

var jsProxyPool = require('proxy-pool');
var mongoDbDataAccess = new jsProxyPool.MongoAccess(mongoUrl, 'proxy');
var poolConfig = new jsProxyPool.ProxyPoolConfiguration(mongoDbDataAccess, 20*1000);
var pool = new jsProxyPool.ProxyPool(poolConfig);

// Requesting
var request = require('request');

router.get('/url/:url', function* () {
  var promise = new Promise((resolve, reject) => {

    pool.getProxy((err, proxy) => {

      var proxy = proxy;
      var url = 'https://google.com/';

      request({
        url: url,
        proxy: {
            host: proxy._address,
            port: proxy._port,
        }
      }, (error, response, body) => {
          if (!error && response.statusCode == 200) {
              resolve(body);
          } else {
              resolve('Proxy: '+proxy._address+':'+proxy._port + '\n' + error);
          }
      });
    });
  });
  this.body = yield promise;
});

var app = koa();
app.use(cors());
app.use(router.routes());
app.listen(8005);
1

1 Answers

0
votes

I've added agent and it resolve my issue + the proxy should be alive.

        var HttpsProxyAgent = require('https-proxy-agent'); 
        var proxy = {
            host: '111.111.111.111',
            port: 1111
        };

        var agent = new HttpsProxyAgent(proxy); 

        request({  
            url: url,
            method: 'GET',
            agent: agent,
            timeout: 10000,
            followRedirect: true,
            maxRedirects: 10,
        }, (error, response, body) => {
            if (error) {
                resolve(error);
            }
            resolve(body);
        });