#!/bin/sh export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/syno/sbin:/usr/syno/bin:/usr/local/sbin:/usr/local/bin # Script to update port used by Deluge torrent client # Developed for use on Synology DSM. Should work on other platforms. # v1.00 (28th April 2018) # Written by Andy Fox _curl_with_error_code () { local curl_error_code http_code exec 17>&1 http_code=$(curl --write-out '\n%{http_code}\n' "$@" | tee /dev/fd/17 | tail -n 1) curl_error_code=$? exec 17>&- if [ $curl_error_code -ne 0 ]; then return $curl_error_code fi if [ $http_code -ge 400 ] && [ $http_code -lt 600 ]; then # echo "HTTP $http_code" >&2 return 127 fi } # Need to check if deluge is running. If it's not, no point in # in continuing with this script. ps w | grep deluged | grep -v grep > /dev/null if [ $? -eq 0 ]; then logger -p user.crit "deluge-port: deluge process detected. Attempting to update port." else logger -p user.crit "deluge-port: deluge is not running. Exiting." exit fi # We need to do this, as when error file created, pia-port.txt is still open # and can't be deleted during that script run. if [ -e /tmp/pia_port.error ]; then logger -p user.crit "deluge-port: Error file detected. Assuming port file is invalid. Deleting." rm /tmp/pia_port.txt rm /tmp/pia_new.txt fi if [ ! -e /tmp/pia_port.txt ]; then logger -p user.crit "deluge-port: Setting up port forward for first time." RESULTCURL="`_curl_with_error_code -o /tmp/pia_port.txt -k https://192.168.0.1/pia_port.txt`" RETURNCURL=$? if [ $RETURNCURL -gt 0 ]; then logger -p user.crit "deluge-port: curl failed to retrieve current Deluge port." logger -p user.crit "deluge-port: curl return code: $RETURNCURL" cp /tmp/pia_port.txt /tmp/pia_port.error exit fi RESULTTRANS=`/usr/local/deluge/env/bin/deluge-console -c /usr/local/deluge/var "config --set listen_ports ($(cat /tmp/pia_port.txt), $(cat /tmp/pia_port.txt))" 2>&1 ` len=${#RESULTTRANS} case "$RESULTTRANS" in *success*) logger -p user.crit "deluge-port: Updated initial deluge port." if [ -e /tmp/pia_port.error ]; then mv /tmp/pia_port.error /tmp/pia_port.error.old fi exit 0 ;; esac # If we get here, there must be an error communicating with main deluge process. logger -p user.crit "deluge-port: Initial port update failed. Cannot communicate with deluge." logger -p user.crit "deluge-port: Error: $RESULTTRANS" cp /tmp/pia_port.txt /tmp/pia_port.error else RESULTCURL="`_curl_with_error_code -o /tmp/pia_new.txt -k https://192.168.0.1/pia_port.txt`" RETURNCURL=$? if [ $RETURNCURL -gt 0 ]; then logger -p user.crit "deluge-port: curl failed to retrieve current Deluge port." logger -p user.crit "deluge-port: curl return code: $RETURNCURL" cp /tmp/pia_new.txt /tmp/pia_port.error exit fi CURPORT=`cat /tmp/pia_port.txt` NEWPORT=`cat /tmp/pia_new.txt` logger -p user.crit "deluge-port: Current port: $CURPORT" logger -p user.crit "deluge-port: New Port: $NEWPORT" if [ "$CURPORT" = "$NEWPORT" ]; then logger -p user.crit "deluge-port: Port not changed. Exiting." exit 0 fi logger -p user.crit "deluge-port: Updating port." mv /tmp/pia_new.txt /tmp/pia_port.txt RESULTTRANS=`/usr/local/deluge/env/bin/deluge-console -c /usr/local/deluge/var "config --set listen_ports ($(cat /tmp/pia_port.txt), $(cat /tmp/pia_port.txt))" 2>&1 ` len=${#RESULTTRANS} case "$RESULTTRANS" in *success*) logger -p user.crit "deluge-port: Updated deluge port." if [ -e /tmp/pia_port.error ]; then mv /tmp/pia_port.error /tmp/pia_port.error.old fi exit 0 ;; esac # If we get here, there must be an error communicating with main deluge process. logger -p user.crit "deluge-port: Port update failed. Cannot communicate with deluge." logger -p user.crit "deluge-port: Error: $RESULTTRANS" cp /tmp/pia_port.txt /tmp/pia_port.error fi