Class: HiddenHippo::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/hidden_hippo/scanner.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, packet_class, *extractors) ⇒ Scanner

Returns a new instance of Scanner.



5
6
7
8
9
# File 'lib/hidden_hippo/scanner.rb', line 5

def initialize(file, packet_class, *extractors)
  @file = file
  @extractors = extractors
  @packet_class = packet_class
end

Instance Method Details

#callObject



11
12
13
14
15
16
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
# File 'lib/hidden_hippo/scanner.rb', line 11

def call
  # call Tshark
  tshark_fields = @packet_class.tshark_fields

  args = [
      '-2', '-Tfields', '-q',
      '-r', @file,
      '-R', @packet_class.filter,
      *tshark_fields.map {|f| ['-e', f]}.flatten
  ]

  Open3.popen3(%w(tshark tshark), *args) do |stdin, stdout, stderr, waiter|
    # we don't need those
    stdin.close

    stdout.each do |line|
      if line.count("\t") != tshark_fields.size - 1
        puts 'Warinig: tshark returned a line of the wrong size. Ignoring it.'
        puts "Offending line: #{line}"
        next
      end

      split_line = line.chomp.split("\t")
                   .map(&:rstrip).map(&:lstrip)
                   .map {|f| f.empty? ? nil : f}

      assoc = tshark_fields.zip split_line
      packet = @packet_class.parse Hash[*assoc.flatten]

      @extractors.each do |extractor|
        extractor.call(packet)
      end
    end

    if waiter.value != 0
      puts "Warning: tshark exited with status code #{waiter.value}."
      puts "tshark #{args.join(' ')}"
      puts stderr.read
    end
  end
end