Connect to Tor network with Ruby 1

Here’s how I got it working. Enjoy!

1. Install Tor on your dev. and production

Mac:

1
brew install tor

Ubuntu:

1
sudo vim /etc/apt/sources.list

Insert inside:

deb http://deb.torproject.org/torproject.org oldstable main

1
sudo apt-get install tor

2. Modify Tor config file to allow remote access via port

1
sudo vim /etc/tor/torrc

Uncomment these two lines:

ControlPort 9051
CookieAuthentication 0

3. Adding Tor to your Ruby app (Gemfile)

1
2
gem 'tor'
gem 'socksify'
1
bundle install

4. Writing Ruby code to use Sock to connect to Tor

Here’s class I use to connect to Tor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'open-uri'
require 'uri'
require 'socksify/http'
require 'tor'
require 'net/telnet'

class ProxyTor
    attr_reader :proxy, :host, :port

    def initialize host = 'localhost', port = 9050, control_port = 9051
      unless Tor.available?
        raise <<-EOS
          Tor isnt installed. Install Tor to use this module.
          See http://torproject.org or `brew install tor`.
        EOS
      end


      TCPSocket::socks_server = host
      TCPSocket::socks_port = port


      @proxy = Net::HTTP.SOCKSProxy host, port
      @host = host
      @port = port
      @control_port = control_port
      @circuit_timeout = 10
    end


    def switch
      localhost = Net::Telnet::new('Host' => @host,
                                   'Port' => @control_port,
                                   'Timeout' => @circuit_timeout,
                                   'Prompt' => /250 OK\n/)
      localhost.cmd("AUTHENTICATE") do |c|
        throw "cannot authenticate to Tor!" if c != "250 OK\n"
      end
      localhost.cmd('SIGNAL NEWNYM') do |c|
        throw "cannot switch Tor to new route!" if c != "250 OK\n"
      end
      localhost.close
    end
  end

5. Ruby code to connect to particular website via Tor

With this code I’m using also retryable gem to try to connect 10 times before I give up.

1
2
3
4
5
6
7
8
9
10
11
@tor_proxy = ::ProxyTor.new

retryable(:tries => 10, :on => [OpenURI::HTTPError, StandardError]) do |retries, exception|
  if exception.class == OpenURI::HTTPError
    @tor_proxy.switch
  end
  html = open(url,
    'User-Agent' => USERAGENT[rand(USERAGENT.size)],
    :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE,
    :proxy => true)
end

Edward de Bono – Six Thinking Hats 0

Failure, Customer Discovery & Development by Steve Blank 0

There’s no such thing as being right 0

Many times I hear people say “You’re right!” or even more “I’m right.”. There’s no such thing as Being Right(tm). Each of us is unique and by this uniqueness we are entitled to see things differently. Therefore being right is irrelevant in other’s person eyes. Stop fighting to be right. Listen and learn instead.

Presenters are probably earning more than not presenters 0

I’m one of the organizers of Ruby Group in Ostrava (Czech Republic). Most of the time I do one presentation as there’s not much people willing to do presentations.

Last time I have looked at the room of all the guys being there present, and splited them to 2 groups. Those that already did presentation and those that didn’t (yet).

Then I had a small realization during my last presentation. Specifically for this group I realized the amount of money one earns is directly proportional to number of presentations one does (did). Isn’t that interesting?

So to all of you being afraid of doing presentations you should do it as it will make you money (indirectly).

Next Page »