Skip to content
This repository has been archived by the owner on Nov 25, 2022. It is now read-only.

Fix port forwarding script #127

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
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
40 changes: 27 additions & 13 deletions rootfs/usr/sbin/set_qbittorrent_port_forwarding.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,32 @@ port="$1"

QBT_CONFIG_FILE="${CONFIG_DIR}/qBittorrent/config/qBittorrent.conf"

if [ -f "$QBT_CONFIG_FILE" ]; then
# if Connection address line exists
if grep -q 'Connection\\PortRangeMin' "$QBT_CONFIG_FILE"; then
# Set connection interface address to the VPN address
sed -i -E 's/^.*\b(Connection.*PortRangeMin)\b.*$/Connection\\PortRangeMin='"$port"'/' "$QBT_CONFIG_FILE"
else
# add the line for configuring interface address to the qBittorrent config file
printf 'Connection\\PortRangeMin=%s' "$port" >>"$QBT_CONFIG_FILE"
fi
if pgrep "qbittorrent-nox" >/dev/null 2>$1; then
# Check if we need to use HTTPS
echo "Setting port via WebUI"
if grep -q 'WebUI\\HTTPS\\Enabled=true' "$QBT_CONFIG_FILE"; then
echo "Using HTTPS"
address=https://127.0.0.1:8080/api/v2/app/setPreferences
else
address=http://127.0.0.1:8080/api/v2/app/setPreferences
fi
curl -k --data 'json={"listen_port": '$port'}' $address
else
# Ensure config directory is created
mkdir -p "$(dirname "$QBT_CONFIG_FILE")"
# Create the configuration file from a template and environment variables
printf '[Preferences]\nConnection\\PortRangeMin=%s\n' "$port" >"$QBT_CONFIG_FILE"
echo "Setting port in config file"
if [ -f "$QBT_CONFIG_FILE" ]; then
# if session port line exists
if grep -q 'Session\\Port' "$QBT_CONFIG_FILE"; then
# Set connection interface address to the VPN address
sed -i -E 's/^.*\b(Session.*Port)\b.*$/Session\\Port='"$port"'/' "$QBT_CONFIG_FILE"
else
# add the line for configuring interface address to the qBittorrent config file
printf 'Session\\Port=%s' "$port" >>"$QBT_CONFIG_FILE"
fi
else
# Ensure config directory is created
mkdir -p "$(dirname "$QBT_CONFIG_FILE")"
# Create the configuration file from a template and environment variables
printf '[BitTorrent]\nSession\\Port=%s\n' "$port" >"$QBT_CONFIG_FILE"
fi
fi