Class: Rex::Proto::Http::HttpLoggerSubscriber

Inherits:
HttpSubscriber show all
Defined in:
lib/rex/proto/http/http_logger_subscriber.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger:) ⇒ HttpLoggerSubscriber

Returns a new instance of HttpLoggerSubscriber.

Raises:

  • (RuntimeError)


12
13
14
15
# File 'lib/rex/proto/http/http_logger_subscriber.rb', line 12

def initialize(logger:)
  raise RuntimeError, "Incompatible logger" unless logger.respond_to?(:print_line) && logger.respond_to?(:datastore)
  @logger = logger
end

Instance Method Details

#on_request(request) ⇒ Object

Parameters:



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rex/proto/http/http_logger_subscriber.rb', line 18

def on_request(request)
  if @logger.datastore['HttpTrace']
    http_trace_colors = @logger.datastore['HttpTraceColors'].blank? ? 'red/blu' : @logger.datastore['HttpTraceColors'] # Set the default colors if none were provided.
    http_trace_colors += '/' if http_trace_colors.count('/') == 0 # Append "/"" to the end of the string if no "/" were found in the string to ensure consistent formatting.
    request_color, response_color = http_trace_colors.gsub('/', ' / ').split('/').map { |color| color&.strip.blank? ? '' : "%bld%#{color.strip}" }
    request = request.to_s(headers_only: @logger.datastore['HttpTraceHeadersOnly'])
    @logger.print_line("#"*20)
    @logger.print_line("# Request:")
    @logger.print_line("#"*20)
    @logger.print_line("%clr#{request_color}#{request}%clr")
  end
end

#on_response(response) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rex/proto/http/http_logger_subscriber.rb', line 32

def on_response(response)
  if @logger.datastore['HttpTrace']
    http_trace_colors = @logger.datastore['HttpTraceColors'].blank? ? 'red/blu' : @logger.datastore['HttpTraceColors'] # Set the default colors if none were provided.
    http_trace_colors += '/' if http_trace_colors.count('/') == 0 # Append "/"" to the end of the string if no "/" were found in the string to ensure consistent formatting.
    request_color, response_color = http_trace_colors.gsub('/', ' / ').split('/').map { |color| color&.strip.blank? ? '' : "%bld%#{color.strip}" }
    @logger.print_line("#"*20)
    @logger.print_line("# Response:")
    @logger.print_line("#"*20)
    if response
      response = response.to_terminal_output(headers_only: @logger.datastore['HttpTraceHeadersOnly'])
      @logger.print_line("%clr#{response_color}#{response}%clr")
    else
      @logger.print_line("No response received")
    end
  end
end