Class: Ronin::Web::Server::ReverseProxy
- Inherits:
-
Object
- Object
- Ronin::Web::Server::ReverseProxy
- Defined in:
- lib/ronin/web/server/reverse_proxy.rb,
lib/ronin/web/server/reverse_proxy/request.rb,
lib/ronin/web/server/reverse_proxy/response.rb
Overview
Reverse proxies Rack requests to other HTTP web servers.
Examples
Standalone Server
reverse_proxy = Ronin::Web::Server::ReverseProxy.new do |proxy|
proxy.on_request do |request|
# ...
end
proxy.on_response do |response|
# ...
end
end
reverse_proxy.run!(host: '0.0.0.0', port: 8080)
App
class App < Ronin::Web::Server::Base
mount '/signin', Ronin::Web::Server::ReverseProxy.new
end
Defined Under Namespace
Standalone Server Methods collapse
- DEFAULT_HOST =
Default host the Proxy will bind to
'0.0.0.0'
- DEFAULT_PORT =
Default port the Proxy will listen on
8080
- DEFAULT_SERVER =
Default server the Proxy will run on
'webrick'
Constant Summary collapse
- IGNORED_HEADERS =
Blacklisted HTTP response Headers.
Standalone Server Methods collapse
-
#quit!(server, handler) ⇒ Object
private
Stops the reverse proxy server.
-
#run!(host: DEFAULT_HOST, port: DEFAULT_PORT, server: DEFAULT_SERVER, **rack_options) ⇒ Object
Runs the reverse proxy as a standalone HTTP server.
Instance Method Summary collapse
-
#call(env) ⇒ (Integer, Hash{String => String}, Array<String>)
Reverse proxies every request using the
Host
header. -
#connection_for(host, port, ssl: nil) ⇒ Ronin::Support::Network::HTTP
private
Creates a new connection or fetches an existing connection.
-
#initialize {|reverse_proxy| ... } ⇒ ReverseProxy
constructor
Creates a new reverse proxy application.
-
#on_request {|request| ... } ⇒ Object
Registers a callback to run on each request.
-
#on_response {|response| ... } ⇒ Object
Registers a callback to run on each response.
-
#reverse_proxy(request) ⇒ ReverseProxy::Response
Reverse proxies the given request.
Constructor Details
#initialize {|reverse_proxy| ... } ⇒ ReverseProxy
Creates a new reverse proxy application.
69 70 71 72 73 |
# File 'lib/ronin/web/server/reverse_proxy.rb', line 69 def initialize @connections = {} yield self if block_given? end |
Instance Method Details
#call(env) ⇒ (Integer, Hash{String => String}, Array<String>)
Reverse proxies every request using the Host
header.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ronin/web/server/reverse_proxy.rb', line 116 def call(env) request = Request.new(env) @on_request_callback.call(request) if @on_request_callback response = reverse_proxy(request) if @on_response_callback if @on_response_callback.arity == 1 @on_response_callback.call(response) else @on_response_callback.call(request,response) end end return [response.status, response.headers, response.body] end |
#connection_for(host, port, ssl: nil) ⇒ Ronin::Support::Network::HTTP
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.
Creates a new connection or fetches an existing connection.
150 151 152 153 154 155 156 |
# File 'lib/ronin/web/server/reverse_proxy.rb', line 150 def connection_for(host,port, ssl: nil) key = [host,port,ssl] @connections.fetch(key) do @connections[key] = Support::Network::HTTP.new(host,port, ssl: ssl) end end |
#on_request {|request| ... } ⇒ Object
Registers a callback to run on each request.
82 83 84 |
# File 'lib/ronin/web/server/reverse_proxy.rb', line 82 def on_request(&block) @on_request_callback = block end |
#on_response {|response| ... } ⇒ Object
Registers a callback to run on each response.
103 104 105 |
# File 'lib/ronin/web/server/reverse_proxy.rb', line 103 def on_response(&block) @on_response_callback = block end |
#quit!(server, handler) ⇒ 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.
Stops the reverse proxy server.
258 259 260 261 |
# File 'lib/ronin/web/server/reverse_proxy.rb', line 258 def quit!(server,handler) # Use thins' hard #stop! if available, otherwise just #stop handler.respond_to?(:stop!) ? handler.stop! : handler.stop end |
#reverse_proxy(request) ⇒ ReverseProxy::Response
Reverse proxies the given request.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/ronin/web/server/reverse_proxy.rb', line 172 def reverse_proxy(request) host = request.host port = request.port ssl = request.scheme == 'https' method = request.request_method.downcase.to_sym path = request.path query = request.query_string headers = request.headers body = request.body.read http = connection_for(host,port, ssl: ssl) http_response = http.request(method,path, query: query, headers: headers, body: body) response_headers = {} http_response.each_capitalized do |name,value| unless IGNORED_HEADERS.include?(name) response_headers[name] = value end end response_body = http_response.body || '' response_status = http_response.code.to_i return Response.new(response_body,response_status,response_headers) end |
#run!(host: DEFAULT_HOST, port: DEFAULT_PORT, server: DEFAULT_SERVER, **rack_options) ⇒ Object
Runs the reverse proxy as a standalone HTTP server.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/ronin/web/server/reverse_proxy.rb', line 229 def run!(host: DEFAULT_HOST, port: DEFAULT_PORT, server: DEFAULT_SERVER, **) server = Rack::Server.new( app: self, server: server, Host: host, Port: port, ** ) server.start do |handler| trap(:INT) { quit!(server,handler) } trap(:TERM) { quit!(server,handler) } end return self end |