Skip to content

Instantly share code, notes, and snippets.

@brbrr
Created May 19, 2015 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brbrr/25bdf6cb4b9bfc53dbeb to your computer and use it in GitHub Desktop.
Save brbrr/25bdf6cb4b9bfc53dbeb to your computer and use it in GitHub Desktop.
Extended WEBrick proxy to catch
# A HttpWatch proxy clone (without the license!)
require 'webrick/httpproxy'
require 'json'
require 'highline/import'
require 'webrick'
require 'cgi'
class CustomWEBrickProxyServer < WEBrick::HTTPProxyServer
def do_PUT(req, res)
perform_proxy_request(req, res) do |http, path, header|
http.put(path, req.body || '', header)
end
end
# This method is not needed for PUT but I added for completeness
def do_OPTIONS(req, res)
res['allow'] = 'GET,HEAD,POST,OPTIONS,CONNECT,PUT'
end
end
module ProxyHelper
@proxy_port = 9998
@host = #host to intercept
@responce = ''
@request = ''
$res_arry = []
$req_arry = []
# noinspection ALL
class << self
def init_proxy
@server = CustomWEBrickProxyServer.new(
Port: @proxy_port,
:AccessLog => [], # suppress standard messages
:ProxyContentHandler => Proc.new do |req,res|
unless req.request_uri.nil?
if req.request_uri.host.include?(@host)
short_resp = $req_arry[0].to_s.lines.first + $res_arry[0].to_s.lines.first
debug short_resp.gsub("\r\n", ' ')
loggers[:proxy_run].debug '>'*75
loggers[:proxy_run].debug req
loggers[:proxy_run].debug '<'*75
loggers[:proxy_run].debug res
loggers[:proxy_run].debug '='*75
$res_arry << res
$req_arry << req
end
end
end
)
%w(TERM INT).each do |signal|
trap(signal) { @server.shutdown }
end
@server
end
def start(srv)
srv.start
end
def stop(srv)
srv.stop
end
end
end
# loggers[:proxy_run] - logger which logs in to file
# used to log when proxy running in separate thread, could be replaced with `puts` if new thread not needed
def start_proxy
$t = Thread.new {
$s= ProxyHelper::init_proxy
ProxyHelper::start($s)
}
end
def stop_proxy
ProxyHelper::stop($s)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment