Created
May 19, 2015 09:35
-
-
Save brbrr/25bdf6cb4b9bfc53dbeb to your computer and use it in GitHub Desktop.
Extended WEBrick proxy to catch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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