Does anyone here use VPNdemon with PIA?

Here's the bash file.
I'm curious to know, where in here I'd have to get it to see the current state of PIA.

I'm sure I could use IPtables, but I also use this server as a media server. It basically shares out the same videos it downloads, to other machines. NFS and Samba.
I also SSH into this machine.
And I teamviewer into this machine (from phone, to manipulate media or start new torrent downloads).
I'd need a somewhat complex IPtable to make all this happen. So I was looking for a somewhat easier solution. My bash scripting knowledge isn't top notch... But it looks like it's listening to uint32 2.  When uint32 changes from 2 to 7 (uint32 7), it fires off it's magic.

I'm confused on how to change that uint32 to whatever PIA is using.

#!/bin/bash

interface="org.freedesktop.NetworkManager.VPN.Connection"
member="VpnStateChanged"
logPath="/tmp/vpndemon"
header="VPNDemon\nhttps://github.com/primaryobjects/vpndemon\n\n"

# Clear log file.
> "$logPath"

list_descendants()
{
    local children=$(ps -o pid= --ppid "$1")

    for pid in $children
    do
        list_descendants "$pid"
    done

    echo "$children"
}

# Consider the first argument as the target process
killProgramName="$1"
if [ -z "$killProgramName" ]
then
    killProgramName=$(zenity --entry --title="VPNDemon" --text="$header Enter name of process to kill when VPN disconnects:")
fi

result=$?
if [ $result = 0 ]
then
    if [ $killProgramName ]
    then
        header="$header Target: $killProgramName\n\n"

        (tail -f "$logPath") |
        {
            zenity --progress --title="VPNDemon" --text="$header Monitoring VPN" --pulsate

            # Kill all child processes upon exit.
            kill $(list_descendants $$)
        } |
        {
            # Monitor for VPNStateChanged event.
            dbus-monitor --system "type='signal',interface='$interface',member='$member'" |
            {
                # Read output from dbus.
                (while read -r line
                do
                    currentDate=`date +'%m-%d-%Y %r'`

                    # Check if this a VPN connection (uint32 2) event.
                    if [ x"$(echo "$line" | grep 'uint32 3')" != x ]
                    then
                        echo "VPN Connected $currentDate"
                        echo "# $header VPN Connected $currentDate" >> "$logPath"
                    fi

                    # Check if this a VPN disconnected (uint32 7) event.
                    if [ x"$(echo "$line" | grep 'uint32 6\|uint32 7')" != x ]
                    then
                        echo "VPN Disconnected $currentDate"
                        echo "# $header VPN Disconnected $currentDate" >> "$logPath"

                        # Kill target process.
                        for i in `pgrep $killProgramName`
                        do
                            # Get process name.
                            name=$(ps -ocommand= -p $i | awk -F/ '{print $NF}' | awk '{print $1}')

                            # Kill process.
                            kill $i

                            # Log result.
                            echo "Terminated $i ($name)"
                            echo "Terminated $i ($name)" >> "$logPath"
                        done
                    fi
                done)
            }
        }
    else
        zenity --error --text="No process name entered."
    fi
fi
Sign In or Register to comment.