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/conditional_throttle.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, ConditionalThrottle, Fail2Ban, Request, Throttle, Track, Whitelist

Constant Summary collapse

VERSION =
'4.1.2'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Attack

Returns a new instance of Attack.



98
99
100
# File 'lib/rack/attack.rb', line 98

def initialize(app)
  @app = app
end

Class Attribute Details

.blacklisted_responseObject

Returns the value of attribute blacklisted_response.



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

def blacklisted_response
  @blacklisted_response
end

.notifierObject

Returns the value of attribute notifier.



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

def notifier
  @notifier
end

.throttled_responseObject

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



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

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

.blacklisted?(req) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/rack/attack.rb', line 58

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

.blacklistsObject



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

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

.cacheObject



80
81
82
# File 'lib/rack/attack.rb', line 80

def cache
  @cache ||= Cache.new
end

.clear!Object



84
85
86
# File 'lib/rack/attack.rb', line 84

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

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



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

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

.increment_throttle_counter(name, discriminator) ⇒ Object



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

def increment_throttle_counter(name, discriminator)
  self.throttles[name].increment_counter(discriminator)
end

.instrument(req) ⇒ Object



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

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

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



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

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

.throttled?(req) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/rack/attack.rb', line 64

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

.throttlesObject



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

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

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



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

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

.tracked?(req) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.tracksObject



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

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

.whitelist(name, &block) ⇒ Object



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

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

.whitelisted?(req) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/rack/attack.rb', line 52

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

.whitelistsObject



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

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

Instance Method Details

#call(env) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rack/attack.rb', line 102

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

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