mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-27 15:07:41 +00:00
Make store proxies lookup dynamic
This commit is contained in:
parent
a4ea2146dd
commit
edaa6c6a91
10 changed files with 51 additions and 41 deletions
|
|
@ -6,6 +6,12 @@ require 'rack/attack/cache'
|
||||||
require 'rack/attack/configuration'
|
require 'rack/attack/configuration'
|
||||||
require 'rack/attack/path_normalizer'
|
require 'rack/attack/path_normalizer'
|
||||||
require 'rack/attack/request'
|
require 'rack/attack/request'
|
||||||
|
require 'rack/attack/store_proxy/dalli_proxy'
|
||||||
|
require 'rack/attack/store_proxy/mem_cache_store_proxy'
|
||||||
|
require 'rack/attack/store_proxy/redis_proxy'
|
||||||
|
require 'rack/attack/store_proxy/redis_store_proxy'
|
||||||
|
require 'rack/attack/store_proxy/redis_cache_store_proxy'
|
||||||
|
require 'rack/attack/store_proxy/active_support_redis_store_proxy'
|
||||||
|
|
||||||
require 'rack/attack/railtie' if defined?(::Rails)
|
require 'rack/attack/railtie' if defined?(::Rails)
|
||||||
|
|
||||||
|
|
@ -21,13 +27,6 @@ module Rack
|
||||||
autoload :Safelist, 'rack/attack/safelist'
|
autoload :Safelist, 'rack/attack/safelist'
|
||||||
autoload :Blocklist, 'rack/attack/blocklist'
|
autoload :Blocklist, 'rack/attack/blocklist'
|
||||||
autoload :Track, 'rack/attack/track'
|
autoload :Track, 'rack/attack/track'
|
||||||
autoload :StoreProxy, 'rack/attack/store_proxy'
|
|
||||||
autoload :DalliProxy, 'rack/attack/store_proxy/dalli_proxy'
|
|
||||||
autoload :MemCacheStoreProxy, 'rack/attack/store_proxy/mem_cache_store_proxy'
|
|
||||||
autoload :RedisProxy, 'rack/attack/store_proxy/redis_proxy'
|
|
||||||
autoload :RedisStoreProxy, 'rack/attack/store_proxy/redis_store_proxy'
|
|
||||||
autoload :RedisCacheStoreProxy, 'rack/attack/store_proxy/redis_cache_store_proxy'
|
|
||||||
autoload :ActiveSupportRedisStoreProxy, 'rack/attack/store_proxy/active_support_redis_store_proxy'
|
|
||||||
autoload :Fail2Ban, 'rack/attack/fail2ban'
|
autoload :Fail2Ban, 'rack/attack/fail2ban'
|
||||||
autoload :Allow2Ban, 'rack/attack/allow2ban'
|
autoload :Allow2Ban, 'rack/attack/allow2ban'
|
||||||
|
|
||||||
|
|
|
||||||
27
lib/rack/attack/base_proxy.rb
Normal file
27
lib/rack/attack/base_proxy.rb
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'delegate'
|
||||||
|
|
||||||
|
module Rack
|
||||||
|
class Attack
|
||||||
|
class BaseProxy < SimpleDelegator
|
||||||
|
class << self
|
||||||
|
def proxies
|
||||||
|
@@proxies ||= []
|
||||||
|
end
|
||||||
|
|
||||||
|
def inherited(klass)
|
||||||
|
proxies << klass
|
||||||
|
end
|
||||||
|
|
||||||
|
def lookup(store)
|
||||||
|
proxies.find { |proxy| proxy.handle?(store) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle?(_store)
|
||||||
|
raise NotImplementedError
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -13,7 +13,12 @@ module Rack
|
||||||
|
|
||||||
attr_reader :store
|
attr_reader :store
|
||||||
def store=(store)
|
def store=(store)
|
||||||
@store = StoreProxy.build(store)
|
@store =
|
||||||
|
if (proxy = BaseProxy.lookup(store))
|
||||||
|
proxy.new(store)
|
||||||
|
else
|
||||||
|
store
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def count(unprefixed_key, period)
|
def count(unprefixed_key, period)
|
||||||
|
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module Rack
|
|
||||||
class Attack
|
|
||||||
module StoreProxy
|
|
||||||
PROXIES = [
|
|
||||||
DalliProxy,
|
|
||||||
MemCacheStoreProxy,
|
|
||||||
RedisStoreProxy,
|
|
||||||
RedisProxy,
|
|
||||||
RedisCacheStoreProxy,
|
|
||||||
ActiveSupportRedisStoreProxy
|
|
||||||
].freeze
|
|
||||||
|
|
||||||
def self.build(store)
|
|
||||||
klass = PROXIES.find { |proxy| proxy.handle?(store) }
|
|
||||||
klass ? klass.new(store) : store
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'delegate'
|
require 'rack/attack/base_proxy'
|
||||||
|
|
||||||
module Rack
|
module Rack
|
||||||
class Attack
|
class Attack
|
||||||
module StoreProxy
|
module StoreProxy
|
||||||
class ActiveSupportRedisStoreProxy < SimpleDelegator
|
class ActiveSupportRedisStoreProxy < BaseProxy
|
||||||
def self.handle?(store)
|
def self.handle?(store)
|
||||||
defined?(::Redis) &&
|
defined?(::Redis) &&
|
||||||
defined?(::ActiveSupport::Cache::RedisStore) &&
|
defined?(::ActiveSupport::Cache::RedisStore) &&
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'delegate'
|
require 'rack/attack/base_proxy'
|
||||||
|
|
||||||
module Rack
|
module Rack
|
||||||
class Attack
|
class Attack
|
||||||
module StoreProxy
|
module StoreProxy
|
||||||
class DalliProxy < SimpleDelegator
|
class DalliProxy < BaseProxy
|
||||||
def self.handle?(store)
|
def self.handle?(store)
|
||||||
return false unless defined?(::Dalli)
|
return false unless defined?(::Dalli)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'delegate'
|
require 'rack/attack/base_proxy'
|
||||||
|
|
||||||
module Rack
|
module Rack
|
||||||
class Attack
|
class Attack
|
||||||
module StoreProxy
|
module StoreProxy
|
||||||
class MemCacheStoreProxy < SimpleDelegator
|
class MemCacheStoreProxy < BaseProxy
|
||||||
def self.handle?(store)
|
def self.handle?(store)
|
||||||
defined?(::Dalli) &&
|
defined?(::Dalli) &&
|
||||||
defined?(::ActiveSupport::Cache::MemCacheStore) &&
|
defined?(::ActiveSupport::Cache::MemCacheStore) &&
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'delegate'
|
require 'rack/attack/base_proxy'
|
||||||
|
|
||||||
module Rack
|
module Rack
|
||||||
class Attack
|
class Attack
|
||||||
module StoreProxy
|
module StoreProxy
|
||||||
class RedisCacheStoreProxy < SimpleDelegator
|
class RedisCacheStoreProxy < BaseProxy
|
||||||
def self.handle?(store)
|
def self.handle?(store)
|
||||||
store.class.name == "ActiveSupport::Cache::RedisCacheStore"
|
store.class.name == "ActiveSupport::Cache::RedisCacheStore"
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'delegate'
|
require 'rack/attack/base_proxy'
|
||||||
|
|
||||||
module Rack
|
module Rack
|
||||||
class Attack
|
class Attack
|
||||||
module StoreProxy
|
module StoreProxy
|
||||||
class RedisProxy < SimpleDelegator
|
class RedisProxy < BaseProxy
|
||||||
def initialize(*args)
|
def initialize(*args)
|
||||||
if Gem::Version.new(Redis::VERSION) < Gem::Version.new("3")
|
if Gem::Version.new(Redis::VERSION) < Gem::Version.new("3")
|
||||||
warn 'RackAttack requires Redis gem >= 3.0.0.'
|
warn 'RackAttack requires Redis gem >= 3.0.0.'
|
||||||
|
|
@ -15,7 +15,7 @@ module Rack
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.handle?(store)
|
def self.handle?(store)
|
||||||
defined?(::Redis) && store.is_a?(::Redis)
|
defined?(::Redis) && store.class == ::Redis
|
||||||
end
|
end
|
||||||
|
|
||||||
def read(key)
|
def read(key)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'delegate'
|
require 'rack/attack/store_proxy/redis_proxy'
|
||||||
|
|
||||||
module Rack
|
module Rack
|
||||||
class Attack
|
class Attack
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue