Using an HTTP proxy when a VPN is used

Background: When I connect to my University’s VPN service I need to use a HTTP proxy in order to access the WWW. I use GNOME and Network-Manager. The Openconnect plugin manages to establish the VPN connection nicely, but previously I had to enable the proxy in Firefox manually afterwards. This is my solution on how to set GNOMEs http proxy settings automatically when a VPN connection with the name “UniiHH” is established and works by using a script in /etc/Network-Manager/dispatch.d.

This is my 10_enablevpnproxy (needs permission 755):

#!/bin/sh -e
# Script to set up the University of Hamburg web proxy when the Openconnect VPN connected
# credits to https://wiki.ubuntuusers.de/NetworkManager/Dispatcher/

VPN_CONNECTION_NAME="UniHH"
USER="spaetz"

# VPN connection started or stopped?
case "$2" in
    vpn-up)
    active_vpn=$(nmcli -t --fields NAME con show --active|grep "${VPN_CONNECTION_NAME}" -q)
    if $active_vpn; then
        # VPN to UNI HH was started
    else
        #Irrelevant VPN started, do nothing
        exit 0
    fi
    # gsettings will fail if dbus is not launched with:
    # "dconf-WARNING **: failed to commit changes to dconf: Cannot autolaunch D-Bus without X11 $DISPLAY
    sudo -u "$USER" dbus-launch gsettings set org.gnome.system.proxy mode 'manual' 
    sudo -u "$USER" dbus-launch gsettings set org.gnome.system.proxy.http host 'proxy.uni-hamburg.de'
    sudo -u "$USER" dbus-launch gsettings set org.gnome.system.proxy.http port 3128
    ##gsettings set org.gnome.system.proxy.ftp host 'proxy.localdomain.com'
    ##gsettings set org.gnome.system.proxy.ftp port 3128
    sudo -u "$USER" dbus-launch gsettings set org.gnome.system.proxy.https host 'proxy.uni-hamburg.de'
    sudo -u "$USER" dbus-launch gsettings set org.gnome.system.proxy.https port 3128
    sudo -u "$USER" dbus-launch gsettings set org.gnome.system.proxy ignore-hosts "['localhost', '127.0.0.0/8', '10.0.0.0/8', '192.168.0.0/16', '172.16.0.0/12' , 'fc00::/8' , '*.fritz.box' ]"
        ;;
    vpn-down)
    # Disable all proxies on VPN shutdown, this might be to simple
    # for your case, it works for me.
    echo "VPN connection was stopped"
    sudo -u "$USER" dbus-launch gsettings set org.gnome.system.proxy mode 'none'
        ;;
esac
exit 0;

The script is run as root by network-manger so, I needed to hardcode the user whose proxy settings I want to modify in the script. And admittedly the part about calling sudo -u $USER dbus-launch multiple times is quite clumsy and should be solved more elegantly. The problem is that gsettings needs to a) run as the user whose values we want to change and b) needs access to a running dbus-session or it will spit out:

Cannot autolaunch D-Bus without X11 $DISPLAY

Helpful links were: https://wiki.archlinux.org/index.php/proxy_settings https://wiki.ubuntuusers.de/NetworkManager/Dispatcher/ http://askubuntu.com/questions/645968/error-cannot-autolaunch-d-bus-without-x11-display

P.S. Of course, sudo needs to be installed for this script.