Module: Rack::Attack
- Defined in:
- lib/rack/attack/cache.rb,
lib/rack/attack/check.rb,
lib/rack/attack/track.rb,
lib/rack/attack/version.rb,
lib/rack/attack/fail2ban.rb,
lib/rack/attack/throttle.rb,
lib/rack/attack/blacklist.rb,
lib/rack/attack/whitelist.rb,
lib/rack/attack/store_proxy.rb,
lib/rack/attack.rb
Defined Under Namespace
Classes: Blacklist, Cache, Check, Fail2Ban, StoreProxy, Throttle, Track, Whitelist
Constant Summary
collapse
- VERSION =
'2.2.1'
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.blacklisted_response ⇒ Object
Returns the value of attribute blacklisted_response.
14
15
16
|
# File 'lib/rack/attack.rb', line 14
def blacklisted_response
@blacklisted_response
end
|
.notifier ⇒ Object
Returns the value of attribute notifier.
14
15
16
|
# File 'lib/rack/attack.rb', line 14
def notifier
@notifier
end
|
.throttled_response ⇒ Object
Returns the value of attribute throttled_response.
14
15
16
|
# File 'lib/rack/attack.rb', line 14
def throttled_response
@throttled_response
end
|
Class Method Details
.blacklist(name, &block) ⇒ Object
20
21
22
|
# File 'lib/rack/attack.rb', line 20
def blacklist(name, &block)
self.blacklists[name] = Blacklist.new(name, block)
end
|
.blacklisted?(req) ⇒ Boolean
72
73
74
75
76
|
# File 'lib/rack/attack.rb', line 72
def blacklisted?(req)
blacklists.any? do |name, blacklist|
blacklist[req]
end
end
|
.blacklists ⇒ Object
33
|
# File 'lib/rack/attack.rb', line 33
def blacklists; @blacklists ||= {}; end
|
.cache ⇒ Object
94
95
96
|
# File 'lib/rack/attack.rb', line 94
def cache
@cache ||= Cache.new
end
|
.call(env) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/rack/attack.rb', line 51
def call(env)
req = Rack::Request.new(env)
if whitelisted?(req)
@app.call(env)
elsif blacklisted?(req)
blacklisted_response[env]
elsif throttled?(req)
throttled_response[env]
else
tracked?(req)
@app.call(env)
end
end
|
.clear! ⇒ Object
98
99
100
|
# File 'lib/rack/attack.rb', line 98
def clear!
@whitelists, @blacklists, @throttles = {}, {}, {}
end
|
.instrument(req) ⇒ Object
90
91
92
|
# File 'lib/rack/attack.rb', line 90
def instrument(req)
notifier.instrument('rack.attack', req) if notifier
end
|
.new(app) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/rack/attack.rb', line 37
def new(app)
@app = app
@notifier ||= ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
@blacklisted_response ||= lambda {|env| [401, {}, ["Unauthorized\n"]] }
@throttled_response ||= lambda {|env|
retry_after = env['rack.attack.match_data'][:period] rescue nil
[429, {'Retry-After' => retry_after.to_s}, ["Retry later\n"]]
}
self
end
|
.throttle(name, options, &block) ⇒ Object
24
25
26
|
# File 'lib/rack/attack.rb', line 24
def throttle(name, options, &block)
self.throttles[name] = Throttle.new(name, options, block)
end
|
.throttled?(req) ⇒ Boolean
78
79
80
81
82
|
# File 'lib/rack/attack.rb', line 78
def throttled?(req)
throttles.any? do |name, throttle|
throttle[req]
end
end
|
.throttles ⇒ Object
34
|
# File 'lib/rack/attack.rb', line 34
def throttles; @throttles ||= {}; end
|
.track(name, &block) ⇒ Object
28
29
30
|
# File 'lib/rack/attack.rb', line 28
def track(name, &block)
self.tracks[name] = Track.new(name, block)
end
|
.tracked?(req) ⇒ Boolean
84
85
86
87
88
|
# File 'lib/rack/attack.rb', line 84
def tracked?(req)
tracks.each_value do |tracker|
tracker[req]
end
end
|
.tracks ⇒ Object
35
|
# File 'lib/rack/attack.rb', line 35
def tracks; @tracks ||= {}; end
|
.whitelist(name, &block) ⇒ Object
16
17
18
|
# File 'lib/rack/attack.rb', line 16
def whitelist(name, &block)
self.whitelists[name] = Whitelist.new(name, block)
end
|
.whitelisted?(req) ⇒ Boolean
66
67
68
69
70
|
# File 'lib/rack/attack.rb', line 66
def whitelisted?(req)
whitelists.any? do |name, whitelist|
whitelist[req]
end
end
|
.whitelists ⇒ Object
32
|
# File 'lib/rack/attack.rb', line 32
def whitelists; @whitelists ||= {}; end
|