From fd9c80b3febc8e1fabc65d9032844129b7af1861 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Tue, 26 Nov 2024 02:17:27 -0600 Subject: [PATCH] new: Added the file qbt-set-forwarded-port.sh --- README.md | 2 +- qbt-set-forwarded-port.sh | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 qbt-set-forwarded-port.sh diff --git a/README.md b/README.md index 6789085..c6718ee 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![Number of scripts](https://img.shields.io/badge/number_of_scripts-43-blue) +![Number of scripts](https://img.shields.io/badge/number_of_scripts-44-blue) # Random Scripts This repository is for random scripts I wrote mostly for personal use. diff --git a/qbt-set-forwarded-port.sh b/qbt-set-forwarded-port.sh new file mode 100644 index 0000000..99c4333 --- /dev/null +++ b/qbt-set-forwarded-port.sh @@ -0,0 +1,39 @@ +#!/bin/sh + +# Adapted from https://github.com/claabs/qbittorrent-port-forward-file/blob/master/main.sh +# Until VPN_PORT_FORWARDING_UP_COMMAND is available, this could be run via a cronjob +# Might need to change the container name in the docker exec line + +set -e + +qbt_username="${QBT_USERNAME:-admin}" +qbt_password="${QBT_PASSWORD:-adminadmin}" +qbt_addr="${QBT_ADDR:-http://localhost:8085}" # ex. http://10.0.1.48:8080 + +port_number=$(docker exec -it qbt-gluetun cat /tmp/gluetun/forwarded_port) + +if [ -z "$port_number" ]; then + echo "Could not figure out which port to set." + exit 1 +fi + +curl --fail --silent --show-error --cookie-jar /tmp/cookies.txt --cookie /tmp/cookies.txt --header "Referer: $qbt_addr" --data "username=$qbt_username" --data "password=$qbt_password" $qbt_addr/api/v2/auth/login 1> /dev/null + +listen_port=$(curl --fail --silent --show-error --cookie-jar /tmp/cookies.txt --cookie /tmp/cookies.txt $qbt_addr/api/v2/app/preferences | jq '.listen_port') + +if [ ! "$listen_port" ]; then + echo "Could not get current listen port, exiting..." + exit 1 +fi + +if [ "$port_number" = "$listen_port" ]; then + echo "Port already set, exiting..." + exit 0 +fi + +echo "Updating port to $port_number" + +curl --fail --silent --show-error --cookie-jar /tmp/cookies.txt --cookie /tmp/cookies.txt --data-urlencode "json={\"listen_port\": $port_number}" $qbt_addr/api/v2/app/setPreferences + +echo "Successfully updated port" +