#!/bin/bash
# shellcheck disable=SC2001,SC2154

# 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
#
# Copyright (C) 2022 nyxnor (Contributor)
# Github:  https://github.com/nyxnor
#
# 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 displays the bridges menu and executes all relevant scripts.
#
# SYNTAX
# ./menu-bridges
#
#
###### SET VARIABLES ######
#
# SIZE OF THE MENU
#
# How many items do you have in the main menu?
NO_ITEMS=14
#
# How many lines are only for decoration and spaces?
NO_SPACER=4
#
#Set the the variables for the menu
MENU_WIDTH=80
MENU_WIDTH_REDUX=60
MENU_HEIGHT_10=10
MENU_HEIGHT_15=15
MENU_HEIGHT_20=20
MENU_HEIGHT_25=25
# MENU_HEIGHT should not exceed 26
MENU_HEIGHT=$((8+NO_ITEMS+NO_SPACER))
MENU_LIST_HEIGHT=$((NO_ITEMS+NO_SPACER))

#Colors (don't change it!)
RED='\033[1;31m'
YELLOW='\033[1;93m'
NOCOLOR='\033[0m'

#Other variables
TORRC="/etc/tor/torrc"
BAK="/etc/tor/torrc.bak"
TORBOX_PATH="/home/torbox/torbox"
RUNFILE="$TORBOX_PATH/run/torbox.run"
TXT_DIR="$TORBOX_PATH/text"
LOOP_NUMBER=0
PING_SERVER=$(grep "^PING_SERVER=" ${RUNFILE} | sed "s|.*http\?://||g") 2>/dev/null


###########################
######## FUNCTIONS ########

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

# This function imports the configuration and makes some preparations
read_config()
{
  # Is TorBox connected to a VPN?
  tun0up=$(ip link | grep tun0)
  clear
  if [ -z "$tun0up" ]; then
    VPNSTRING1="Connect"
    VPNSTRING2="to"
  else
    VPNSTRING1="Disconnect"
    VPNSTRING2="from"
  fi

  # Is the automatic counteractions feature activated?
  if pgrep -f "log_check.py"; then
    clear
    LOGCHECK="Deactivate"
    LOGCHECKSTATUSb="ON"
  else
    clear
    LOGCHECK="Activate"
    LOGCHECKSTATUSb="OFF"
  fi

  # Is the Countermeasure against a tightly configured firewall active?
  MODE_FIREWALL=$(grep -o "^ReachableAddresses " ${TORRC} | head -1)
  clear
  if [ "$MODE_FIREWALL" = "ReachableAddresses " ]; then
    FIREWALL="ON"
  else
    FIREWALL="OFF"
  fi

  # Is the Countermeasure against a disconnection when idle feature active?
  if pgrep -f "ping -q $PING_SERVER" ; then
    clear
    PING="ON"
  else
    clear
    PING="OFF"
  fi

	# Is the OBFS4 Bridge Relay running?
	MODE_BR=$(grep "^BridgeRelay" ${TORRC})
	if [ "$MODE_BR" = "BridgeRelay 1" ]; then TOGGLE07b="ON"; else TOGGLE07b="OFF"; fi

  # number_of_obfs4_bridges()
  # How many OBFS4 bridges do we have? readarray reads into an array beginning with index 0
  # Following variables can be used:
  # $configured_bridges_deactivated -> An array with all deactivated OBFS4 bridges
  # $configured_bridges_activated -> An array with all activated OBFS4 bridges
  # $number_configured_bridges_deactivated -> Number of deactivated bridges
  # $number_configured_bridges_activated -> Number of activated bridges
  # $number_configured_bridges_total -> Total number of bridges
  number_of_obfs4_bridges

	# number_of_snowflake_bridges()
	# How many Snowflake bridges do we have? readarray reads into an array beginning with index 0
	# Following variables can be used:
	# $configured_snowflake_bridges_deactivated -> An array with all deactivated Snowflake bridges
	# $configured_snowflake_bridges_activated -> An array with all activated Snowflake bridges
	# $number_configured_snowflake_bridges_deactivated -> Number of deactivated bridges
	# $number_configured_snowflake_bridges_activated -> Number of activated bridges
	# $number_configured_snowflake_bridges_total -> Total number of bridges
	number_of_snowflake_bridges
}

######## PREPARATIONS ########
do_we_use_bridges
read_config

