Skip to content

Instantly share code, notes, and snippets.

@oogali
Created January 10, 2013 03:08
Show Gist options
  • Select an option

  • Save oogali/4499106 to your computer and use it in GitHub Desktop.

Select an option

Save oogali/4499106 to your computer and use it in GitHub Desktop.
Really? I still have to do this in 2013?
require 'ipaddr'
module Monitoring
module Middleware
class Ip
def initialize(app, options = {})
trusted = options[:trusted] || [ '127.0.0.1/32' ]
@trusted = trusted.map { |network| IPAddr.new network }
@app = app
end
def call(env)
if env['HTTP_X_FORWARDED_FOR'] and trusted? env['REMOTE_ADDR']
env['REMOTE_ADDR'] = env['HTTP_X_FORWARDED_FOR']
end
@app.call(env)
end
private
def trusted?(ip)
@trusted.each { |network| return true if network === ip }
false
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment