CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/proto/http/http_logger_subscriber.rb
Views: 11704
1
# -*- coding: binary -*-
2
3
require 'rex/socket'
4
require 'rex/text'
5
require 'digest'
6
7
module Rex
8
module Proto
9
module Http
10
11
class HttpLoggerSubscriber < HttpSubscriber
12
def initialize(logger:)
13
raise RuntimeError, "Incompatible logger" unless logger.respond_to?(:print_line) && logger.respond_to?(:datastore)
14
@logger = logger
15
end
16
17
# (see Rex::Proto::Http::HttpSubscriber#on_request)
18
def on_request(request)
19
if @logger.datastore['HttpTrace']
20
http_trace_colors = @logger.datastore['HttpTraceColors'].blank? ? 'red/blu' : @logger.datastore['HttpTraceColors'] # Set the default colors if none were provided.
21
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.
22
request_color, response_color = http_trace_colors.gsub('/', ' / ').split('/').map { |color| color&.strip.blank? ? '' : "%bld%#{color.strip}" }
23
request = request.to_s(headers_only: @logger.datastore['HttpTraceHeadersOnly'])
24
@logger.print_line("#"*20)
25
@logger.print_line("# Request:")
26
@logger.print_line("#"*20)
27
@logger.print_line("%clr#{request_color}#{request}%clr")
28
end
29
end
30
31
# (see Rex::Proto::HttpSubscriber#on_response)
32
def on_response(response)
33
if @logger.datastore['HttpTrace']
34
http_trace_colors = @logger.datastore['HttpTraceColors'].blank? ? 'red/blu' : @logger.datastore['HttpTraceColors'] # Set the default colors if none were provided.
35
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.
36
request_color, response_color = http_trace_colors.gsub('/', ' / ').split('/').map { |color| color&.strip.blank? ? '' : "%bld%#{color.strip}" }
37
@logger.print_line("#"*20)
38
@logger.print_line("# Response:")
39
@logger.print_line("#"*20)
40
if response
41
response = response.to_terminal_output(headers_only: @logger.datastore['HttpTraceHeadersOnly'])
42
@logger.print_line("%clr#{response_color}#{response}%clr")
43
else
44
@logger.print_line("No response received")
45
end
46
end
47
end
48
end
49
end
50
end
51
end
52
53