Skip to content

Commit

Permalink
Add multiple ports support. (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-anc committed Sep 22, 2024
1 parent 6edba08 commit 02b9649
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The following env variables can be used to configure the mod (all are optional)
| `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 retries in case of error (in s). |
| `GSP_PORT_INDEX` | `1` | Index of port to use from gluetun. Set to `2` to use the second one, etc. Only if you have multiple ports forwarded. |
| `GSP_QBT_USERNAME` | | Qbittorrent username. |
| `GSP_QBT_PASSWORD` | | Qbittorrent password. |
| `GSP_QBT_PASSWORD_FILE`| | Qbittorrent password file (for [docker secret](https://docs.docker.com/compose/use-secrets/) use). This supplants `GSP_QBT_PASSWORD`. |
Expand Down Expand Up @@ -133,6 +134,7 @@ User GID: 1000
+---------------------------------------------------------+
| Qbittorrent address : http://localhost:8080 |
| Gluetun address : http://localhost:8000 |
| Port index : 1 |
+---------------------------------------------------------+
04/10/24 01:03:49 [GSP] - Waiting for Qbittorrent WebUI ...
Expand Down
14 changes: 13 additions & 1 deletion 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 @@ -6,6 +6,7 @@
MAX_SLEEP_TIME="${GSP_SLEEP:-60}"
RETRY_DELAY="${GSP_RETRY_DELAY:-10}"
INIT_RETRY_WAIT="${GSP_INIT_RETRY_WAIT:-10}"
PORT_INDEX="$(( ${GSP_PORT_INDEX:-1} - 1))"
[ "${MAX_SLEEP_TIME}" -eq "${RETRY_DELAY}" ] && (( RETRY_DELAY = RETRY_DELAY - 1 )) # To avoid errors later
GLUETUN="${GSP_GTN_ADDR:-http://localhost:8000}"
QBITTORRENT="${GSP_QBT_ADDR:-http://localhost:${WEBUI_PORT:-8080}}"
Expand All @@ -19,13 +20,15 @@ DEBUG="${GSP_DEBUG:-false}"
spaces=' '
QBT_display="${QBITTORRENT}${spaces}"
GTN_display="${GLUETUN}${spaces}"
INDEX_DISPLAY="${GSP_PORT_INDEX:-1}${spaces}"

echo "\
+---------------------------------------------------------+
| Gluetun sync port (GSP) mod loaded |
+---------------------------------------------------------+
| Qbittorrent address : ${QBT_display::33}|
| Gluetun address : ${GTN_display::37}|
| Port index : ${INDEX_DISPLAY::42}|
+---------------------------------------------------------+
"

Expand Down Expand Up @@ -110,7 +113,16 @@ get_ports(){
;;
esac
[ "${QBT}" ] && QBT_PORT=$(curl --silent --fail --show-error ${QBT_COOKIES} "${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)
[ "${GTN}" ] && {
GTN_PORTS=$(curl --silent --fail --show-error "${GLUETUN}/v1/openvpn/portforwarded" | jq ".port" 2>/dev/null)
if echo "${GTN_PORTS}" | grep -Fq '[' ; then # Handle the case of multiple ports - ex : [10550,20550,30550]
GTN_PORT=$(echo "${GTN_PORTS}" | jq .[${PORT_INDEX}] 2>/dev/null)
[ "${GTN_PORT}" = "null" ] && log "[ERROR] GSP_PORT_INDEX (${GSP_PORT_INDEX:-1}) > Nb of ports ($(( $(echo "${GTN_PORTS}" | grep -c ',') + 1 )))."\
&& GTN_PORT=0
else
GTN_PORT="${GTN_PORTS}"
fi
}
}

if [ "${DEBUG,,}" = "true" ]; then
Expand Down

0 comments on commit 02b9649

Please sign in to comment.