Object
Rack middleware for limiting access based on IP address
path => ipmasks ipmasks: Array of remote addresses which are allowed to access
use Rack::Access, '/backend' => [ '127.0.0.1', '192.168.1.0/24' ]
# File lib/rack/contrib/access.rb, line 50 def call(env) @original_request = Request.new(env) ipmasks = ipmasks_for_path(env) return forbidden! unless ip_authorized?(ipmasks) status, headers, body = @app.call(env) [status, headers, body] end
# File lib/rack/contrib/access.rb, line 72 def forbidden! [403, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []] end
# File lib/rack/contrib/access.rb, line 58 def ipmasks_for_path(env) path = env["PATH_INFO"].to_s hHost, sName, sPort = env.values_at('HTTP_HOST','SERVER_NAME','SERVER_PORT') @mapping.each do |host, location, match, ipmasks| next unless (hHost == host || sName == host || (host.nil? && (hHost == sName || hHost == sName+':'+sPort))) next unless path =~ match && rest = $1 next unless rest.empty? || rest[0] == // return ipmasks end nil end
# File lib/rack/contrib/access.rb, line 29 def remap(mapping) mapping.map { |location, ipmasks| if location =~ %{\Ahttps?://(.*?)(/.*)} host, location = $1, $2 else host = nil end unless location[0] == // raise ArgumentError, "paths need to start with /" end location = location.chomp('/') match = Regexp.new("^#{Regexp.quote(location).gsub('/', '/+')}(.*)", nil, 'n') ipmasks.collect! do |ipmask| ipmask.is_a?(IPAddr) ? ipmask : IPAddr.new(ipmask) end [host, location, match, ipmasks] }.sort_by { |(h, l, m, a)| [h ? -h.size : (-1.0 / 0.0), -l.size] } # Longest path first end
Generated with the Darkfish Rdoc Generator 2.