From 87bf46826bc287f9ce58e4c5291750684d3fb248 Mon Sep 17 00:00:00 2001 From: Kristian Haugene Date: Sun, 9 Nov 2014 19:52:52 +0100 Subject: [PATCH] created script for port updates. need to dynamically get rpc username and password do some cron stuff. issue #1 --- Dockerfile | 16 ++++++++--- updateTransmissionPort.sh | 57 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 4 deletions(-) create mode 100755 updateTransmissionPort.sh diff --git a/Dockerfile b/Dockerfile index 046a63e..f2cdef0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,28 +7,36 @@ MAINTAINER Kristian Haugene VOLUME /data +# Update package sources list RUN apt-get update + +# Add transmission ppa repository for latest releases RUN apt-get -y install software-properties-common RUN add-apt-repository ppa:transmissionbt/ppa + +# Update packages and install software RUN apt-get update RUN apt-get install -y transmission-cli RUN apt-get install -y transmission-common RUN apt-get install -y transmission-daemon RUN apt-get install -y supervisor RUN apt-get install -y openvpn -RUN mkdir -p /var/log/supervisor - -# Not generally in use. But nice when starting up container interactively +RUN apt-get install -y curl RUN apt-get install -y screen +# Create directories +RUN mkdir -p /var/log/supervisor + +# Add configuration and scripts ADD piaconfig/config.ovpn /etc/openvpn/config.ovpn ADD piaconfig/credentials.txt /etc/openvpn/credentials.txt ADD piaconfig/ca.crt /etc/openvpn/ca.crt ADD piaconfig/crl.pem /etc/openvpn/crl.pem ADD startOpenVPN.sh /etc/openvpn/start.sh ADD transmissionSettings.json /etc/transmission-daemon/settings.json +ADD updateTransmissionPort.sh /etc/transmission-daemon/updatePort.sh ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf +# Expose port and run supervisord EXPOSE 9091 - CMD ["/usr/bin/supervisord"] diff --git a/updateTransmissionPort.sh b/updateTransmissionPort.sh new file mode 100755 index 0000000..2de2809 --- /dev/null +++ b/updateTransmissionPort.sh @@ -0,0 +1,57 @@ +#/bin/bash + +# Settings +PIA_PASSWD_FILE=/etc/openvpn/credentials.txt + +username=$(head -1 $PIA_PASSWD_FILE) +passwd=$(tail -1 $PIA_PASSWD_FILE) +local_vpn_ip=$(ip addr show tun0 | grep inet | awk '{ print $2 }') +pia_client_id_file=/etc/transmission-daemon/pia_client_id +port_assignment_url=https://www.privateinternetaccess.com/vpninfo/port_forward_assignment + +# +# First get a port from PIA +# + +new_client_id() { + head -n 100 /dev/urandom | md5sum | tr -d " -" | tee $pia_client_id_file +} + +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 + +# Get the port +pia_response=$(curl -d "user=$username&pass=$passwd&client_id=$pia_client_id&local_ip=$local_vpn_ip" $port_assignment_url) + +new_port=$(echo $pia_response | grep -oE "[0-9]+") +echo "Got new port $new_port from pia" + +# +# Now, set port in Transmission +# + +# Check if transmission remote is set up with authentication +auth_enabled=$(grep 'rpc-authentication-required\"' settings.json | grep -oE 'true|false') +if [[ "true" = "$auth_enabled" ]] + then + echo "transmission auth required" + myauth="--auth username:password" + else + echo "transmission auth not required" + myauth="" +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 + 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 +