Class: Rack::Dedos::Filters::UserAgent

Inherits:
Base
  • Object
show all
Defined in:
lib/rack/dedos/filters/user_agent.rb

Constant Summary

Constants inherited from Base

Base::DEFAULT_OPTIONS

Instance Attribute Summary

Attributes inherited from Base

#app, #options

Instance Method Summary collapse

Methods inherited from Base

#call

Constructor Details

#initializeUserAgent

Returns a new instance of UserAgent.

Parameters:

  • options (Hash)

    a customizable set of options



11
12
13
14
15
16
17
# File 'lib/rack/dedos/filters/user_agent.rb', line 11

def initialize(*)
  super
  @cache_url = options[:cache_url] or fail "cache URL not set"
  @cache_period = options[:cache_period] || 900
  @cache_key_prefix = options[:cache_key_prefix]
  cache   # hit once to fail on errors at boot
end

Instance Method Details

#allowed?(request, ip) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rack/dedos/filters/user_agent.rb', line 19

def allowed?(request, ip)
  case cache.get(ip)
  when nil              # first contact
    cache.set(ip, request.user_agent)
    true
  when 'BLOCKED'        # already blocked
    false
  when request.user_agent   # user agent hasn't changed
    true
  else                  # user agent has changed
    cache.set(ip, 'BLOCKED')
    false
  end
rescue => error
  warn("rack-dedos: request from #{ip} allowed due to error: #{error.message}")
  true
end