Standardize scripts, use bash and double brackets

This commit is contained in:
Kristian Haugene
2019-03-09 18:52:06 +01:00
parent 43abe72c2a
commit 66c7634f18
9 changed files with 41 additions and 42 deletions

View File

@@ -1,4 +1,4 @@
#! /bin/sh
#! /bin/bash
echo "Wait for tunnel to be fully initialized and PIA is ready to give us a port"
sleep 15
@@ -9,8 +9,8 @@ sleep 15
# Settings
TRANSMISSION_PASSWD_FILE=/config/transmission-credentials.txt
transmission_username=$(head -1 $TRANSMISSION_PASSWD_FILE)
transmission_passwd=$(tail -1 $TRANSMISSION_PASSWD_FILE)
transmission_username=$(head -1 ${TRANSMISSION_PASSWD_FILE})
transmission_passwd=$(tail -1 ${TRANSMISSION_PASSWD_FILE})
pia_client_id_file=/etc/transmission/pia_client_id
transmission_settings_file=${TRANSMISSION_HOME}/settings.json
@@ -19,11 +19,11 @@ transmission_settings_file=${TRANSMISSION_HOME}/settings.json
#
new_client_id() {
head -n 100 /dev/urandom | sha256sum | tr -d " -" | tee $pia_client_id_file
head -n 100 /dev/urandom | sha256sum | tr -d " -" | tee ${pia_client_id_file}
}
pia_client_id="$(cat $pia_client_id_file 2>/dev/null)"
if [ -z "${pia_client_id}" ]; then
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)
fi
@@ -33,26 +33,26 @@ 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
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)
if [ $pia_curl_exit_code -ne 0 ]; then
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
if [[ ! -z "$error" ]]; then
echo "PIA returned an error: $error"
exit
fi
# Get new port, check if empty
new_port=$(echo "$pia_response" | grep -oE "[0-9]+")
if [ -z "$new_port" ]; then
if [[ -z "$new_port" ]]; then
echo "Could not find new port from PIA"
exit
fi
@@ -65,7 +65,7 @@ echo "Got new port $new_port from PIA"
# Check if transmission remote is set up with authentication
auth_enabled=$(grep 'rpc-authentication-required\"' "$transmission_settings_file" \
| grep -oE 'true|false')
if [ "true" = "$auth_enabled" ]
if [[ "true" = "$auth_enabled" ]]
then
echo "transmission auth required"
myauth="--auth $transmission_username:$transmission_passwd"
@@ -76,8 +76,8 @@ fi
# get current listening port
transmission_peer_port=$(transmission-remote $myauth -si | grep Listenport | grep -oE '[0-9]+')
if [ "$new_port" != "$transmission_peer_port" ]; then
if [ "true" = "$ENABLE_UFW" ]; then
if [[ "$new_port" != "$transmission_peer_port" ]]; then
if [[ "true" = "$ENABLE_UFW" ]]; then
echo "Update UFW rules before changing port in Transmission"
echo "denying access to $transmission_peer_port"
@@ -87,11 +87,11 @@ if [ "$new_port" != "$transmission_peer_port" ]; then
ufw allow "$new_port"
fi
transmission-remote $myauth -p "$new_port"
transmission-remote ${myauth} -p "$new_port"
echo "Checking port..."
sleep 10
transmission-remote $myauth -pt
transmission-remote ${myauth} -pt
else
echo "No action needed, port hasn't changed"
fi