Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/qbittorrent-port-update.sh /scripts/qbittorrent-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 && \
Expand Down
124 changes: 124 additions & 0 deletions extras/scripts/qbittorrent-port-update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/bin/sh

# Description: This script updates the peer-port for the qBittorrent torrent client using its WebUI API.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See qdm12/gluetun-wiki@55ffd92

We should have a "down" mode to change the port to something else.
Or, maybe better, have it set it to some random port i.e. 9876, and then to the actual port we want, such that there is only an up command to set.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what's best here; the provider I'm using is always giving a unique port.
For now, I set it to 0 as a "safe" port to reset the setting. If we use any "random" port, there is a minor chance that it might match with an actual port.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting it to 0 is a good idea, although that's not done here. Maybe we could do it right before calling the API to set to the desired port?

# Note: For this to work, "Bypass authentication for clients on localhost" should be enabled
# in the WebUI settings (json key bypass_local_auth).

build_default_url() {
port="${1:-$WEBUI_PORT}"
echo "http://127.0.0.1:${port}/api"
}

# default values
WEBUI_PORT="8080"
DEFAULT_URL=$(build_default_url)
WGET_OPTS="--retry-connrefused --tries=5"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might require some time until qbittorrent is up and listening when I start my compose stack

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment for this? 😉


usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Update qBittorrent peer-port via API"
echo ""
echo "Options:"
echo " -h, --help Show this help message and exit."
echo " -u, --user USER Specify the qBittorrent username."
echo " (Omit if not required)"
echo " -p, --pass PASS Specify the qBittorrent password."
echo " (Omit if not required)"
echo " -P, --port PORT Specify the qBittorrent peer-port."
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should accept a comma separated list of ports from Gluetun's {{PORTS}} to simplify usage of this script.
And then it should use $(echo {{PORTS}} | cut -d, -f1) to take only the first port just in case, since qbitorrent supports only one port.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I got it right, does it mean we should use it like this bittorrent-port-update.sh --port {{PORTS}}?

E.g. via VPN_PORT_FORWARDING_UP_COMMAND=/bin/sh -c "/scripts/qbittorrent-port-update.sh --port {{PORTS}} --webui-port 9081"

If so, it's already supported, this is exactly how I used it since beggining.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, but {{PORTS}} can have multiple ports so for convenience and a shorter command, let's cut and use the first port within the script. The flag should be renamed to --ports and indicate that despite the name it only picks the first port

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the other way around, let's keep it named as --port to indicate that only one of them is used.

The logic to split and keep the first was already there
https://github.com/astappiev/gluetun/blob/839e318c9c30676c8ecd9f042c524e87a1634f32/extras/scripts/qbittorrent-port-update.sh#L60

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually....
Added support for {{PORT}} variable in fcdba0a which takes the first of the ports. No reason to have it supported in scripts!

echo " REQUIRED"
echo " -W, --webui-port PORT Specify the qBittorrent WebUI Port."
echo " Default: ${WEBUI_PORT}"
echo " -U, --url URL Specify the qBittorrent API URL."
echo " DEFAULT: ${DEFAULT_URL}"
echo " Overrides --webui-port option."
echo "Example:"
echo " $0 -u admin -p **** --port 40409"
}

while [ $# -gt 0 ]; do
case "$1" in
-h | --help)
usage
exit 0
;;
-u | --user)
USERNAME="$2"
_USECRED=true
shift 2
;;
-p | --pass)
PASSWORD="$2"
_USECRED=true
shift 2
;;
-P | --port)
PORTS="$2"
PORT=$(echo "$PORTS" | cut -d',' -f1)
shift 2
;;
-W | --webui-port)
WEBUI_PORT="$2"
shift 2
;;
-U | --url)
PREF_URL="$2"
shift 2
;;
*)
echo "Unknown option: $1"
usage
exit 1
;;
esac
done

if [ -z "${PORT}" ]; then
echo "ERROR: No qBittorrent peer-port provided!"
exit 1
fi

if [ -z "${PREF_URL+x}" ]; then
PREF_URL=$(build_default_url)
fi

if [ "${_USECRED}" ]; then
# make sure username AND password were provided
if [ -z "${USERNAME}" ]; then
echo "ERROR: qBittorrent username not provided."
exit 1
fi
if [ -z "${PASSWORD}" ]; then
echo "ERROR: qBittorrent password not provided."
exit 1
fi

cookie=$(wget ${WGET_OPTS} -qO- \
--header "Referer: ${PREF_URL}" \
--post-data "username=${USERNAME}&password=${PASSWORD}" \
"${PREF_URL}/v2/auth/login" \
--server-response 2>&1 | \
grep -i "set-cookie:" | \
sed 's/.*set-cookie: //I;s/;.*//')

if [ -z "${cookie}" ]; then
echo "ERROR: Could not authenticate with qBittorrent."
exit 1
fi

# set cookie for future requests
WGET_OPTS="${WGET_OPTS} --header=Cookie:$cookie"
fi

# update peer host via API
wget ${WGET_OPTS} -qO- --post-data="json={\"random_port\":false}" "$PREF_URL/v2/app/setPreferences"
wget ${WGET_OPTS} -qO- --post-data="json={\"listen_port\":$PORT}" "$PREF_URL/v2/app/setPreferences"

# check if wget command succeeded
if [ $? -ne 0 ]; then
echo "ERROR: Could not update qBittorrent peer-port."
exit 1
fi

echo "Success! qBittorrent peer-port updated to ${PORT}"
exit 0