Jeff Beck
Jeff Beck

Reputation: 3938

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 supports a proxy?

Upvotes: 2

Views: 1051

Answers (2)

Lachlan Roche
Lachlan Roche

Reputation: 25956

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

Upvotes: 1

James A. Rosen
James A. Rosen

Reputation: 65252

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

Upvotes: 0

Related Questions