我有一個HTTP攔截代理Ruby編寫的開始:在Ruby中幫助HTTP攔截代理?
require 'socket' # Get sockets from stdlib
server = TCPServer.open(8080) # Socket to listen on port 8080
loop { # Servers run forever
Thread.start(server.accept) do |client|
puts "** Got connection!"
@output = ""
@host = ""
@port = 80
while line = client.gets
line.chomp!
if (line =~ /^(GET|CONNECT) .*(\.com|\.net):(.*) (HTTP\/1.1|HTTP\/1.0)$/)
@port = $3
elsif (line =~ /^Host: (.*)$/ && @host == "")
@host = $1
end
print line + "\n"
@output += line + "\n"
# This *may* cause problems with not getting full requests,
# but without this, the loop never returns.
break if line == ""
end
if (@host != "")
puts "** Got host! (#{@host}:#{@port})"
out = TCPSocket.open(@host, @port)
puts "** Got destination!"
out.print(@output)
while line = out.gets
line.chomp!
if (line =~ /^<proxyinfo>.*<\/proxyinfo>$/)
# Logic is done here.
end
print line + "\n"
client.print(line + "\n")
end
out.close
end
client.close
end
}
這個簡單的代理,我提出解析目的地出HTTP請求,然後讀取HTTP響應和基於特殊的HTML標記進行邏輯。代理大部分工作正常,但似乎無法處理二進制數據和HTTPS連接。
我該如何解決這些問題?