title: Rails using kickstarter rack-attack IP blacklisting with Cloudflare
body: <p>Rails app, using Kickstarter's <a href="https://github.com/kickstarter/rack-attack" rel="noreferrer">rack-attack</a> </p>
  
  <p>Within my config/rack-attack.rb file, I have:</p>
  
  <pre><code>class Rack::Attack
    Rack::Attack.blacklist ('block ip') do |req|
      # Request are blocked if the return value is truthy
      '68.888.23.22' == req.ip
      # req.ip if IPCat.datacenter?(req.ip)
    end
  end
  </code></pre>
  
  <p>This worked fine until I started using CloudFlare.  The req.ip is now a Cloudflare IP vs that actual end user's IP</p>
  
  <p>I had a similar issue when trying to save the user's IP to my server logs (was saving the Cloudflare IPs). In order to fix this, I added the following to my application controller:</p>
  
  <pre><code>module ActionDispatch
    class Request &lt; Rack::Request
      alias :remote_ip_orig :remote_ip
      def remote_ip
        @remote_ip ||= (@env['HTTP_CF_CONNECTING_IP'] || remote_ip_orig)
      end
    end
  end
  </code></pre>
  
  <p>Is there a similar process in order to use the HTTP_CF_CONNECTING_IP as the req.ip within rack-attack?</p>