From 905ad6767f174dbcfef4a771019189c5eb104a29 Mon Sep 17 00:00:00 2001 From: Juanlu Date: Mon, 2 Dec 2024 00:15:08 +0100 Subject: [PATCH 1/6] Add script to update Transmission peer port The script `transmission-port-update.sh` updates the _peer port_ used by the torrent client Transmission. The peer port is the port used and announced by Transmission to accept incoming connections. --- extras/scripts/transmission-port-update.sh | 121 +++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100755 extras/scripts/transmission-port-update.sh diff --git a/extras/scripts/transmission-port-update.sh b/extras/scripts/transmission-port-update.sh new file mode 100755 index 000000000..64f12d437 --- /dev/null +++ b/extras/scripts/transmission-port-update.sh @@ -0,0 +1,121 @@ +#!/bin/sh + +# Description: This script updates the peer port for the Transmission torrent +# client using its RPC API. +# Author: Juan Luis Font + +# default values +DEFAULT_URL="http://localhost:9091/transmission/rpc" +WGET_OPTS="--quiet" + +usage() { + echo "Usage: $0 [OPTIONS]" + echo + echo "Options:" + echo " -h, --help Show this help message and exit." + echo " -u, --user USER Specify the Transmission RPC user name." + echo " (Omit if not required)" + echo " -p, --pass PASS Specify the Transmission RPC password." + echo " (Omit if not required)" + echo " -P, --port PORT Specify the Transmission Peer Port." + echo " REQUIRED" + echo " -U, --url URL Specify the Transmission RPC URL." + echo " DEFAULT: ${DEFAULT_URL}" + echo "Example:" + echo " $0 -u admin -p **** 40409" + exit 1 +} + +while [ $# -gt 0 ]; do + case "$1" in + -h | --help) + usage + ;; + -u | --user) + USERNAME="$2" + _USECRED=true + shift 2 + ;; + -p | --pass) + PASSWORD="$2" + _USECRED=true + shift 2 + ;; + -P | --port) + PORT="$2" + shift 2 + ;; + -U | --url) + RPC_URL="$2" + shift 2 + ;; + *) + echo "Unknown option: $1" + usage + ;; + esac +done + +if [ -z "${PORT}" ]; then + echo "ERROR: No PORT provided!" + exit 1 +fi + +if [ "${_USECRED}" ]; then + # make sure username AND password were provided + if [ -z "${USERNAME}" ] || [ -z "${PASSWORD}" ]; then + echo "ERROR: Username or password not provided." + exit 1 + else + # basic auth options, --auth-no-challenge avoids 409 Conflict + WGET_OPTS=" + ${WGET_OPTS} + --auth-no-challenge + --user=${USERNAME} + --password=${PASSWORD} + " + fi +fi + +if [ -z "${RPC_URL+x}" ]; then + RPC_URL="${DEFAULT_URL}" +fi + +# get the X-Transmission-Session-Id +SESSION_ID=$( + wget \ + ${WGET_OPTS} \ + --server-response \ + "$RPC_URL" 2>&1 | + grep 'X-Transmission-Session-Id:' | + awk '{print $2}' +) + +# generate payload string +PAYLOAD=$(printf '{ + "method": "session-set", + "arguments": { + "peer-port": %s + } +}' "$PORT") + +# update peer host via API +RES=$( + wget \ + ${WGET_OPTS} \ + --header="Content-Type: application/json" \ + --header="X-Transmission-Session-Id: $SESSION_ID" \ + --post-data="${PAYLOAD}" \ + "$RPC_URL" \ + -O - +) + +# check string returned by wget +SUCCESS='{"arguments":{},"result":"success"}' +if [ "$RES" = "$SUCCESS" ]; then + echo "Success! Peer port updated to ${PORT}" + exit 0 +else + echo "ERROR: Could not update peer port" + exit 1 +fi From 3ee4da7f1e89d289ee512be680e07e609408e8f8 Mon Sep 17 00:00:00 2001 From: JuanLu Date: Wed, 22 Jan 2025 23:17:39 +0100 Subject: [PATCH 2/6] Add script improvements - better error handling - support for comma-separated list of ports (use only first one) - style corrections - improved messages --- extras/scripts/transmission-port-update.sh | 41 ++++++++++++++-------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/extras/scripts/transmission-port-update.sh b/extras/scripts/transmission-port-update.sh index 64f12d437..94c9c03a6 100755 --- a/extras/scripts/transmission-port-update.sh +++ b/extras/scripts/transmission-port-update.sh @@ -10,7 +10,9 @@ WGET_OPTS="--quiet" usage() { echo "Usage: $0 [OPTIONS]" - echo + echo "" + echo "Update Transmission peer-port via RPC API" + echo "" echo "Options:" echo " -h, --help Show this help message and exit." echo " -u, --user USER Specify the Transmission RPC user name." @@ -18,18 +20,20 @@ usage() { echo " -p, --pass PASS Specify the Transmission RPC password." echo " (Omit if not required)" echo " -P, --port PORT Specify the Transmission Peer Port." + echo " If PORT is a comma-separated list of ports," + echo " only use the first one." echo " REQUIRED" echo " -U, --url URL Specify the Transmission RPC URL." echo " DEFAULT: ${DEFAULT_URL}" echo "Example:" echo " $0 -u admin -p **** 40409" - exit 1 } while [ $# -gt 0 ]; do case "$1" in -h | --help) usage + exit 0 ;; -u | --user) USERNAME="$2" @@ -42,7 +46,8 @@ while [ $# -gt 0 ]; do shift 2 ;; -P | --port) - PORT="$2" + PORTS="$2" + PORT=$(echo "$PORTS" | cut -d',' -f1) shift 2 ;; -U | --url) @@ -52,29 +57,33 @@ while [ $# -gt 0 ]; do *) echo "Unknown option: $1" usage + exit 1 ;; esac done if [ -z "${PORT}" ]; then - echo "ERROR: No PORT provided!" + echo "ERROR: No Transmission peer-port provided!" exit 1 fi if [ "${_USECRED}" ]; then # make sure username AND password were provided - if [ -z "${USERNAME}" ] || [ -z "${PASSWORD}" ]; then - echo "ERROR: Username or password not provided." + if [ -z "${USERNAME}" ]; then + echo "ERROR: Transmission RPC Username not provided." + exit 1 + fi + if [ -z "${PASSWORD}" ]; then + echo "ERROR: Transmission RPC Password not provided." exit 1 - else - # basic auth options, --auth-no-challenge avoids 409 Conflict - WGET_OPTS=" + fi + # basic auth options, --auth-no-challenge avoids 409 Conflict + WGET_OPTS=" ${WGET_OPTS} --auth-no-challenge --user=${USERNAME} --password=${PASSWORD} " - fi fi if [ -z "${RPC_URL+x}" ]; then @@ -82,6 +91,7 @@ if [ -z "${RPC_URL+x}" ]; then fi # get the X-Transmission-Session-Id +# shellcheck disable=SC2086 SESSION_ID=$( wget \ ${WGET_OPTS} \ @@ -100,6 +110,7 @@ PAYLOAD=$(printf '{ }' "$PORT") # update peer host via API +# shellcheck disable=SC2086 RES=$( wget \ ${WGET_OPTS} \ @@ -112,10 +123,10 @@ RES=$( # check string returned by wget SUCCESS='{"arguments":{},"result":"success"}' -if [ "$RES" = "$SUCCESS" ]; then - echo "Success! Peer port updated to ${PORT}" - exit 0 -else - echo "ERROR: Could not update peer port" +if [ "$RES" != "$SUCCESS" ]; then + echo "ERROR: Could not update Transmission peer-port" exit 1 fi + +echo "Success! Transmission peer-port updated to ${PORT}" +exit 0 From d839868e1a37318a8bfc2d37698e202d80c285ad Mon Sep 17 00:00:00 2001 From: JuanLu Date: Wed, 22 Jan 2025 23:29:38 +0100 Subject: [PATCH 3/6] Add script Transmission script to Dockerfile --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index 1415abcbe..6dbdb8776 100644 --- a/Dockerfile +++ b/Dockerfile @@ -224,6 +224,7 @@ ENV VPN_SERVICE_PROVIDER=pia \ ENTRYPOINT ["/gluetun-entrypoint"] EXPOSE 8000/tcp 8888/tcp 8388/tcp 8388/udp HEALTHCHECK --interval=5s --timeout=5s --start-period=10s --retries=3 CMD /gluetun-entrypoint healthcheck +COPY extras/scripts/transmission-port-update.sh /scripts/transmission-port-update.sh ARG TARGETPLATFORM RUN apk add --no-cache --update -l wget && \ apk add --no-cache --update -X "https://dl-cdn.alpinelinux.org/alpine/v3.17/main" openvpn\~2.5 && \ From 2d6ab23e4ab3367552677e34b6e659076595ba48 Mon Sep 17 00:00:00 2001 From: juanlufont Date: Mon, 27 Jan 2025 12:05:40 +0100 Subject: [PATCH 4/6] Improve error message Co-authored-by: Quentin McGaw --- extras/scripts/transmission-port-update.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extras/scripts/transmission-port-update.sh b/extras/scripts/transmission-port-update.sh index 94c9c03a6..f6291f997 100755 --- a/extras/scripts/transmission-port-update.sh +++ b/extras/scripts/transmission-port-update.sh @@ -124,7 +124,7 @@ RES=$( # check string returned by wget SUCCESS='{"arguments":{},"result":"success"}' if [ "$RES" != "$SUCCESS" ]; then - echo "ERROR: Could not update Transmission peer-port" + echo "ERROR: Could not update Transmission peer-port: ${RES}" exit 1 fi From f8be206c888d3cb3a9a3ac456cf797337c96d0e5 Mon Sep 17 00:00:00 2001 From: juanlufont Date: Mon, 27 Jan 2025 12:06:04 +0100 Subject: [PATCH 5/6] Remove redundant exit 0 Co-authored-by: Quentin McGaw --- extras/scripts/transmission-port-update.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extras/scripts/transmission-port-update.sh b/extras/scripts/transmission-port-update.sh index f6291f997..002390520 100755 --- a/extras/scripts/transmission-port-update.sh +++ b/extras/scripts/transmission-port-update.sh @@ -79,7 +79,7 @@ if [ "${_USECRED}" ]; then fi # basic auth options, --auth-no-challenge avoids 409 Conflict WGET_OPTS=" - ${WGET_OPTS} + --quiet --auth-no-challenge --user=${USERNAME} --password=${PASSWORD} From 08a18a1c3921b7f4925aba6af8712fe290d0c7e3 Mon Sep 17 00:00:00 2001 From: Juanlu Date: Mon, 27 Jan 2025 12:14:03 +0100 Subject: [PATCH 6/6] Move --quiet out of WGET_OPTS --- extras/scripts/transmission-port-update.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/extras/scripts/transmission-port-update.sh b/extras/scripts/transmission-port-update.sh index 002390520..95e59911c 100755 --- a/extras/scripts/transmission-port-update.sh +++ b/extras/scripts/transmission-port-update.sh @@ -6,7 +6,7 @@ # default values DEFAULT_URL="http://localhost:9091/transmission/rpc" -WGET_OPTS="--quiet" +WGET_OPTS="" usage() { echo "Usage: $0 [OPTIONS]" @@ -79,7 +79,6 @@ if [ "${_USECRED}" ]; then fi # basic auth options, --auth-no-challenge avoids 409 Conflict WGET_OPTS=" - --quiet --auth-no-challenge --user=${USERNAME} --password=${PASSWORD} @@ -94,6 +93,7 @@ fi # shellcheck disable=SC2086 SESSION_ID=$( wget \ + --quiet \ ${WGET_OPTS} \ --server-response \ "$RPC_URL" 2>&1 | @@ -113,6 +113,7 @@ PAYLOAD=$(printf '{ # shellcheck disable=SC2086 RES=$( wget \ + --quiet \ ${WGET_OPTS} \ --header="Content-Type: application/json" \ --header="X-Transmission-Session-Id: $SESSION_ID" \