Skip to content

Instantly share code, notes, and snippets.

@alfredkrohmer
Last active October 13, 2017 21:53

Revisions

  1. @devkid devkid revised this gist Oct 13, 2017. No changes.
  2. @devkid devkid created this gist Oct 13, 2017.
    50 changes: 50 additions & 0 deletions vpn-domains.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    #!/bin/bash

    set -ex

    # ----------------------------------------------------------------------------
    # Configuration:

    case "$CONNECTION_ID"
    in
    "Some VPN name") # enter name of VPN connection in NetworkManager here
    DOMAINS=( # enter domains that should be routed over the VPN here
    some-domain.com
    another-domain.net
    )
    ;;

    # repeat for more VPN connections

    *)
    exit 0
    ;;
    esac

    # ----------------------------------------------------------------------------

    DBUS_PATH="/org/freedesktop/resolve1"
    DBUS_IF="org.freedesktop.resolve1"

    INTERFACE="$1"
    IF_INDEX="$(cat "/sys/class/net/$INTERFACE/ifindex")"

    if [[ $2 != "vpn-up" ]]
    then
    exit 0
    fi

    DOMAIN_STR=""
    FIRST=true
    for DOMAIN in ${DOMAINS[@]}
    do
    if [[ $FIRST == true ]]
    then
    FIRST=false
    else
    DOMAIN_STR="${DOMAIN_STR}, "
    fi
    DOMAIN_STR="${DOMAIN_STR}('${DOMAIN}', true)"
    done

    gdbus call --system --dest $DBUS_IF --object-path $DBUS_PATH --method $DBUS_IF.Manager.SetLinkDomains $IF_INDEX "[$DOMAIN_STR]"