Friday, December 14, 2012

How to set up proxy auto config on Ubuntu Desktop

Suppose you are using your Ubuntu Desktop laptop at home and workplace. Assume that when you are at your workplace, the corporate network your laptop is connected to is behind proxy. You then have to turn on/off proxy depending on where you are. Of course you can manually update proxy settings of Ubuntu Desktop every time your network changes, but that's quite cumbersome. That's when proxy auto-config helps.

A proxy auto-config (PAC) is a powerful mechanism that allows one to conditionally define proxy settings for web browsers. Using PAC, you can automatically switch proxy settings based on destination URL, IP address of local host, time of day, etc. As you can imagine, PAC is extremely useful for setting up proxy exceptions, proxy load balancing, network-aware conditional proxy, etc.

In the following, I will show you how to set up PAC on Ubuntu Desktop so that you can automatically switch between "no-proxy" and "proxy" based on the IP address of local host.

Creating a PAC file for automatic proxy switching is not complicated. In a PAC file, you essentially define FindProxyForURL(url, host) JavaScript function which is supposed to return the proxy to use when fetching a given URL. Create a PAC file as follows.
$ sudo vi /etc/proxy.pac

function FindProxyForURL(url, host)
{
  if (isInNet(myIpAddress(), "1.2.3.0", "255.255.255.0")) {
    if (shExpMatch(url, "http:*"))
      return "PROXY my.proxy.com:8000" ;
    if (shExpMatch(url, "https:*"))
      return "PROXY my.proxy.com:8000" ;
    if (shExpMatch(url, "ftp:*"))
      return "PROXY my.proxy.com:8000" ;
    return "DIRECT";
  } else {
    return "DIRECT";
  }
}

In this PAC file, if you are connected to 1.2.3.0/24 network (assuming that is the corporate network), you use proxy (my.proxy.com:8000). Otherwise, you do not use proxy at all.

Once you have created this PAC file, go to "System Settings" -> "Network" -> "Proxy Settings", and choose "Automatic" method in network proxy. Then type "file:///etc/proxy.pac" in configuration url field.

Before finalizing, there is an important thing to check. In the PAC file you created, myIpAddress() is supposed to return the IP address of localhost correctly. As a final step, you should verify that is the case by using "hostname" command.
$ hostname -i

If the hostname command returns "127.0.0.1", not an actual IP address assigned to your laptop, then myIpAddress() will also return "127.0.0.1", and the above proxy auto configuration will fail. To get around this problem, you need to set up the real IP address of local host somewhere.

In Linux, you can hard code the IP address of local host in /etc/hosts. However, since the IP address of localhost may keep changing depending on where you are, you can write a start-up script which automatically generates /etc/hosts upon boot.

To do that, first rename the original /etc/hosts to something else, which will then be used to generate an actual /etc/hosts to use.
$ sudo mv /etc/hosts /etc/hosts.custom

Now, create the following script which generates /etc/hosts from /etc/hosts.custom.
$ sudo vi /sbin/hostname.sh

#!/bin/bash

WIRED_IP=`ifconfig eth0 | sed -ne 's/.*inet addr:\([^ ]*\).*/\1/p'`
WIRELESS_IP=`ifconfig wlan0 | sed -ne 's/.*inet addr:\([^ ]*\).*/\1/p'`
HOST_IP=${WIRED_IP:-$WIRELESS_IP}
HOST_NAME="your_host_name"

cat /etc/hosts.custom > /etc/hosts

cat <<EOF >> /etc/hosts
# This file is automatically generated by /sbin/hostname.sh
$HOST_IP $HOST_NAME
EOF

exit 0

The script hostname.sh takes the IP address of either eth0 (for wired) or wlan0 (for wireless), and puts it in /etc/hosts.

Finally, add the name of the script to /etc/rc.local, so that it gets executed automatically upon boot.
$ sudo vi /etc/rc.local
/sbin/hostname.sh

Now, reboot your laptop, and check that your hostname can successfully be resolved to IP address.
$ hostname -i
1.2.3.100

That is all that's needed to set up proxy auto configuration on Ubuntu Desktop. Now your Ubuntu Desktop will automatically turn on or off proxy depending on where you are at home or workplace.

No comments:

Post a Comment