RPI Deluge PIA Port Forward Script

Trying to get PIA Port Forwarding working your Raspberry Pi (RPI) and Deluge – look no further this script will get you up and running.

  • Login to your Raspberry Pi
  • cd /home/pi
  • sudo nano portforward.sh
  • Paste in the below script (after you have updated the relevant sections)
#!/usr/bin/env bash
# Source: http://www.htpcguides.com
# Adapted from https://github.com/blindpet/piavpn-portforward/
# Author: Mike
# Based on https://github.com/crapos/piavpn-portforward
# Updated by HTGSD.com 19/05/2019

# Change all "<<<PIAPASSWORD>>>" or similar fields to match your specific details i.e. USERNAME=myusername
# Assumes you are using Openvpn for Private Internet Access (PIA) and have Deluge Daemon running on your RPI

# Set path for root Cron Job
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

USERNAME=<<<PIAUSERNAME>>>
VPNINTERFACE=tun0
PASSWORD=<<<PIAPASSWORD>>>
VPNLOCALIP=$(ifconfig $VPNINTERFACE | awk '/inet / {print $2}' | awk 'BEGIN { FS = ":" } {print $(NF)}')
CURL_TIMEOUT=5
CLIENT_ID=$(uname -v | sha1sum | awk '{ print $1 }')

# set to 1 if using VPN Split Tunnel
SPLITVPN="0"

DELUGEUSER=<<<DELUGEUSERNAME>>>
DELUGEPASS=<<<DELUGEPASSWORD>>>
# This one is often localhost but change it to the server IP if you are running deluge remotely
DELUGEHOST=localhost

#get VPNIP
VPNIP=$(curl -m $CURL_TIMEOUT --interface $VPNINTERFACE "http://ipinfo.io/ip" --silent --stderr -)
echo $VPNIP

#request new port
PORTFORWARDJSON=$(curl -m $CURL_TIMEOUT --silent --interface $VPNINTERFACE  'https://www.privateinternetaccess.com/vpninfo/port_forward_assignment' -d "user=$USERNAME&pass=$PASSWORD&client_id=$CLIENT_ID&local_ip=$VPNLOCALIP" | head -1)
#trim VPN forwarded port from JSON
PORT=$(echo $PORTFORWARDJSON | awk 'BEGIN{r=1;FS="{|:|}"} /port/{r=0; print $3} END{exit r}')
echo $PORT  

#change firewall rules if SPLITVPN is set to 1
if [ "$SPLITVPN" -eq "1" ]; then
    #change firewall rules if necessary
    IPTABLERULETWO=$(iptables -L INPUT -n --line-numbers | grep -E "2.*reject-with icmp-port-unreachable" | awk '{ print $8 }')
    if [ -z $IPTABLERULETWO ]; then
        sudo iptables -D INPUT 2
        sudo iptables -I INPUT 2 -i $VPNINTERFACE -p tcp --dport $PORT -j ACCEPT
    else
        sudo iptables -I INPUT 2 -i $VPNINTERFACE -p tcp --dport $PORT -j ACCEPT
    fi
fi

#change deluge port on the fly assumes you are using port 58846 for Deluged if not change the port below

deluge-console "connect $DELUGEHOST:58846 $DELUGEUSER $DELUGEPASS; config --set listen_ports ($PORT,$PORT)"
  • save the file (ctrl x – follow instructions)
  • sudo chmod +x portforward.sh
  • Test it out ./portforward.sh
  • You should see:
    > xxx.xxx.xxx.xx (your IP)
    > xxxxx (your PIA port forwarding port)
    > Setting listen_ports to (xxxxx, xxxxx)..
    > Configuration value successfully updated.
  • If it’s all working then setup your cron to run this every x minutes. The example below will run it every 30 minutes. Google how to use cron if you want to change it.
  • sudo crontab -e
  • add the following to the bottom of the file
    30 * * * * /home/pi/portforward.sh

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.