Skip to content

Instantly share code, notes, and snippets.

@joshprice
Created August 1, 2009 05:45
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 joshprice/159577 to your computer and use it in GitHub Desktop.
Save joshprice/159577 to your computer and use it in GitHub Desktop.
Multiplexing Proxy
curl http://localhost:9999 -d destip=127.0.0.1
require "rubygems"
require "webrick/httpproxy"
module WEBrick
class HTTPRequest
attr_writer :host, :port, :request_uri, :unparsed_uri, :request_line, :path
end
end
class MultiplexingProxy < WEBrick::HTTPProxyServer
def initialize(options)
super(options)
end
def service(req, res)
puts "*" * 50
dest = destination(req)
puts "Destination: #{dest}"
site_uri = URI.parse(dest)
req.host = req.request_uri.host = site_uri.host
req.port = req.request_uri.port = site_uri.port
req.request_line.gsub(/^(\w+) .+ (HTTP.+)$/, "#{$1} #{req.path} #{$2}")
req.unparsed_uri = req.path
super(req, res)
print_response(res)
end
def destination(req)
req.body.match(/destip=(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)[1]
end
def print_response(res)
puts "="*50 + res.body + "="*50
end
end
s = MultiplexingProxy.new(:Port => 9999)
trap("INT") {s.shutdown}
s.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment