mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-27 15:07:41 +00:00
refactor: remove repeated initialization of anonymous_blocklists/safelists
This commit is contained in:
parent
6addaa11d0
commit
9a726bd29b
1 changed files with 9 additions and 21 deletions
|
|
@ -27,7 +27,7 @@ class Rack::Attack
|
||||||
autoload :Allow2Ban, 'rack/attack/allow2ban'
|
autoload :Allow2Ban, 'rack/attack/allow2ban'
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :notifier, :blocklisted_response, :throttled_response
|
attr_accessor :notifier, :blocklisted_response, :throttled_response, :anonymous_blocklists, :anonymous_safelists
|
||||||
|
|
||||||
def safelist(name = nil, &block)
|
def safelist(name = nil, &block)
|
||||||
safelist = Safelist.new(name, block)
|
safelist = Safelist.new(name, block)
|
||||||
|
|
@ -35,8 +35,7 @@ class Rack::Attack
|
||||||
if name
|
if name
|
||||||
self.safelists[name] = safelist
|
self.safelists[name] = safelist
|
||||||
else
|
else
|
||||||
@anonymous_safelists ||= []
|
anonymous_safelists << safelist
|
||||||
@anonymous_safelists << safelist
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -46,21 +45,18 @@ class Rack::Attack
|
||||||
if name
|
if name
|
||||||
self.blocklists[name] = blocklist
|
self.blocklists[name] = blocklist
|
||||||
else
|
else
|
||||||
@anonymous_blocklists ||= []
|
anonymous_blocklists << blocklist
|
||||||
@anonymous_blocklists << blocklist
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def blocklist_ip(ip_address)
|
def blocklist_ip(ip_address)
|
||||||
@anonymous_blocklists ||= []
|
|
||||||
ip_blocklist_proc = lambda { |request| IPAddr.new(ip_address).include?(IPAddr.new(request.ip)) }
|
ip_blocklist_proc = lambda { |request| IPAddr.new(ip_address).include?(IPAddr.new(request.ip)) }
|
||||||
@anonymous_blocklists << Blocklist.new(nil, ip_blocklist_proc)
|
anonymous_blocklists << Blocklist.new(nil, ip_blocklist_proc)
|
||||||
end
|
end
|
||||||
|
|
||||||
def safelist_ip(ip_address)
|
def safelist_ip(ip_address)
|
||||||
@anonymous_safelists ||= []
|
|
||||||
ip_safelist_proc = lambda { |request| IPAddr.new(ip_address).include?(IPAddr.new(request.ip)) }
|
ip_safelist_proc = lambda { |request| IPAddr.new(ip_address).include?(IPAddr.new(request.ip)) }
|
||||||
@anonymous_safelists << Safelist.new(nil, ip_safelist_proc)
|
anonymous_safelists << Safelist.new(nil, ip_safelist_proc)
|
||||||
end
|
end
|
||||||
|
|
||||||
def throttle(name, options, &block)
|
def throttle(name, options, &block)
|
||||||
|
|
@ -111,27 +107,19 @@ class Rack::Attack
|
||||||
|
|
||||||
def clear_configuration
|
def clear_configuration
|
||||||
@safelists, @blocklists, @throttles, @tracks = {}, {}, {}, {}
|
@safelists, @blocklists, @throttles, @tracks = {}, {}, {}, {}
|
||||||
@anonymous_blocklists = []
|
self.anonymous_blocklists = []
|
||||||
@anonymous_safelists = []
|
self.anonymous_safelists = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear!
|
def clear!
|
||||||
warn "[DEPRECATION] Rack::Attack.clear! is deprecated. Please use Rack::Attack.clear_configuration instead"
|
warn "[DEPRECATION] Rack::Attack.clear! is deprecated. Please use Rack::Attack.clear_configuration instead"
|
||||||
clear_configuration
|
clear_configuration
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def anonymous_blocklists
|
|
||||||
@anonymous_blocklists ||= []
|
|
||||||
end
|
|
||||||
|
|
||||||
def anonymous_safelists
|
|
||||||
@anonymous_safelists ||= []
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Set defaults
|
# Set defaults
|
||||||
|
@anonymous_blocklists = []
|
||||||
|
@anonymous_safelists = []
|
||||||
@notifier = ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
|
@notifier = ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
|
||||||
@blocklisted_response = lambda { |_env| [403, { 'Content-Type' => 'text/plain' }, ["Forbidden\n"]] }
|
@blocklisted_response = lambda { |_env| [403, { 'Content-Type' => 'text/plain' }, ["Forbidden\n"]] }
|
||||||
@throttled_response = lambda { |env|
|
@throttled_response = lambda { |env|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue