Class: Raven::Utils::RealIp

Inherits:
Object
  • Object
show all
Defined in:
lib/raven/utils/real_ip.rb

Constant Summary collapse

LOCAL_ADDRESSES =
[
  "127.0.0.1",      # localhost IPv4
  "::1",            # localhost IPv6
  "fc00::/7",       # private IPv6 range fc00::/7
  "10.0.0.0/8",     # private IPv4 range 10.x.x.x
  "172.16.0.0/12",  # private IPv4 range 172.16.0.0 .. 172.31.255.255
  "192.168.0.0/16", # private IPv4 range 192.168.x.x
].map { |proxy| IPAddr.new(proxy) }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip_addresses) ⇒ RealIp

Returns a new instance of RealIp.



21
22
23
# File 'lib/raven/utils/real_ip.rb', line 21

def initialize(ip_addresses)
  self.ip_addresses = ip_addresses
end

Instance Attribute Details

#ipObject

Returns the value of attribute ip.



19
20
21
# File 'lib/raven/utils/real_ip.rb', line 19

def ip
  @ip
end

#ip_addressesObject

Returns the value of attribute ip_addresses.



19
20
21
# File 'lib/raven/utils/real_ip.rb', line 19

def ip_addresses
  @ip_addresses
end

Instance Method Details

#calculate_ipObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/raven/utils/real_ip.rb', line 25

def calculate_ip
  # CGI environment variable set by Rack
  remote_addr = ips_from(ip_addresses[:remote_addr]).last

  # Could be a CSV list and/or repeated headers that were concatenated.
  client_ips    = ips_from(ip_addresses[:client_ip])
  real_ips      = ips_from(ip_addresses[:real_ip])
  forwarded_ips = ips_from(ip_addresses[:forwarded_for])

  ips = [client_ips, real_ips, forwarded_ips, remote_addr].flatten.compact

  # If every single IP option is in the trusted list, just return REMOTE_ADDR
  self.ip = filter_local_addresses(ips).first || remote_addr
end