Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to check if a user is coming from a given IP block and redirect?

Here's the code I wrote in the Application controller:

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :redirect_if_bolivia

  private

  def redirect_if_bolivia
    if (bolivia_ip_block).includes? request.remote_ip
        #redirect user to www.foo.com
    end
  end

  def bolivia_ip_block
    %w {
        12.144.86.0 - 12.144.87.255
31.201.1.176 - 31.201.1.179
46.36.198.101 - 46.36.198.105
46.136.172.0 - 46.136.172.255
63.65.11.0 - 63.65.12.254
65.173.56.0 - 65.173.63.255
67.23.241.179 - 67.23.241.198
72.32.164.56 - 72.32.164.63
72.46.244.32 - 72.46.244.47
74.91.16.48 - 74.91.16.55
74.91.16.208 - 74.91.16.215
74.91.20.48 - 74.91.20.71
74.112.134.120 - 74.112.134.127
74.112.135.104 - 74.112.135.111
74.205.37.16 - 74.205.37.23
78.24.205.32 - 78.24.205.47
98.129.27.88 - 98.129.27.95
98.129.91.40 - 98.129.91.47
166.114.0.0 - 166.114.255.255
167.157.0.0 - 167.157.255.255
174.143.165.80 - 174.143.165.87
186.0.156.0 - 186.0.159.255
186.2.0.0 - 186.2.127.255
186.27.0.0 - 186.27.127.255
190.0.248.0 - 190.0.255.255
190.3.184.0 - 190.3.191.255
    }
  end
end

So basically, what is the easiest way to check if a request.remote_ip belongs to a Bolivia IP block?

I don't think that iterating through each block and spitting out a corresponding IP, adding it to an array is efficient. In fact, that would lead to me create an array of thousands of entries for every single request.

I'm sure this is not a new problem, so I'd love a proven solution to this.

Maybe I can check if the first three octets match, then it belongs to the block? How would I do that simple string comparison in Ruby?

like image 775
Only Bolivian Here Avatar asked Jan 17 '23 12:01

Only Bolivian Here


1 Answers

You can convert the subnets to CIDR notation since blocks are not arbitrary. Then you can take advantage of IPAddr#include

require 'ipaddr'

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :redirect_if_bolivia

  private

  def redirect_if_bolivia
    if bolivian_blocks.any? { |block| block.include?(request.remote_ip) }
        #redirect user to www.foo.com
    end
  end

  def bolivian_blocks
    %w{
      12.144.86.0/23
      31.201.1.176/30
    }.map { |subnet| IPAddr.new subnet }
  end
end
like image 172
AndrewF Avatar answered Jan 31 '23 10:01

AndrewF