Skip to content

Instantly share code, notes, and snippets.

@bonsaiviking
Created April 25, 2012 15:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bonsaiviking/2490691 to your computer and use it in GitHub Desktop.
Save bonsaiviking/2490691 to your computer and use it in GitHub Desktop.
Request a new identity from Tor via web request (suggest to make a bookmark)
#!/usr/bin/perl
use strict;
use warnings;
use HTTP::Daemon;
use IO::Socket;
my $torport=9051;
my $password="footor";
my $good = HTTP::Response->new(
200,
"OK",
[],
q{<html><head><title>New Identity</title></head>
<body><h1>New Identity Granted</h1></body></html>}
);
my $MAXNEWNYMTIME=10;
my $toofast = HTTP::Response->new(
200,
"OK",
[],
q{<html><head><title>Too Fast</title></head>
<body><h1>Too soon to request New Identity</h1>
<p>New identity will be granted automatically within 10 seconds</p>
</body></html>}
);
my $httpd = HTTP::Daemon->new(
#LocalAddr => '192.168.100.80',
LocalPort => 8080,
);
my $lastime = time;
while (my $conn = $httpd->accept)
{
my $thisresponse = \$good;
REQUEST:
{
my $req = $conn->get_request;
if ($req->method eq 'GET'
and $req->uri->path eq "/newnym")
{
if ((time - $lastime) < $MAXNEWNYMTIME)
{
$thisresponse = \$toofast;
}
my $tor = IO::Socket::INET->new(
PeerAddress => '127.0.0.1',
PeerPort => $torport,
) or die "Problem making socket: $!";
print $tor 'AUTHENTICATE "',$torpass,'"';
my $resp = <$tor>;
unless ($resp =~ /^250/)
{
$conn->send_error(500);
last REQUEST;
}
print $tor 'SIGNAL NEWNYM';
unless ($resp =~ /^250/)
{
$conn->send_error(500);
last REQUEST;
}
$conn->send_response($$thisresponse);
$tor->close;
}
else {
$conn->send_error()
}
}
$conn->close;
undef $conn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment