Search code examples
node.jsproxyrequestrequest-promiseproxies

How can I use proxies in Request-Promise requests?


I'm trying to use a HTTP proxy file, choosing a random proxy for each request, then requesting using the request-promise library.

const rp = require('request-promise'),
    fs = require('fs')

var proxies = []

function sendR() {
    let proxy = getProxy()
    console.log('Got proxy ' + proxy)
    let requestThing = rp.defaults({
        method: 'GET',
        proxy: 'http://' + proxy
    })
    requestThing('http://server.test.ip:1234').then((body) => {
        console.log(body)
    })
}

function getProxy() {
    return proxies[Math.floor(Math.random() * proxies.length)]
}

fs.readFile('proxy.txt', (err, data) => {
    if (err) throw err
    proxies = data.toString().split("\n")
})

setTimeout(sendR, 1000)

I'm getting the error Unhandled rejection RequestError: Error: connect ETIMEDOUT 1.1.1.1:80, and I've checked all proxies very frequently.


Solution

  • Just continuing to test worked. Doing this fixed it.

    const rp = require('request-promise'),
        fs = require('fs')
    
    var proxies = []
    
    function sendR() {
        let proxy = getProxy()
        console.log('Got proxy ' + proxy)
        let requestThing = rp.defaults({
            method: 'GET',
            proxy: 'http://' + proxy,
            jar: true
        })
        requestThing('http://a').then((body) => {
            console.log(body)
        }).catch((e) => {
            console.log('Error with proxy ' + proxy)
        })
    }
    
    function getProxy() {
        return proxies[Math.floor(Math.random() * proxies.length)]
    }
    
    fs.readFile('proxy.txt', (err, data) => {
        if (err) throw err
        proxies = data.toString().split("\n")
    })
    
    setTimeout(() => {
        setInterval(sendR, 50)
    }, 1000)
    

    I guess the issue was the proxies, as testing a lot of them eventually went through. My mistake. https://i.sstatic.net/7vooX.png