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:

Turn on/off proxy setting

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):

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.