Class: Ronin::Web::CLI::Commands::ReverseProxy Private
- Inherits:
-
Ronin::Web::CLI::Command
- Object
- Core::CLI::Command
- Ronin::Web::CLI::Command
- Ronin::Web::CLI::Commands::ReverseProxy
- Includes:
- Core::CLI::Logging
- Defined in:
- lib/ronin/web/cli/commands/reverse_proxy.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Starts a HTTP proxy server.
Usage
ronin-web reverse-proxy [options] [--host HOST] [--port PORT]
Options
-H, --host HOST Host to listen on (Default: localhost)
-p, --port PORT Port to listen on (Default: 8080)
-b, --show-body Print the request and response bodies
--rewrite-requests /REGEXP/:REPLACE
Rewrite request bodies
--rewrite-responses /REGEXP/:REPLACE
Rewrite response bodies
-h, --help Print help information
Instance Method Summary collapse
-
#initialize(**kwargs) ⇒ ReverseProxy
constructor
private
Initializes the
reverse-proxy
command. -
#parse_rewrite_rule(value) ⇒ (Regexp, String), (String, String)
private
Parses a rewrite rule.
-
#print_body(body) ⇒ Object
private
Prints a request or response body.
-
#rewrite_body(body, rules) ⇒ String
private
Rewrites a request or response body.
-
#run ⇒ Object
private
Runs the
ronin-web reverse-proxy
command.
Constructor Details
#initialize(**kwargs) ⇒ ReverseProxy
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes the reverse-proxy
command.
102 103 104 105 106 107 |
# File 'lib/ronin/web/cli/commands/reverse_proxy.rb', line 102 def initialize(**kwargs) super(**kwargs) @rewrite_requests = [] @rewrite_responses = [] end |
Instance Method Details
#parse_rewrite_rule(value) ⇒ (Regexp, String), (String, String)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parses a rewrite rule.
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ronin/web/cli/commands/reverse_proxy.rb', line 184 def parse_rewrite_rule(value) if (index = value.rindex('/:')) regexp = Regexp.new(value[1...index]) replace = value[(index + 2)..] return [regexp, replace] elsif (index = value.rindex(':')) string = value[0...index] replace = value[(index + 1)..] return [string, replace] end end |
#print_body(body) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prints a request or response body.
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/ronin/web/cli/commands/reverse_proxy.rb', line 164 def print_body(body) case body when StringIO, IO body.each_line do |line| puts line end body.rewind else puts body end end |
#rewrite_body(body, rules) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Rewrites a request or response body.
205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/ronin/web/cli/commands/reverse_proxy.rb', line 205 def rewrite_body(body,rules) body = case body when StringIO, IO then body.read when Array then body.join else body.to_s end rules.each do |(pattern,replace)| body.gsub!(pattern,replace) end return body end |
#run ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Runs the ronin-web reverse-proxy
command.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ronin/web/cli/commands/reverse_proxy.rb', line 112 def run proxy = Ronin::Web::Server::ReverseProxy.new do |proxy| proxy.on_request do |request| puts "[#{request.ip} -> #{request.host_with_port}] #{request.request_method} #{request.url}" request.headers.each do |name,value| puts "> #{name}: #{value}" end puts unless @rewrite_requests.empty? request.body = rewrite_body(request.body,@rewrite_requests) end print_body(request.body) if [:show_body] end proxy.on_response do |response| puts "< HTTP/1.1 #{response.status}" response.headers.each do |name,value| puts "< #{name}: #{value}" end puts unless @rewrite_responses.empty? response.body = rewrite_body(response.body,@rewrite_responses) end print_body(response.body) if [:show_body] end end log_info "Starting proxy server on #{[:host]}:#{[:port]} ..." begin proxy.run!(host: [:host], port: [:port]) rescue Errno::EADDRINUSE => error log_error(error.) exit(1) end log_info "shutting down ..." end |