Last active
November 28, 2023 01:36
-
-
Save joshuag/fe7a87c271c7205b5ac72ce8f414aba5 to your computer and use it in GitHub Desktop.
FreeBSD/Truenas Core Bash script to automatically map transmission peer port with PrivateInternetAccess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# These three environment variables should be set, or you can hardcode them into this script | |
PIA_USER=$PIA_USER | |
PIA_PASS=$PIA_PASS | |
TRANSMISSION_HOST=$TRANSMISSION_HOST | |
generateTokenResponse=$(curl -s --location --request POST \ | |
'https://www.privateinternetaccess.com/api/client/v2/token' \ | |
--form "username=$PIA_USER" \ | |
--form "password=$PIA_PASS" ) | |
token=$(echo "$generateTokenResponse" | jq -r '.token') | |
gateway=$(netstat -rn | grep -oE "0.0.0.0\/1(.+)10\.[0-9]+\.[0-9]+\.[0-9]+" | tr -d "a-z :" | sed -n "s/0.0.0.0\/1//p") | |
readarray -t payload_arr < <(curl -ks "https://$gateway:19999/getSignature?token=$token" | jq -r .payload,.signature) | |
payload="${payload_arr[0]}" | |
token="${payload_arr[1]}" | |
port=$(echo "$payload" | base64 -d | jq -r .port) | |
while true; do | |
#PIA will automatically drop the port mapping without a keep-alive. This will request the same port every 15 minutes | |
output=$(curl -sGk --data-urlencode "payload=$payload" --data-urlencode "signature=$token" https://"$gateway":19999/bindPort) | |
sessionid="$(curl -is "$TRANSMISSION_HOST"/transmission/rpc | grep -oE "<code>([^<]+)</code>" | grep -oE ": [^<]+" | awk -F': ' '{print $2}' | awk -F'</code>' '{print $1}')" | |
set_port=$(curl -sX POST "$TRANSMISSION_HOST"/transmission/rpc/ -H "Content-Type: application/json" -H "X-Transmission-Session-Id: $sessionid" -d "{\"method\": \"session-set\",\"arguments\": {\"peer-port\": $port}}") | |
echo $set_port | |
sleep 900 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment