Class: Rack::Attack

Inherits:
Object
  • 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/store_proxy/dalli_proxy.rb,
lib/rack/attack/store_proxy/redis_store_proxy.rb,
lib/rack/attack.rb

Defined Under Namespace

Modules: StoreProxy Classes: Allow2Ban, Blacklist, Cache, Check, Fail2Ban, Request, Throttle, Track, Whitelist

Constant Summary collapse

VERSION =
'4.3.0'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Attack

Returns a new instance of Attack.



89
90
91
# File 'lib/rack/attack.rb', line 89

def initialize(app)
  @app = app
end

Class Attribute Details

.blacklisted_responseObject

Returns the value of attribute blacklisted_response.



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

def blacklisted_response
  @blacklisted_response
end

.notifierObject

Returns the value of attribute notifier.



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

def notifier
  @notifier
end

.throttled_responseObject

Returns the value of attribute throttled_response.



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

def throttled_response
  @throttled_response
end

Class Method Details

.blacklist(name, &block) ⇒ Object



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

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

.blacklisted?(req) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/rack/attack.rb', line 49

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

.blacklistsObject



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

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

.cacheObject



71
72
73
# File 'lib/rack/attack.rb', line 71

def cache
  @cache ||= Cache.new
end

.clear!Object



75
76
77
# File 'lib/rack/attack.rb', line 75

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

.instrument(req) ⇒ Object



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

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

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



30
31
32
# File 'lib/rack/attack.rb', line 30

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

.throttled?(req) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/rack/attack.rb', line 55

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

.throttlesObject



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

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

.track(name, options = {}, &block) ⇒ Object



34
35
36
# File 'lib/rack/attack.rb', line 34

def track(name, options = {}, &block)
  self.tracks[name] = Track.new(name, options, block)
end

.tracked?(req) ⇒ Boolean

Returns:

  • (Boolean)


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

def tracked?(req)
  tracks.each_value do |tracker|
    tracker[req]
  end
end

.tracksObject



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

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

.whitelist(name, &block) ⇒ Object



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

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

.whitelisted?(req) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/rack/attack.rb', line 43

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

.whitelistsObject



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

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

Instance Method Details

#call(env) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rack/attack.rb', line 93

def call(env)
  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