Class: Spanx::Actor::Analyzer

Inherits:
Object
  • Object
show all
Includes:
Helper::Timing
Defined in:
lib/spanx/actor/analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::Timing

#period_marker

Constructor Details

#initialize(config) ⇒ Analyzer

Returns a new instance of Analyzer.



16
17
18
19
20
21
22
23
24
# File 'lib/spanx/actor/analyzer.rb', line 16

def initialize config
  @config = config
  @audit_file = config[:audit_file]
  @notifiers = []
  initialize_notifiers(config) if config[:analyzer][:blocked_ip_notifiers]

  @blocked_ips = []
  @previously_blocked_ips = []
end

Instance Attribute Details

#blocked_ipsObject

Returns the value of attribute blocked_ips.



14
15
16
# File 'lib/spanx/actor/analyzer.rb', line 14

def blocked_ips
  @blocked_ips
end

#configObject

Returns the value of attribute config.



14
15
16
# File 'lib/spanx/actor/analyzer.rb', line 14

def config
  @config
end

#notifiersObject

Returns the value of attribute notifiers.



14
15
16
# File 'lib/spanx/actor/analyzer.rb', line 14

def notifiers
  @notifiers
end

Instance Method Details

#analyze_all_ipsObject

Look through every IP on the stack. IPs that fulfill a PeriodCheck are pushed onto a redis block list.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/spanx/actor/analyzer.rb', line 39

def analyze_all_ips
  return unless Spanx::IPChecker.enabled?

  @previously_blocked_ips = Spanx::IPChecker.rate_limited_identifiers
  ips = Spanx::IPChecker.tracked_identifiers

  Logger.logging "analyzed #{ips.size} IPs" do
    ips.each do |ip|
      blocked_ip = Spanx::IPChecker.new(ip).analyze
      blocked_ips << blocked_ip if blocked_ip
    end
  end

  Logger.log "blocking [#{blocked_ips.size}] ips" unless blocked_ips.empty?
  call_notifiers(blocked_ips)
  blocked_ips.clear
end

#runObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/spanx/actor/analyzer.rb', line 26

def run
  Thread.new do
    Thread.current[:name] = "analyzer"
    Logger.log "starting analyzer loop..."
    loop do
      analyze_all_ips()
      sleep config[:analyzer][:analyze_interval]
    end
  end
end