TaskPipe/OpenProxyManager.pm

filter: Statementstmt 28.9% 35 / 121 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 47.3% 9 / 19 Podpod 0.0% 0 / 5 totaltotal 26.9% 44 / 163 CC 12 SCAR 22.2
CC
12
Cov
25%
test_proxy
30.0
__ANON__
12.0
next_available_proxy
12.0
CRAP 9.2
« TaskPipe/InterpParam.pm ↑ Summary TaskPipe/Tool/Command_ClearTables.pm »
1 package TaskPipe::OpenProxyManager;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Module::Runtime 'require_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Digest::MD5 qw(md5_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use TaskPipe::UserAgentManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use TaskPipe::ThreadManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 with 'MooseX::ConfigCascade';
12  
13  
14 1 has gm => (is => 'rw', isa => 'TaskPipe::SchemaManager');
15  
16 has my_ip => (is => 'rw', isa => 'Str', lazy => 1, default => sub{
17 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
18  
19 0 my $ua_mgr = TaskPipe::UserAgentManager->new(
20 gm => $self->gm
21 );
22 0 my $ip = $ua_mgr->check_ip;
23 0 return $ip;
24 1 });
25  
26  
27 has ua_mgr => (is => 'ro', isa => 'TaskPipe::UserAgentManager', lazy => 1, default => sub{
28 0 ▶ TaskPipe::UserAgentManager->new(
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 gm => $_[0]->gm
30 );
31 1 });
32  
33  
34 has settings => (is => 'rw', isa => __PACKAGE__.'::Settings', default => sub{
35 1 ▶ my $module = __PACKAGE__.'::Settings';
Subroutine __ANON__: called CC=1 SCAR=0.0
36 1 require_module( $module );
37 1 $module->new;
38 1 });
39  
40  
41 has lists => (is => 'ro', isa => 'ArrayRef', lazy => 1, default => sub{
42 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
43 0 my $seen = {}; #guard against duplicate entries in list
44  
45 0 my $lists = [];
46 0 foreach my $list_name ( @{$self->settings->ip_list_names} ){
47 0 ▶ next if $seen->{$list_name};
Branch: 1 branch
BranchTrueFalseTotal
if $seen->{$list_name} 0 0 0
48  
49 0 my $mod = __PACKAGE__.'::IPList_'.$list_name;
50 0 require_module( $mod );
51  
52 0 my $list = $mod->new(
53 gm => $self->gm
54 );
55  
56 0 push @$lists,$list;
57 0 $seen->{$list_name} = 1;
58 }
59  
60 0 return $lists;
61 1 });
62  
63  
64  
65  
66  
67 sub fetch_proxies{
68 0 ▶ my $self = shift;
Subroutine fetch_proxies: not called CC=2 SCAR=17.9
Pod undocumented
69  
70 0 my $logger = Log::Log4perl->get_logger;
71  
72 0 my $tm = TaskPipe::ThreadManager->new(
73 gm => $self->gm,
74 name => "OpenProxy",
75 max_threads => $self->settings->max_threads,
76 forks => 0
77 );
78  
79 0 $tm->init;
80  
81 0 my $count = 0;
82 0 my @lists = @{$self->lists};
83 0 for my $list_i (0..$#lists){
84  
85 0 my $list = $lists[$list_i];
86 0 my $list_name = $list->list_name;
87  
88 0 $list->fetch_proxies;
89  
90 }
91  
92 0 $tm->wait_children;
93 0 $tm->finalize;
94 }
95  
96  
97  
98  
99 sub test_proxy{
100 0 ▶ my ($self,$proxy) = @_;
Subroutine test_proxy: not called CC=5 SCAR=34.0
Pod undocumented
101  
102 0 my $logger = Log::Log4perl->get_logger;
103  
104 0 my $uri = $self->settings->proxy_scheme.'://'.$proxy->ip.':'.$proxy->port;
105 0 $logger->debug("Open proxy checker: Testing proxy $uri");
106  
107 0 my $ua_mgr = TaskPipe::UserAgentManager->new(
108 gm => $self->gm
109 );
110  
111 0 $ua_mgr->ua_handler->call( 'proxy', $self->settings->protocols => $uri );
112  
113 0 my $ip = $ua_mgr->check_ip;
114 0 ▶ $logger->debug("ip [$ip] my ip [".$self->my_ip."]") if $ip;
Branch: 1 branch
BranchTrueFalseTotal
if $ip 0 0 0
115  
116 0 my $success = 0;
117 0 ▶ if ( $ip ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ip) 0 0 0
118 0 $logger->debug("Successful response from proxy $uri");
119
120 0 ▶ if ( $ip ne $self->my_ip ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ip ne $self->my_ip) { } 0 0 0
121 0 $logger->debug("Proxy $uri does not project originating IP - test successful");
122 0 $success = 1;
123 } else {
124 0 $logger->debug("Proxy $uri seems to project originating IP - test failed");
125 }
126 }
127  
128 0 ▶ my $status = $success ? 'available' : 'dud';
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
129  
130 0 my $dt = DateTime->now;
131  
132 0 $proxy->update({
133 checked_dt => $dt,
134 status => $status
135 });
136  
137 0 return $success;
138  
139 }
140  
141  
142  
143 sub test_proxies{
144 0 ▶ my $self = shift;
Subroutine test_proxies: not called CC=2 SCAR=17.9
Pod undocumented
145  
146 0 my $logger = Log::Log4perl->get_logger;
147  
148 0 my $dt = DateTime->now->subtract( minutes => 10 );
149 0 my $dud_dt = DateTime->now->subtract( days => +$self->settings->clean_dud_proxies_after );
150  
151 0 my $dtp = $self->gm->schema->storage->datetime_parser;
152  
153 0 $self->gm->table('open_proxy')->search({
154 checked_dt => { '<', +$dtp->format_datetime( $dud_dt ) },
155 status => 'dud'
156 })->delete_all;
157  
158 0 my $proxy_rs = $self->gm->table('open_proxy')->search([{
159 checked_dt => undef
160 }, {
161 checked_dt => { '<', $dtp->format_datetime( $dt ) },
162 status => { '!=', 'dud' }
163 }], {
164 order_by => { -asc => 'checked_dt' }
165 });
166
167 0 my $tm = TaskPipe::ThreadManager->new(
168 gm => $self->gm,
169 name => "OpenProxy",
170 max_threads => $self->settings->max_threads,
171 forks => 0
172 );
173  
174 0 $tm->init;
175  
176 0 my $count = 0;
177 0 my $total = $proxy_rs->count;
178  
179 0 ▶ while( my $proxy = $proxy_rs->next ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $proxy = $proxy_rs->next) 0 0 0
180 0 $count++;
181  
182 $tm->execute( sub{
183 #my ($thread_id) = @_;
184 0 ▶ $self->test_proxy(
Subroutine __ANON__: not called CC=1 SCAR=6.9
185 #$thread_id,
186 $proxy
187 );
188 },
189 0 $count,
190 $count == $total
191 );
192 }
193  
194 0 $tm->wait_children;
195 0 $tm->finalize;
196 }
197  
198  
199 sub next_available_proxy{
200 0 ▶ my $self = shift;
Subroutine next_available_proxy: not called CC=3 SCAR=24.8
Pod undocumented
201  
202 0 my $logger = Log::Log4perl->get_logger;
203 0 my $proxy = $self->next_available_proxy_attempt;
204  
205 0 my $fails = 0;
206 0 ▶ while ( ! $proxy ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($proxy) 0 0 0
207 0 $fails++;
208 0 ▶ confess "Failed to find an available proxy on ".$self->settings->max_unavailable_fails." attempts" if ( $self->settings->max_available_fails >= $fails );
Branch: 1 branch
BranchTrueFalseTotal
if $self->settings->max_available_fails >= $fails 0 0 0
209 0 $logger->warn("No proxy appears to be available. Waiting ".$self->settings->unavailable_poll_interval." seconds");
210 0 sleep( $self->settings->unavailable_poll_interval );
211 0 $proxy = $self->next_available_proxy_attempt;
212 }
213  
214 0 return $proxy;
215 }
216
217  
218  
219  
220 sub next_available_proxy_attempt{
221 0 ▶ my $self = shift;
Subroutine next_available_proxy_attempt: not called CC=2 SCAR=17.9
Pod undocumented
222  
223 0 my $reserve_id = "reserved-".md5_base64( $$ * rand );
224  
225 $self->gm->schema->txn_do( sub{
226 0 ▶ my $rs = $self->gm->table('open_proxy')->search({
Subroutine __ANON__: not called CC=1 SCAR=6.9
227 status => 'available'
228 },
229 {
230 rows => 1,
231 'for' => 'update',
232 order_by => { -asc => 'last_used_dt' }
233 });
234 0 $rs->update({
235 status => $reserve_id
236 });
237 0 });
238  
239 0 my $proxy = $self->gm->table('open_proxy')->find({
240 status => $reserve_id
241 });
242  
243 0 ▶ if ( $proxy ){
Branch: 1 branch
BranchTrueFalseTotal
if ($proxy) 0 0 0
244 0 $proxy->update({
245 last_used_dt => DateTime->now,
246 status => 'available'
247 });
248 }
249  
250 0 return $proxy;
251 }
252  
253 =head1 NAME
254  
255 TaskPipe::OpenProxyManager - open proxy management for TaskPipe
256  
257 =head1 DESCRIPTION
258  
259 It is not recommended to use this module directly. See the general manpages for TaskPipe
260  
261 =head1 AUTHOR
262  
263 Tom Gracey <tomgracey@gmail.com>
264  
265 =head1 COPYRIGHT AND LICENSE
266  
267 Copyright (c) Tom Gracey 2018
268  
269 TaskPipe is free software, licensed under
270  
271 The GNU Public License Version 3
272  
273 =cut
274  
275 1 __PACKAGE__->meta->make_immutable;
276 1 1;
277  
278  
279  
280  
281
« TaskPipe/InterpParam.pm ↑ Summary TaskPipe/Tool/Command_ClearTables.pm »