line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::ProxySelector;
|
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
5069
|
use 5.006;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict;
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings;
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
31
|
|
6
|
1
|
|
|
1
|
|
697
|
use integer;
|
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
4
|
|
7
|
1
|
|
|
1
|
|
2386
|
use LWP::UserAgent;
|
|
1
|
|
|
|
|
70635
|
|
|
1
|
|
|
|
|
781
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.02';
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# rand() is used, let's try to make the most of it...
|
11
|
|
|
|
|
|
|
srand;
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Constructor. Enables Inheritance
|
14
|
|
|
|
|
|
|
sub new {
|
15
|
0
|
|
|
0
|
1
|
|
my $this = shift;
|
16
|
0
|
|
0
|
|
|
|
my $class = ref($this) || $this;
|
17
|
0
|
|
|
|
|
|
my $self = {};
|
18
|
0
|
|
|
|
|
|
bless $self, $class;
|
19
|
|
|
|
|
|
|
|
20
|
0
|
0
|
|
|
|
|
if (@_) {
|
21
|
0
|
|
|
|
|
|
my %options = @_;
|
22
|
0
|
|
|
|
|
|
$self->{options} = \%options;
|
23
|
|
|
|
|
|
|
}
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Defaults
|
26
|
0
|
0
|
|
|
|
|
unless ($self->{options}{sites}) {
|
27
|
0
|
|
|
|
|
|
@{$self->{options}{sites}} = ('http://www.multiproxy.org/txt_anon/proxy.txt','http://www.samair.ru/proxy/fresh-proxy-list.htm');
|
|
0
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
}
|
29
|
0
|
|
0
|
|
|
|
$self->{options}{num_tries} ||= 5;
|
30
|
0
|
|
0
|
|
|
|
$self->{options}{testsite} ||= 'http://www.google.com';
|
31
|
0
|
|
0
|
|
|
|
$self->{options}{testflag} ||= 1;
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Return initialized object
|
34
|
0
|
|
|
|
|
|
return $self;
|
35
|
|
|
|
|
|
|
}
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Accept an anonymous proxy
|
38
|
|
|
|
|
|
|
sub set_proxy {
|
39
|
0
|
|
|
0
|
1
|
|
my ($self) = @_;
|
40
|
0
|
|
|
|
|
|
my ($counter, $rc) = (0,0);
|
41
|
0
|
|
0
|
|
|
|
do {
|
42
|
0
|
|
|
|
|
|
$rc = _set_proxy(@_);
|
43
|
0
|
|
|
|
|
|
++$counter;
|
44
|
|
|
|
|
|
|
}
|
45
|
|
|
|
|
|
|
until (($rc ne 'retest') || ($counter > $self->{options}{num_tries}));
|
46
|
0
|
0
|
|
|
|
|
$rc = 'All proxies checked failed to perform as expected' if ($rc eq 'retest');
|
47
|
0
|
|
|
|
|
|
return $rc;
|
48
|
|
|
|
|
|
|
}
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _set_proxy {
|
51
|
0
|
|
|
0
|
|
|
my ($self, $ua) = @_;
|
52
|
0
|
|
|
|
|
|
my $rc;
|
53
|
0
|
|
|
|
|
|
eval {
|
54
|
|
|
|
|
|
|
# From now, things can only go wrong :-)
|
55
|
0
|
|
|
|
|
|
$rc = 0;
|
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
my $list_page = $self->{options}{sites}[int(rand(scalar(@{$self->{options}{sites}})))];
|
|
0
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my $response = $ua->get($list_page);
|
59
|
0
|
|
|
|
|
|
my @proxy_list = $response->content() =~ /([\w\.\-]{7,}:\d{1,5})/g;
|
60
|
0
|
0
|
|
|
|
|
unless (@proxy_list) {
|
61
|
0
|
|
|
|
|
|
warn "Couldn't find any proxies in $list_page\n";
|
62
|
0
|
|
|
|
|
|
$rc = 1;
|
63
|
|
|
|
|
|
|
}
|
64
|
0
|
|
|
|
|
|
$self->{selected_proxy} = $proxy_list[int(rand(@proxy_list))];
|
65
|
0
|
|
|
|
|
|
$ua->proxy(['http', 'ftp'], 'http://' . $self->{selected_proxy});
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Proxy test, if required
|
68
|
0
|
0
|
0
|
|
|
|
$rc = $self->test_proxy($ua) if (($self->{options}{testflag}) && ($rc == 0));
|
69
|
|
|
|
|
|
|
};
|
70
|
0
|
0
|
|
|
|
|
if ($@) {
|
71
|
0
|
|
|
|
|
|
warn("Error occured in set_proxy - $@, Last system error: $!\n");
|
72
|
0
|
|
|
|
|
|
return $@;
|
73
|
|
|
|
|
|
|
}
|
74
|
0
|
|
|
|
|
|
return $rc;
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
}
|
77
|
|
|
|
|
|
|
# Tell the caller what proxy has been selected
|
78
|
|
|
|
|
|
|
sub get_proxy {
|
79
|
0
|
|
|
0
|
1
|
|
my $self = shift;
|
80
|
0
|
|
|
|
|
|
return $self->{selected_proxy};
|
81
|
|
|
|
|
|
|
}
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Test the proxy by trying to access a site
|
84
|
|
|
|
|
|
|
# Return 0 for success, 1 for failure.
|
85
|
|
|
|
|
|
|
sub test_proxy {
|
86
|
0
|
|
|
0
|
0
|
|
my ($self, $ua) = @_;
|
87
|
0
|
|
|
|
|
|
my $response = $ua->get($self->{options}{testsite});
|
88
|
0
|
0
|
|
|
|
|
$response->is_success() ? return 0 : return 1;
|
89
|
|
|
|
|
|
|
}
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1;
|
92
|
|
|
|
|
|
|
__END__
|