Scenario of this document: At corporate level our users have laptops, depending on where they are Firefox should detect the proxy settings if:

  • The user is using the laptop with an Ethernet cable (at the office)
  • The user is using the laptop via Wi-Fi (at the office)
  • The user is connected outside the office (doesn’t matter if Ethernet or Wi-Fi).

The worst option is installing a webserver, then configuring a PAC file as mentioned here, one better option is to create a PAC file and load it directly into firefox as explained here, finally you have plugins like ProxySwitcher or SmartProxy. In this document we won’t discuss those alternatives. Instead we’ll work with scripting in bash and the Network Manager service in Linux.

Before we begin you must know that this is possible but, with this procedure we’ll always have to restart Firefox after each change.

In this document isn’t present a dynamic way to change the proxy, it always require the restart of Firefox.

¿Why not udev?

Well, although network devices are hardware, connections aren’t. That’s why you won’t see any changes when you plug or unplug a cable or even when you change from Wi-Fi to Ethernet.

We will work in this example with Network Manager, Lubuntu 16.04, this will work in Debian and Ubuntu if we are using Network Manager.

The scripts and changes

This script modifies the preferences of Firefox to change if the proxy is enabled or not.  Manually add this lines with your proxy in prefs.js before executing the script:

# your proxy
user_pref("network.proxy.autoconfig_url", "http://corporateproxy:8080/some_pac.pac");
# disabled proxy = 0 , enabled = 2
user_pref("network.proxy.type", 0);

Turn on/off proxy setting

#!/bin/bash
# mortiz: change our blue coat proxy when the user is connected by cable at our office

BLUECOAT_PROXY='http://ocorporate.proxy:8080/some_pac.pac'
BLUECOAT_STATUS=$(curl -sI $BLUECOAT_PROXY -m3 | grep '' -m1 | awk {'print $2'})
IFACE_STATUS=`cat /sys/class/net/eth0/operstate`
FIREFOX_PROXY_RULE='user_pref("network.proxy.type", 2);'
FIREFOX_PREFERENCES="/home/corporateuser/.mozilla/firefox/abc123.default/prefs.js" # a fixed profile for firefox                                                                                
IS_PROXY_ENABLED_FIREFOX=`grep 'network.proxy.type' $FIREFOX_PREFERENCES`

#We test if our proxy is reachable and the status of our Eth0 is UP
if [ $IFACE_STATUS == 'up' ] && [ $BLUECOAT_STATUS == '200' ]
then
        # verificamos si esta habilitado el proxy
        if [ "$IS_PROXY_ENABLED_FIREFOX" == 'user_pref("network.proxy.type", 2);' ]
        then
                : # pass /  in case someone changed it manually     
        else
                #Enabling proxy...
                `sed -i 's/.*network.proxy.type.*/user_pref("network.proxy.type", 2);/' $FIREFOX_PREFERENCES`
                `pkill -x firefox` # killing firefox instances
        fi
else
        # disabling proxy...
        `sed -i 's/.*network.proxy.type.*/user_pref("network.proxy.type", 0);/' $FIREFOX_PREFERENCES`
        `pkill -x firefox` # killing firefox instances
fi

BLUECOUAT_STATUS: We verify with curl that the corporate proxy connection is HTTP 200 OK. The m3 parameter is the max amount of time (3 seconds) that will look for the proxy.

FIREFOX_PROXY_RULE: Be careful, the rule may change depending on the proxy settings you want. Check the different values of the property by changing your proxy options in Firefox to select the one that applies for you. In this case is for Preferences > Advanced > Network > Proxy Settings > Automatic proxy configuration URL.

IFACE_STATUS= For particular reasons I have to check the status in this system file rather than using the arguments from Network Manager. (Network Manager passes 2 parameters to every script it runs like this explained here):

INTERFACE=$1
STATUS=$2

Now the only thing you need to do is to copy the script into /etc/NetworkManager/dispatcher.d/. Every time you connect or disconnect the cable your proxy settings in Firefox will change and it will be closed, so you must launch Firefox again and it will load the new settings.