How do you block specific IP adress in Ruby on Rai

2019-04-12 09:02发布

问题:

I am in charge of a few live websites made in Ruby on Rails. I have a few IP adresses that keep attacking these sites and I would like to block their IP adresses. I know they can get around this wall with a proxy but I do wish to make it harder for them and would love to know where I need to set this up in my ruby on rails app. Thank you everyone!

回答1:

class ApplicationController < ActionController::Base
  before_filter :block_ip_addresses

  protected

  def block_ip_addresses
    head :unauthorized if current_ip_address == "XX.XX.XX.XX"
  end

  def current_ip_address
    request.env['HTTP_X_REAL_IP'] || request.env['REMOTE_ADDR']
  end
end

Thanks to: https://stackoverflow.com/a/10895438/1466095