gather-public-proxy
Version:
Find free anonymous proxy
133 lines (105 loc) • 2.26 kB
JavaScript
/*!
* Gather Public Proxy
* Copyright(c) 2014 Gautier Fauchart <gautier.fauchart@gmail.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var request = require("request");
var _ = require("lodash");
var Q = require("q");
/**
* expose replyChecker
*/
module.exports = replyChecker;
/**
* define
*/
var URL_TEST = 'http://myip.dnsdynamic.org'
/**
* setup replyChecker
* @param {Object} options
* @api private
*/
function replyChecker(_option){
this.option = _option;
this.queue = [];
this.inProgress = 0;
this.online = true;
}
/**
* start engine
* @return promise
* @api public
*/
replyChecker.prototype.start = function(){
this.deferred = Q.defer();
return this.deferred.promise;
}
/**
* Stop locationChecker, prevent request and resolve promise
* @api public
*/
replyChecker.prototype.end = function(){
this.online = false;
this.queue = null;
this.deferred.resolve();
}
/**
* set stopInput = true, for stopping engine when queue is empty
* @api public
*/
replyChecker.prototype.endInput = function(){
this.stopInput = true;
if (this.inProgress == 0 && this.queue.length == 0)
this.end();
}
/**
* add proxy to queue
* @param {Array} proxies
* @api public
*/
replyChecker.prototype.add = function(proxies){
var that = this;
proxies.forEach(function(proxy){
that.queue.push(proxy);
});
_(this.option.thread - this.inProgress).times(function(){
that.execute();
});
}
/**
* execute request
* @api private
*/
replyChecker.prototype.execute = function(){
var that = this;
if (!this.online)
return;
if (this.inProgress >= this.option.thead)
return;
var proxy = this.queue.shift();
if (!proxy){
if (this.stopInput && this.inProgress == 0)
this.end();
return
}
this.inProgress++;
var start = +(new Date());
options = {
url: URL_TEST,
proxy: "http://" + proxy.ip + ":" + proxy.port,
timeout: 4000
};
request(options, function(error, response, body) {
that.inProgress--;
if (!error && response.statusCode == 200) {
proxy.delay = (+(new Date())) - start;
that.deferred.notify(proxy);
}
_.defer(function(){
that.execute();
});
});
}