Class: Rack::Attack
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/rack/attack/cache.rb,
lib/rack/attack/check.rb,
lib/rack/attack/track.rb,
lib/rack/attack/request.rb,
lib/rack/attack/version.rb,
lib/rack/attack/fail2ban.rb,
lib/rack/attack/throttle.rb,
lib/rack/attack/allow2ban.rb,
lib/rack/attack/blacklist.rb,
lib/rack/attack/whitelist.rb,
lib/rack/attack/store_proxy.rb,
lib/rack/attack/path_normalizer.rb,
lib/rack/attack/store_proxy/dalli_proxy.rb,
lib/rack/attack/store_proxy/redis_store_proxy.rb,
lib/rack/attack.rb
Defined Under Namespace
Modules: FallbackPathNormalizer, StoreProxy
Classes: Allow2Ban, Blacklist, Cache, Check, Fail2Ban, Request, Throttle, Track, Whitelist
Constant Summary
collapse
- VERSION =
'4.3.1'
- PathNormalizer =
if defined?(::ActionDispatch::Journey::Router::Utils)
::ActionDispatch::Journey::Router::Utils
elsif defined?(::Journey::Router::Utils)
::Journey::Router::Utils
else
FallbackPathNormalizer
end
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(app) ⇒ Attack
Returns a new instance of Attack.
90
91
92
|
# File 'lib/rack/attack.rb', line 90
def initialize(app)
@app = app
end
|
Class Attribute Details
.blacklisted_response ⇒ Object
Returns the value of attribute blacklisted_response.
21
22
23
|
# File 'lib/rack/attack.rb', line 21
def blacklisted_response
@blacklisted_response
end
|
.notifier ⇒ Object
Returns the value of attribute notifier.
21
22
23
|
# File 'lib/rack/attack.rb', line 21
def notifier
@notifier
end
|
.throttled_response ⇒ Object
Returns the value of attribute throttled_response.
21
22
23
|
# File 'lib/rack/attack.rb', line 21
def throttled_response
@throttled_response
end
|
Class Method Details
.blacklist(name, &block) ⇒ Object
27
28
29
|
# File 'lib/rack/attack.rb', line 27
def blacklist(name, &block)
self.blacklists[name] = Blacklist.new(name, block)
end
|
.blacklisted?(req) ⇒ Boolean
50
51
52
53
54
|
# File 'lib/rack/attack.rb', line 50
def blacklisted?(req)
blacklists.any? do |name, blacklist|
blacklist[req]
end
end
|
.blacklists ⇒ Object
40
|
# File 'lib/rack/attack.rb', line 40
def blacklists; @blacklists ||= {}; end
|
.cache ⇒ Object
72
73
74
|
# File 'lib/rack/attack.rb', line 72
def cache
@cache ||= Cache.new
end
|
.clear! ⇒ Object
76
77
78
|
# File 'lib/rack/attack.rb', line 76
def clear!
@whitelists, @blacklists, @throttles, @tracks = {}, {}, {}, {}
end
|
.instrument(req) ⇒ Object
68
69
70
|
# File 'lib/rack/attack.rb', line 68
def instrument(req)
notifier.instrument('rack.attack', req) if notifier
end
|
.throttle(name, options, &block) ⇒ Object
31
32
33
|
# File 'lib/rack/attack.rb', line 31
def throttle(name, options, &block)
self.throttles[name] = Throttle.new(name, options, block)
end
|
.throttled?(req) ⇒ Boolean
56
57
58
59
60
|
# File 'lib/rack/attack.rb', line 56
def throttled?(req)
throttles.any? do |name, throttle|
throttle[req]
end
end
|
.throttles ⇒ Object
41
|
# File 'lib/rack/attack.rb', line 41
def throttles; @throttles ||= {}; end
|
.track(name, options = {}, &block) ⇒ Object
35
36
37
|
# File 'lib/rack/attack.rb', line 35
def track(name, options = {}, &block)
self.tracks[name] = Track.new(name, options, block)
end
|
.tracked?(req) ⇒ Boolean
62
63
64
65
66
|
# File 'lib/rack/attack.rb', line 62
def tracked?(req)
tracks.each_value do |tracker|
tracker[req]
end
end
|
.tracks ⇒ Object
42
|
# File 'lib/rack/attack.rb', line 42
def tracks; @tracks ||= {}; end
|
.whitelist(name, &block) ⇒ Object
23
24
25
|
# File 'lib/rack/attack.rb', line 23
def whitelist(name, &block)
self.whitelists[name] = Whitelist.new(name, block)
end
|
.whitelisted?(req) ⇒ Boolean
44
45
46
47
48
|
# File 'lib/rack/attack.rb', line 44
def whitelisted?(req)
whitelists.any? do |name, whitelist|
whitelist[req]
end
end
|
.whitelists ⇒ Object
39
|
# File 'lib/rack/attack.rb', line 39
def whitelists; @whitelists ||= {}; end
|
Instance Method Details
#call(env) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/rack/attack.rb', line 94
def call(env)
env['PATH_INFO'] = PathNormalizer.normalize_path(env['PATH_INFO'])
req = Rack::Attack::Request.new(env)
if whitelisted?(req)
@app.call(env)
elsif blacklisted?(req)
self.class.blacklisted_response.call(env)
elsif throttled?(req)
self.class.throttled_response.call(env)
else
tracked?(req)
@app.call(env)
end
end
|