# Resetting
cd "$(dirname "$0")" || exit 1
shopt -s checkwinsize
[ -f nohup.out ] && sudo rm nohup.out
stty intr ^c
trap

# Resetting run-file (the initial EXITID in the runfile has to be 0)
sudo sed -i "s/^EXITID=.*/${EXITID}/" ${RUNFILE}

###### DISPLAY THE MENU ######
clear

CHOICE=$(whiptail --cancel-button "Back" --title "TorBox v.0.5.5 - COUNTERMEASURE MENU" --menu "Choose an option (ESC -> back to the main menu)            ${BRIDGESTRING}" $MENU_HEIGHT $MENU_WIDTH $MENU_LIST_HEIGHT \
"==" "=============================================[Informational]===" \
" 1" "ALL ABOUT BRIDGES AND PLUGGABLE TRANSPORTS - READ ME FIRST!"  \
"==" "=============================================[OBFS4 bridges]==="  \
" 2" "Activate configured OBFS4 bridges"  \
" 3" "Add additional OBFS4 bridges"  \
" 4" "List all $number_configured_bridges_total OBFS4 bridges"  \
" 5" "Delete configured OBFS4 bridges"  \
" 6" "Deactivate configured OBFS4 bridges"  \
"==" "=========================================[SNOWFLAKE bridges]===" \
" 7" "Activate configured SNOWFLAKE bridges"  \
" 8" "Add additional SNOWFLAKE bridges"  \
" 9" "List all $number_configured_snowflake_bridges_total SNOWFLAKE bridges"  \
"10" "Delete configured SNOWFLAKE bridges"  \
"11" "Deactivate configured SNOWFLAKE bridges"  \
"==" "==============================================[MEEK bridges]===" \
"12" "$MEEKSTRINGb MEEK-AZURE to circumvent censorship" \
"==" "======================================[More Countermeasures]===" \
"13" "Enter the advanced tor configuration editor"  \
"14" "$LOGCHECK TorBox's automatic counteractions - $LOGCHECKSTATUSb"  \
"15" "Change or randomize TorBox's hostname" \
"16" "Countermeasure against a tightly configured firewall - $FIREWALL" \
"17" "Countermeasure against a disconnection when idle feature - $PING" \
"18" "$VPNSTRING1 TorBox $VPNSTRING2 a VPN" \
3>&1 1>&2 2>&3)
exitstatus=$?
# exitstatus == 255 means that the ESC key was pressed
[ "$exitstatus" == "255" ] && exit 0

