Class: ThrottleMachines::RackMiddleware::Fail2Ban
- Inherits:
-
Object
- Object
- ThrottleMachines::RackMiddleware::Fail2Ban
- Defined in:
- lib/throttle_machines/rack_middleware/fail2_ban.rb
Instance Attribute Summary collapse
-
#bantime ⇒ Object
readonly
Returns the value of attribute bantime.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#findtime ⇒ Object
readonly
Returns the value of attribute findtime.
-
#maxretry ⇒ Object
readonly
Returns the value of attribute maxretry.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #banned?(request) ⇒ Boolean
- #count(request) ⇒ Object
-
#initialize(name, options, &block) ⇒ Fail2Ban
constructor
A new instance of Fail2Ban.
Constructor Details
#initialize(name, options, &block) ⇒ Fail2Ban
Returns a new instance of Fail2Ban.
8 9 10 11 12 13 14 15 |
# File 'lib/throttle_machines/rack_middleware/fail2_ban.rb', line 8 def initialize(name, , &block) @name = name @block = block @maxretry = [:maxretry] || 5 @findtime = [:findtime] || 60 @bantime = [:bantime] || 300 end |
Instance Attribute Details
#bantime ⇒ Object (readonly)
Returns the value of attribute bantime.
6 7 8 |
# File 'lib/throttle_machines/rack_middleware/fail2_ban.rb', line 6 def bantime @bantime end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
6 7 8 |
# File 'lib/throttle_machines/rack_middleware/fail2_ban.rb', line 6 def block @block end |
#findtime ⇒ Object (readonly)
Returns the value of attribute findtime.
6 7 8 |
# File 'lib/throttle_machines/rack_middleware/fail2_ban.rb', line 6 def findtime @findtime end |
#maxretry ⇒ Object (readonly)
Returns the value of attribute maxretry.
6 7 8 |
# File 'lib/throttle_machines/rack_middleware/fail2_ban.rb', line 6 def maxretry @maxretry end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/throttle_machines/rack_middleware/fail2_ban.rb', line 6 def name @name end |
Instance Method Details
#banned?(request) ⇒ Boolean
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/throttle_machines/rack_middleware/fail2_ban.rb', line 17 def banned?(request) discriminator = discriminator_for(request) return false unless discriminator key = "fail2ban:#{@name}:#{discriminator}" # Use a globally managed BreakerMachines circuit as the ban mechanism breaker = BreakerMachines::Registry.instance.get_or_create_dynamic_circuit( key, self, failure_threshold: @maxretry, failure_window: @findtime, reset_timeout: @bantime ) # Check if circuit is open (banned) if breaker.open? stats = breaker.stats now = BreakerMachines.monotonic_time time_until_unban = if stats.opened_at remaining = @bantime - (now - stats.opened_at) remaining.positive? ? remaining : 0 else @bantime end request.env['rack.attack.matched'] = @name request.env['rack.attack.match_type'] = :fail2ban request.env['rack.attack.match_discriminator'] = discriminator request.env['rack.attack.match_data'] = { discriminator: discriminator, maxretry: @maxretry, findtime: @findtime, bantime: @bantime, failures: stats.failure_count, time_until_unban: time_until_unban } ThrottleMachines::RackMiddleware.instrument(request) true else false end end |
#count(request) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/throttle_machines/rack_middleware/fail2_ban.rb', line 62 def count(request) discriminator = discriminator_for(request) return unless discriminator key = "fail2ban:#{@name}:#{discriminator}" # Use the breaker to record a failure if block returns true return unless yield breaker = BreakerMachines::Registry.instance.get_or_create_dynamic_circuit( key, self, failure_threshold: @maxretry, failure_window: @findtime, reset_timeout: @bantime ) # Record failure by trying to call through the breaker # and letting it fail begin breaker.call { raise 'Fail2Ban failure' } rescue StandardError # Expected - this records the failure end end |