Module: PatronusFati::MessageProcessor

Extended by:
FactoryBase
Included in:
Alert, Bssid, Capability, Client, Critfail, Error, Protocols, Ssid, Terminate
Defined in:
lib/patronus_fati/message_processor.rb

Defined Under Namespace

Modules: Alert, Bssid, Capability, Client, Critfail, Error, Protocols, Ssid, Terminate

Class Method Summary collapse

Methods included from FactoryBase

class_to_name, factory, ignored_types, included, registered_factories

Class Method Details

.cleanup_modelsvoid



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/patronus_fati/message_processor.rb', line 5

def self.cleanup_models
  @next_cleanup ||= Time.now.to_i + 60

  if @next_cleanup <= Time.now.to_i
    @next_cleanup = Time.now.to_i + 60

    close_inactive_connections

    offline_access_points
    offline_clients
  end
end

.close_inactive_connectionsvoid



18
19
20
21
22
23
24
# File 'lib/patronus_fati/message_processor.rb', line 18

def self.close_inactive_connections
  DataModels::Connection.instances.each do |_, connection|
    connection.announce_changes
  end

  DataModels::Connection.instances.reject! { |_, conn| conn.presence.dead? }
end

.handle(message_obj) ⇒ void



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/patronus_fati/message_processor.rb', line 70

def self.handle(message_obj)
  if !PatronusFati.past_initial_flood? && @last_msg_received && (Time.now.to_f - @last_msg_received) >= 0.8
    PatronusFati.past_initial_flood!
  end
  @last_msg_received = Time.now.to_f

  periodic_flush
  report_recently_seen
  result = factory(class_to_name(message_obj), message_obj)
  cleanup_models
  result
rescue => e
  PatronusFati.logger.error('Error processing the following message object:')
  PatronusFati.logger.error(message_obj.inspect)
  PatronusFati.logger.error('%s: %s' % [e.class, e.message])
  e.backtrace.each do |l|
    PatronusFati.logger.error(l)
  end

  # Need to ensure our backtrace doesn't get sent to kismet
  nil
end

.ignored_typesvoid



93
94
95
96
# File 'lib/patronus_fati/message_processor.rb', line 93

def self.ignored_types
  [:ack, :battery, :bssidsrc, :channel, :clisrc, :gps, :info, :kismet,
   :plugin, :source, :status, :time]
end

.offline_access_pointsvoid



26
27
28
29
30
31
32
33
# File 'lib/patronus_fati/message_processor.rb', line 26

def self.offline_access_points
  DataModels::AccessPoint.instances.each do |bssid, access_point|
    access_point.cleanup_ssids
    access_point.announce_changes
  end

  DataModels::AccessPoint.instances.reject! { |_, ap| ap.presence.dead? }
end

.offline_clientsvoid



35
36
37
38
39
40
41
42
# File 'lib/patronus_fati/message_processor.rb', line 35

def self.offline_clients
  DataModels::Client.instances.each do |_, client|
    client.cleanup_probes
    client.announce_changes
  end

  DataModels::Client.instances.reject! { |_, ap| ap.presence.dead? }
end

.periodic_flushvoid



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/patronus_fati/message_processor.rb', line 98

def self.periodic_flush
  @next_sync ||= Time.now.to_i + 300

  if @next_sync <= Time.now.to_i
    # Add a variability of +/- half an hour within a day
    @next_sync = Time.now.to_i + 84600 + rand(3600)

    access_points = []
    clients = []

    PatronusFati::DataModels::AccessPoint.instances.each do |bssid, access_point|
      next unless access_point.active?
      PatronusFati.event_handler.event(:access_point, :sync, access_point.full_state, access_point.diagnostic_data)
      access_points << bssid
    end

    PatronusFati::DataModels::Client.instances.each do |mac, client|
      next unless client.active?
      PatronusFati.event_handler.event(:client, :sync, client.full_state, client.diagnostic_data)
      clients << mac
    end

    all_online = { access_points: access_points, clients: clients }
    PatronusFati.event_handler.event(:both, :sync, all_online)
  end
end

.report_recently_seenvoid



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/patronus_fati/message_processor.rb', line 44

def self.report_recently_seen
  @next_recent_msg ||= Time.now.to_i + 240

  if @next_recent_msg <= Time.now.to_i
    @next_recent_msg = Time.now.to_i + 240
    cutoff_time = Time.now.to_i - 300

    aps = DataModels::AccessPoint.instances.map do |bssid, ap|
      next unless ap.active? && ap.presence.visible_since?(cutoff_time)
      bssid
    end.compact

    clients = DataModels::Client.instances.map do |mac, client|
      next unless client.active? && client.presence.visible_since?(cutoff_time)
      mac
    end.compact

    return if clients.empty? && aps.empty?
    PatronusFati.event_handler.event(
      :sync,
      :recent,
      { access_points: aps, clients: clients }
    )
  end
end