Created
May 1, 2012 17:49
-
-
Save sugar84/2570008 to your computer and use it in GitHub Desktop.
proxy, anyevent::http::socks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use DB::Local; | |
use AnyEvent::HTTP::Socks; | |
use constant CHECK_TIME => 15 * 60; | |
use constant TIMEOUT => 10; | |
use constant MAX_GETS => 60; | |
my ($ua, $dbh, $url, $proxies, $now, $cv); | |
$dbh = DB::Local->new->dbh; | |
$url = 'http://google.com/'; | |
$now = time; | |
$cv = AE::cv; | |
$AnyEvent::HTTP::USERAGENT = | |
'Mozilla/5.0 (X11; Linux x86_64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1'; | |
# here will be arrayref of hashrefs like: {ip => .., port =>, type => .., id => .. } | |
$proxies = $dbh->get_proxy_list; | |
my ($alive, $dead, $amount, $cur, $watcher, $max); | |
$max = $#$proxies; | |
$cur = 0; | |
$alive = 0; | |
$dead = 0; | |
$amount = 0; | |
$watcher = AE::timer(0, 1, sub | |
{ | |
while ($amount < MAX_GETS && $cur <= $max) | |
{ | |
my ($proxy, $pr_type, $pr_def, $valid); | |
$proxy = $proxies->[$cur]; | |
$cur++; | |
if ($proxy->{'type'} eq 'http') | |
{ | |
$pr_type = 'http'; | |
$pr_def = [ $proxy->{'ip'}, $proxy->{'port'} ]; | |
} | |
elsif ($proxy->{'type'} ~~ ['socks4', 'socks4']) | |
{ | |
$pr_type = 'socks'; | |
$pr_def = $proxy->{'type'} . '://' . $proxy->{'ip'} . ':' . $proxy->{'port'}; | |
} | |
else { | |
return; | |
} | |
$amount++; | |
http_get($url, | |
$pr_type => $pr_def, | |
'timeout' => TIMEOUT, | |
sub | |
{ | |
my ($data, $headers) = @_; | |
my $id = $proxy->{'id'}; | |
my $curr = $cur; | |
if ($headers->{'Status'} == 200) | |
{ | |
$alive++; | |
$valid = 'OK'; | |
} | |
else | |
{ | |
$dead++; | |
$valid = 'NO'; | |
} | |
$dbh->update_proxy(undef, $valid, time, $id); | |
$amount--; | |
print "curr: $curr; in parralel: $amount; _dead_: $dead; " . | |
"_alive_: $alive $valid\n"; | |
return; | |
}, | |
); | |
} | |
if ($cur > $max && $amount == 0) | |
{ | |
$cv->send; | |
undef $watcher; | |
} | |
return; | |
}); | |
$cv->recv; | |
print "dead: $dead, alive: $alive\n"; | |
print "the end\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Похоже на баг :)
$proxy->{'type'} ~~ ['socks4', 'socks4']