Sha256: 3d262189b1def22995ac84327541a0f03b15420ae509e58c7e5d8cd6ffe6a6dd

Contents?: true

Size: 991 Bytes

Versions: 2

Compression:

Stored size: 991 Bytes

Contents

module FirewallConstraint
  require 'ipaddress'
  class Constraint
    def initialize(ips = [])
      if ips.respond_to? :call
        @ips = ips
      else
        ips = [ips].flatten
        @config = !ips.empty? ? ips :
          YAML.load_file(Rails.root.join('config','firewall_constraint.yml'))[Rails.env]
          

        @ips = @config
      end
    end

    def matches?(request)
      client_ip = IPAddress::parse(request.env["HTTP_X_FORWARDED_FOR"] ? request.env["HTTP_X_FORWARDED_FOR"] : request.remote_ip)
      parsed_ips.each do |ip|
        begin
          return true if ip.include?(client_ip)
        rescue NoMethodError => nme
        end
      end
      false
    end

    def parsed_ips
      cur_ips = ips
      if cur_ips == @old_ips
        @cached_parsed_ips
      else
        @old_ips = cur_ips
        @cached_parsed_ips = cur_ips.map{|c| IPAddress::parse(c)}
      end

    end

    def ips
      @ips.respond_to?(:call) ? @ips.call : @ips
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
firewall_constraint-0.0.5 lib/firewall_constraint.rb
firewall_constraint-0.0.4 lib/firewall_constraint.rb