Skip to content

Commit

Permalink
another approach to fixing ip issue on heroku with custom SSL cert
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh committed Feb 7, 2012
1 parent 926cd45 commit 3ff8ec3
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/geo_ip.rb
Expand Up @@ -2,14 +2,15 @@
require 'timeout'

class GeoIP
TRUSTED_PROXY = /^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.|^::1$|^fd[0-9a-f]{2}:.+|^localhost$/i
def initialize(geoip_host = 'freegeoip.net')
@geoip_host = geoip_host
@cache = {}
end

def call(env, cb = env['async.callback'])
fw = env.fetch("HTTP_X_FORWARDED_FOR", "").split(",").last.to_s.strip
ip = fw.presence || Rack::Request.new(env).ip
forwarded_ips = env['HTTP_X_FORWARDED_FOR'] ? env['HTTP_X_FORWARDED_FOR'].strip.split(/[,\s]+/) : []
ip = forwarded_ips.reject { |ip| ip =~ TRUSTED_PROXY }.last || env['REMOTE_ADDR']

@cache.clear if @cache.size > 1024 # magic values ftw!
return @cache[ip] if @cache.include? ip
Expand Down

0 comments on commit 3ff8ec3

Please sign in to comment.