Controlling Tor client with Ruby
Please take a look at Tor control protocol. You can control circuits using telnet.
http://thesprawl.org/memdump/?entry=8
To switch to a new circuit wich switches to a new endpoint:
require 'net/telnet'
def switch_endpoint
localhost = Net::Telnet::new("Host" => "localhost", "Port" => "9051", "Timeout" => 10, "Prompt" => /250 OK\n/)
localhost.cmd('AUTHENTICATE ""') { |c| print c; throw "Cannot authenticate to Tor" if c != "250 OK\n" }
localhost.cmd('signal NEWNYM') { |c| print c; throw "Cannot switch Tor to new route" if c != "250 OK\n" }
localhost.close
end
Be aware of the delay to make a new circuit, may take couple seconds, so you'd better add a delay in the code, or check if your address has changed by calling some remote IP detection site.
Get new identity in Tor from Ruby script
You have to put your password in double quotes:
localhost.cmd('AUTHENTICATE "hi"') { |c| print c; throw "Cannot authenticate to Tor" if c != "250 OK\n" }
That is, the password is that you hashed with tor --hash-password hi
, but put in double quotes.
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
Not able to access page data, using anemone with socksify gem and Tor
There are a number of problems that could be causing this to happen. First, if ntp is not running on your machine, and the time is off by even a little bit, you will not be able do use the socks server to do anything complicated. This happened to me. You need to install ntp and make sure it has synced before doing anything.
Second, you may find that a lot of this commands like socksify are obsolete. The best way I have found to make sure that everything happens through the socks port without dns leakage is by using curl, which has bindings for many languages. You can carefully watch the traffic with tcpdump to make sure it isn't leaking, and it is watertight in my experience.
I'd also suggest that you look at torsocks, which has recently been updated by dgoulet on github. This replaces tsocks, which the outdated socksify_ruby is based on.
Finally, hidden services have been under great strain lately, because a bot has decided to start up a few million Tor clients. Make sure you can connect with the Tor Browser Bundle, assuming the project you are working on is trying to crawl hidden service.
You didn't actually say that this project involves Tor or hidden services, but you did tag it with Tor.
Can a non-TOR client access a TOR web service?
The Absolute answer to your question is no! all the hidden service of TOR is accessible through the TOR network. Access a hidden service is completely different from accessing a web service through INTERNET. It goes through rendezvous and introduction points of the TOR network.
Related Topics
Gem Install Mongrel Fails with Ruby 1.9.1
Activeadmin Forbiddenattributeserror
Attempting to Install Libv8, "Failed to Build Gem Native Extension"
Rendering a JSON Object of a Join-Model and Its Associated Models
Ruby Array to Hash: Each Element the Key and Derive Value from It
Is There a Ruby One-Line "Return If X"
Begin Rescue Not Catching Error
Rake Db:Migration Not Working on Travis-Ci Build
Postgres Range Fields + Rails Forms
Pretty Print to a File in Ruby
Referencing Model with String Input
Rails - Paperclip Validating Attachment Size When It Shouldn't Be
Rails Custom Validation - Only One Record Can Be True
Programmatically Getting Full Ruby Version