Saturday, July 18, 2009

Perl: Using LWP with a SOCKS Proxy

For this example, we'll use LWP to connect through a SOCKS proxy to the Tor anonymizing network. Under Ubuntu/Debian, installation of Tor is as simple as:
sudo apt-get install tor
Next, install the LWP::Protocol::socks Perl module to add support for the "socks" scheme. Installing from CPAN:
sudo cpan LWP::Protocol::socks
You should now be able to use LWP with Tor:

use strict;
use LWP::UserAgent;

my $ua = LWP::UserAgent->new(
agent => q{Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; YPC 3.2.0; .NET
CLR 1.1.4322)},
$ua->proxy([qw/ http https /] => 'socks://localhost:9050'); # Tor proxy
my $rsp = $ua->get('');
print $rsp->content;


BrendanM said...


Thanks for this great example. Is it possible to create a proxy chain, ie server -> Tor -> HTTP proxy within the PERL LWP structure? Or, could you set something up at the command line that sends a regular request to an HTTP proxy from LWP through TOR on one's server?

mla said...

@Brendan: Hmmm. I'm not sure. You could write a custom HTTP proxy that forwards all requests through Tor. Is that what you're looking for?

IIRC there's a Perl module to set up a simple HTTP proxy in just a few lines. You could probably customize it to use Tor.

Anonymous said...

I couldn't get that script to work.
Script says:
"501 Protocol scheme 'socks' is not supported".

And Tor log file says:
"Dec 09 13:20:08.007 [warn] Socks version 71 not recognized. (Tor is not an http proxy.)".


mla said...

@anon: It sounds like the LWP::Protocol::socks module isn't installed or isn't being found.

Did it look like it installed fine? You could try requiring the module directly and see if it finds it.

Unknown said...

Just wanted to say thanks for this, great instructions, worked fine.

Anonymous said...

thank you! this worked fine

Anonymous said...

ah man. this is just what i was looking for thank you!

Anonymous said...

Great job! Thank you very much!!:)