geekondemand has asked for the wisdom of the Perl Monks concerning the following question:

I am looking for a way to tell the LWP modules (specifically LWP::UserAgent, but if there's a solution it's probably not specific to that) to tunnel through a SOCKS proxy or a chain of SOCKS proxies. I cannot find any documentation about this in either the SOCKS or LWP manual pages.

Replies are listed 'Best First'.
Re: LWP Request Through SOCKS Proxy
by gam3 (Curate) on Apr 17, 2005 at 04:16 UTC
    I know nothing about SOCKS, but, since no one seems to want to answer this, I'll give it a shot.

    Can you get Net::SOCKS to run. You should be able to modify the code below to do a get from the SOCKS proxy. If you can get this to work, I don't think that it will be hard to make LWP work with it.

    use Net::SOCKS; my $sock = new Net::SOCKS(socks_addr => '192.168.1.3', socks_port => 1080, user_id => 'the_user', user_password => 'the_password', force_nonanonymous => 1, protocol_version => 5); # connect to http port and request /index.html my $f= $sock->connect(peer_addr => '192.168.1.3', peer_port => 80); print $f "GET /index.html\n"; # example writing to socket while (<$f>) { print } # example reading from socket $sock->close();
    -- gam3
    A picture is worth a thousand words, but takes 200K.
      See this example: http://mlawire.blogspot.com/2009/07/using-lwp-with-tor.html
Re: LWP Request Through SOCKS Proxy
by salva (Canon) on Apr 17, 2005 at 09:43 UTC
    everything makes me think that you can't

    libwww (the C library that LWP wraps) has some support for SOCKS proxies, but it seems that this functionality is not available from perl.

    I can see two work arounds:

    1) the difficult one: go hacking inside LWP and make the SOCKS support available in perl. Don't forget to submmit a patch to the maintainer ;-)

    2) the no so difficult one: code an standalone HTTP proxy => SOCKS gateway. Maybe you can do it combining HTTP::Proxy and Net::SOCKS or IO::Socket::Socks.

    You can also check LWP alternatives like curl or neon, both have perl wrappers, though I doubt they can support SOCKS either.

      You can just open a local socket and have it connect to the SOCKS server. So you give LWP an address like http://localhost:8080/. Update: added code sample
      -- gam3
      A picture is worth a thousand words, but takes 200K.
        What do you mean?

        You need to talk using the SOCKS protocol with the SOCKS server, so you need a gateway between LWP (than can only talk to HTTP proxies) and the SOCKS server

        (after the update) I see, but this aproach is going to fail in all but the simplest cases: if the remote server runs several virtual servers, if it includes absolute URLs in some response or in redirects, etc.