Skip to content

Instantly share code, notes, and snippets.

@verisgit
Last active February 16, 2019 15:36
Show Gist options
  • Save verisgit/081c7dd523b5dc58bf34846f0b6fd000 to your computer and use it in GitHub Desktop.
Save verisgit/081c7dd523b5dc58bf34846f0b6fd000 to your computer and use it in GitHub Desktop.
Dynamic port forwarding for Raspberry Pi (RPI) running OVPN Private Internet Access (PIA) with qbittorrent
#!/bin/sh
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin
# Private Internet Access Advanced Port Forward Script for RPI / Raspberry Pi with qBittorrent. Can be adapted to other torrent clients.
# Updated 16/02/2019
#Prerequisites:
#Raspberry Pi running OVPN with PIA scripts (https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=223589&p=1371204#p1371204)
#QBittorent must be set to allow unauthorised localhost access. Options>WebUI>Authentication>Bypass authentication for localhost
#NOTE: Once working you need to add a cron job to run this every hours
#PIA recommend checking the port at least once an hour to see if the mapping has changed.
#The easiest way to do this is by running 'crontab -e'.
#An editor will open (usually vi), and you simply insert the below into the file. Note change the path and filename to suit your setup.
#*/5 * * * * /home/pi/portchange.sh > /home/pi/.pflog 2>&1
# Add your PIA username and password below
USERNAME="CHANGEME"
PASSWORD="CHANGEME"
# The script will create a client_id for you this is where it will be saved
PIACLIENTID=/home/pi/.pia_client_id
# Check to see if we have a valid PIA Client ID file.
# If not, create one. Linux is included for illustration only.
if [ ! -e $PIACLIENTID ]; then
head -n 100 /dev/urandom | md5sum | tr -d " -" > $PIACLIENTID
logger "pia-port: Created new PIA Client ID."
fi
# Find out the tunnelling device for your VPN and get your IP address.
# There are several options presented here. Personally, I prefer to use
# the interface which I know relates to my VPN tunnel for forwarding.
DEVICE=`ifconfig | grep -o "tun[0-9]"`
LOCAL_IP=`ifconfig $DEVICE | grep -Po "(?<=inet.)[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"`
# Get the port number for the forwarded port
PORT=`curl -d "user=$USERNAME&pass=$PASSWORD&client_id=$(cat $PIACLIENTID)&local_ip=$LOCAL_IP" https://www.privateinternetaccess.com/vpninfo/port_forward_assignment`
PORTNUM=`echo $PORT | grep -oE "[0-9]+"`
logger "pia-port: Port number acquired: $PORTNUM"
echo $PORTNUM > /home/pi/VPNport.txt
logger "pia-port: Current port forward: $CURPORT"
#Update port in QBittorent
curl -i -X POST -d "json=%7B%22listen_port%22%3A${PORTNUM}%7D" http://localhost:8080/command/setPreferences
# Tests - If something doesn't seem to be working then uncomment the following lines to troubleshoot
echo "Tests:"
echo "Device (should be tun0-9):" $DEVICE
echo "Local IP (should be 10.x.x.x):" $LOCAL_IP
echo "Port:" $PORT
echo "Port Number" $PORTNUM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment