class SC::Rack::RestrictIP
Public Class Methods
new(app, allow_ips=[])
click to toggle source
# File lib/sproutcore/rack/restrict_ip.rb, line 13 def initialize(app, allow_ips=[]) @app = app @allow = allow_ips end
Public Instance Methods
call(env)
click to toggle source
# File lib/sproutcore/rack/restrict_ip.rb, line 47 def call(env) ip = env['REMOTE_ADDR'] is_valid = false @allow.each {|mask| if ip_is_valid(ip, mask) is_valid = true break end } if is_valid return @app.call(env) else SC.logger << "Blocked connection attempt by ip: #{ip}\n" return [403, { 'Content-Type' => 'text/plain' }, "YOU CANNOT BEEZ HERE."] end end
ip_is_valid(ip, mask)
click to toggle source
checks if an IP, such as 127.0.0.1, matches a mask, such as 127...*
# File lib/sproutcore/rack/restrict_ip.rb, line 19 def ip_is_valid(ip, mask) ip_parts = ip.split('.') mask_parts = mask.split('.') if mask_parts.length != 4 SC.logger.fatal "Invalid IP mask: #{mask}\n" exit end ip_idx = 0 mask_parts.each {|mask_part| ip_part = ip_parts[ip_idx] # * means anything matches if mask_part == '*' next end if ip_part != mask_part return false end ip_idx = ip_idx + 1 } return true end