Gem, Apt, and authenticated proxy servers

August 9th, 2006

–Update 9/29/06
It appears rubygems 0. I actually haveWhat warnings do you have for DesogenGeneric DesogenDesogenHonest and thorough review of DesogenBest Price Of Desogen, Online Pharmacy Phentermine, Coral Calcium Supreme. Des·o·gen (d s-j n) A trademark for an oral contraceptive containing ethinyl estradiol and desogestrel. Online Desogen I am 5′5″ 107-115 lbs. View complete and up to date DesogenDec 21, 2007 DesogenCompare Prices and Save at SHOP. 9.0 supports basic proxy authentication. The documentation says you can do the following:

: gem search rails --remote \
-p http://user:pass@proxy.com

However, this method appears to be bugg Low Prices On green pearl necklace. Get the latest design sari,saris, gift items form our online storegreen pearls manufacturers directory - over 3000000 registered importers and exporters. Online Green Pearls 95 shipping and customer reviews on all products. Find Shopping Deals at Yahoo. y. The alternate, which worked fine, is to define the following environment variables:

:export HTTP_PROXY="http://proxy.com"
:export HTTP_PROXY_USER="user"
:export HTTP_PROXY_PASS="pass"

For my inaugural post I thought I’d share some helpful code for working around authenticate proxies. I’ve been stuck manually downloading .debs a few times. When I discovered the same issue with gem I thought it might be interesting to see if I could come up with a simple solution.

I settled on a simple server that sits bewteen the application you want to use and the real proxy. It intercepts outgoing requests and injects the necessary proxy authentication into the header. I believe it should work with everything except WebDAV (this would be easy) and SSL (not quite as ea NOW® Mega Hoodia delivers the herbal power of 100% pure Hoodia gordonii. A Natural Weight Loss And Hunger Management Formula. Online Mega Hoodia Will Hoodia Gordonii Burn Fat. Compare and buy Diet Pills. sy). Also, it only supports basic proxy authentication. Feel free to use and extend this however you wish. Onward to the meat of the post.

So lets say I need to install rails. First I need to start my interceptor:
: ruby proxy.rb 9999 evilproxy.someclient.com 80 user pass

Then all I need to do is tell gem to use my proxy:
: gem install rails --http-proxy http://localhost:9999

Configure apt like this:
: echo 'Acquire::http::Proxy "http://localhost:9999";' > /etc/apt/apt.conf.d/proxy

Finally, here’s the code:

require 'socket'
require 'fcntl'
	
#We want to see console messages immediately
def pflush(string)
  print string
  STDOUT.flush
end
	
class HttpAuthInjector
  CHUNK_SIZE = 2048  
	
  def initialize(port = 9999, proxy_host = 'localhost', proxy_port = 80, proxy_user = \"\", proxy_pass = \"\")
    #I thought there was a way to avoid this boiler plate crap
    @port = port
    @proxy_host = proxy_host
    @proxy_port = proxy_port
    @proxy_user = proxy_user
    @proxy_pass = proxy_pass
  end
	
  def start
    @server = TCPserver.open @port
    @auth_string = \"Proxy-Authorization: Basic \" + [\"#{@proxy_user}:#{@proxy_pass}\"].pack('m')
	
    while true
      client = @server.accept
      proxy = TCPSocket.new @proxy_host, @proxy_port
      configure_socket client, proxy
      request_stream client, proxy
      response_stream client, proxy
    end
  end
	
  def configure_socket(*socks)
     socks.each { |s| s.sync = true }
  end
	
  def request_stream(client, proxy)
      req = \"\"
      stream client, proxy do
          #I can't do req < < client.readpartial.  Why?
          d = client.readpartial CHUNK_SIZE
          req << d
          next unless req.index \"\n\"
	
          if req =~ /^POST|GET|PUT|DELETE .+\n$/
            req.insert(req.index(\"\n\") + 1, @auth_string)
            pflush \"#{client}\n[#{req}]\n\"
          end
	
          proxy.print(req)
          req = \"\"
      end
  end
	
  def response_stream(client, proxy)
    stream client, proxy do
      client.print proxy.readpartial(CHUNK_SIZE)
    end
  end
	
  def stream(client, proxy)
    Thread.new {
      begin
        while true
          yield
        end
      rescue EOFError, IOError
        #noop
      rescue Exception => ex
        pflush “#{ex.class}: #{ex.message}\n\t#{ex.backtrace}”
      ensure
        client.close unless client.closed?
        proxy.close unless  Nizoral 2% Dandruff Shampoo. We will match any competitor’s price. Online Nizoral NizoralAccurate, FDA approved NizoralSporanox 100mg 60 Capsules $104. Nizoral, a broad-spectrum antifungal drug available in tablet form, may be given In some people, Nizoral may cause serious or even fatal damage to the liver. proxy.closed?
      end
    }
  end
	
end
	
if ARGV.size < 5
 puts \"Usage: ruby proxy.rb local_port proxy_host proxy_port proxy_user proxy_pass\"
else
  HttpAuthInjector.new(*ARGV).start
end