BLOG

KB No. 2549: TESTING THE FUNCTIONING OF A PROXY.PAC

How to validate the operation of a proxy.pac using a pactester.

Objective

Validate the operation of a proxy.pac using a pactester.

Background

A proxy.pac file is used to declare the proxy address and port to client workstation browsers. How can you verify that the declared proxy.pac file is working?

You can check the functionality of a proxy.pac file using the pacparser tool.

Pacparser is a library for parsing the automatic proxy configuration of a .PAC file. Automatic proxy configuration files are a widely used method of proxy configuration today. Web browsers can use a .PAC file to determine which proxy server to use or whether a direct connection is required for a given URL. .PAC files are written in JavaScript and can be programmed to return different substitution methods (e.g., "PROXY from Proxy1: port; DIRECT") depending on the URL, source IP address, protocol, etc.

.PAC files open up a lot of possibilities. .PAC files are now a widely accepted method for proxy management, and companies use them in the enterprise environment. The most popular web browsers support .PAC files.

Since the mechanism for evaluating the .PAC proxy file is generated in the browser and cannot be accessed from outside, the only way to know which proxy the browser will use for a specific URL is to manually inspect the proxy.PAC file. However, manual inspection can be tedious and prone to errors proportional to the size of the file.

The idea behind pacparser is to facilitate the analysis of .PAC files (C and Python are currently supported).

Steps

Step 1

  1. The first step is to install this tool, which is available via the following command for Linux system environments:

    $ sudo apt-get install libpacparser1 python-pacparser libpacparser-dev

    For Windows or Mac OS X systems, go to this address: https://code.google.com/p/pacparser/downloads/list

  2. To install the pacparser and pactester C library in the Windows environment:

    Download the pacparser-vvv-win32.zip archive and extract it. A pacparser-v.v.v directory will be created.

  3. Copy all .DLL files and pactester.exe to a location where the system can find them (for example, to directories that are already in the system path, such as C:\Windows) or simply add the pacparser directory to the system path (PATH).
  4. Link libpacparser.a and pacparser.lib in pacparser respectively with the mingw and Visual Studio compilation tools. Instructions for linking in pacparser on Visual Studio: http://code.google.com/p/pacparser/wiki/LinkingOnVisualStudio

Python module

To install the pacparser Python module: download the pacparser–Python25–vvv–win32.zip package and extract it. At the command prompt (cmd), go to the pacparser–Python25–vvv–win32 directory and run setup.py install:

C:\temp > cd pacparser - Python25 - 1.0.5 -win32
C:\temp\pacparser - Python25 - 1.0.5 -win32 > setup.py install

If it fails for any reason, simply copy the pacparser directory to the Python folder: C:\Python25\Lib\site-packages\pacparser.

Step 2

Once the pacparser tool is installed, you can test the proxy.pac configuration file with Python:

Please note: If the proxypac is hosted on Olfeo, it must be copied to the client machine on which pacparser is installed.
>>> import pacparser
>>> pacparser.init()
>>> pacparser.parse_pac_file('examples/wpad.dat')
>>> pacparser.find_proxy('http://www.google.com', 'www.google.com')
'PROXY proxy1.manugarg.com:3128; PROXY proxy2.manugarg.com:3128; DIRECT'
>>> pacparser.find_proxy('http://www2.manugarg.com', 'www2.manugarg.com')
'DIRECT'
>>> pacparser.cleanup()
>>>
Step 3

It is also possible to test the .PAC file with C language:

manugarg@hobbiton:~$ cat pactest.c
#include <stdio.h>
#include <pacparser.h>

int main(int argc, char* argv[])
{
  char *proxy;
  pacparser_init();
  pacparser_parse_pac_file(argv[1]);
  proxy = pacparser_find_proxy(argv[2], argv[3]);
  printf("%s\n", proxy);
  pacparser_cleanup();
}

manugarg@hobbiton:~$ gcc -o pactest pactest.c -lpacparser
manugarg@hobbiton:~$ ./pactest wpad.dat http://www.google.com www.google.com
PROXY proxy1.manugarg.com:3128; PROXY proxy2.manugarg.com:3128; DIRECT

Validation

If the conditions are met, pacparser will return the desired proxy or return DIRECT ( no proxy).

This tool makes it easy to test the conditions of a .PAC file and thus avoid errors during deployment.