Last Updated: June 30, 2019
·
452
· creaktive

Seamless Proxy Auto-Config (a.k.a. Web Proxy Auto Discovery) for CLI apps

Suppose you are in a corporate network environment and often times find yourself manually setting/unsetting the HTTP_PROXY environment variable in order to access different hosts (for instance, yay proxy for the external hosts and nay proxy for the internal ones). Sounds familiar? In this case, a tool called depac might help you.

The problem

Corporate proxies are meant to steer GUI browser users via Proxy Auto-Config. In a nutshell, a browser like Internet Explorer downloads a special routing file from a virtual host served by the proxy itself. This file consists of a JavaScript code that usually contains a humongous if/else if/else clause that maps the requested hostname to the address of the proxy host capable of contacting the requested hostname.

Now, CLI clients don't usually implement JavaScript, and therefore can not decide which proxy to use by themselves.

The solution

depac uses a portable lightweight (albeit limited) JavaScript engine implementation in order to
parse the PAC file. Then, it creates a relay proxy that forwards the requests to the routes assigned by the PAC logic.

depac is usually started in the beginning of login session, and through use of environment variables it's relay proxy can be located and automatically used for all the user agents that do support HTTP_PROXY variables.

(this technique is somewhat similar to what ssh-agent does. In fact, half of the previous paragraph was stolen from ssh-agent manual page :)

The biggest advantage of depac in comparison to the similar solutions like pac4cli is that the former does not require a system-wide installation. Both the JavaScript engine and the relay proxy are implemented in pure Perl language and require no dependencies except for Perl v5.10 itself (which is omnipresent anyway).

INSTALLATION

$ curl -o ~/bin/depac https://raw.githubusercontent.com/creaktive/dePAC/master/depac
$ chmod +x ~/bin/depac
$ echo 'eval $(~/bin/depac)' >> ~/.profile

Or you can use wget and call it with perl (feel free to mix):

$ wget -O ~/depac https://raw.githubusercontent.com/creaktive/dePAC/master/depac
$ echo 'eval $(perl ~/depac)' >> ~/.profile

You can also use depac in an ad-hoc fashion, without a shared instance running in the background:

$ depac -- wget -r -np https://something.com