Connect to Tor network with ruby
I think it's like specifying proxy server for your HTTP connection. I don't know how it works in Ruby. But it will not be different from configuring browsers. Just set proxy server setting to 127.0.0.1:8118
.
Ruby, Tor and Net::HTTP::Proxy
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.
How to Build and Send an HTTP Request to a Tor Hidden Service with Ruby
Curl only uses a proxy if you set it up in your "curl"-block.
For example:
c = Curl::Easy.new() do |curl|
curl.proxy_tunnel = true
curl.proxy_type = Curl::CURLPROXY_SOCKS5 # also available and default Curl::CURLPROXY_HTTP
curl.proxy_url = '127.0.0.1:9050' # local tor client/proxy
curl.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0" #the user agent string from the most recent version of the tor browser
curl.verbose = true
curl.url = url # your example url
curl.perform
curl.inspect
end
Unfortunately curl does not use the proxy for hostname resolution. In other words, I did't find a way to force curl to use the proxy for hostname resolution.
But you can try
#enable socksify debug
Socksify::debug = true
#own try via direct use of socksify and Net::HTTP
uri = URI.parse('http://am4wuhz3zifexz5u.onion/') #a known, functioning hidden service
# some debug stuff - just ignore ;-)
puts uri
puts uri.host
puts uri.port
puts uri.path
res1 = Net::HTTP.SOCKSProxy('127.0.0.1', 9050).start(uri.host, uri.port) do |http|
http.get(uri.path)
end
Most anonymous way to crawl a search engine
If you want to be anonymous on Internet, one of the best choice is tor. You can use it by yourself and access to any website
First you have to connect to the tor network
tor --SOCKSPort 9050
Then you can use this gem: https://github.com/astro/socksify-ruby
And access any website anonymously:
require 'socksify/http'
require 'net/http'
Net::HTTP.SOCKSProxy('127.0.0.1', 9050).start('some.website.com', 80) do |http|
http.get('/')
end
Please read tor documentation if you need to be anonymous for security reasons, it is complete and important if you want to use it correctly.
The target website can easily know the traffic is comming from tor network (but it won't known where the traffic come from), be reasonable while crawling, you don't want webmaster to block tor...
Related Topics
How to Create Temp Dir in Ruby
Dynamically Create Class Attributes with Attr_Accessor
How to Determine Leap Year in Ruby
Cross-Platform Means of Getting User's Home Directory in Ruby
Run Code Only If Script Called from the Command Line
Run a Ruby Library from the Command-Line
How to Fix Libv8 Error from Gemfile on Mavericks
Change HTML Form Id Generated by Form_For Rails 3.1
Why Does Date Exist in Ruby Before It Is Required
How to Remove All Elements That Satisfy a Condition in Array in Ruby
Rails Redirect_To :Back Not Working
Automatically Precompile Assets Before Pushing to Heroku