You may have seen my other question related to piping a Perl script designed to connect to a webpage through Tor. I've decided that this was too much of a hassle and I now need a short tutorial on how to build a proxy in a Perl script that will allow me to connect to hidden services and (later) download files from the server.
2 Answers
LWP::UserAgent can it, if LWP::Protocol::socks and Tor are installed. LWP::Protocol::socks is available as liblwp-protocol-socks-perl software package at least in the repositories of Debian and Ubuntu; when LWP::Protocol::socks is already installed, you can forget its name and just call LWP::UserAgent.
I had a similar question a week ago, and I written the script testtor.perl:
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
my $url;
if ($ARGV[0]) {
$url = $ARGV[0];
}
else {
exit;
}
my $ua = new LWP::UserAgent(agent => 'perl');
$ua -> proxy([qw(http https)] => 'socks://127.0.0.1:9050');
my $response = $ua -> get($url);
my $page = $response -> decoded_content;
print "$page\n";
It is not a CGI script; so, it does not print HTTP headers. It is for calling it from another script or terminal. E.g., the line in terminal
./testtor.perl https://www.iplocation.net/find-ip-address | lynx -stdin
will open in Lynx a page with an IP address of some Tor exit relay. And
./testtor.perl http://podger42hd4zi2oc.onion/ | lynx -stdin
works, too.
-
Thank you! Your code didn't originally work, but I made some modifications and it now works :) Now I need to pick a hidden service to try it with....– NoNameCommented Aug 7, 2018 at 2:45
-
Is there a way to find specific files on the server, not just the mainpage?– NoNameCommented Aug 7, 2018 at 2:48
-
@NoName, what line in my script was not working at your computer? Have you installed LWP::Protocol::socks and Tor? About your question, have you tried locate? Commented Aug 7, 2018 at 13:11
-
@NoName, I am sorry to say it, but it looks like you have read my answer inattentively. The script, as I written it, needs one command line argument (when a Perl programme is called with arguments, they all are available as the
@ARGV
array; e.g., if you called/path/to/script foo bar
then $ARGV[0] = 'foo' and $ARGV[1] = 'bar'). If you call./testtor.perl https://check.torproject.org/ | lynx -stdin
(just using my script in its original form), you will see "Congratulations. This browser is configured to use Tor" etc. Commented Aug 7, 2018 at 21:05 -
I apologize for that. I'm new to code, and I suppose I don't have enough insight yet to arbitrarily criticize code errors. To clarify, the original intent of the question was not to use the browser, but simply use CLI to fetch pages. We may have misunderstood each other :)– NoNameCommented Aug 8, 2018 at 1:48
You need IO::Socket::Socks module. It can be used as SOCKS5 client, point it to running tor as socks server. Usually runs on ports 9050 or 9150.