| 1 | package TaskPipe::OpenProxyManager::IPList; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use Web::Scraper; | ||
| 5 | use TaskPipe::UserAgentManager_ProxyNet_TOR; | ||
| 6 | use Digest::MD5 'md5_hex'; | ||
| 7 | use Log::Log4perl; | ||
| 8 | use Module::Runtime 'require_module'; | ||
| 9 | use Data::Dumper; | ||
| 10 | use Carp; | ||
| 11 | use Try::Tiny; | ||
| 12 | |||
| 13 | with 'MooseX::ConfigCascade'; | ||
| 14 | |||
| 15 | has run_info => (is => 'rw', isa => 'TaskPipe::RunInfo', default => sub{ | ||
| 16 | TaskPipe::RunInfo->new; | ||
| 17 | }); | ||
| 18 | |||
| 19 | has gm => (is => 'rw', isa => 'TaskPipe::SchemaManager'); | ||
| 20 | |||
| 21 | has list_name => (is => 'ro', isa => 'Str', default => sub{ | ||
| 22 | # I wanted to call this just 'name', but if called as such | ||
| 23 | # method calls always return undef. Moose bug? | ||
| 24 | |||
| 25 | my $self = shift; | ||
| 26 | |||
| 27 | my $name = ref($self); | ||
| 28 | my $package = __PACKAGE__; | ||
| 29 | |||
| 30 | $name =~ s/^$package//; | ||
| 31 | $name =~ s/^_//; | ||
| 32 | return $name; | ||
| 33 | }); | ||
| 34 | |||
| 35 | has settings => ( | ||
| 36 | is => 'rw', | ||
| 37 | isa => __PACKAGE__.'::Settings', | ||
| 38 | default => sub{ | ||
| 39 | my $module = __PACKAGE__.'::Settings'; | ||
| 40 | require_module( $module ); | ||
| 41 | $module->new; | ||
| 42 | } | ||
| 43 | ); | ||
| 44 | |||
| 45 | |||
| 46 | has ua_mgr => (is => 'rw', isa => 'TaskPipe::UserAgentManager', lazy => 1, builder => 'build_ua_mgr'); | ||
| 47 | |||
| 48 | sub build_ua_mgr{ | ||
| 49 | my $self = shift; | ||
| 50 | |||
| 51 | try { | ||
| 52 | |||
| 53 | my $ua_handler_mod = $self->list_settings->ua_handler_module; | ||
| 54 | require_module( $ua_handler_mod ); | ||
| 55 | my $ua_handler = $ua_handler_mod->new( | ||
| 56 | gm => $self->gm | ||
| 57 | ); | ||
| 58 | |||
| 59 | my $ua_mod = $self->list_settings->ua_mgr_module; | ||
| 60 | require_module( $ua_mod ); | ||
| 61 | my $ua_mgr = $ua_mod->new( | ||
| 62 | gm => $self->gm, | ||
| 63 | ua_handler => $ua_handler | ||
| 64 | ); | ||
| 65 | |||
| 66 | return $ua_mgr; | ||
| 67 | |||
| 68 | } catch { | ||
| 69 | |||
| 70 | confess "Error in build_ua_mgr: ".$_; | ||
| 71 | |||
| 72 | }; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | sub fetch_proxies{ | ||
| 78 | my $self = shift; | ||
| 79 | |||
| 80 | my $proxies = []; | ||
| 81 | my $found = 0; | ||
| 82 | |||
| 83 | my $tm = TaskPipe::ThreadManager->new( | ||
| 84 | gm => $self->gm, | ||
| 85 | name => "OpenProxy", | ||
| 86 | max_threads => $self->settings->max_threads, | ||
| 87 | forks => 0 | ||
| 88 | ); | ||
| 89 | |||
| 90 | my $finished = 0; | ||
| 91 | $self->ua_mgr->init; | ||
| 92 | my @page_index = @{$self->page_index}; | ||
| 93 | |||
| 94 | for my $i (0..$#page_index){ | ||
| 95 | $tm->execute( sub{ | ||
| 96 | $self->next_proxy_set( $page_index[$i] ); | ||
| 97 | }, $i + 1, $i == $#page_index ); | ||
| 98 | } | ||
| 99 | |||
| 100 | $tm->wait_children; | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | sub next_proxy_set{ | ||
| 105 | my ($self, $page_i) = @_; | ||
| 106 | |||
| 107 | my $logger = Log::Log4perl->get_logger; | ||
| 108 | $self->ua_mgr( $self->build_ua_mgr ); | ||
| 109 | $self->ua_mgr->init; | ||
| 110 | |||
| 111 | my $url = $self->url_from_page_index( $page_i ); | ||
| 112 | my $resp = $self->ua_mgr->request('get', $url ); | ||
| 113 | |||
| 114 | my $ip_ports; | ||
| 115 | if ( $resp && $resp->is_success ){ | ||
| 116 | |||
| 117 | $ip_ports = $self->get_ip_ports( $resp->decoded_content, $url ); | ||
| 118 | |||
| 119 | if ( $ip_ports && @$ip_ports ){ | ||
| 120 | $self->insert_ip_ports( $ip_ports ); | ||
| 121 | $logger->debug($self->list_name.": found ".scalar(@$ip_ports)." on page $page_i: ".Dumper( $ip_ports ) ); | ||
| 122 | } | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | my $finished = $self->is_finished( $resp, $ip_ports ); | ||
| 127 | |||
| 128 | if (! $finished && ( ! $resp || ! $resp->is_success ) ){ | ||
| 129 | $self->ua_mgr->change_ip if $self->ua_mgr->can('change_ip'); | ||
| 130 | } | ||
| 131 | |||
| 132 | $logger->debug("next_proxy_set finished: ".$finished); | ||
| 133 | return $finished; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | sub is_finished{ | ||
| 140 | my ($self,$resp,$ip_ports) = @_; | ||
| 141 | |||
| 142 | my $finished = 0; | ||
| 143 | if ( ($resp && $resp->status_line =~ /^404/ ) | ||
| 144 | || ! $ip_ports | ||
| 145 | || ! @$ip_ports | ||
| 146 | ){ | ||
| 147 | $finished = 1; | ||
| 148 | } | ||
| 149 | |||
| 150 | return $finished; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | sub page_index{ | ||
| 156 | my ($self) = @_; | ||
| 157 | |||
| 158 | my $resp; | ||
| 159 | my $count = 0; | ||
| 160 | my $page_index_url = $self->page_index_url; | ||
| 161 | do { | ||
| 162 | $resp = $self->ua_mgr->request('get', $page_index_url ); | ||
| 163 | $count++; | ||
| 164 | |||
| 165 | } while ( ! $resp->is_success && $count < $self->settings->max_retries ); | ||
| 166 | |||
| 167 | return [] unless $resp->is_success; | ||
| 168 | |||
| 169 | my $page_index = $self->scrape_page_index( $resp, $page_index_url ); | ||
| 170 | |||
| 171 | return $page_index; | ||
| 172 | |||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | |||
| 177 | sub scrape_page_index{ | ||
| 178 | my ($self,$resp,$url) = @_; | ||
| 179 | |||
| 180 | my $last_page_index = $self->last_page_index_ws->scrape( | ||
| 181 | $resp->decoded_content, | ||
| 182 | $url | ||
| 183 | ); | ||
| 184 | |||
| 185 | my $page_index = $last_page_index ? [1..$last_page_index] : []; | ||
| 186 | return $page_index; | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | sub get_ip_ports{ | ||
| 193 | my ($self, $content, $ref_url) = @_; | ||
| 194 | |||
| 195 | my $ip_ports = $self->ws->scrape( $content, $ref_url ); | ||
| 196 | |||
| 197 | return $ip_ports; | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | sub insert_ip_ports{ | ||
| 202 | my ($self,$ip_ports) = @_; | ||
| 203 | |||
| 204 | my $proxies = []; | ||
| 205 | foreach my $ip_port ( @$ip_ports ){ | ||
| 206 | $ip_port->{ip} =~ s/^\s*//; | ||
| 207 | $ip_port->{ip} =~ s/\s*$//; | ||
| 208 | $ip_port->{port} ||= $self->settings->default_port; | ||
| 209 | $ip_port->{port} =~ s/^\s*//; | ||
| 210 | $ip_port->{port} =~ s/\s*$//; | ||
| 211 | |||
| 212 | next unless $ip_port->{ip} | ||
| 213 | && $ip_port->{ip} =~ /^\d+\.\d+\.\d+\.\d+$/ | ||
| 214 | && $ip_port->{port} | ||
| 215 | && $ip_port->{port} =~ /^\d+$/; | ||
| 216 | |||
| 217 | my $open_proxy_row = $self->gm->table('open_proxy')->find_or_create({ | ||
| 218 | ip => $ip_port->{ip}, | ||
| 219 | port => $ip_port->{port}, | ||
| 220 | list_name => $self->list_name, | ||
| 221 | status => 'untested' | ||
| 222 | },{ | ||
| 223 | key => 'primary' | ||
| 224 | }); | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | =head1 NAME | ||
| 229 | |||
| 230 | TaskPipe::OpenProxyManager::IPList - the base class for IPLists | ||
| 231 | |||
| 232 | =head1 DESCRIPTION | ||
| 233 | |||
| 234 | TaskPipe's Open Proxy system grabs Lists of open proxy IPs that are available on the internet. Inherit from this class to create a new list. The format of the inherited package is normally: | ||
| 235 | |||
| 236 | package TaskPipe::OpenProxyManager::IPList_NewList; | ||
| 237 | use Moose; | ||
| 238 | use Web::Scraper | ||
| 239 | extends 'TaskPipe::OpenProxyManager::IPList'; | ||
| 240 | |||
| 241 | has page_index_ws => (is => 'ro', isa => 'Web::Scraper', default => sub{ | ||
| 242 | scraper => { | ||
| 243 | |||
| 244 | # define scraper to scrape page index of pages | ||
| 245 | # which have the ips on | ||
| 246 | |||
| 247 | } | ||
| 248 | }); | ||
| 249 | |||
| 250 | has ws => (is => 'ro', isa => 'Web::Scraper', default => sub{ | ||
| 251 | scraper => { | ||
| 252 | |||
| 253 | # define scraper to pull the ips from each list page | ||
| 254 | # (return an arrayref) | ||
| 255 | |||
| 256 | } | ||
| 257 | }); | ||
| 258 | |||
| 259 | sub page_index_url{ | ||
| 260 | my ($self) = @_; | ||
| 261 | |||
| 262 | # return the url of the page index | ||
| 263 | # (define it in a ::Settings package?) | ||
| 264 | |||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | sub url_from_page_index{ | ||
| 269 | my ($self,$index_value) = @_; | ||
| 270 | |||
| 271 | # turn index_value into the url for | ||
| 272 | # the list page and return it | ||
| 273 | |||
| 274 | }); | ||
| 275 | |||
| 276 | ; | ||
| 277 | |||
| 278 | See the other C<TaskPipe::IPList_> packages for examples | ||
| 279 | |||
| 280 | =cut | ||
| 281 | |||
| 282 | |||
| 283 | 1; |