November 9, 2020

Deluge on Linux – VPN only

By Geekphreek

We all like a bit of privacy, especially when we open ourselves up to a little Torrent action. My preferred software is Deluge and I use SurfShark VPN on Linux which is via OpenVPN.

The issue is, what if you’re connection drops? All of a sudden your packets are flying over the local ISP for a bit of track and trace. Not ideal! On Windows this isn’t an issue as the SurfShark VPN client does a killswitch but it’s not available on Linux. I decided to do this with a simple script launched every minute from Cron, you may wish to change if you’re security conscious:

#!/bin/bash
# Save as /usr/local/bin/check_tunX.sh
# Change the <username> to your username!

# Cron entry:
# */1 * * * * /usr/local/bin/check_tunX.sh &> /dev/null

# Check for VPN, if not kill Deluge and if up
# then make sure Deluge is running
if [ ! $(ip a | grep tun1 | wc -l) -gt 1 ];
then
        echo "VPN down, killing deluge"
        killall deluge-gtk
else
        echo "VPN up"
        pgrep -x deluge-gtk > /dev/null && echo "Deluge Running" || XAUTHORITY=/home/<USERNAME>/.Xauthority DISPLAY=:0 /usr/bin/deluge-gtk
fi

I don’t run headless, so this works well for me. I run two vpn’s, so you may need to adjust tun1 to tun0.

Enjoy!