Skip to content

Add script to update qBittorrent peer port #2700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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.
# 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"

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."
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