Class: BetterCap::Parsers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/sniffer/parsers/base.rb

Overview

Base class for BetterCap::Parsers.

Constant Summary collapse

@@loaded =

Hash of available parsers ( parser name -> class name )

{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Initialize this parser.



70
71
72
73
# File 'lib/bettercap/sniffer/parsers/base.rb', line 70

def initialize
  @filters = []
  @name = 'BASE'
end

Class Method Details

.availableObject

Return a list of available parsers names.



30
31
32
# File 'lib/bettercap/sniffer/parsers/base.rb', line 30

def available
  @@loaded.keys
end

.from_cmdline(v) ⇒ Object

Parse the v command line argument and return a list of parser names. Will raise BetterCap::Error if one or more parser names are not valid.

Raises:



36
37
38
39
40
41
42
43
44
45
# File 'lib/bettercap/sniffer/parsers/base.rb', line 36

def from_cmdline(v)
  raise BetterCap::Error, "No parser names provided" if v.nil?

  avail = available
  list = v.split(',').collect(&:strip).collect(&:upcase).reject{ |c| c.empty? }
  list.each do |parser|
    raise BetterCap::Error, "Invalid parser name '#{parser}'." unless avail.include?(parser) or parser == '*'
  end
  list
end

.inherited(subclass) ⇒ Object

Called when this base class is inherited from one of the parsers.



22
23
24
25
26
27
# File 'lib/bettercap/sniffer/parsers/base.rb', line 22

def inherited(subclass)
  name = subclass.name.split('::')[2].upcase
  if name != 'CUSTOM'
    @@loaded[name] = subclass.name
  end
end

.load_by_names(parsers) ⇒ Object

Return a list of BetterCap::Parsers instances by their parsers names.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bettercap/sniffer/parsers/base.rb', line 48

def load_by_names(parsers)
  loaded = []

  @@loaded.each do |name,cname|
    if parsers.include?(name) or parsers == ['*']
      Logger.debug "Loading parser #{name} ( #{cname} ) ..."
      loaded << BetterCap::Loader.load(cname).new
    end
  end

  loaded
end

.load_custom(expression) ⇒ Object

Load and return an instance of the BetterCap::Parsers::Custom parser given the expression Regex object.



63
64
65
66
# File 'lib/bettercap/sniffer/parsers/base.rb', line 63

def load_custom(expression)
  Logger.debug "Loading custom parser: '#{expression}' ..."
  [ BetterCap::Parsers::Custom.new(expression) ]
end

Instance Method Details

#on_packet(pkt) ⇒ Object

This method will be called from the BetterCap::Sniffer for each incoming packet ( +pkt ) and will apply the parser filter to it.



77
78
79
80
81
82
83
84
# File 'lib/bettercap/sniffer/parsers/base.rb', line 77

def on_packet( pkt )
  s = pkt.to_s
  @filters.each do |filter|
    if s =~ filter
      StreamLogger.log_raw( pkt, @name, pkt.payload )
    end
  end
end