Sha256: b6e683f370e09ce0c3d52a9871ed79dafa252fbf379c84e844b2d9284e45bf5f

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

require 'rack/traffic_signal/version'
require 'rack/traffic_signal/config'
require 'rack/traffic_signal/exceptions'
require 'rack/traffic_signal/middleware'
require 'rack/traffic_signal/railtie' if defined? Rails

module Rack
  module TrafficSignal
    def self.setup
      yield(config)
    end

    def self.config
      @config ||= Config.new
    end

    def self.skip?(env)
      config.skip_proc.call(env)
    end

    def self.skip_with_warning?(env)
      config.skip_with_warning_proc.call(env)
    end

    def self.internal_access?(env)
      remote_ip = IPAddress(request_from(env))
      config.internal_ips
        .map { |internal_ip| IPAddress(internal_ip) }
        .any? { |internal_ip| internal_ip.include?(remote_ip) if remote_ip.class == internal_ip.class }
    end

    def self.has_secret_word?(env)
      env.fetch('HTTP_X_RACK_TRAFFIC_SIGNAL_SECRET', nil) == config.secret_word
    end

    def self.skip_path?(env)
      path = env['PATH_INFO']
      path = path.chop if path[-1] == '/'
      config.skip_paths.any? do |skip_path|
        if skip_path.is_a? Regexp
          path.match(skip_path)
        elsif skip_path.is_a? String
          path == skip_path
        else
          false
        end
      end
    end

    private
    def self.request_from(env)
      return env['REMOTE_ADDR'] unless env['HTTP_X_FORWARDED_FOR']
      env['HTTP_X_FORWARDED_FOR'].split(/,/)[0].strip
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-traffic-signal-0.1.4 lib/rack/traffic_signal.rb