UNPKG

gather-public-proxy

Version:
164 lines (130 loc) 3.78 kB
/*! * Gather Public Proxy * Copyright(c) 2014 Gautier Fauchart <gautier.fauchart@gmail.com> * MIT Licensed */ /** * Module dependencies. */ var _ = require("lodash"); var request = require("request"); var Q = require("q"); var fs = require("fs"); var ReplyChecker = require("./reply-checker") var CountryChecker = require("./location-checker") /** * DEFINES */ var DEFAULT_THREAD = 3; /** * Setup gpp * @api private */ var gpp = function gpp(){ } /** * init gpp * if undefined or wrong, re-set default options * @param {Object} option * @param {Function} callback * @api private */ gpp.prototype.init = function(_option, _fn){ this.option = _fn == undefined ? {} : _option; this.fn = typeof _fn === "function" ? _fn : _option; if (typeof this.option.thread != "number" || this.option.thread < 1) this.option.thread = DEFAULT_THREAD; this.option.search = this.option.search == "fast" ? "fast" : "best"; this.option.max = this.option.max >= 1 ? this.option.max : undefined; this.verbose = this.option.verbose; this.replyChecker = new ReplyChecker(this.option); this.countryChecker = new CountryChecker(this.option); this.finalContainer = []; } /** * start searching * @param {Object} option * @param {Function} callback * @api private */ gpp.prototype.search = function(){ var that = this; this.replyChecker.start().then(function(){ that.countryChecker.endInput(); }, console.warn, function(proxy){ that.verbose && console.warn(proxy.ip + " replied 200") that.countryChecker.add(proxy); }); this.countryChecker.start().then(function(){ that.renderList(true); }, console.warn, function(proxy){ that.verbose && console.warn(proxy.ip + " contry code " + proxy.country_code) that.finalContainer.push(proxy); that.renderList(false); }); this.askProviders().then(function(){ that.replyChecker.endInput(); }, console.warn, function(proxies){ that.verbose && console.warn(proxies.length + " proxies found") that.replyChecker.add(proxies); }); } /** * ask all providers and gave feteched proxies to replychecker * deferred.notify is used as callback when some proxies are feteched * @return promise * @api private */ gpp.prototype.askProviders = function(){ var deferred = Q.defer(); var path = __dirname + "/providers/"; var providers = fs.readdirSync(path); providers = _.filter(providers, function(name){return !!(/\.js$/.exec(name))}); providers = providers.map(function(name){ var providerPath = path + name; var provider = new (require(providerPath))(); return provider.run(deferred); }); Q.all(providers).then(function(){ deferred.resolve(); }); return deferred.promise; } /** * Render proxies list from option * if sufficient invoke callback * @return {Array} * @api */ gpp.prototype.renderList = function(end){ if (!end && this.option.search == "best") return; var list = _.clone(this.finalContainer, true); if (this.option.country) list = _.where(list, { 'country_code': this.option.country }); list.sort(function(a, b){ return a.delay - b.delay; }); list = this.option.max ? list.splice(0, this.option.max) : list; if (list.length == this.option.max){ this.replyChecker.end(); this.countryChecker.end(); } else if (!end){ return } this.verbose && console.warn("end, invoke callback with " + list.length + " proxy!") this.verbose = false; this.fn(list); this.fn = null; } /** * expose run runction * @param {object} option * @param {Function} callback * @api public */ module.exports = function(option, callback){ var inst = new gpp(); inst.init(option, callback); inst.search(); };