You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

updatePort.sh 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #! /bin/sh
  2. # Source our persisted env variables from container startup
  3. . /etc/transmission/environment-variables.sh
  4. # Settings
  5. PIA_PASSWD_FILE=/config/openvpn-credentials.txt
  6. TRANSMISSION_PASSWD_FILE=/config/transmission-credentials.txt
  7. pia_username=$(head -1 $PIA_PASSWD_FILE)
  8. pia_passwd=$(tail -1 $PIA_PASSWD_FILE)
  9. transmission_username=$(head -1 $TRANSMISSION_PASSWD_FILE)
  10. transmission_passwd=$(tail -1 $TRANSMISSION_PASSWD_FILE)
  11. pia_client_id_file=/etc/transmission/pia_client_id
  12. transmission_settings_file=${TRANSMISSION_HOME}/settings.json
  13. #
  14. # First get a port from PIA
  15. #
  16. new_client_id() {
  17. head -n 100 /dev/urandom | md5sum | tr -d " -" | tee $pia_client_id_file
  18. }
  19. pia_client_id="$(cat $pia_client_id_file 2>/dev/null)"
  20. if [ -z ${pia_client_id} ]; then
  21. echo "Generating new client id for PIA"
  22. pia_client_id=$(new_client_id)
  23. fi
  24. # Get the port
  25. port_assignment_url="http://209.222.18.222:2000/?client_id=$pia_client_id"
  26. pia_response=$(curl -s -f $port_assignment_url)
  27. pia_curl_exit_code=$?
  28. if [ -z $pia_response ]; then
  29. echo "Port forwarding is already activated on this connection, has expired, or you are not connected to a PIA region that supports port forwarding"
  30. fi
  31. # Check for curl error (curl will fail on HTTP errors with -f flag)
  32. if [ $pia_curl_exit_code -ne 0 ]; then
  33. echo "curl encountered an error looking up new port: $pia_curl_exit_code"
  34. exit
  35. fi
  36. # Check for errors in PIA response
  37. error=$(echo $pia_response | grep -oE "\"error\".*\"")
  38. if [ ! -z "$error" ]; then
  39. echo "PIA returned an error: $error"
  40. exit
  41. fi
  42. # Get new port, check if empty
  43. new_port=$(echo $pia_response | grep -oE "[0-9]+")
  44. if [ -z "$new_port" ]; then
  45. echo "Could not find new port from PIA"
  46. exit
  47. fi
  48. echo "Got new port $new_port from PIA"
  49. #
  50. # Now, set port in Transmission
  51. #
  52. # Check if transmission remote is set up with authentication
  53. auth_enabled=$(grep 'rpc-authentication-required\"' $transmission_settings_file | grep -oE 'true|false')
  54. if [ "true" = "$auth_enabled" ]
  55. then
  56. echo "transmission auth required"
  57. myauth="--auth $transmission_username:$transmission_passwd"
  58. else
  59. echo "transmission auth not required"
  60. myauth=""
  61. fi
  62. # get current listening port
  63. transmission_peer_port=$(transmission-remote $myauth -si | grep Listenport | grep -oE '[0-9]+')
  64. if [ "$new_port" != "$transmission_peer_port" ]; then
  65. if [ "true" = "$ENABLE_UFW" ]; then
  66. echo "Update UFW rules before changing port in Transmission"
  67. echo "denying access to $TRANSMISSION_PEER_PORT"
  68. ufw deny $TRANSMISSION_PEER_PORT
  69. echo "allowing $new_port through the firewall"
  70. ufw allow $new_port
  71. fi
  72. transmission-remote $myauth -p "$new_port"
  73. echo "Checking port..."
  74. sleep 10
  75. transmission-remote $myauth -pt
  76. else
  77. echo "No action needed, port hasn't changed"
  78. fi