2015-05-24 16:32:08 +00:00
|
|
|
#! /bin/sh
|
2014-11-09 18:52:52 +00:00
|
|
|
|
|
|
|
# Settings
|
2015-08-07 21:07:55 +00:00
|
|
|
PIA_PASSWD_FILE=/config/openvpn-credentials.txt
|
2014-11-16 13:19:56 +00:00
|
|
|
TRANSMISSION_PASSWD_FILE=/config/transmission-credentials.txt
|
2014-11-09 18:52:52 +00:00
|
|
|
|
2014-11-16 13:19:56 +00:00
|
|
|
pia_username=$(head -1 $PIA_PASSWD_FILE)
|
|
|
|
pia_passwd=$(tail -1 $PIA_PASSWD_FILE)
|
|
|
|
transmission_username=$(head -1 $TRANSMISSION_PASSWD_FILE)
|
|
|
|
transmission_passwd=$(tail -1 $TRANSMISSION_PASSWD_FILE)
|
2014-11-09 18:52:52 +00:00
|
|
|
local_vpn_ip=$(ip addr show tun0 | grep inet | awk '{ print $2 }')
|
|
|
|
pia_client_id_file=/etc/transmission-daemon/pia_client_id
|
2014-11-11 19:35:39 +00:00
|
|
|
transmission_settings_file=/etc/transmission-daemon/settings.json
|
2014-11-09 18:52:52 +00:00
|
|
|
port_assignment_url=https://www.privateinternetaccess.com/vpninfo/port_forward_assignment
|
|
|
|
|
|
|
|
#
|
|
|
|
# First get a port from PIA
|
|
|
|
#
|
|
|
|
|
|
|
|
new_client_id() {
|
|
|
|
head -n 100 /dev/urandom | md5sum | tr -d " -" | tee $pia_client_id_file
|
|
|
|
}
|
|
|
|
|
|
|
|
pia_client_id="$(cat $pia_client_id_file 2>/dev/null)"
|
2015-05-24 18:40:25 +00:00
|
|
|
if [ -z ${pia_client_id} ]; then
|
2014-11-09 18:52:52 +00:00
|
|
|
echo "Generating new client id for PIA"
|
|
|
|
pia_client_id=$(new_client_id)
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Get the port
|
2014-11-16 13:19:56 +00:00
|
|
|
pia_response=$(curl -d "user=$pia_username&pass=$pia_passwd&client_id=$pia_client_id&local_ip=$local_vpn_ip" $port_assignment_url)
|
2014-11-09 18:52:52 +00:00
|
|
|
|
|
|
|
new_port=$(echo $pia_response | grep -oE "[0-9]+")
|
|
|
|
echo "Got new port $new_port from pia"
|
|
|
|
|
|
|
|
#
|
|
|
|
# Now, set port in Transmission
|
|
|
|
#
|
|
|
|
|
|
|
|
# Check if transmission remote is set up with authentication
|
2014-11-11 19:35:39 +00:00
|
|
|
auth_enabled=$(grep 'rpc-authentication-required\"' $transmission_settings_file | grep -oE 'true|false')
|
2015-05-24 18:40:25 +00:00
|
|
|
if [ "true" = "$auth_enabled" ]
|
2014-11-09 18:52:52 +00:00
|
|
|
then
|
|
|
|
echo "transmission auth required"
|
2014-11-16 13:19:56 +00:00
|
|
|
myauth="--auth $transmission_username:$transmission_passwd"
|
2014-11-09 18:52:52 +00:00
|
|
|
else
|
|
|
|
echo "transmission auth not required"
|
|
|
|
myauth=""
|
|
|
|
fi
|
|
|
|
|
|
|
|
# get current listening port
|
|
|
|
transmission_peer_port=$(transmission-remote $myauth -si | grep Listenport | grep -oE '[0-9]+')
|
2015-05-24 18:40:25 +00:00
|
|
|
if [ "$new_port" != "$transmission_peer_port" ]
|
2014-11-09 18:52:52 +00:00
|
|
|
then
|
|
|
|
transmission-remote $myauth -p "$new_port"
|
|
|
|
echo "Checking port..."
|
|
|
|
sleep 10 && transmission-remote $myauth -pt
|
|
|
|
else
|
|
|
|
echo "No action needed, port hasn't changed"
|
|
|
|
fi
|
|
|
|
|