我有一个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连接。
我该如何解决这些问题?