From 0ba2a55cbb366c666ff986c4664ddf831f5f7b9f Mon Sep 17 00:00:00 2001 From: Kurs Developer Date: Sun, 2 Apr 2017 21:31:17 +0200 Subject: [PATCH] Delay --up script execution until after TCP/UDP connection establishment with peer. Also add error handling with logging when PIA port response is empty, issue #220 --- openvpn/start.sh | 2 +- transmission/updatePort.sh | 33 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/openvpn/start.sh b/openvpn/start.sh index f37159cdb..ad4eba23e 100755 --- a/openvpn/start.sh +++ b/openvpn/start.sh @@ -44,7 +44,7 @@ echo $TRANSMISSION_RPC_PASSWORD >> /config/transmission-credentials.txt # Persist transmission settings for use by transmission-daemon dockerize -template /etc/transmission/environment-variables.tmpl:/etc/transmission/environment-variables.sh /bin/true -TRANSMISSION_CONTROL_OPTS="--script-security 2 --up /etc/transmission/start.sh --down /etc/transmission/stop.sh" +TRANSMISSION_CONTROL_OPTS="--script-security 2 --up-delay --up /etc/transmission/start.sh --down /etc/transmission/stop.sh" if [ -n "${LOCAL_NETWORK-}" ]; then eval $(/sbin/ip r l m 0.0.0.0 | awk '{if($5!="tun0"){print "GW="$3"\nINT="$5; exit}}') diff --git a/transmission/updatePort.sh b/transmission/updatePort.sh index 3d8026331..796948a36 100755 --- a/transmission/updatePort.sh +++ b/transmission/updatePort.sh @@ -24,25 +24,30 @@ new_client_id() { pia_client_id="$(cat $pia_client_id_file 2>/dev/null)" if [ -z ${pia_client_id} ]; then - echo "Generating new client id for PIA" - pia_client_id=$(new_client_id) + echo "Generating new client id for PIA" + pia_client_id=$(new_client_id) fi # Get the port port_assignment_url="http://209.222.18.222:2000/?client_id=$pia_client_id" pia_response=$(curl -s -f $port_assignment_url) +pia_curl_exit_code=$? + +if [ -z $pia_response ]; then + echo "Port forwarding is already activated on this connection, has expired, or you are not connected to a PIA region that supports port forwarding" +fi # Check for curl error (curl will fail on HTTP errors with -f flag) -ret=$? -if [ $ret -ne 0 ]; then - echo "curl encountered an error looking up new port: $ret" +if [ $pia_curl_exit_code -ne 0 ]; then + echo "curl encountered an error looking up new port: $pia_curl_exit_code" + exit fi # Check for errors in PIA response error=$(echo $pia_response | grep -oE "\"error\".*\"") if [ ! -z "$error" ]; then - echo "PIA returned an error: $error" - exit + echo "PIA returned an error: $error" + exit fi # Get new port, check if empty @@ -61,9 +66,9 @@ echo "Got new port $new_port from PIA" auth_enabled=$(grep 'rpc-authentication-required\"' $transmission_settings_file | grep -oE 'true|false') if [ "true" = "$auth_enabled" ] then - echo "transmission auth required" - myauth="--auth $transmission_username:$transmission_passwd" - else + echo "transmission auth required" + myauth="--auth $transmission_username:$transmission_passwd" +else echo "transmission auth not required" myauth="" fi @@ -72,9 +77,9 @@ fi transmission_peer_port=$(transmission-remote $myauth -si | grep Listenport | grep -oE '[0-9]+') if [ "$new_port" != "$transmission_peer_port" ] then - transmission-remote $myauth -p "$new_port" - echo "Checking port..." - sleep 10 && transmission-remote $myauth -pt - else + transmission-remote $myauth -p "$new_port" + echo "Checking port..." + sleep 10 && transmission-remote $myauth -pt +else echo "No action needed, port hasn't changed" fi