diff --git a/dl-server/install.sh b/dl-server/install.sh new file mode 100644 index 0000000..a13f503 --- /dev/null +++ b/dl-server/install.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +run_directory=$(dirname "$0") + +source "$run_directory/variables.conf" + +if [ -f $run_directory/.disabled ]; then + echo "Installation is disabled. Exiting..." + exit 0 +fi + +echo "Copying Systemd units to system directory..." +sudo cp $run_directory/transmission-daemon.service /etc/systemd/system/ +sudo sed -i -e "s/PH_AUTHORIZED_ADDRESSES/$authorized_ips/g" /etc/systemd/system/transmission-daemon.service +sudo sed -i -e "s/PH_USER/$user/g" /etc/systemd/system/transmission-daemon.service +sudo sed -i -e "s/PH_PASSWORD/$password/g" /etc/systemd/system/transmission-daemon.service +sudo sed -i -e "s:PH_DL_LOCATION:$dl_location:g" /etc/systemd/system/transmission-daemon.service +sudo cp $run_directory/transmission-vpn.service /etc/systemd/system/ +sudo sed -i -e "s:PH_DIRECTORY:$(pwd):g" /etc/systemd/system/transmission-vpn.service +sudo cp $run_directory/transmission-vpn.timer /etc/systemd/system/ + +echo "Finished install. Exiting..." +exit 0 diff --git a/dl-server/transmission-daemon.service b/dl-server/transmission-daemon.service new file mode 100644 index 0000000..e839231 --- /dev/null +++ b/dl-server/transmission-daemon.service @@ -0,0 +1,16 @@ +[Unit] +Description=Transmission BitTorrent Daemon (KTO) +Wants=transmission-vpn.service +BindsTo=transmission-vpn.timer +After=network.target transmission-vpn.timer + +[Service] +User=debian-transmission +Type=notify +ExecStart=/usr/bin/transmission-daemon -f --log-error -a PH_AUTHORIZED_ADDRESSES -C -t -u PH_USER -v PH_PASSWORD -w PH_DL_LOCATION +ExecStop=/bin/kill -s STOP $MAINPID +ExecReload=/bin/kill -s HUP $MAINPID +NoNewPrivileges=true + +[Install] +WantedBy=multi-user.target diff --git a/dl-server/transmission-vpn.service b/dl-server/transmission-vpn.service new file mode 100644 index 0000000..c5ab4fe --- /dev/null +++ b/dl-server/transmission-vpn.service @@ -0,0 +1,9 @@ +[Unit] +Description=Make sure VPN is active for Transmission +OnFailure=transmission-daemon.service + +[Service] +ExecStart=PH_DIRECTORY/transmission-vpn.sh + +[Install] +WantedBy=default.target diff --git a/dl-server/transmission-vpn.sh b/dl-server/transmission-vpn.sh new file mode 100644 index 0000000..7ffe722 --- /dev/null +++ b/dl-server/transmission-vpn.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# The purpose of this script is to check if VPN connection is active +# This script will be called (via a service) in 2 situations: + # On a regular basis when transmission is running + # This will be achieved with a timer started and stopped with transmission service + # On transmission starting +# When the script exits with failure, associated service should try to start transmission service +# Thus this script will be executed again + # We can enter an infinite loop, but this will effectively prevent transmission from connecting +# If both services are stopped, this script will never be run by error + +source "$run_directory/variables.conf" + +# Parameters +myip_request="curl -s -4 https://ifconfig.co" +vpn_service="openvpn-client@vpn.service" +transmission_service="transmission-daemon.service" + +# Main process +ip=$($myip_request) +if [[ $ip = $real_ip ]]; then + systemctl stop $transmission_service + systemctl stop $vpn_service + sleep 3 + systemctl start $vpn_service + sleep 5 + exit 1 +elif [[ $ip =~ (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3} ]]; then + exit 0 +else + exit 1 +fi diff --git a/dl-server/transmission-vpn.timer b/dl-server/transmission-vpn.timer new file mode 100644 index 0000000..ef27ebb --- /dev/null +++ b/dl-server/transmission-vpn.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Regular check for VPN while Transmission is running +BindsTo=transmission-daemon.service + +[Timer] +OnUnitInactiveSec=10m + +[Install] +WantedBy=default.target diff --git a/dl-server/variables.conf.template b/dl-server/variables.conf.template new file mode 100644 index 0000000..b804f90 --- /dev/null +++ b/dl-server/variables.conf.template @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +# Copy this file as variables.conf, with the correct values + +real_ip="" # for VPN check +authorized_ips="" # for transmission web interface +user="" # user for transmission web interface +password="" # password for transmission web interface +dl_location="" # location of Downloads directory