Class: ThrottleMachines::RackMiddleware
- Inherits:
-
Object
- Object
- ThrottleMachines::RackMiddleware
- Extended by:
- Forwardable
- Defined in:
- lib/throttle_machines/rack_middleware.rb,
lib/throttle_machines/rack_middleware/track.rb,
lib/throttle_machines/rack_middleware/request.rb,
lib/throttle_machines/rack_middleware/safelist.rb,
lib/throttle_machines/rack_middleware/throttle.rb,
lib/throttle_machines/rack_middleware/blocklist.rb,
lib/throttle_machines/rack_middleware/fail2_ban.rb,
lib/throttle_machines/rack_middleware/allow2_ban.rb,
lib/throttle_machines/rack_middleware/configuration.rb
Overview
Advanced Rack middleware for rate limiting and request filtering
Defined Under Namespace
Classes: Allow2Ban, Blocklist, Configuration, Fail2Ban, Request, Safelist, Throttle, Track
Class Attribute Summary collapse
-
.configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
.enabled ⇒ Object
Returns the value of attribute enabled.
-
.notifier ⇒ Object
Returns the value of attribute notifier.
Class Method Summary collapse
-
.clear! ⇒ Object
rubocop:enable Rails/Delegate.
- .configure(&block) ⇒ Object
-
.instrument(request) ⇒ Object
Instrument for ActiveSupport::Notifications compatibility.
-
.reset! ⇒ Object
rubocop:disable Rails/Delegate -- Ruby 3.4 compatibility issue with delegate.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ RackMiddleware
constructor
A new instance of RackMiddleware.
Constructor Details
#initialize(app) ⇒ RackMiddleware
Returns a new instance of RackMiddleware.
62 63 64 |
# File 'lib/throttle_machines/rack_middleware.rb', line 62 def initialize(app) @app = app end |
Class Attribute Details
.configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
13 14 15 |
# File 'lib/throttle_machines/rack_middleware.rb', line 13 def configuration @configuration end |
.enabled ⇒ Object
Returns the value of attribute enabled.
12 13 14 |
# File 'lib/throttle_machines/rack_middleware.rb', line 12 def enabled @enabled end |
.notifier ⇒ Object
Returns the value of attribute notifier.
12 13 14 |
# File 'lib/throttle_machines/rack_middleware.rb', line 12 def notifier @notifier end |
Class Method Details
.clear! ⇒ Object
rubocop:enable Rails/Delegate
44 45 46 |
# File 'lib/throttle_machines/rack_middleware.rb', line 44 def clear! @configuration = Configuration.new end |
.configure(&block) ⇒ Object
33 34 35 36 |
# File 'lib/throttle_machines/rack_middleware.rb', line 33 def configure(&block) @configuration ||= Configuration.new @configuration.instance_eval(&block) if block end |
.instrument(request) ⇒ Object
Instrument for ActiveSupport::Notifications compatibility
49 50 51 52 53 54 |
# File 'lib/throttle_machines/rack_middleware.rb', line 49 def instrument(request) return unless notifier event_type = request.env['rack.attack.match_type'] notifier.instrument("#{event_type}.throttle_machines", request: request) end |
.reset! ⇒ Object
rubocop:disable Rails/Delegate -- Ruby 3.4 compatibility issue with delegate
39 40 41 |
# File 'lib/throttle_machines/rack_middleware.rb', line 39 def reset! ThrottleMachines.reset! end |
Instance Method Details
#call(env) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/throttle_machines/rack_middleware.rb', line 66 def call(env) return @app.call(env) if !self.class.enabled || env['rack.attack.called'] env['rack.attack.called'] = true request = Request.new(env) # Always use the current class-level configuration configuration = self.class.configuration if configuration.safelisted?(request) @app.call(env) elsif configuration.blocklisted?(request) configuration.blocklisted_responder.call(request) elsif configuration.throttled?(request) configuration.throttled_responder.call(request) else configuration.tracked?(request) @app.call(env) end end |