#!ruby proxyh = ENV['http_proxy_host'] || 'p-goodway' proxyp = ENV['http_proxy_port'] || 3128 server = ENV['target_host'] || 'foo.bar.net' port = ENV['target_port'] || 443 localhost = ENV['listen_host'] || 'localhost' localport = ENV['listen_port'] || 443 require 'socket' def bounce(in1, out1, in2, out2, timeout=nil) buf1 = '' buf2 = '' loop do rd = [] wr = [] rd << in1 if buf1.length < 1024*16 rd << in2 if buf2.length < 1024*16 wr << out1 if buf1.length > 0 wr << out2 if buf2.length > 0 rd, wr = IO.select(rd, wr, nil, (Integer(timeout) rescue nil)) break unless rd case rd[0] when in1 # str = in1.readpartial 1 rescue nil str = in1.read 1 rescue nil break unless str buf1 << str when in2 # str = in2.readpartial 1 rescue nil str = in2.read 1 rescue nil break unless str buf2 << str end case wr[0] when out1 len = out1.write buf1 buf1[0...len] = '' when out2 len = out2.write buf2 buf2[0...len] = '' end end end l = TCPServer.new(localhost, localport) loop do puts "waiting connection on #{localhost}:#{localport}" a = l.accept puts "connecting to proxy" c = TCPSocket.open(proxyh, proxyp) c.write "CONNECT #{server}:#{port} HTTP/1.0\r\n" c.write "\r\n" line = nil puts line while (line = c.gets.chomp) =~ /./ puts "bouncing" begin bounce(a, c, c, a, 600) rescue Object end puts "done" c.shutdown c.close a.shutdown a.close end