remote_ip()
public
Determine originating IP address. REMOTE_ADDR is the standard but will fail
if the user is behind a proxy. HTTP_CLIENT_IP and/or HTTP_X_FORWARDED_FOR
are set by proxies so check for these if REMOTE_ADDR is a proxy.
HTTP_X_FORWARDED_FOR may be a comma- delimited list in the case of multiple
chained proxies; the last address which is not trusted is the originating
IP.
Show source
def remote_ip
if TRUSTED_PROXIES !~ @env['REMOTE_ADDR']
return @env['REMOTE_ADDR']
end
if @env.include? 'HTTP_CLIENT_IP'
if @env.include? 'HTTP_X_FORWARDED_FOR'
raise ActionControllerError.new("IP spoofing attack?!\nHTTP_CLIENT_IP=\#{@env['HTTP_CLIENT_IP'].inspect}\nHTTP_X_FORWARDED_FOR=\#{@env['HTTP_X_FORWARDED_FOR'].inspect}\n")
end
return @env['HTTP_CLIENT_IP']
end
if @env.include? 'HTTP_X_FORWARDED_FOR' then
remote_ips = @env['HTTP_X_FORWARDED_FOR'].split(',')
while remote_ips.size > 1 && TRUSTED_PROXIES =~ remote_ips.last.strip
remote_ips.pop
end
return remote_ips.last.strip
end
@env['REMOTE_ADDR']
end