mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-03 10:55:52 +00:00
based on gist from @ktheory https://gist.github.com/ktheory/5723534 Modified slightly to use fail2ban `filter` terminology to simplify Rack::Attack initializer configuration (only one block is requred for this approach instead of 2)
42 lines
1.2 KiB
Ruby
42 lines
1.2 KiB
Ruby
module Rack
|
|
module Attack
|
|
class Fail2Ban
|
|
class << self
|
|
def filter(name, discriminator, options)
|
|
bantime = options[:bantime] or raise ArgumentError, "Must pass bantime option"
|
|
findtime = options[:findtime] or raise ArgumentError, "Must pass findtime option"
|
|
maxretry = options[:maxretry] or raise ArgumentError, "Must pass maxretry option"
|
|
|
|
if yield
|
|
fail!(name, discriminator, bantime, findtime, maxretry)
|
|
else
|
|
banned?(discriminator)
|
|
end
|
|
end
|
|
|
|
private
|
|
def fail!(name, discriminator, bantime, findtime, maxretry)
|
|
count = cache.count("#{name}:#{discriminator}", findtime)
|
|
if count >= maxretry
|
|
ban!(discriminator, bantime)
|
|
end
|
|
|
|
# Return true for blacklist
|
|
true
|
|
end
|
|
|
|
def ban!(discriminator, bantime)
|
|
cache.write("fail2ban:#{discriminator}", 1, bantime)
|
|
end
|
|
|
|
def banned?(discriminator)
|
|
cache.read("fail2ban:#{discriminator}")
|
|
end
|
|
|
|
def cache
|
|
Rack::Attack.cache
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|