Module: Rack::Attack

Defined in:
lib/rack/attack/cache.rb,
lib/rack/attack/check.rb,
lib/rack/attack/version.rb,
lib/rack/attack/throttle.rb,
lib/rack/attack/blacklist.rb,
lib/rack/attack/whitelist.rb,
lib/rack/attack.rb

Defined Under Namespace

Classes: Blacklist, Cache, Check, Throttle, Whitelist

Constant Summary collapse

VERSION =
'0.2.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.blacklisted_responseObject

Returns the value of attribute blacklisted_response.



11
12
13
# File 'lib/rack/attack.rb', line 11

def blacklisted_response
  @blacklisted_response
end

.cacheObject (readonly)

Returns the value of attribute cache.



10
11
12
# File 'lib/rack/attack.rb', line 10

def cache
  @cache
end

.notifierObject (readonly)

Returns the value of attribute notifier.



10
11
12
# File 'lib/rack/attack.rb', line 10

def notifier
  @notifier
end

.throttled_responseObject

Returns the value of attribute throttled_response.



11
12
13
# File 'lib/rack/attack.rb', line 11

def throttled_response
  @throttled_response
end

Class Method Details

.blacklist(name, &block) ⇒ Object



17
18
19
# File 'lib/rack/attack.rb', line 17

def blacklist(name, &block)
  self.blacklists[name] = Blacklist.new(name, block)
end

.blacklisted?(req) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
# File 'lib/rack/attack.rb', line 66

def blacklisted?(req)
  blacklists.any? do |name, blacklist|
    blacklist[req]
  end
end

.blacklistsObject



26
# File 'lib/rack/attack.rb', line 26

def blacklists; @blacklists ||= {}; end

.call(env) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rack/attack.rb', line 44

def call(env)
  req = Rack::Request.new(env)

  if whitelisted?(req)
    return @app.call(env)
  end

  if blacklisted?(req)
    blacklisted_response[env]
  elsif throttled?(req)
    throttled_response[env]
  else
    @app.call(env)
  end
end

.clear!Object



82
83
84
# File 'lib/rack/attack.rb', line 82

def clear!
  @whitelists, @blacklists, @throttles = {}, {}, {}
end

.instrument(req) ⇒ Object



78
79
80
# File 'lib/rack/attack.rb', line 78

def instrument(req)
  notifier.instrument('rack.attack', req) if notifier
end

.new(app) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rack/attack.rb', line 29

def new(app)
  @app = app

  # Set defaults
  @cache ||= Cache.new
  @notifier ||= ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
  @blacklisted_response ||= lambda {|env| [503, {}, ['Blocked']] }
  @throttled_response   ||= lambda {|env|
    retry_after = env['rack.attack.match_data'][:period] rescue nil
    [503, {'Retry-After' => retry_after}, ['Retry later']]
  }

  self
end

.throttle(name, options, &block) ⇒ Object



21
22
23
# File 'lib/rack/attack.rb', line 21

def throttle(name, options, &block)
  self.throttles[name] = Throttle.new(name, options, block)
end

.throttled?(req) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/rack/attack.rb', line 72

def throttled?(req)
  throttles.any? do |name, throttle|
    throttle[req]
  end
end

.throttlesObject



27
# File 'lib/rack/attack.rb', line 27

def throttles;  @throttles  ||= {}; end

.whitelist(name, &block) ⇒ Object



13
14
15
# File 'lib/rack/attack.rb', line 13

def whitelist(name, &block)
  self.whitelists[name] = Whitelist.new(name, block)
end

.whitelisted?(req) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/rack/attack.rb', line 60

def whitelisted?(req)
  whitelists.any? do |name, whitelist|
    whitelist[req]
  end
end

.whitelistsObject



25
# File 'lib/rack/attack.rb', line 25

def whitelists; @whitelists ||= {}; end