module RequestInfo::Detectors::IpDetector

TODO Write some notes on configuration & security in README.

Public Instance Methods

analyze(env) click to toggle source
Calls superclass method
# File lib/request_info/detectors/ip_detector.rb, line 11
def analyze(env)
  super

  results = RequestInfo.results
  ip = request_ip(env)

  results.ip = ip
  results.ipinfo = RequestInfo::GeoIP.instance.lookup(ip)
end

Private Instance Methods

obtain_ip_from_rack(env) click to toggle source

Obtain client's IP address from Rack::Request. May return proxy address if it adds a non-private IP address to X-Forwarded-For header.

github.com/rack/rack/blob/d1363a66ab217/lib/rack/request.rb#L420

# File lib/request_info/detectors/ip_detector.rb, line 47
def obtain_ip_from_rack(env)
  Rack::Request.new(env).ip
end
obtain_ip_from_rails(env) click to toggle source

Obtain client's IP address from ActionDispatch::RemoteIp middleware provided by Rails.

This is preferred over using Rack::Request because ActionDispatch::RemoteIp middleware must be enabled purposely, and is more customizable (proxies whitelisting is customizable).

Please read security notes before enabling ActionDispatch::RemoteIp middleware in your application. It may do harm if used incorrectly: api.rubyonrails.org/classes/ActionDispatch/RemoteIp.html

# File lib/request_info/detectors/ip_detector.rb, line 39
def obtain_ip_from_rails(env)
  env["action_dispatch.remote_ip"].try(:to_s)
end
request_ip(env) click to toggle source

Extracts the IP address from env

# File lib/request_info/detectors/ip_detector.rb, line 25
def request_ip(env)
  obtain_ip_from_rails(env) || obtain_ip_from_rack(env)
end