Skip to content

Instantly share code, notes, and snippets.

@vekexasia
Last active January 27, 2025 08:28
Show Gist options
  • Save vekexasia/961c00d3c1d49a23980f54c25b269200 to your computer and use it in GitHub Desktop.
Save vekexasia/961c00d3c1d49a23980f54c25b269200 to your computer and use it in GitHub Desktop.
AutoNat qBittorrent
[Unit]
Description=AutoNat
After=multi-user.target
[Service]
Type=simple
ExecStart=/root/autonat.sh
Restart=always
RestartSec=5s
StartLimitIntervalSec=500
StartLimitBurst=5
[Install]
WantedBy=multi-user.target
#!/usr/bin/env bash
QBITTORRENT="${GSP_QBT_ADDR:-http://localhost:${WEBUI_PORT:-8080}}"
QBT_COOKIES="--cookie-jar /tmp/cookies.txt --cookie /tmp/cookies.txt"
# Check and install qbittorrent
QBT_PORT=""
qbt_refresh_port() {
QBT_PORT=$(curl --silent --fail --show-error ${CURL_OPT} ${QBT_COOKIES} "${QBITTORRENT}/api/v2/app/preferences" | jq ".listen_port" 2>/dev/null)
echo "qBittorrent Listen Port is: ${QBT_PORT}"
}
qbt_refresh_port
while true ; do
date
gateway="10.2.0.1"
mapping_info=$(natpmpc -a 1 0 tcp 60 -g $gateway)
if [[ $mapping_info =~ Mapped\ public\ port\ ([0-9]+) ]]; then
mapped_port="${BASH_REMATCH[1]}"
echo "Mapped public port: $mapped_port"
# Check if the mapped port has changed
if [[ "$mapped_port" != "$QBT_PORT" ]]; then
echo "Changing qBittorrent port to $mapped_port"
# Change qBittorrent port
curl \
--silent \
${CURL_OPT} \
--request POST \
${QBT_COOKIES} \
--url "${QBITTORRENT}/api/v2/app/setPreferences" \
--data "json={\"listen_port\": $mapped_port}"
qbt_refresh_port
else
echo "Mapped port has not changed. Skipping qBittorrent port update."
fi
else
echo "ERROR: Failed to extract Mapped public port from natpmpc output"
fi
sleep 45
done
you need to change qbittorrent setting to bypass authentication on localhost
- WebUI -> Authentication -> tick Bypass authentiction for clients on localhost
Works both docker and standalone as long as bittorrent is listening on localhost interface. when using docker i suggest changing the docker compose to either forward all ports or use **hosts** network mode
The autonat.service file goes inside the `/etc/systemd/system/` folder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment