I installed tor
on a ubuntu-server with the command
sudo apt-get install tor
and I also installed socks:
sudo cpan LWP::Protocol::socks
After this i wrote a perl script to read websites using the tor network, which works very well (simplified example):
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new('agent' => 'any user agent string'},
);
$ua->proxy([qw/ http https /] => 'socks://localhost:9050'); # Tor proxy
$ua->cookie_jar({});
my $response = $ua->get('http://www.example.com/');
print $response->content;
As said before, this works very well, but there are some servers who block some tor exit nodes. Instead of sending the expected content with http-status 200 (ok)
they send the status 403 (forbidden)
. But some minutes later the same server sends 200 (ok)
together with the expected content, so I'm pretty sure that the problem are exit nodes who's IP addresses are blacklisted.
I know, that tor
itself changes the route (and so the exit node) as far as I know every 10 minutes. But in the case, that a server sends 403
because it has made bad experiences with the concrete exit node that I'm using in this moment, I don't want to wait 10 minutes to get another exit node that hopefully is not blocked.
I want to change my perl script into something like this:
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new('agent' => 'any user agent string'},
);
$ua->proxy([qw/ http https /] => 'socks://localhost:9050'); # Tor proxy
$ua->cookie_jar({});
my $code = 403;
my $content = '';
while ($code > 399) {
my $response = $ua->get('http://www.example.com/');
$code = $response->code();
if ($code > 399) {
#####################################
# TELL TOR TO USE A NEW EXIT NODE #
#####################################
} else {
$content = $response->content;
}
}
print $content;
Note, that this example is simplified: Not all http-status above 400 are usefull to change the route, and there are also some other reasons, that make it necessary to tell tor
to use a new exit node.
My question is:
a) How can a perl script tell tor
to change its route?
b) What other possibilities are there to control tor
from a perl script?