Skip to content

Commit

Permalink
Implement qbittorrent authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
t-anc committed Apr 11, 2024
1 parent 7261bda commit 27dd0b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ This mod is to be used with [linuxserver/qbittorrent container](https://github.c
Follow the instructions [here](https://docs.linuxserver.io/general/container-customization/#docker-mods).
With the following link for the mod `ghcr.io/t-anc/gsp-qbittorent-gluetun-sync-port-mod:main`.

- You will need to enable `Bypass authentication for clients on localhost` inside qbittorrent's `settings` > `Web UI`. (Authentication method not implemented yet)
- You will need to enable `Bypass authentication for clients on localhost` inside qbittorrent's `settings` > `Web UI`. Otherwise you can set the `GSP_QBT_USERNAME` and `GSP_QBT_PASSWORD` variables.
- If you have enabled the `Enable Host header validation` option, you will need to add `localhost` to the `Server domains` list.


## Variables

The following env variables can be used to configure the mod (none is compulsory) :
The following env variables can be used to configure the mod (all are optional) :
| Variable | Default value | Comment |
|:----------------------:|:-----------------------:|----------------------------------------------------------------------------------------------------------|
| `GSP_GTN_ADDR` | `http://localhost:8000` | Gluetun API host address. |
| `GSP_QBT_ADDR` | `http://localhost:8080` | Qbittorrent API host address. If the env variable `WEBUI_PORT` is set, it will be used as default. |
| `GSP_SLEEP` | `60` | Time between checks in seconds. |
| `GSP_RETRY_DELAY` | `10` | Time between retry in case of error (in s). |
| `GSP_QBT_USERNAME` | | NOT IMPLEMENTED YET |
| `GSP_QBT_PASSWORD` | | NOT IMPLEMENTED YET |
| `GSP_QBT_USERNAME` | | Qbittorrent username |
| `GSP_QBT_PASSWORD` | | Qbittorrent password |
| `GSP_SKIP_INIT_CHECKS` | `false` | Set to true to disable qbt config checks ("Bypass authentication on localhost", etc). |
| `GSP_MINIMAL_LOGS` | `true` | Set to false to enable "Ports did not change." logs. |
| `GSP_DEBUG` | `false` | Set to `true` to enable mod's `set -x`. /!\ FOR DEBUG ONLY. |
Expand Down
24 changes: 22 additions & 2 deletions root/etc/s6-overlay/s6-rc.d/svc-mod-gluetun-sync-port/run
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,32 @@ echo "\
+---------------------------------------------------------+
"

qbt_login(){
curl --fail --silent --show-error \

This comment has been minimized.

Copy link
@t-anc

t-anc Apr 12, 2024

Author Owner

"show-error" + "2>/dev/null" ..... Will fix later.

--cookie-jar /tmp/cookies.txt --cookie /tmp/cookies.txt \
--url "${QBITTORRENT}/api/v2/auth/login" \
--data "username=${QBT_USERNAME}" \
--data "password=${QBT_PASSWORD}" > /dev/null 2>&1
}

init_checks(){
conf_file='/config/qBittorrent/qBittorrent.conf'
if [ "${GSP_SKIP_INIT_CHECKS,,}" = 'true' ]; then
MSG_prefix="[WARNING]"
else
MSG_prefix="[ERROR]"
fi

if { [ -z "${QBT_USERNAME}" ] || [ -z "${QBT_PASSWORD}" ] ;} && [ "$(grep 'LocalHostAuth' ${conf_file} | awk -F'=' '{print $2}')" != "false" ]; then
err='true'
log "${MSG_prefix} The \"Bypass authentication for clients on localhost\" setting is not set."
elif [ -n "${QBT_USERNAME}" ] && [ -n "${QBT_PASSWORD}" ] && [ "$(qbt_login; curl --write-out '%{http_code}' --silent --output /dev/null \
--cookie-jar /tmp/cookies.txt --cookie /tmp/cookies.txt \
--url "${QBITTORRENT}/api/v2/app/preferences" \
--data "username=${QBT_USERNAME}" \
--data "password=${QBT_PASSWORD}")" -ne 200 ]; then
err='true'
log "${MSG_prefix} Qbittorrent authentication failed, check the credentials."
fi

if [ "$(curl --write-out '%{http_code}' --silent --output /dev/null "${QBITTORRENT}")" -eq 401 ]; then
Expand Down Expand Up @@ -75,7 +90,7 @@ get_ports(){
GTN='true'
;;
esac
[ "${QBT}" ] && QBT_PORT=$(curl --silent --fail --show-error "${QBITTORRENT}/api/v2/app/preferences" | jq ".listen_port" 2>/dev/null)
[ "${QBT}" ] && QBT_PORT=$(curl --silent --fail --show-error --cookie-jar /tmp/cookies.txt --cookie /tmp/cookies.txt "${QBITTORRENT}/api/v2/app/preferences" | jq ".listen_port" 2>/dev/null)
[ "${GTN}" ] && GTN_PORT=$(curl --silent --fail --show-error "${GLUETUN}/v1/openvpn/portforwarded" | jq ".port" 2>/dev/null)
}

Expand All @@ -93,6 +108,10 @@ init_checks
# Infinite loop
while :; do
SLEEP_TIME="${MAX_SLEEP_TIME}"
if [ -n "${QBT_USERNAME}" ] && [ -n "${QBT_PASSWORD}" ]; then
qbt_login
fi

get_ports

if [ -z "${GTN_PORT}" ] || [ "${GTN_PORT}" -eq 0 ]; then
Expand All @@ -114,6 +133,7 @@ while :; do
curl \
--silent \
--request POST \
--cookie-jar /tmp/cookies.txt --cookie /tmp/cookies.txt \
--url "${QBITTORRENT}/api/v2/app/setPreferences" \
--data "json={\"listen_port\": $GTN_PORT}"
# get the updated value from qbittorrent
Expand Down

0 comments on commit 27dd0b2

Please sign in to comment.