Skip to content

Instantly share code, notes, and snippets.

@willbailey
Created April 3, 2010 00:04
Show Gist options
  • Save willbailey/353896 to your computer and use it in GitHub Desktop.
Save willbailey/353896 to your computer and use it in GitHub Desktop.
transparent proxy
require 'rubygems'
require 'em-proxy'
require 'ruby-debug'
require 'cgi'
puts "starting"
puts "\n\n\n\n"
Proxy.start(:host => "192.168.1.113", :port => 3000) do |conn|
conn.server :srv, :host => "mail.yahooapis.com", :port => 80
conn.on_data do |data|
puts "original"
print data
puts "\n\n\n\n\n"
data.gsub!(/192\.168\.1\.113:3000/, "mail.yahooapis.com")
data.gsub!(/Via.*\n/,'')
data.gsub!(/X-Forwarded-For.*\n/,'')
data.gsub!(/X-Forwarded-Host.*\n/,'')
data.gsub!(/X-Forwarded-Server.*\n/,'')
data.gsub!(/Cookie.*\n/,'')
data.gsub!(/Content-type.*/,"Content-Type: application/json")
headers, body = *data.split(/\r\n\r\n/)
if body
body_parts = body.split(/&/).map{|p|
CGI.unescape(p)
}
oauth_parts = body_parts.select{|p| p.match(/^oauth/)}.map{|h|
parts = h.split(/\=/)
k = parts[0]
v = parts[1, parts.size].join("=")
if h.match(/oauth_signature=/)
v+="="
end
"#{k}=\"#{CGI.escape(v)}\""
}
api_call = body_parts.select{|p| p.match(/zen_api_call/)}.first.split(/\=/).last
oauth_parts.reject!{|p|p.match(/oauth_signature_method/)}
oauth_parts.unshift("oauth_signature_method=\"HMAC-SHA1\"")
headers += "\r\nAuthorization: OAuth #{oauth_parts.join(", ")}"
headers += "\r\nAccept: application/json"
new_content_length = api_call.length
headers.gsub!(/Content-Length.*/,"Content-Length: #{new_content_length}")
data = "#{headers}\r\n\r\n#{api_call}\r\n"
# data.gsub!(/Referer.*\n/,'')
# data.gsub!(/Connection: Keep-Alive/,'Keep-Alive: 115')
puts "request"
print data
puts "\n\n\n\n"
end
data
end
# modify / process response stream
conn.on_response do |backend, resp|
print resp
resp.gsub!(/"\*\.yahoo.com"/, '"*" ')
puts "\n\n\n\n"
resp
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment