Jump to content

How to automate port forwarding in Gluetun/Transmission


FunkyBuddha

Recommended Posts

Posted

Challenge: When using the VPN client Gluetun, the port forward is dynamic. It can change over time.

Solution: Identify Gluetun's forwarded port and update Transmission's listening port.

How it works:

  • Pass a parameter that identifies your Gluetun/Transmission Docker instance
  • Get port forward value from Gluetun
  • Check to see if the port forward has change from last check
  • Update Transmission's config file with the port forward
  • Save the new port forward value
  • Schedule this script every 5 minutes

Edit: Remove the "-it" from the docker exec if you're running from cron.

 

Bash Script:

Quote

gluetun_portfwd() {
    if [[ -z "$1" ]]; then
        echo missing parameter
        exit
    fi
    
    local curr_dt=$(date +"%d%b%Y")
    local trans_num=$1
    local cfg_file=settings.json
    local cfg_dir=/path/to/AppData/Config/transmission$trans_num
    local cfg_fname=$cfg_dir/$cfg_file
    touch $cfg_dir/wan_port.txt
    
    if [[ $trans_num -eq 1 ]]; then
        local portfwd=$(docker exec -it gluetun1-gluetun1-1 cat /tmp/gluetun/forwarded_port)
    fi
    if [[ $trans_num -eq 2 ]]; then
        local portfwd=$(docker exec -it gluetun2-gluetun2-1 cat /tmp/gluetun/forwarded_port)
    fi
    if [[ $trans_num -eq 3 ]]; then
        local portfwd=$(docker exec -it gluetun3-gluetun3-1 cat /tmp/gluetun/forwarded_port)
    fi
    local prev_wanport=$(cat $cfg_dir/wan_port.txt)

    if [[ $portfwd -eq $prev_wanport ]] || [[ -z "$portfwd" ]]; then
        return
    fi
    
    echo $portfwd > $cfg_dir/wan_port.txt
    sudo docker stop transmission$trans_num
#    clear
    cp $cfg_fname ~/
    cp ~/$cfg_file ~/bkup/$cfg_file.$trans_num.$curr_dt
    sed -i 's/\"peer-port\": [0-9]*/\"peer-port\": '"$portfwd"'/g' ~/$cfg_file
#    less ~/$cfg_file
    cp ~/$cfg_file $cfg_fname
    sudo docker start transmission$trans_num
}

 

  • Thanks 1

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...