Skip to content

Instantly share code, notes, and snippets.

@nphyx
Last active December 23, 2019 03:43
Show Gist options
  • Save nphyx/c0313ed68d02033ec7fd90228dd47038 to your computer and use it in GitHub Desktop.
Save nphyx/c0313ed68d02033ec7fd90228dd47038 to your computer and use it in GitHub Desktop.
Update transmission bind address & peer port for Private Internet Access
#!/bin/bash
# Transmission address binding & port configurator for transmission-gtk (does not apply to transmission daemon)
#
# This script relies on my other script, piaport: https://gist.github.com/nphyx/e24c5349469f8dbca627761fafd57fbe
# It also requires gawk, and assumes it's in /usr/bin/gawk. If it's somewhere else for you change it.
#
# It must be run manually whenever VPN goes down and up again.
#
# You should already have bind-address-ipv4 and bind-address-ipv6 configured in your transmission settings.json!
#
# These have to be set manually, but when they're set transmission will only listen on your VPN tunnel device, which means
# it will never transmit over non-VPN devices. Super helpful but a pain to update since transmission has no GUI config
# for these settings.
#
# This script fixes that.
# YOUR VPN DEVICE NAME GOES HERE (typically 'tun0' but try "ip addr" to find it)
dev="tun0"
# YOUR TRANSMISSION CONFIG DIRECTORY GOES HERE
confdir=$HOME/.config/transmission
# use ss to detect transmission since it'll have a network port and won't conflict with anything else named 'transmission' we hope...
function get_pid {
pid=$( ss -tlnp | grep transmission | awk -F "=" "/pid=/{ gsub(\",.*\", \"\", \$2); print \$2}" )
}
echo "Get a new PIA port:"
port=$( sudo piaport )
echo "setting port to: $port"
ipv4=$( ip addr | grep -i -A 5 $dev | /usr/bin/gawk -F" " "/inet /{ print \$2 }" )
ipv6=$( ip addr | grep -i -A 5 $dev | /usr/bin/gawk -F" " "/inet6 /{ print \$2 }" | /usr/bin/gawk -F"/" "// { print \$1 }" )
echo "setting ip4 to: $ipv4"
echo "setting ip6 to: $ipv6"
echo "Checking if transmission is up..."
get_pid
if test -z $pid; then
echo "transmission not found"
else
echo "found PID for transmission: $pid, shutting down..."
kill $pid >/dev/null 2>/dev/null
echo "waiting for shutdown..."
while kill -0 $pid 2>/dev/null; do echo "..."; sleep 1; done
fi
cp $confdir/settings.json $confdir/settings.json.bak
/usr/bin/gawk -i inplace "\
{ gsub(/\"bind-address-ipv4\": \".*\"/, \"\\\"bind-address-ipv4\\\": \\\"$ipv4\\\"\") }; \
{ gsub(/\"bind-address-ipv6\": \".*\"/, \"\\\"bind-address-ipv6\\\": \\\"$ipv6\\\"\") }; \
{ gsub(/\"peer-port\": .*/, \"\\\"peer-port\\\": $port,\") }; \
{ print }" $confdir/settings.json
echo "starting transmission..."
transmission-gtk & disown transmission-gtk
echo "done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment