Class: Rack::TorTag

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/tor_tag.rb

Constant Summary collapse

DEFAULT_PARAMS =
{
  # You could run your own if you want.
  # https://www.torproject.org/projects/tordnsel.html.en
  :dnsel => 'ip-port.exitlist.torproject.org',
  :host_ips => nil, # will get from env
  :host_port => nil
}

Instance Method Summary collapse

Constructor Details

#initialize(app, params = {}) ⇒ TorTag

Returns a new instance of TorTag.



17
18
19
20
21
# File 'lib/rack/tor_tag.rb', line 17

def initialize(app, params = {})
  @app = app
  @params = DEFAULT_PARAMS.merge(params)
  # raise ArgumentError, "Must pass :host_ips" unless RESOLV_WORKS
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack/tor_tag.rb', line 23

def call(env)
  ip = env['action_dispatch.remote_ip']
  # Getting the right IP is hard. Let's punt.
  # http://api.rubyonrails.org/classes/ActionDispatch/RemoteIp.html
  # http://blog.gingerlime.com/2012/rails-ip-spoofing-vulnerabilities-and-protection/
  raise 'Must run after ActionDispatch::RemoteIp' if ip.blank?

  case is_tor?(ip, env)
  when true
    env['tor'] = true
    env['tor_ip'] = ip # stick it elsewhere
    env['action_dispatch.remote_ip'] = '127.0.0.2' # make all Tor IPs look alike
  when false
    env['tor'] = false
  else
    env['tor'] = nil # i.e. unknown
  end

  # Continue normal processing
  @app.call(env)
end