Class: ThrottleMachines::RackMiddleware::Fail2Ban

Inherits:
Object
  • Object
show all
Defined in:
lib/throttle_machines/rack_middleware/fail2_ban.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options, &block)
  @name = name
  @block = block

  @maxretry = options[:maxretry] || 5
  @findtime = options[:findtime] || 60
  @bantime = options[:bantime] || 300
end

Instance Attribute Details

#bantimeObject (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

#blockObject (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

#findtimeObject (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

#maxretryObject (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

#nameObject (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

Returns:

  • (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