Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
return setTimeout(function() {that.nextProxy(host, cb)}, UseWaitTime)
}
return this.nextProxy(host, cb)
} else if(proxy.blocked) {
// check if the block has timed out
if(Date.now() < proxy.blocked.getTime() + BlockTimeout) {
return this.nextProxy(host, cb)
} else {
logger.info('reviving blocked proxy ' + proxy.proxy + ' after ' + BlockTimeout + 'ms')
proxy.blocked = false
}
} else if(proxy.broken) {
// check if the repair time is over
if(Date.now() < proxy.broken.getTime() + RepairTime) {
return this.nextProxy(host, cb)
} else {
logger.info('reviving broken proxy ' + proxy.proxy + ' after ' + RepairTime + 'ms')
proxy.broken = false
}
}
return cb(null, proxy)
}
function shutdown() {
logger.info('shutting down')
server.close()
fs.writeFile('.proxies.tmp', JSON.stringify(Proxies.list), function () {
process.exit(0)
})
}
function reportStatus() {
var status = Proxies.status()
status['wait (s)'] = Proxies.waitTime? Math.round(Proxies.waitTime / 1000): 0
logger.info(JSON.stringify(status))
}
var Port = Config.port,
DefaultTimeout = Config.defaultTimeout,
BindAddress = Config.bindAddress,
GraceTime = Config.graceTime || 0,
NextReqTimeout = Config.nextRequestTimeout || 2000,
BlockErrors = ['ETIMEDOUT', 'ECONNRESET', 'EHOSTUNREACH', 'ESOCKETTIMEDOUT', 'ECONNREFUSED']
app.use(bodyParser.json());
app.get('/status', sendStatus)
app.get('/proxies', sendProxies)
app.post('/admin', admin)
app.get('/', handleRequest)
if(fs.existsSync('.proxies.tmp')) {
logger.info('restoring previous state')
fs.readFile('.proxies.tmp', function(err, json) {
if(err) return logger.error(err)
var dates = ['blocked', 'broken', 'lastRequest']
Proxies.setList(JSON.parse(json))
for(var i = 0; i < Proxies.list.length; i++) {
var proxy = Proxies.list[i]
proxy.inUse = false
for(var key in proxy) {
if(dates.indexOf(key) !== -1 && proxy[key] !== false) {
proxy[key] = new Date(proxy[key])
}
}
}
})
}
function serverStarted() {
logger.info('service running at http://%s:%s',
server.address().address, server.address().port);
}