Auto proxy settings using .pac file

Dec 16 2011 Published under Linux

I always have this problem:
Switching proxy on/off depending on where I am: ( isAtWorkplace ) ? proxyOn : proxyOff;
And it’s pretty annoying at times. I have been looking for an elegant way to fix this problem for a long time and I think I finally found a satisfying solution.
My approach is to use a pac file to check my current IP address and apply proxy settings accordingly.
Note that this approach will not work if your workplace and home LAN happens to use the same subnet, ie something like 192.168.1.xx. You might want to come up with something else if that’s case, for example change your home subnet address to something else.

Using a PAC file:
This is the straight forward part. We will create a PAC file to tell the system (or browser if you want proxy settings to be effective only in browsers) how to configure our proxy settings. PAC file is a JavaScript file which tell the system/browser to load its proxy configuration information dynamically. Create a file ‘proxy.pac’ (or whatever filename you want) and place it anywhere you want with the following content. Change the subnet mask accordingly:

//file: proxy.pac
function FindProxyForURL(url, host) {
    if (
    	isInNet(myIpAddress(), "192.168.11.0", "255.255.255.0")
    ) {
        return "PROXY my.proxy.server.com:8080";
    } else {
        return "DIRECT";
    }
}

This will configure the system/browser to check if the current computer IP address is in the subnet and apply proxy settings accordingly.
Now set the system/browser proxy settings to point to the proxy.pac file. In my case, Automatic proxy configuration URL is set to file:///home/birdy/Documents/scripts/proxy.pac

Well there is a problem. On my system, ‘myIpAddress()’ does not return the eth0 or wlan0 address but the host address 127.0.1.0. This address is set in /etc/hosts file.

$ more /etc/hosts
127.0.0.1	localhost
127.0.1.0	HAL2000

So in other words the condition to match the IP address will never be run and proxy settings will not be set correctly.

Auto update /etc/hosts file upon establishing internet connection
To fixed this problem, we have to update /etc/hosts file so that it looks something like this:

$ more /etc/hosts
127.0.0.1	localhost
current_ip_add	HAL2000

Two separate shell scripts are used to automatically achieve this. To do so, prepare a first script which is responsible for updating /etc/hosts file with current ip address. Place this script anywhere you like.

#!/bin/sh
#file: fixhosts.sh

set -e

if [ "$IFACE" = lo ]; then
    exit 0
fi

myHostName=HAL2000

# Remove current line with hostname at the end of line
sed -i '/'$myHostName'$/ d' /etc/hosts 

# Add new entry to hosts file (wlan0)
ipaddr=$(ifconfig  | grep -A 1 wlan0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
if [ -n "$ipaddr" ] 
then
	echo "$ipaddr $myHostName" >> /etc/hosts
fi

# Add new entry to hosts file (eth0)
ipaddr=$(ifconfig  | grep -A 1 eth0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
if [ -n "$ipaddr" ]
then
	echo "$ipaddr $myHostName" >> /etc/hosts
fi

Prepare a second script which will automatically run the fixhosts.sh script when internet connection is established. Put this file in ‘/etc/NetworkManager/dispatcher.d/’. See this post for more information on this.

#!/bin/bash
#file: 90fixhosts.sh

IF=$1
STATUS=$2
 
if [ "$IF" == "eth0" ] || [ "$IF" == "wlan0" ]
then
    case "$2" in
        up)
        logger -s "NM Script up triggered"
        /path-to-scripts/fixhosts.sh
	;;
        down)
        logger -s "NM Script down triggered"
        /path-to-scripts/fixhosts.sh
        #command2
        ;;
        pre-up)
        logger -s "NM Script pre-up triggered"
        #command3
        ;;
        post-down)
        logger -s "NM Script post-down triggered"
        #command4
        ;;
        *)
        ;;
    esac
fi

Now, when internet conenction is established, ’90fixhosts.sh’ script will be executed, in which will evoke ‘fixhosts.sh’ script to update /etc/hosts file, which is used by proxy.pac file to set proxy settings.

ref:
http://shihabuddin.blogspot.com/2008/04/update-etchosts-with-ip-address.html
http://stackoverflow.com/questions/6343834/how-to-avoid-127-0-0-1-on-myipaddress-in-pac-files

No responses yet

Comments are closed at this time.

Login