Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
proxy = Proxies.nextProxy(url.host, function(err, proxy) {
if(err) {
if(err === 'ALL_BLOCKED') {
res.status(503).end('all proxies are blocked')
} else {
res.status(503).end('all proxies are broken')
}
return
}
logger.debug('%s (%s)', query.url, proxy)
// handle grace time for preventing blocks or/and send the request
if(GraceTime !== 0 && proxy.lastRequest && Date.now() < proxy.lastRequest.getTime() + GraceTime) {
var wait = proxy.lastRequest.getTime() + GraceTime - Date.now()
logger.debug('have to wait for %sms to prevent a block on proxy %s', wait, proxy.proxy)
Proxies.waitTime+= wait
setTimeout(function() {
sendRequest(proxy, url, timeout, req, res)
}, wait)
} else {
sendRequest(proxy, url, timeout, req, res)
}
})
}
proxy = Proxies.nextProxy(url.host, function(err, proxy) {
if(err) {
if(err === 'ALL_BLOCKED') {
res.status(503).end('all proxies are blocked')
} else {
res.status(503).end('all proxies are broken')
}
return
}
logger.debug('%s (%s)', query.url, proxy)
// handle grace time for preventing blocks or/and send the request
if(GraceTime !== 0 && proxy.lastRequest && Date.now() < proxy.lastRequest.getTime() + GraceTime) {
var wait = proxy.lastRequest.getTime() + GraceTime - Date.now()
logger.debug('have to wait for %sms to prevent a block on proxy %s', wait, proxy.proxy)
Proxies.waitTime+= wait
setTimeout(function() {
sendRequest(proxy, url, timeout, req, res)
}, wait)
} else {
sendRequest(proxy, url, timeout, req, res)
}
})
}
ProxyManager.nextProxy = function(host, cb) {
if(this.allBroken()) {
if(this.allBlocked()) {
this._allBlocked = true
return cb('ALL_BLOCKED')
}
return cb('ALL_BROKEN')
}
// increment the index value for the host
if(typeof Index[host] === 'undefined' || list.length === Index[host] + 1) {
if(typeof Index[host] === 'undefined') {
logger.debug('all proxies used, starting new cycle')
}
Index[host] = 0
} else {
Index[host]++
}
var proxy = list[Index[host]]
if(proxy.errors >= MaxErrors && proxy.hits === 0) {
// if a proxy
return this.nextProxy(host, cb)
} else if(!AllowMultipleCalls && proxy.inUse) {
// once we hit a proxy in use, we check if any other is free at the moment,
// if not, we sleep for a small amount of time
if(!AllowMultipleCalls && this.allInUse()) {
var that = this
this.timeWaited+= UseWaitTime