Created
January 10, 2013 03:08
-
-
Save oogali/4499106 to your computer and use it in GitHub Desktop.
Really? I still have to do this in 2013?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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