This is a two part question. I'm needing to restrict a rails site that I'm throwing on development server to only a few IP addresses, so the public can't access it. (Basic HTTP auth doesn't 'entirely' work as the auth breaks a Flash uploader in the project.)
Based on what I've Googled, this is what I've come up with in my routes file...
class WhitelistConstraint
def initialize
@ips = '127.0.0.1'
end
def matches?(request)
@ips.include?(request.remote_ip)
end
end
MyProject::Application.routes.draw do
constraints WhitelistConstraint.new do
# all my routing stuff here
end
end
Works pretty good. However, I need to modify this in order to work with several IP addresses. I tried using a array on @ips, as well as looping through an each loop, but neither worked.
On top of that, the second part of my question...I may need to check only against a segment of the IP, like '127.0.0'. How would I do that?
Or simply use apache's .htaccess:
It is also possible to surround your route declaration with a scope like so:
what about using NetAddr::CIDR?
and something like this?
This way you can specify the blocks of IPs that should be whitelisted, and not have to worry about the partial matches?
I didn't know you could do this through routes, my approach would be to just have a
before_filter
in theApplicationController
and just have something that does: