Class: Ronin::Web::CLI::Commands::ReverseProxy Private

Inherits:
Ronin::Web::CLI::Command show all
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

Since:

  • 1.0.0

Instance Method Summary collapse

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.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Since:

  • 1.0.0



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.

Parameters:

  • value (String)

Returns:

  • ((Regexp, String), (String, String))

Since:

  • 1.0.0



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

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.

Parameters:

  • body (IO, StringIO, Array<String>, String)

    The request/response body to print. May be a IO/StringIO object, an Array of Strings, or a String.

Since:

  • 1.0.0



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.

Parameters:

  • body (IO, StringIO, Array<String>, String)

Returns:

  • (String)

Since:

  • 1.0.0



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

#runObject

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.

Since:

  • 1.0.0



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 options[: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 options[:show_body]
    end
  end

  log_info "Starting proxy server on #{options[:host]}:#{options[:port]} ..."

  begin
    proxy.run!(host: options[:host], port: options[:port])
  rescue Errno::EADDRINUSE => error
    log_error(error.message)
    exit(1)
  end

  log_info "shutting down ..."
end