Guest User

unRAID Script for ProtonVPN / qBittorent (u/the-ephus)

a guest
Jul 22nd, 2025
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.54 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. # CONFIGURATION
  4. CONTAINER="qbittorrent" # exact name of the torrent container in unRAID
  5. LISTENING_PORT="6881" # port that the torrent software is expecting to listen on for P2P connections (found in webUI settings)
  6. WGTUNNEL="10.2.0.1" # Local tunnel network pool (change last number to 1). Provided in VPN provider's Wireguard configuration.
  7. LOGFILE="/var/log/natpmp_forward.log" # where the script logs will be stored - this script will create the file if needed. /var/log directory is stored in RAM and will not persist on reboot.
  8. LOG_RETENTION_DAY=3 # how often (in days) the log file is purged and a new one created
  9. INTERVAL=45 # How often this script loops, in seconds. Some VPN providers will only reserve a port for 60 seconds.
  10.  
  11. if ! docker exec "$CONTAINER" which natpmpc &>/dev/null; then
  12.     echo "natpmpc not found in container '$CONTAINER'. Exiting." >&2
  13.     exit 1
  14. fi
  15.  
  16. while true; do
  17.     mkdir -p "$(dirname "$LOGFILE")"
  18.    
  19.     # Rotate log if older than retention period
  20.     if [[ -f "$LOGFILE" ]]; then
  21.         FILE_AGE_DAYS=$(( ( $(date +%s) - $(stat -c %Y "$LOGFILE") ) / 86400 ))
  22.       if (( FILE_AGE_DAYS >= LOG_RETENTION_DAY )); then
  23.           rm -f "$LOGFILE"
  24.       fi
  25.     fi
  26.    
  27.     DATE_LINE="===== $(date) ====="
  28.     echo "$DATE_LINE"
  29.     echo "$DATE_LINE" >> "$LOGFILE"
  30.  
  31.     # Check if container is running
  32.     if ! docker inspect -f '{{.State.Running}}' "$CONTAINER" 2>/dev/null | grep -q true; then
  33.         echo "Container '$CONTAINER' is NOT running." | tee -a "$LOGFILE"
  34.         sleep "$INTERVAL"
  35.         continue
  36.     fi
  37.  
  38.     # Run natpmpc for TCP
  39.     TCP_OUTPUT=$(docker exec "$CONTAINER" natpmpc -a 0 "$LISTENING_PORT" tcp 1200 -g "$WGTUNNEL" 2>&1)
  40.     echo "TCP Mapping Output:" >> "$LOGFILE"
  41.     echo "$TCP_OUTPUT" >> "$LOGFILE"
  42.     echo "" >> "$LOGFILE"
  43.  
  44.     # Run natpmpc for UDP
  45.     UDP_OUTPUT=$(docker exec "$CONTAINER" natpmpc -a 0 "$LISTENING_PORT" udp 1200 -g "$WGTUNNEL" 2>&1)
  46.     echo "UDP Mapping Output:" >> "$LOGFILE"
  47.     echo "$UDP_OUTPUT" >> "$LOGFILE"
  48.     echo "" >> "$LOGFILE"
  49.    
  50.     # Extract mapped port
  51.     MAPPED_PORT=$(echo "$UDP_OUTPUT" | grep -oP 'Mapped public port \K[0-9]+' | tail -n1)
  52.  
  53.      if [[ -z "$MAPPED_PORT" || ! "$MAPPED_PORT" =~ ^[0-9]+$ ]]; then
  54.         echo "Failed to map port or retrieve mapped port. Check natpmpc output above." | tee -a "$LOGFILE"
  55.         echo "" >> "$LOGFILE"
  56.     else
  57.         echo "VPN port mapped successfully: $MAPPED_PORT to $LISTENING_PORT" | tee -a "$LOGFILE"
  58.         echo "" >> "$LOGFILE"
  59.     fi
  60.  
  61.     sleep "$INTERVAL"
  62. done
Add Comment
Please, Sign In to add comment