CHOICE=$(echo "$CHOICE" | tr -d ' ')
case "$CHOICE" in

  # Display the read.me
  1)
    INPUT=$(cat $TXT_DIR/help-bridges-text)
    whiptail --title "TorBox - INFO (scroll down!)" --msgbox --scrolltext "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH
  ;;

  # Activate OBFS4 bridges to circumvent censorship
  2)
		if [ "$MODE_MEEK" = "Bridge meek_lite " ] || [ "$MODE_SNOW" = "Bridge snowflake " ]; then
    	whiptail --title "TorBox - INFO" --textbox $TXT_DIR/no_meek-snow-please-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
  	fi
		if [ "$FIREWALL" = "ON" ]; then
			INPUT=$(cat $TXT_DIR/no_antifirewall-please-text)
			if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
				sudo sed -i "s/^ReachableAddresses /#ReachableAddresses /g" ${TORRC}
			fi
		fi
  	if [ -z $number_configured_bridges_total ] || [ "$number_configured_bridges_total" == "0" ]; then
    	INPUT=$(cat $TXT_DIR/add-bridges-first-text)
    	if (whiptail --title "TorBox - INFO (scroll down!)" --scrolltext --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
      	sudo bash bin/bridges_add_obfs4 "$MODE_BRIDGES" 0
    	else
      	deactivate_obfs4_bridges
      	trap "bash menu-bridges; exit 0" EXIT
      	exit 0
    	fi
		fi
		if [ "$MODE_BRIDGES" != "UseBridges 1" ]; then
    	INPUT=$(cat $TXT_DIR/activate-obfs4-bridges-text)
    	if (whiptail --title "TorBox - INFO (scroll down!)" --scrolltext --defaultno --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
    		sudo bash bin/bridges_activate_obfs4
    	else
    		trap "bash menu-bridges; exit 0" EXIT
    		exit 0
			fi
		else
			sudo bash bin/bridges_activate_obfs4
    fi
  	read_config
  ;;

  # Add OBFS4 bridges
  3)
    sudo bash bin/bridges_add_obfs4 "$MODE_BRIDGES" 1
    read_config
  ;;

  # List all OBFS4 bridges configured in torrc and if online display the status of the bridges
  4)
    list_all_obfs4_bridges
  ;;

  # Delete OBFS4 bridges
  5)
    if [ -z $number_configured_bridges_total ] || [ "$number_configured_bridges_total" == "0" ]; then
      clear
      echo -e "${YELLOW}[!] There are no configured OBFS4 bridges -> nothing to delete!${NOCOLOR}"
      sleep 5
    else
      whiptail --title "TorBox - INFO" --textbox $TXT_DIR/remove-obfs4-bridges-text $MENU_HEIGHT_20 $MENU_WIDTH
      sudo bash bin/bridges_remove_obfs4 "$MODE_BRIDGES"
      read_config
    fi
  ;;

  # Deactivate bridges
  6)
    if [ -z $number_configured_bridges_total ] || [ "$number_configured_bridges_total" == "0" ]; then
      clear
      echo -e "${YELLOW}[!] There are no configured OBFS4 bridges -> nothing to deactivate!${NOCOLOR}"
      sleep 5
    else
      if [ "$MODE_MEEK" = "Bridge meek_lite " ] || [ "$MODE_SNOW" = "Bridge snowflake " ]; then
        clear
        echo -e "${YELLOW}[!] No OBFS4 bridges are activated!${NOCOLOR}"
        echo -e "${RED}[+] If you want to use OBFS4 bridges, you have to activate them first with menu entry 2 or 3.${NOCOLOR}"
        sleep 5
      else
        whiptail --title "TorBox - INFO" --textbox $TXT_DIR/deactivate-obfs4-bridges-text $MENU_HEIGHT_15 $MENU_WIDTH
        sudo bash bin/bridges_deactivate_obfs4
        read_config
      fi
    fi
  ;;

  # Activate/deactivate Snowflake bridges to circumvent censorship
  7)
    if [ "$MODE_MEEK" = "Bridge meek_lite " ] || [ "$MODE_OBFS4" = "Bridge obfs4 " ]; then
      whiptail --title "TorBox - INFO" --textbox $TXT_DIR/no_meek-please-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
    fi
		if [ "$FIREWALL" = "ON" ]; then
			INPUT=$(cat $TXT_DIR/no_antifirewall-please-text)
			if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
				sudo sed -i "s/^ReachableAddresses /#ReachableAddresses /g" ${TORRC}
			fi
		fi
		if [ -z $number_configured_snowflake_bridges_total ] || [ "$number_configured_snowflake_bridges_total" == "0" ]; then
      INPUT=$(cat $TXT_DIR/add-bridges-first-text)
      if (whiptail --title "TorBox - INFO (scroll down!)" --scrolltext --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
        sudo bash bin/bridges_add_snowflake "$MODE_BRIDGES" 0
      else
        deactivate_snowflake_bridges
        trap "bash menu-bridges; exit 0" EXIT
        exit 0
      fi
		fi
		if [ "$MODE_BRIDGES" != "UseBridges 1" ]; then
    	INPUT=$(cat $TXT_DIR/activate-snowflake-bridges-text)
    	if (whiptail --title "TorBox - INFO (scroll down!)" --scrolltext --defaultno --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
      	sudo bash bin/bridges_activate_snowflake
    	else
      	trap "bash menu-bridges; exit 0" EXIT
      	exit 0
    	fi
		else
			sudo bash bin/bridges_activate_snowflake
		fi
    read_config
  ;;

	# Add Snowflake bridges
  8)
    sudo bash bin/bridges_add_snowflake "$MODE_BRIDGES" 1
    read_config
  ;;

  # List all Snowflake bridges configured in torrc and if online display the status of the bridges
  9)
	  list_all_snowflake_bridges
	;;

	# Delete Snowflake bridges
  10)
    if [ -z $number_configured_snowflake_bridges_total ] || [ "$number_configured_snowflake_bridges_total" == "0" ]; then
      clear
      echo -e "${YELLOW}[!] There are no configured Snowflake bridges -> nothing to delete!${NOCOLOR}"
      sleep 5
    else
      whiptail --title "TorBox - INFO" --textbox $TXT_DIR/remove-snowflake-bridges-text $MENU_HEIGHT_20 $MENU_WIDTH
      sudo bash bin/bridges_remove_snowflake "$MODE_BRIDGES"
      read_config
    fi
  ;;

	# Deactivate Snowflake bridges
  11)
    if [ -z $number_configured_snowflake_bridges_total ] || [ "$number_configured_snowflake_bridges_total" == "0" ]; then
      clear
      echo -e "${YELLOW}[!] There are no configured Snowflake bridges -> nothing to deactivate!${NOCOLOR}"
      sleep 5
    else
      if [ "$MODE_MEEK" = "Bridge meek_lite " ] || [ "$MODE_OBFS4" = "Bridge obfs4 " ]; then
        clear
        echo -e "${YELLOW}[!] No Snowflake bridges are activated!${NOCOLOR}"
        echo -e "${RED}[+] If you want to use Snowflake bridges, you have to activate them first with menu entry 7 or 8.${NOCOLOR}"
        sleep 5
      else
        whiptail --title "TorBox - INFO" --textbox $TXT_DIR/deactivate-snowflake-bridges-text $MENU_HEIGHT_15 $MENU_WIDTH
				deactivate_snowflake_bridges menu-bridges
        read_config
      fi
    fi
  ;;

  # Use Meek-Azure to circumvent censorship
  12)
    if [ "$MODE_OBFS4" = "Bridge obfs4 " ] || [ "$MODE_SNOW" = "Bridge snowflake " ]; then
      whiptail --title "TorBox - INFO" --textbox $TXT_DIR/no_snow-please-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
    fi
    sudo bash bin/meek-azure $MEEKSTRING $SNOWSTRING
    read_config
  ;;

  # Enter the advanced configuration editor
  13)
    INPUT=$(cat $TXT_DIR/advanced-bridges-text)
    if (whiptail --title "TorBox - INFO" --defaultno --no-button "DON'T CHANGE" --yes-button "CHANGE NOW" --yesno "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH); then
      sudo cp ${TORRC} ${BAK}
      bin/vitor
      INPUT=$(cat $TXT_DIR/restart-tor-text)
      if (whiptail --title "TorBox - INFO" --defaultno --no-button "NO - DON'T (RE)START" --yes-button "YES - (RE)START" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
        clear
        restarting_tor menu-bridges
      fi
      read_config
    fi
  ;;

  # Activate/deactivate TorBox's automatic counteractions
  14)
    if [ "$LOGCHECK" = "Deactivate" ]; then
      echo -e "${RED}[+] Deactivating TorBox's automatic counteractions feature...${NOCOLOR}"
      sudo pkill -f "log_check.py"
      echo -e "${RED}[+] ...configuration removed...${NOCOLOR}"
      (sudo rm $TORBOX_PATH/log_check_config.py) &>/dev/null
      echo -e "${RED}[+] ...log removed!${NOCOLOR}"
      (sudo find /var/log/tor/ | grep "automat" | xargs -d"\n" sudo rm) &>/dev/null
      (sudo sed -i "s/^LOGCHECK=.*/LOGCHECK=0/" $RUNFILE) &>/dev/null
      sleep 1
      clear
      echo -e "${RED}[+] TorBox's automatic counteractions feature is deactivated!${NOCOLOR}"
      sleep 5
    fi
    if [ "$LOGCHECK" = "Activate" ]; then

      ###### DISPLAY THE AVAILABLE OPTIONS ######
			CHOICE2=$(whiptail --nocancel --title "TorBox v.0.5.5 - AUTOMATIC COUNTERACTIONS" --checklist --separate-output "\nPlease, use SPACE to select/deselect, ENTER to apply, ESC to go back." $MENU_HEIGHT_15 $MENU_WIDTH 6 \
      "1" "Protection against out of sync time" ON \
      "2" "Protection against entry guard failing and overloaded tor network" ON \
      "3" "Protection against interrupted WiFi connection with the Internet" ON \
      "4" "Protection against excessive number of failing tor circuits" ON \
      "5" "Protection against excessive connection failing with tor circuits" ON \
      "6" "Protection against tor bootstrapping problems" ON  \
      3>&1 1>&2 2>&3)

      if [ ! -z "$CHOICE2" ]; then
        mapfile -t CHOICE2 <<< "$CHOICE2"
				clear
        for ARGUMENT in "${CHOICE2[@]}"; do
          [ -z "$ARGUMENT" ] && exit 0
          echo -e "${RED}[+] Activating TorBox's automatic countermeasure - protection number $ARGUMENT${NOCOLOR}"
          sleep 1
          LOOP_NUMBER=$((LOOP_NUMBER+1))
          [ $LOOP_NUMBER = 1 ] && (cp $TORBOX_PATH/install/log_check_config/header $TORBOX_PATH/log_check_config.py) &>/dev/null
          [[ "$ARGUMENT" =~ [1-6] ]] && (cat $TORBOX_PATH/install/log_check_config/00"${ARGUMENT}" >> $TORBOX_PATH/log_check_config.py) &>/dev/null
        done
        if [ ! -z "$LOOP_NUMBER" ]; then
          (cat $TORBOX_PATH/install/log_check_config/footer >> $TORBOX_PATH/log_check_config.py) &>/dev/null
          (sudo sed -i "s/^LOGCHECK=.*/LOGCHECK=1/" $RUNFILE) &>/dev/null
          sudo ./log_check.py &
          clear
          echo -e "${RED}[+] TorBox's automatic countermeasure feature is activated!${NOCOLOR}"
          sleep 5
        fi
        read_config
      fi
    fi
  ;;

	# Change or randomize TorBox's hostname
  15)
		INPUT=$(cat $TXT_DIR/randomize_hostname-text)
		HOSTNAME=$(hostname)
		INPUT=$(sed "s/HOSTNAMEANKER/$HOSTNAME/g" <<< "$INPUT")
		if (whiptail --title "TorBox Installation on Raspberry Pi OS" --defaultno --yes-button "CHANGE!" --yesno "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH); then
			exitstatus=$?
			# exitstatus == 255 means that the ESC key was pressed
			if [ "$exitstatus" != "255" ]; then
				if (whiptail --title "TorBox Installation on Raspberry Pi OS" --no-button "SET HOSTNAME" --yes-button "RANDOMIZE HOSTNAME" --yesno "You can set a specific hostname or use a randomized one. Please choose..." $MENU_HEIGHT_10 $MENU_WIDTH); then
					# shellcheck disable=SC2002
					HOSTNAME=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1)
				else
					exitstatus=$?
					# exitstatus == 255 means that the ESC key was pressed
					if [ "$exitstatus" != "255" ]; then
						HOSTNAME=$(whiptail --title "TorBox Installation on Raspberry Pi OS" --inputbox "\nEnter the hostname:" $MENU_HEIGHT_10 $MENU_WIDTH_REDUX 3>&1 1>&2 2>&3)
						if [[ $HOSTNAME != *[0123456789ABCDEFGHIJKLMNOPQRSTUVWXZYabcdefghijklmnopqrstuvwxzy-]* ]]; then
							HOSTNAME=$(tr -dc 'a-zA-Z0-9' <<<$HOSTNAME)
						fi
						if ${#HOSTNAME} -gt 64 ; then
							HOSTNAME=$(head -c 64 <<<$HOSTNAME)
						fi
					fi
				fi
			fi
			# exitstatus == 255 means that the ESC key was pressed
			if [ "$exitstatus" != "255" ] && [ "$HOSTNAME" != "" ]; then
				clear
				echo -e "${RED}[+] Setting up the hostname...${NOCOLOR}"
				sleep 1
				(sudo hostnamectl set-hostname "$HOSTNAME") 2>/dev/null
				clear
				sudo systemctl restart systemd-hostnamed
				if grep "127.0.1.1.*" /etc/hosts ; then
					(sudo sed -i "s/127.0.1.1.*/127.0.1.1\t$HOSTNAME/g" /etc/hosts) 2>/dev/null
					clear
				else
					(sudo sed -i "s/^::1/127.0.1.1\t$HOSTNAME\n::1/g" /etc/hosts) 2>/dev/null
					clear
				fi
				whiptail --title "TorBox - INFO" --msgbox "TorBox's hostname is now changed to $HOSTNAME\n\nYou have to log out and again log into your console to see the change." $MENU_HEIGHT_10 $MENU_WIDTH_REDUX
			fi
		fi
  ;;

  # Countermeasure against a tightly configured firewall
  16)
    if [ "$FIREWALL" = "OFF" ] || [ -z "$FIREWALL" ]; then
			if [ "$TOGGLE07b" = "ON" ]; then
				INPUT="\n\nTo activate the countermeasure against tightly configured firewalls, you have first to DEACTIVATE OBFS4 Bridge Relay Mode in the OBFS4 Bridge Relay sub-menu. You can not run both settings simultaneously!"
				whiptail --title "TorBox - INFO" --msgbox "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH
				trap "bash menu-bridges; exit 0" EXIT
				exit 1
			fi
      INPUT=$(cat $TXT_DIR/firewall-text-on)
      if (whiptail --title "TorBox - INFO" --defaultno --yesno "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH); then
        sudo sed -i "s/^#ReachableAddresses /ReachableAddresses /g" ${TORRC}
        INPUT=$(cat $TXT_DIR/restart-tor-text)
        if (whiptail --title "TorBox - INFO" --no-button "NO - DON'T (RE)START" --yes-button "YES - (RE)START" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
          clear
          restarting_tor menu-bridges
          clear
          echo -e "${RED}[+] Countermeasure against a tightly configured firewall activated!${NOCOLOR}"
          sleep 2
        else
          clear
          echo -e "${RED}[+] Countermeasure against a tightly configured firewall set but not yet activated!${NOCOLOR}"
          sleep 2
        fi
      fi
    fi
    if [ "$FIREWALL" = "ON" ]; then
      if (whiptail --title "TorBox - INFO" --defaultno --yesno "Do you want to deactivate against a tightly configured firewall?" $MENU_HEIGHT_10 $MENU_WIDTH_REDUX); then
        sudo sed -i "s/^ReachableAddresses /#ReachableAddresses /g" ${TORRC}
        INPUT=$(cat $TXT_DIR/restart-tor-text)
        if (whiptail --title "TorBox - INFO" --no-button "NO - DON'T (RE)START" --yes-button "YES - (RE)START" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
          clear
          restarting_tor menu-bridges
          clear
          echo -e "${RED}[+] Countermeasure against a tightly configured firewall deactivated!${NOCOLOR}"
          sleep 2
        else
          clear
          echo -e "${RED}[+] Countermeasure against a tightly configured firewall unset but not yet deactivated!${NOCOLOR}"
          sleep 2
        fi
      fi
    fi
  ;;

  # Countermeasure against a disconnection when idle feature
  17)
    if [ "$PING" = "OFF" ] || [ -z "$PING" ]; then
      INPUT=$(cat $TXT_DIR/ping-text-on)
      if (whiptail --title "TorBox - INFO" --defaultno --yesno "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH); then
        ping -q "$PING_SERVER" >/dev/null &
        #Alternative Möglichkeit: screen -dm ping google.com
        echo -e "${RED}[+] Countermeasure against a disconnect when idle feature activated!${NOCOLOR}"
        sleep 2
      fi
    fi
    if [ "$PING" = "ON" ]; then
      if (whiptail --title "TorBox - INFO" --defaultno --yesno "Do you want to deactivate the countermeasure against a disconnect when idle feature?" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX); then
        sudo killall ping
        echo -e "${RED}[+] Countermeasure against a disconnect when idle feature deactivated!${NOCOLOR}"
        sleep 2
      fi
    fi
  ;;

  # Connect/Disconnect TorBox to/from VPN
  18)
    if [ -z "$tun0up" ] ; then
      INPUT=$(cat $TXT_DIR/connecting-VPN-text)
      if (whiptail --title "TorBox - INFO" --defaultno --yesno "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH); then
        connecting_to_VPN
			  sudo systemctl restart tor &
				tun0up=$(ip link | grep tun0)
      fi
    else
      INPUT=$(cat $TXT_DIR/disconnecting-VPN-text)
      if (whiptail --title "TorBox - INFO" --defaultno --yesno "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH); then
        clear
        echo -e "${RED}[+] Disonnecting OpenVPN server...${NOCOLOR}"
        sudo killall openvpn
        echo -e "${RED}[+] Please wait, we need 15 second to configure the interface...${NOCOLOR}"
        sleep 15
				sudo systemctl restart tor &
        echo ""
        echo -e "${RED}[+] It may take some time for Tor to reconnect.${NOCOLOR}"
        echo -e "${RED}[+] In case of problems, use main menu entry 5-10.${NOCOLOR}"
        sleep 5
        tun0up=$(ip link | grep tun0)
      fi
    fi
  ;;

  *)
    clear
    exit 0
esac

bash menu-bridges
