Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you block specific IP adress in Ruby on Rails

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!

like image 812
ZachyBear Avatar asked Apr 21 '14 14:04

ZachyBear


1 Answers

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

like image 155
Zero Fiber Avatar answered Sep 30 '22 15:09

Zero Fiber