开发者

net/imap from behind a proxy

I would like to use the net/imap library in ruby behind a authenticated proxy, I was starting to dig in and I'm wondering if there is a way to do this already or if I need to make my own version of the net/imap library that supp开发者_JAVA技巧orts a proxy?


It is possible to tunnel any socket connection through a HTTPS proxy server.

To do this:

  • open a socket to your proxy server
  • send "CONNECT hostname : portnumber HTTP/1.0\n\r\n\r\n"
  • read from the socket until you see the end of the HTTP headers (2 blank lines)
  • your socket is now connected

Here is a ruby example of such a tunnel.

Reasons this will fail:

  • most network admins will only allow CONNECT to port 443
  • proxy server has proxy authentication


The easiest way to hack libraries that don't support proxy information is to replace Net::HTTP with an instance of Net::HTTP::Proxy:

# somewhere before you load net/imap
proxy = Net::HTTP::Proxy(address, host)
Net.class_eval do
  remove_const :HTTP
  HTTP = proxy
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