#!/bin/bash

# This file is part of TorBox, an easy to use anonymizing router based on Raspberry Pi.
# Copyright (C) 2026 radio_24
# Contact: anonym@torbox.ch
# Website: https://www.torbox.ch
# Github:  https://github.com/radio24/TorBox
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it is useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# DESCRIPTION
# This file, together with log_check.py, automatically reacts to log-related events.
#
# SYNTAX
# sudo bash automat <entry1> [<entry2>]
#
# <entryX> Entry number which will be executed
#
#
##### SET VARIABLES ######
# IMPORTANT: rc.local needs absolute paths, it is not recommended to use the torbox.lib!
ARGUMENT=$1
ARGUMENT2=$2
TORBOX_PATH="/home/torbox/torbox"
RUNFILE="$TORBOX_PATH/run/torbox.run"
LOG=""
RETURN=0

#include lib
.  /home/torbox/torbox/lib/torbox.lib

# Check and create log file
DATE=$(date)
sudo -u debian-tor test -f /var/log/tor/automat.log && LOG="/var/log/tor/automat.log"
if [ -z "$LOG" ]; then
	LOG="/var/log/tor/automat.log"
  (printf %s"[$DATE] - Log file created!\n" | sudo -u debian-tor tee $LOG) &>/dev/null
fi

# Entry 1: Reset Tor and force a change of the permanent entry node
# This should work in every configuration
if [ $ARGUMENT = 1 ]; then
  sudo systemctl stop tor
  (sudo rm -r /var/lib/tor/cached-certs) &>/dev/null
  (sudo rm -r /var/lib/tor/cached-consensus) &>/dev/null
  (sudo rm -r /var/lib/tor/cached-descriptors) &>/dev/null
  (sudo rm -r /var/lib/tor/cached-descriptors.new) &>/dev/null
  (sudo rm -r /var/lib/tor/cached-microdesc-consensus) &>/dev/null
  (sudo rm -r /var/lib/tor/cached-microdescs) &>/dev/null
  (sudo rm -r /var/lib/tor/cached-microdescs.new) &>/dev/null
  (sudo rm -r /var/lib/tor/diff-cache) &>/dev/null
  (sudo rm -r /var/lib/tor/lock) &>/dev/null
  (sudo rm -r /var/lib/tor/state) &>/dev/null
  sleep 2
  (printf %s"[$DATE] - TACA TRIGGER #1  - Reset Tor and force a change of the permanent entry node.\n" | sudo -u debian-tor tee -a $LOG) &>/dev/null
  sudo systemctl stop tor
  sudo systemctl restart tor

# Entry 2: After losing a wireless connection, we try to get a new one and restart Tor
# This should work only with wlan0 and wlan1 and if the password of the wifi is stored
# In case of tor bootstrapping problems (matching "*connections have failed*")
# automat 2 1 is executed, which tries to reconnect the wireless connection (if available)
# and flushes the tor configuration.
elif [ $ARGUMENT = 2 ]; then
  INTERNET_IFACE=$(grep "^INTERNET_IFACE=" $RUNFILE | sed "s/.*=//g")
  if [ "$INTERNET_IFACE" == "wlan0" ] || [ "$INTERNET_IFACE" == "wlan1" ]; then
    RETURN=$(sudo $TORBOX_PATH/lib/torbox_wireless_manager.py -i $INTERNET_IFACE -a)
		if [ $RETURN = 1 ]; then
			(printf %s"[$DATE] - TACA TRIGGER #2a - Restart wifi connection - successful.\n" | sudo -u debian-tor tee -a $LOG) &>/dev/null
		else
			(printf %s"[$DATE] - TACA TRIGGER #2a - Restart wifi connection - failed!\n" | sudo -u debian-tor tee -a $LOG) &>/dev/null
		fi
    sleep 2
		# Is INTERNET_IFACE ready?
		STATICIP1=$(ip addr show $INTERNET_IFACE | grep -w inet)
		# If wlan0 or wlan1 doesn't have an IP address then we have to do something about it!
		if [ -z "$STATICIP1" ] ; then
			(printf %s"[$DATE] - TACA TRIGGER #2a - IP address on $INTERNET_IFACE is missing - restart interface!\n" | sudo -u debian-tor tee -a $LOG) &>/dev/null
			(sudo ifdown $INTERNET_IFACE) &>/dev/null
			# Cannot be run in the background because then it jumps into the next if-then-else clause (still missing IP)
			(sudo ifup $INTERNET_IFACE) &>/dev/null
		fi
		if [ ! -z "$ARGUMENT2" ]; then
			(printf %s"[$DATE] - TACA TRIGGER #2b - Changing to TACA TRIGGER $ARGUMENT2.\n" | sudo -u debian-tor tee -a $LOG) &>/dev/null
			bash automat $ARGUMENT2
		else
			(printf %s"[$DATE] - TACA TRIGGER #2b - Restart tor.\n" | sudo -u debian-tor tee -a $LOG) &>/dev/null
    	sudo systemctl stop tor
    	sudo systemctl restart tor
		fi
  fi

# Entry 3: Restart Tor, and reconect wifi, if needed.
# This should work in every configuration
elif [ $ARGUMENT = 3 ]; then
	INTERNET_IFACE=$(grep "^INTERNET_IFACE=" $RUNFILE | sed "s/.*=//g")
	if [ "$INTERNET_IFACE" == "wlan0" ] || [ "$INTERNET_IFACE" == "wlan1" ]; then
		RETURN=$(sudo $TORBOX_PATH/lib/torbox_wireless_manager.py -i $INTERNET_IFACE -a)
		if [ $RETURN = 1 ]; then
			(printf %s"[$DATE] - TACA TRIGGER #3  - Restart wifi connection - successful.\n" | sudo -u debian-tor tee -a $LOG) &>/dev/null
		else
			(printf %s"[$DATE] - TACA TRIGGER #3  - Restart wifi connection - failed!\n" | sudo -u debian-tor tee -a $LOG) &>/dev/null
		fi
		sleep 2
		# Is INTERNET_IFACE ready?
		STATICIP1=$(ip addr show $INTERNET_IFACE | grep -w inet)
		# If wlan0 or wlan1 doesn't have an IP address then we have to do something about it!
		if [ -z "$STATICIP1" ] ; then
			(printf %s"[$DATE] - TACA TRIGGER #3  - IP address on $INTERNET_IFACE is missing - restart interface!\n" | sudo -u debian-tor tee -a $LOG) &>/dev/null
			(sudo ifdown $INTERNET_IFACE) &>/dev/null
			# Cannot be run in the background because then it jumps into the next if-then-else clause (still missing IP)
			(sudo ifup $INTERNET_IFACE) &>/dev/null
		fi
	fi
  (printf %s"[$DATE] - TACA TRIGGER #3  - Restart tor.\n" | sudo -u debian-tor tee -a $LOG) &>/dev/null
  sudo systemctl stop tor
  sudo systemctl restart tor

# Entry 4: Restart Tor
# This should work in every configuration
elif [ $ARGUMENT = 4 ]; then
	(printf %s"[$DATE] - TACA TRIGGER #4  - Restart tor.\n" | sudo -u debian-tor tee -a $LOG) &>/dev/null
	sudo systemctl restart tor

# Entry 5: Synchronize time
# This should work in every configuration
elif [ $ARGUMENT = 5 ]; then
	(sudo /usr/sbin/ntpdate pool.ntp.org) &>/dev/null; fi
	sudo systemctl restart tor
