Ruby, Tor and Net::HTTP::Proxy

Chthonic Project picture Chthonic Project · Nov 13, 2012 · Viewed 7.5k times · Source

My apologies in advance if this is a noobish doubt: I want to use a proxy in my Ruby code to fetch a few web pages. And I want to be sneaky about it! So I am using Tor.

I have Tor running, and I am able to use Net::HTTP.get(uri) as usual. But I can't figure out how to use Net::HTTP::Proxy to fetch the uri. I also can't figure out how using Tor will help make my fetches anonymous.

Any help is greatly appreciated. Please don't just add a link to the ruby-doc page for Net::HTTP::Proxy. If I had understood that, I would not be asking this here :-)


Another easy way to do this is using SOCKSify, but in this case, I receive the following error:

/usr/lib/ruby/gems/1.9.2-p290/gems/socksify-1.5.0/lib/socksify.rb:189:in 'socks_authenticate': SOCKS version not supported (SOCKSError)

I have never done any network programming before. Any guidance about this will also be very helpful. Thanks :-)

Answer

A.D. picture A.D. · Dec 14, 2012

You are using HTTP proxy class, so you must provide IP of HTTP proxy. Tor Browser has not HTTP proxy bundled.

So you can either install some proxy software e.g. Privoxy and configure it to use Tor's SOCKS:

In config.txt forward-socks4a / 127.0.0.1:9050 .

then use Privoxy's default listen-address in your script:

proxy = Net::HTTP::Proxy('127.0.0.1',8118)

or use SOCKSify. According to docs:

require 'socksify/http'
uri = URI.parse('http://rubyforge.org/')
Net::HTTP.SOCKSProxy('127.0.0.1', 9050).start(uri.host, uri.port) do |http|
  http.get(uri.path)
end

No need for additional software..

Third solution is to use SOCKSify as follows:

$ socksify_ruby localhost 9050 script.rb

which redirect all TCP connections of a Ruby script, which means you don't need to use any Proxy code at all.

For clarification you have to understand that 127.0.0.1:9050 is Tor's SOCKS address and 127.0.0.1:8118 is address of Privoxy.