Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Ruby failing to connect to my Tor network?

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?
like image 938
Dave Avatar asked Sep 11 '16 16:09

Dave


1 Answers

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!

like image 62
drew010 Avatar answered Oct 27 '22 14:10

drew010