gather-public-proxy
Version:
Find free anonymous proxy
128 lines (103 loc) • 2.47 kB
JavaScript
/*!
* Gather Public Proxy
* Copyright(c) 2014 Gautier Fauchart <gautier.fauchart@gmail.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var request = require("request")
var cheerio = require("cheerio")
var Q = require("q")
var _ = require("lodash")
/**
* Expose letushide
*/
module.exports = letushide;
/**
* Setup proxy
* @param {String} ip
* @param {String} port
* @api private
*/
function Proxy(ip, port){
this.ip = ip;
this.port = port;
}
/**
* checks if proxy is valid.
* @return boolean
* @api private
*/
Proxy.prototype.isValid = function(){
var ipRegex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
var portRegex = /^[0-9]+$/;
return ipRegex.exec(this.ip) && portRegex.exec(this.port);
}
/**
* Setup letushide.
* @api public
*/
function letushide(){
this.base = "http://letushide.com";
this.path = "/filter/http,hap,all/";
this.indexPagination = 1;
this.online = true;
}
/**
* in case proxies are callback, stop search
* @api public
*/
letushide.prototype.end = function(html){
this.online = false;
}
/**
* Parse html for proxys, return true if there is more pagination.
* @param {String} html
* @return {Boolean}
* @api private
*/
letushide.prototype.parse = function(html){
var $ = cheerio.load(html);
var proxies = [];
$("div#data tr#data").each(function(){
var ip = $(this).find("td#link").text().trim();
var port = $(this).find("td:nth-child(3)").text().trim();
var proxy = new Proxy(ip, port);
if (proxy.isValid()) proxies.push(proxy);
});
this.sharedDeferred.notify(proxies);
return html.indexOf(this.path + this.indexPagination) != -1
}
/**
* Fetch page content.
* @api private
*/
letushide.prototype.fetch = function(){
var url = this.base + this.path + (this.indexPagination++);
var that = this;
if (!this.online)
return;
request(url, function(error, response, body){
if (error || response.statusCode !== 200){
console.warn("lethushide : error")
that.deferred.resolve();
} else if (that.parse(body) && false){
_.defer(function(){
that.fetch();
});
}else{
that.deferred.resolve();
}
});
}
/**
* Run deferred
* @api public
*/
letushide.prototype.run = function(_sharedDeferred){
this.sharedDeferred = _sharedDeferred;
this.deferred = Q.defer();
this.fetch();
return this.deferred.promise;
}