| 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
| |||||||||||
| 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 | # my ($self,$proxy) = @_; | ||||||||||
| 101 | |||||||||||
| 102 | # my $logger = Log::Log4perl->get_logger; | ||||||||||
| 103 | |||||||||||
| 104 | # my $uri = $self->settings->proxy_scheme.'://'.$proxy->ip.':'.$proxy->port; | ||||||||||
| 105 | # $logger->debug("Open proxy checker: Testing proxy $uri"); | ||||||||||
| 106 | |||||||||||
| 107 | # my $ua_mgr = TaskPipe::UserAgentManager->new( | ||||||||||
| 108 | # gm => $self->gm | ||||||||||
| 109 | # ); | ||||||||||
| 110 | |||||||||||
| 111 | # $ua_mgr->ua_handler->call( 'proxy', $self->settings->protocols => $uri ); | ||||||||||
| 112 | |||||||||||
| 113 | # my $ip = $ua_mgr->check_ip; | ||||||||||
| 114 | # $logger->debug("ip [$ip] my ip [".$self->my_ip."]") if $ip; | ||||||||||
| 115 | |||||||||||
| 116 | # my $success = 0; | ||||||||||
| 117 | # if ( $ip ){ | ||||||||||
| 118 | # $logger->debug("Successful response from proxy $uri"); | ||||||||||
| 119 | # | ||||||||||
| 120 | # if ( $ip ne $self->my_ip ){ | ||||||||||
| 121 | # $logger->debug("Proxy $uri does not project originating IP - test successful"); | ||||||||||
| 122 | # $success = 1; | ||||||||||
| 123 | # } else { | ||||||||||
| 124 | # $logger->debug("Proxy $uri seems to project originating IP - test failed"); | ||||||||||
| 125 | # } | ||||||||||
| 126 | # } | ||||||||||
| 127 | |||||||||||
| 128 | # my $status = $success ? 'available' : 'dud'; | ||||||||||
| 129 | |||||||||||
| 130 | # my $dt = DateTime->now; | ||||||||||
| 131 | |||||||||||
| 132 | # $proxy->update({ | ||||||||||
| 133 | # checked_dt => $dt, | ||||||||||
| 134 | # status => $status | ||||||||||
| 135 | # }); | ||||||||||
| 136 | |||||||||||
| 137 | # return $success; | ||||||||||
| 138 | |||||||||||
| 139 | #} | ||||||||||
| 140 | |||||||||||
| 141 | |||||||||||
| 142 | sub test_proxy{ | ||||||||||
| 143 | 0 | ▶ | my ($self,$proxy) = @_; | ||||||||
|
Subroutine
test_proxy: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 144 | |||||||||||
| 145 | 0 | my $logger = Log::Log4perl->get_logger; | |||||||||
| 146 | |||||||||||
| 147 | 0 | my $uri = $proxy->{scheme}.'://'.$proxy->{ip}.':'.$proxy->{port}; | |||||||||
| 148 | 0 | $logger->info("Open proxy checker: Testing proxy $uri"); | |||||||||
| 149 | |||||||||||
| 150 | 0 | my $ua_mgr = TaskPipe::UserAgentManager->new( | |||||||||
| 151 | gm => $self->gm | ||||||||||
| 152 | ); | ||||||||||
| 153 | |||||||||||
| 154 | 0 | $ua_mgr->ua_handler->call( 'proxy', $self->settings->protocols => $uri ); | |||||||||
| 155 | |||||||||||
| 156 | 0 | my $ip = $ua_mgr->check_ip; | |||||||||
| 157 | 0 | ▶ | $logger->debug("ip [$ip] my ip [".$self->my_ip."]") if $ip; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | |||||||||||
| 159 | 0 | my $success = 0; | |||||||||
| 160 | 0 | ▶ | if ( $ip ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | 0 | $logger->info("Successful response from proxy $uri"); | |||||||||
| 162 | |||||||||||
| 163 | 0 | ▶ | if ( $ip ne $self->my_ip ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 164 | 0 | $logger->info("Proxy $uri does not project originating IP - test successful"); | |||||||||
| 165 | 0 | $success = 1; | |||||||||
| 166 | } else { | ||||||||||
| 167 | 0 | $logger->info("Proxy $uri seems to project originating IP - test failed"); | |||||||||
| 168 | } | ||||||||||
| 169 | } else { | ||||||||||
| 170 | 0 | $logger->info("Could not proxy using $uri"); | |||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | # my $status = $success ? 'available' : 'dud'; | ||||||||||
| 174 | |||||||||||
| 175 | # my $dt = DateTime->now; | ||||||||||
| 176 | |||||||||||
| 177 | # $proxy->update({ | ||||||||||
| 178 | # checked_dt => $dt, | ||||||||||
| 179 | # status => $status | ||||||||||
| 180 | # }); | ||||||||||
| 181 | |||||||||||
| 182 | 0 | return $success; | |||||||||
| 183 | |||||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | |||||||||||
| 187 | |||||||||||
| 188 | sub test_proxies{ | ||||||||||
| 189 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
test_proxies: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 190 | |||||||||||
| 191 | 0 | my $logger = Log::Log4perl->get_logger; | |||||||||
| 192 | |||||||||||
| 193 | 0 | my $dt = DateTime->now->subtract( minutes => 10 ); | |||||||||
| 194 | 0 | my $dud_dt = DateTime->now->subtract( days => +$self->settings->clean_dud_proxies_after ); | |||||||||
| 195 | |||||||||||
| 196 | 0 | my $dtp = $self->gm->schema->storage->datetime_parser; | |||||||||
| 197 | |||||||||||
| 198 | 0 | $self->gm->table('open_proxy')->search({ | |||||||||
| 199 | checked_dt => { '<', +$dtp->format_datetime( $dud_dt ) }, | ||||||||||
| 200 | status => 'dud' | ||||||||||
| 201 | })->delete_all; | ||||||||||
| 202 | |||||||||||
| 203 | 0 | my $proxy_rs = $self->gm->table('open_proxy')->search([{ | |||||||||
| 204 | checked_dt => undef | ||||||||||
| 205 | }, { | ||||||||||
| 206 | checked_dt => { '<', $dtp->format_datetime( $dt ) }, | ||||||||||
| 207 | status => { '!=', 'dud' } | ||||||||||
| 208 | }], { | ||||||||||
| 209 | order_by => { -asc => 'checked_dt' } | ||||||||||
| 210 | }); | ||||||||||
| 211 | |||||||||||
| 212 | 0 | my $tm = TaskPipe::ThreadManager->new( | |||||||||
| 213 | gm => $self->gm, | ||||||||||
| 214 | name => "OpenProxy", | ||||||||||
| 215 | max_threads => $self->settings->max_threads, | ||||||||||
| 216 | forks => 0 | ||||||||||
| 217 | ); | ||||||||||
| 218 | |||||||||||
| 219 | 0 | $tm->init; | |||||||||
| 220 | |||||||||||
| 221 | 0 | my $count = 0; | |||||||||
| 222 | 0 | my $total = $proxy_rs->count; | |||||||||
| 223 | |||||||||||
| 224 | 0 | ▶ | while( my $proxy = $proxy_rs->next ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 225 | 0 | $count++; | |||||||||
| 226 | |||||||||||
| 227 | $tm->execute( sub{ | ||||||||||
| 228 | #my ($thread_id) = @_; | ||||||||||
| 229 | 0 | ▶ | $self->test_proxy( | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 230 | #$thread_id, | ||||||||||
| 231 | $proxy | ||||||||||
| 232 | ); | ||||||||||
| 233 | }, | ||||||||||
| 234 | 0 | $count, | |||||||||
| 235 | $count == $total | ||||||||||
| 236 | ); | ||||||||||
| 237 | } | ||||||||||
| 238 | |||||||||||
| 239 | 0 | $tm->wait_children; | |||||||||
| 240 | 0 | $tm->finalize; | |||||||||
| 241 | } | ||||||||||
| 242 | |||||||||||
| 243 | |||||||||||
| 244 | sub next_available_proxy{ | ||||||||||
| 245 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
next_available_proxy: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 246 | |||||||||||
| 247 | 0 | my $logger = Log::Log4perl->get_logger; | |||||||||
| 248 | 0 | my $proxy = $self->next_available_proxy_attempt; | |||||||||
| 249 | |||||||||||
| 250 | 0 | my $fails = 0; | |||||||||
| 251 | 0 | ▶ | while ( ! $proxy ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 252 | 0 | $fails++; | |||||||||
| 253 | 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
| |||||||||||
| 254 | 0 | $logger->warn("No proxy appears to be available. Waiting ".$self->settings->unavailable_poll_interval." seconds"); | |||||||||
| 255 | 0 | sleep( $self->settings->unavailable_poll_interval ); | |||||||||
| 256 | 0 | $proxy = $self->next_available_proxy_attempt; | |||||||||
| 257 | } | ||||||||||
| 258 | |||||||||||
| 259 | 0 | return $proxy; | |||||||||
| 260 | } | ||||||||||
| 261 | |||||||||||
| 262 | |||||||||||
| 263 | |||||||||||
| 264 | |||||||||||
| 265 | sub next_available_proxy_attempt{ | ||||||||||
| 266 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
next_available_proxy_attempt: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 267 | |||||||||||
| 268 | 0 | my $reserve_id = "reserved-".md5_base64( $$ * rand ); | |||||||||
| 269 | |||||||||||
| 270 | $self->gm->schema->txn_do( sub{ | ||||||||||
| 271 | 0 | ▶ | my $rs = $self->gm->table('open_proxy')->search({ | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 272 | status => 'available' | ||||||||||
| 273 | }, | ||||||||||
| 274 | { | ||||||||||
| 275 | rows => 1, | ||||||||||
| 276 | 'for' => 'update', | ||||||||||
| 277 | order_by => { -asc => 'last_used_dt' } | ||||||||||
| 278 | }); | ||||||||||
| 279 | 0 | $rs->update({ | |||||||||
| 280 | status => $reserve_id | ||||||||||
| 281 | }); | ||||||||||
| 282 | 0 | }); | |||||||||
| 283 | |||||||||||
| 284 | 0 | my $proxy = $self->gm->table('open_proxy')->find({ | |||||||||
| 285 | status => $reserve_id | ||||||||||
| 286 | }); | ||||||||||
| 287 | |||||||||||
| 288 | 0 | ▶ | if ( $proxy ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 289 | 0 | $proxy->update({ | |||||||||
| 290 | last_used_dt => DateTime->now, | ||||||||||
| 291 | status => 'available' | ||||||||||
| 292 | }); | ||||||||||
| 293 | } | ||||||||||
| 294 | |||||||||||
| 295 | 0 | return $proxy; | |||||||||
| 296 | } | ||||||||||
| 297 | |||||||||||
| 298 | =head1 NAME | ||||||||||
| 299 | |||||||||||
| 300 | TaskPipe::OpenProxyManager - open proxy management for TaskPipe | ||||||||||
| 301 | |||||||||||
| 302 | =head1 DESCRIPTION | ||||||||||
| 303 | |||||||||||
| 304 | It is not recommended to use this module directly. See the general manpages for TaskPipe | ||||||||||
| 305 | |||||||||||
| 306 | =head1 AUTHOR | ||||||||||
| 307 | |||||||||||
| 308 | Tom Gracey <tomgracey@gmail.com> | ||||||||||
| 309 | |||||||||||
| 310 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 311 | |||||||||||
| 312 | Copyright (c) Tom Gracey 2018 | ||||||||||
| 313 | |||||||||||
| 314 | TaskPipe is free software, licensed under | ||||||||||
| 315 | |||||||||||
| 316 | The GNU Public License Version 3 | ||||||||||
| 317 | |||||||||||
| 318 | =cut | ||||||||||
| 319 | |||||||||||
| 320 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 321 | 1 | 1; | |||||||||
| 322 | |||||||||||
| 323 | |||||||||||
| 324 | |||||||||||
| 325 | |||||||||||
| 326 | |||||||||||