Class: BetterCap::StreamLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/proxy/stream_logger.rb

Overview

Raw or http streams pretty logging.

Constant Summary collapse

@@MAX_REQ_SIZE =
50
@@CODE_COLORS =
{
  '2' => :green,
  '3' => :light_black,
  '4' => :yellow,
  '5' => :red
}

Class Method Summary collapse

Class Method Details

.addr2s(addr) ⇒ Object

Search for the addr IP address inside the list of collected targets and return its compact string representation ( @see BetterCap::Target#to_s_compact ).


28
29
30
31
32
33
34
35
36
37
# File 'lib/bettercap/proxy/stream_logger.rb', line 28

def self.addr2s( addr )
  ctx = Context.get

  return 'local' if addr == ctx.ifconfig[:ip_saddr]

  target = ctx.find_target addr, nil
  return target.to_s_compact unless target.nil?

  addr
end

.log_http(is_https, client, request, response) ⇒ Object

Log a HTTP ( HTTPS if is_https is true ) stream performed by the client with the request and response most important informations.


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bettercap/proxy/stream_logger.rb', line 49

def self.log_http( is_https, client, request, response )
  request_s  = "#{is_https ? 'https' : 'http'}://#{request.host}#{request.url}"
  response_s = "( #{response.content_type} )"
  request_s  = request_s.slice(0..@@MAX_REQ_SIZE) + '...' unless request_s.length <= @@MAX_REQ_SIZE
  code       = response.code[0]

  if @@CODE_COLORS.has_key? code
    response_s += " [#{response.code}]".send( @@CODE_COLORS[ code ] )
  else
    response_s += " [#{response.code}]"
  end

  Logger.raw "[#{self.addr2s(client)}] #{request.verb.light_blue} #{request_s} #{response_s}"
end

.log_raw(pkt, label, payload) ⇒ Object

Log a raw packet ( pkt ) data payload using the specified label.


40
41
42
43
44
45
# File 'lib/bettercap/proxy/stream_logger.rb', line 40

def self.log_raw( pkt, label, payload )
  nl    = if label.include?"\n" then "\n" else " " end
  label = label.strip
  Logger.raw( "[#{self.addr2s(pkt.ip_saddr)} > #{self.addr2s(pkt.ip_daddr)}:#{pkt.tcp_dst}] " \
             "[#{label.green}]#{nl}#{payload.strip.yellow}" )
end