Methods
C
I
N
T
Class Public methods
new(env, middleware)
    # File actionpack/lib/action_dispatch/middleware/remote_ip.rb, line 35
35:       def initialize(env, middleware)
36:         @env          = env
37:         @middleware   = middleware
38:         @calculated_ip = false
39:       end
Instance Public methods
calculate_ip()

Determines originating IP address. REMOTE_ADDR is the standard but will be wrong if the user is behind a proxy. Proxies will set HTTP_CLIENT_IP and/or HTTP_X_FORWARDED_FOR, so we prioritize those. HTTP_X_FORWARDED_FOR may be a comma-delimited list in the case of multiple chained proxies. The last address which is not a known proxy will be the originating IP.

    # File actionpack/lib/action_dispatch/middleware/remote_ip.rb, line 47
47:       def calculate_ip
48:         client_ip     = @env['HTTP_CLIENT_IP']
49:         forwarded_ips = ips_from('HTTP_X_FORWARDED_FOR')
50:         remote_addrs  = ips_from('REMOTE_ADDR')
51: 
52:         check_ip = client_ip && @middleware.check_ip
53:         if check_ip && !forwarded_ips.include?(client_ip)
54:           # We don't know which came from the proxy, and which from the user
55:           raise IpSpoofAttackError, "IP spoofing attack?!" \
56:             "HTTP_CLIENT_IP=#{@env['HTTP_CLIENT_IP'].inspect}" \
57:             "HTTP_X_FORWARDED_FOR=#{@env['HTTP_X_FORWARDED_FOR'].inspect}"
58:         end
59: 
60:         not_proxy = client_ip || forwarded_ips.last || remote_addrs.first
61: 
62:         # Return first REMOTE_ADDR if there are no other options
63:         not_proxy || ips_from('REMOTE_ADDR', :allow_proxies).first
64:       end
to_s()
    # File actionpack/lib/action_dispatch/middleware/remote_ip.rb, line 66
66:       def to_s
67:         return @ip if @calculated_ip
68:         @calculated_ip = true
69:         @ip = calculate_ip
70:       end
Instance Protected methods
ips_from(header, allow_proxies = false)
    # File actionpack/lib/action_dispatch/middleware/remote_ip.rb, line 74
74:       def ips_from(header, allow_proxies = false)
75:         ips = @env[header] ? @env[header].strip.split(/[,\s]+/) : []
76:         allow_proxies ? ips : ips.reject{|ip| ip =~ @middleware.proxies }
77:       end