Why is Ruby failing to connect to my Tor network?
Asked Answered
C

1

9

I’m using Ruby on Rails 4.2.7 on Mac El Capitan and just installed the Tor browser (v 6.0.4). I fired up my Tor browser, have verified its running by viewing a couple of web pages, but using this gem — https://github.com/dryruby/tor.rb , when I run my script, Ruby doesn’t believe Tor is running

    require 'tor'
    ...
    puts "avaailble: #{Tor.available?}"
    puts "version: #{Tor.version}"

Returns

avaailble: false
version:

Indeed, when I try and make a Tor request using the https://github.com/brunogh/tor_requests gem, the web page request returns immediately, leading me to believe the Tor network isn’t being used because in a Tor browser it takes much longer (here is the code I’m using to amen a web page request) …

    uri = URI.parse(url)
    Net::HTTP.SOCKSProxy('127.0.0.1', 9150).start(uri.host, uri.port) do |http|
      f = http.get(uri.path)
    end

How do I make my Ruby/Rails code connect to my locally running Tor network?

Edit: In respnse to the answer given, here is what I set my PATH and DYLD_LIBRARY_PATH variables to …

localhost:myproject davea$ echo $PATH
/usr/local/opt/coreutils/libexec/gnubin:/opt/local/bin:/opt/local/sbin:/Users/davea/.rvm/gems/ruby-2.3.0/bin:/Users/davea/.rvm/gems/ruby-2.3.0@global/bin:/Users/davea/.rvm/rubies/ruby-2.3.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin/:/opt/gradle-2.7/bin:/opt/apache-maven-3.3.3/bin:/Users/    davea/.rvm/bin:/usr/local/mysql/bin:/Applications/TorBrowser.app/Contents/MacOS/Tor:/Users/davea/.rvm/bin:/usr/local/mysql/bin:/Applications/TorBrowser.app/Contents/MacOS/Tor
localhost:myproject davea$ echo $DYLD_LIBRARY_PATH
/Applications/TorBrowser.app/Contents/MacOS/Tor:/usr/local/mysql/lib:/usr/local/mysql/lib:

and here is ht output in my Rails console trying the commands listed …

localhost:myproject davea$ rails console
Running via Spring preloader in process 49987
Loading development environment (Rails 4.2.7.1)
2.3.0 :001 > 
2.3.0 :002 >   Tor::Config::CONFDIR = '/Applications/TorBrowser.app//Contents/MacOS/Tor'
(irb):2: warning: already initialized constant Tor::Config::CONFDIR
/Users/davea/.rvm/gems/ruby-2.3.0/gems/tor-0.1.2/lib/tor/config.rb:21: warning: previous definition of CONFDIR was here
 => "/Applications/TorBrowser.app//Contents/MacOS/Tor" 
2.3.0 :003 > Tor.available?
Carolincarolina answered 11/9, 2016 at 16:52 Comment(4)
That module probably doesn't pick up on the Tor Browser's instance of Tor because it runs it's own daemon separate with different settings from what normal Tor instances use. If you call http.get to a site like ipchicken.com is it printing your actual IP or a Tor one?Minni
The first Tor module is also failing to detect whether it's available or the version because it doesn't know how to locate the Tor browser's tor binary since it isn't in your path.Minni
I'm confused about what to put in the path. THe Tor application is installed in my /Applications/TorBrowser.app/ directory. Is that what I'm adding to the PATH?Carolincarolina
The path would need to be the path where the tor binary lives, which I think is deeper inside TorBrowser.app than that (I'm only familiar with the structure on Linux and Windows where it's in 'Browser/TorBrowser/Tor/'. You'll also need to (for tor_requests) override it's default config because it's set to use port 9050, not 9150 which Tor Browser uses.Minni
M
2

Here is how you can make brunogh/tor_requests work with Tor Browser (easy):

require 'tor_requests'

Tor.configure do |config|
    config.ip = "127.0.0.1"
    config.port = "9150"
    config.add_header('User-Agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0')
end

res = Tor::HTTP.get(URI('https://drew-phillips.com/ip-info/'))
p res.code
p res.body

To get dryruby/tor working involved a bit more work:

It depends on your ENV PATH variable to find the Tor binary and Tor browser has some libraries (at least on Linux) within it's path that aren't found if you try to execute it directly. Seems this should support allowing you to add the path in code instead of relying on PATH in my opinion.

Trying to run Tor Browser's tor binary from the console yields (more on this later, may not apply to Mac):

tor: symbol lookup error: tor-browser_en-US/Browser/TorBrowser/Tor/tor: undefined 
symbol: evutil_secure_rng_set_urandom_device_file

Also, installing the Gem from source doesn't give us the latest version available on GitHub and there appears to be a fix to the version method that isn't included with the Gem version 0.1.2. Because of this I pulled the source and tell the program to load the Gem from a custom path.

The working code:

require 'rubygems'
$:.unshift "./tor/lib"
require 'tor'

Tor::Config::CONFDIR = '/home/me/tor-browser_en-US/Browser/TorBrowser/Data/Tor'

p Tor::Config::CONFDIR
p Tor.available?
p Tor.version

Now, in order to have it run successfully, you'll need to set your PATH and LD_LIBRARY_PATH (on Mac this is DYLD_LIBRARY_PATH I believe).

So I run the Ruby code like this:

PATH=/home/me/tor-browser_en-US/Browser/TorBrowser/Tor:$PATH \
LD_LIBRARY_PATH=/home/me/tor-browser_en-US/Browser/TorBrowser/Tor:$LD_LIBRARY_PATH \
ruby tor.rb

This puts Tor Browser as the first search path for binaries and libraries.

Then with this I was able to get the following output:

true
"0.2.8.6"

Hope that helps!

Minni answered 14/9, 2016 at 19:35 Comment(8)
I'm not using "dryruby/tor" so I assume I should only follow your advice above the line "To get dryruby/tor working"?Carolincarolina
Maybe, in your OP you posted code using dryruby/tor (gem tor). If you don't need to call available? or version then most of that stuff is unnecessary. My other answer about talking to the controller doesn't require any of that path stuff.Minni
Perhaps Mac doesn't use the cookie method, you can always find out by running telnet localhost 9151 and then send AUTHENTICATE by itself once connected. If it lets you connect, no password or cookie is required, if it's denied then it's most likely using cookie auth.Minni
Oh you are so right -- I am using drytor. My bad, let me try what you posted.Carolincarolina
I updated my question with the output of what you suggested in my question. Hopefully I'm setting the PATH and DYLD_LIBRARY_PATH correctly, but I still get that "false" message when I query available?Carolincarolina
available? is literally just checking that a binary called tor exists within your path and is executable. The library makes no mention that it works w/ Mac (you may need to stop using Tor Browser and use the legit daemon). Maybe on Mac it's called something else. You might also try changing the Tor path to just /Applications/TorBrowser.app/Contents/MacOS and presumably the tor binary lives in there. If the binary is called Tor instead of tor it won't work (maybe call Tor.program_path(:program_name => 'Tor') and see what it prints.Minni
I didn't realize available? only checked if it is installed. I'm looking for something that will tell me if Tor is actually running.Carolincarolina
Yeah that does nothing to tell you if it's running. Also "running" doesn't necessarily mean you're connected to the network or have enough info to establish connections. GETINFO dormant on the controller can help with that, or just make a request to the SOCKS port and see if you get a response to be 100% sure. If you leave tor running but don't use it for a while and you're not a relay, you can go dormant which means it may need to take some extra steps at the next request to re-establish circuits.Minni

© 2022 - 2024 — McMap. All rights reserved.