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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #! /bin/sh
  2. echo "Wait for tunnel to be fully initialized and PIA is ready to give us a port"
  3. sleep 15
  4. # Source our persisted env variables from container startup
  5. . /etc/transmission/environment-variables.sh
  6. # Settings
  7. TRANSMISSION_PASSWD_FILE=/config/transmission-credentials.txt
  8. transmission_username=$(head -1 $TRANSMISSION_PASSWD_FILE)
  9. transmission_passwd=$(tail -1 $TRANSMISSION_PASSWD_FILE)
  10. pia_client_id_file=/etc/transmission/pia_client_id
  11. transmission_settings_file=${TRANSMISSION_HOME}/settings.json
  12. #
  13. # First get a port from PIA
  14. #
  15. new_client_id() {
  16. head -n 100 /dev/urandom | sha256sum | tr -d " -" | tee $pia_client_id_file
  17. }
  18. pia_client_id="$(cat $pia_client_id_file 2>/dev/null)"
  19. if [ -z "${pia_client_id}" ]; then
  20. echo "Generating new client id for PIA"
  21. pia_client_id=$(new_client_id)
  22. fi
  23. # Get the port
  24. port_assignment_url="http://209.222.18.222:2000/?client_id=$pia_client_id"
  25. pia_response=$(curl -s -f "$port_assignment_url")
  26. pia_curl_exit_code=$?
  27. if [ -z "$pia_response" ]; then
  28. echo "Port forwarding is already activated on this connection, has expired, or you are not connected to a PIA region that supports port forwarding"
  29. fi
  30. # Check for curl error (curl will fail on HTTP errors with -f flag)
  31. if [ $pia_curl_exit_code -ne 0 ]; then
  32. echo "curl encountered an error looking up new port: $pia_curl_exit_code"
  33. exit
  34. fi
  35. # Check for errors in PIA response
  36. error=$(echo "$pia_response" | grep -oE "\"error\".*\"")
  37. if [ ! -z "$error" ]; then
  38. echo "PIA returned an error: $error"
  39. exit
  40. fi
  41. # Get new port, check if empty
  42. new_port=$(echo "$pia_response" | grep -oE "[0-9]+")
  43. if [ -z "$new_port" ]; then
  44. echo "Could not find new port from PIA"
  45. exit
  46. fi
  47. echo "Got new port $new_port from PIA"
  48. #
  49. # Now, set port in Transmission
  50. #
  51. # Check if transmission remote is set up with authentication
  52. auth_enabled=$(grep 'rpc-authentication-required\"' "$transmission_settings_file" \
  53. | 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