Class: Honeypot::TrueRemoteIp

Inherits:
Object
  • Object
show all
Defined in:
lib/honeypot/true_remote_ip.rb

Overview

Middleware for Rack applications. Remote hosts will be tied together with remote requests.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ TrueRemoteIp

Returns a new instance of TrueRemoteIp.



6
7
8
# File 'lib/honeypot/true_remote_ip.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#_possible_remote_ips(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/honeypot/true_remote_ip.rb', line 25

def _possible_remote_ips(env)
  candidates = Array.new
  # nicely provided by Rails 3
  if env['action_dispatch.remote_ip']
    candidates.push env['action_dispatch.remote_ip']
  end
  # saved by honeypot between requests
  if env['rack.session']
    candidates.push env['rack.session']['honeypot.true_remote_ip']
  end
  candidates
end

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/honeypot/true_remote_ip.rb', line 10

def call(env)
  ip = ::Honeypot.true_remote_ip _possible_remote_ips(env)

  # For the next request, in case the next time we see this session the remote ip is obscured
  # (for example, that happens if you're on engineyard and the request comes in over SSL)
  if ip and env.has_key? 'rack.session'
    env['rack.session']['honeypot.true_remote_ip'] = ip
  end
  
  # For use by other middleware or the app itself
  env['honeypot.true_remote_ip'] = ip

  @app.call env
end