在Ruby中帮助HTTP拦截代理? [英] Help with HTTP Intercepting Proxy in Ruby?

查看:197
本文介绍了在Ruby中帮助HTTP拦截代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  require'socket'#从stdlib获取套接字

server = TCPServer.open(8080)#监听端口8080上的套接字
循环{#服务器永远运行
Thread.start(server.accept)do | client |
放置**有连接!
@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 +\\\

@output + = line +\\\

#这可能会导致没有收到完整请求的问题
#但没有这个,循环永远不会返回。
break if line ==
end
if(@host!=)
puts**主持人!(#{主持人}:#{@端口})
out = TCPSocket.open(@host,@port)
放入** Got destination!
out.print(@output)
while line = out.gets
line.chomp!
if(line =〜/^<proxyinfo>.*<\/proxyinfo> $ / $)
#逻辑在这里完成。
end
print line +\\\

client.print(line +\\\

end
out.close
end
client.close
end
}

这个简单的代理我从HTTP请求中解析目标,然后读取HTTP响应并根据特殊的HTML标记执行逻辑。该代理大部分工作正常,但似乎无法处理二进制数据和HTTPS连接。



如何解决这些问题?

$ b $首先,您可能会更好地构建现有的Ruby HTTP代理实现。 Ruby标准库中已经有这样的一个,即使用WEBrick :: HTTPProxyServer 。例如,查看基于同一个类的实现的相关问题: Webrick透明代理



关于代理HTTPS,您只能传递原始字节,不能做更多的事情。由于HTTPS受加密保护,因此无法在HTTP协议级别检查内容。它只是一个不透明的字节流。


I have the beginnings of an HTTP Intercepting Proxy written in Ruby:

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
}

This simple proxy that I made parses the destination out of the HTTP request, then reads the HTTP response and performs logic based on special HTML tags. The proxy works for the most part, but seems to have trouble dealing with binary data and HTTPS connections.

How can I fix these problems?

解决方案

First, you would probably be better off building on an existing Ruby HTTP proxy implementation. One such is already available in the Ruby standard library, namely WEBrick::HTTPProxyServer. See for example this related question for an implementation based on that same class: Webrick transparent proxy.

Regarding proxying HTTPS, you can't do much more than just pass the raw bytes. As HTTPS is cryptographically protected, you cannot inspect the contents at the HTTP protocol level. It is just an opaque stream of bytes.

这篇关于在Ruby中帮助HTTP拦截代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