Skip to content

Instantly share code, notes, and snippets.

@thrnz
Created September 15, 2020 04:18
Show Gist options
  • Save thrnz/dcbaa0af66c70af8e302a1c7eb75484a to your computer and use it in GitHub Desktop.
Save thrnz/dcbaa0af66c70af8e302a1c7eb75484a to your computer and use it in GitHub Desktop.
Helper container to pass forwarded port to Deluge
FROM alpine:latest
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing/" >> /etc/apk/repositories && \
echo "http://dl-cdn.alpinelinux.org/alpine/edge/community/" >> /etc/apk/repositories && \
echo "http://dl-cdn.alpinelinux.org/alpine/edge/main/" >> /etc/apk/repositories && \
apk add --no-cache bash deluge
ADD pia-port.sh /scripts/pia-port.sh
RUN chmod +x /scripts/*.sh
CMD ["/scripts/pia-port.sh"]
#!/bin/bash
trap 'exit 0' SIGTERM
OLDPORT=0
PORT=0
while true
do
[ -r "/pia-shared/port.dat" ] && PORT=$(cat /pia-shared/port.dat)
if [ $OLDPORT -ne $PORT ]; then
echo "Setting Deluge port settings ($PORT)..."
deluge-console --config=/deluge/conf "config --set listen_ports ($PORT,$PORT)"
OLDPORT=$PORT
fi
sleep 30 &
wait $!
done
@alliefitter
Copy link

I wrote a post-up.sh script which saves the wg0 interface's IP address to /pia-shared/ip.dat and modified this script to update deluge automatically when it changes after a restart.
post-up.sh

#!/usr/bin/env bash

ifconfig wg0 | grep 'inet addr:' | cut -d: -f2| cut -d' ' -f1 > /pia-shared/ip.dat

pia-port.sh

#!/bin/bash

trap 'exit 0' SIGTERM

OLDPORT=0
PORT=0
OLD_IP=""
IP=""

while true
do
  [ -r "/pia-shared/port.dat" ] && PORT=$(cat /pia-shared/port.dat)
  [ -r "/pia-shared/ip.dat" ] && IP=$(cat /pia-shared/ip.dat)

  if [ $OLDPORT -ne $PORT ]; then
    echo "Setting Deluge port settings ($PORT)..."
    deluge-console --config=/deluge/conf "config --set listen_ports ($PORT,$PORT)"
    OLDPORT=$PORT
  fi

  if [[ $OLD_IP != $IP ]]; then
    echo "Setting Deluge listen interface setting ($IP)..."
    deluge-console --config=/deluge/conf "config --set listen_interface $IP"
    OLD_IP=$IP
  fi
  sleep 30 &
  wait $!
done

@andrewfraley
Copy link

andrewfraley commented Dec 14, 2023

Here's a version that works with an external Deluge instance and authenticates with a username and password.

In your docker-compose.yml

deluge-port-helper:
    restart: unless-stopped
    build: /path/to/deluge-port-helper
    volumes:
        - pia-shared:/pia-shared:ro
    network_mode: "service:vpn"
    depends_on:
        - vpn
    environment:
        - DELUGE_HOST=10.0.0.5
        - DELUGE_PORT=58846
        - DELUGE_USER=delugeuser
        - DELUGE_PASS=delugepass

pia-port.sh

#!/bin/bash

trap 'exit 0' SIGTERM

OLDPORT=0
PORT=0

while true
do
  [ -r "/pia-shared/port.dat" ] && PORT=$(cat /pia-shared/port.dat)

  if [ $OLDPORT -ne $PORT ]; then
    echo "Setting Deluge port settings ($PORT) on $DELUGE_HOST:$DELUGE_PORT"
    deluge-console "connect $DELUGE_HOST:$DELUGE_PORT $DELUGE_USER $DELUGE_PASS ; config --set listen_ports ($PORT,$PORT)"
    OLDPORT=$PORT
  fi
  sleep 30 &
  wait $!
done

If you need to add a user to deluge, add to ~/.config/deluge/auth and restart deluged
user:pass:10

@andrewfraley
Copy link

andrewfraley commented May 11, 2024

Some recent change in Alpine started causing these errors coming from the deluge binary:

Error relocating /usr/lib/libpython3.12.so.1.0: pwritev2: symbol not found
Error relocating /usr/lib/libpython3.12.so.1.0: preadv2: symbol not found

The solution is to upgrade the musl package when building the container, so your Dockerfile would then look like:

FROM alpine:latest
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing/" >> /etc/apk/repositories && \
    echo "http://dl-cdn.alpinelinux.org/alpine/edge/community/" >> /etc/apk/repositories && \
    echo "http://dl-cdn.alpinelinux.org/alpine/edge/main/" >> /etc/apk/repositories && \
    apk add --no-cache bash deluge && \
    apk upgrade musl
ADD pia-port.sh /scripts/pia-port.sh
RUN chmod +x /scripts/*.sh
CMD ["/scripts/pia-port.sh"]

@n-insaidoo
Copy link

Thank you @andrewfraley this apk upgrade musl saved me!

@itavero
Copy link

itavero commented Dec 23, 2024

Did someone already publish this to ghcr.io or Docker Hub?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment