Parsing Proxy Auto-Config files

One thing I’ve always been wanting to be able to do was parse a [proxy auto-config](http://en.wikipedia.org/wiki/Proxy_auto-config) file from the command line.

I only recently found the [pacparser](http://code.google.com/p/pacparser/) library but found it limiting because it isn’t easy to install and use. So I decided to write a rubygem that would parse a pac file.

I started using the [`execjs` gem](https://rubygems.org/gems/execjs) but one thing I quickly figured out is that you cannot implement all of the pac functions in pure JavaScript. Most notably is the `dnsResolve(host)` function which you cannot write in JavaScript because it lacks a way to make DNS queries.

So I took the code from execjs that deals with the four native runtimes and modified it to make ruby methods available in the JavaScript runtime. And now it’s available in the [`pac` gem](https://rubygems.org/gems/pac).

You will need a JavaScript runtime installed to be able to use the `pac` gem, I recommend [`therubyracer`](https://rubygems.org/gems/therubyracer) gem for ruby and [`therubyrhino`](https://rubygems.org/gems/therubyrhino) for jruby. [`johnson`](https://rubygems.org/gems/johnson) only works with ruby 1.8 and [`mustang`](https://rubygems.org/gems/mustang) requires the `v8` library to be installed.

“`
gem install therubyracer pac
“`

And now you can use the `parsepac` executable.

parsepac http://cloud.github.com/downloads/samuelkadolph/ruby-pac/sample.pac http://samuel.kadolph.com

Or in ruby.

require “rubygems”
require “pac”

pac = PAC.load(“http://cloud.github.com/downloads/samuelkadolph/ruby-pac/sample.pac”)
pac.find(“http://samuel.kadolph.com”) # => “DIRECT”

pac = PAC.read(“sample.pac”)

pac = PAC.source <<-JS function FindProxyForURL(url, host) { return "DIRECT"; } JS

Leave a Reply

Your email address will not be published. Required fields are marked *