#!/bin/bash

# This file is a 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 displays the main menu and executes all relevant scripts.
#
# SYNTAX
# ./menu [<INTERNETSELECTION>]
#
# <INTERNETSELECTION> will execute one of the routines, which configures
# TorBox to route the data from an internal to an external interface.
#
###### SET VARIABLES ######
#
# SIZE OF THE MENU
#
# How many items do you have in the main menu?
NO_ITEMS=15
#
# How many lines are only for decoration and spaces?
NO_SPACER=3
#
#Set the the variables for the menu
MENU_WIDTH=80
MENU_WIDTH_REDUX=60
MENU_HEIGHT_15=15
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
RED='\033[1;31m'
YELLOW='\033[1;93m'
NOCOLOR='\033[0m'

# Identify the Operating System
CHECK_OS="$(lsb_release -si)"
CHECK_OS="$(echo "$CHECK_OS" | tail -n1)"

#Other variables
TORSTATS="sudo -u debian-tor nyx"
TORRC="/etc/tor/torrc"
RUNFILE="/home/torbox/torbox/run/torbox.run"
EXITID="EXITID=0"
MMENUSTRING=""
INTERNETSELECTION=$1

# Read configuration from run/torbox.run
TORBOX_MINI=$(grep "^TORBOX_MINI=.*" ${RUNFILE} | sed "s/.*=//g")
TORBOX_MINI_DEFAULT=$(grep "^TORBOX_MINI_DEFAULT=.*" ${RUNFILE} | sed "s/.*=//g")

#Check freshinstalled
FRESHINSTALLED=$(grep "^FRESH_INSTALLED=.*" ${RUNFILE} | sed "s/.*=//g") 2>/dev/null

#Check if this installation is on a cloud
ON_A_CLOUD=$(grep "^ON_A_CLOUD=.*" ${RUNFILE} | sed "s/.*=//g")

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

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

# TIS FUNCTION HAS TO BE INTEGRATED INTO THE LIB
# This function imports the configuration and makes some preparations
# TOGGLE07 / TOGGLE08 represents the status of the Bridge Relay mode
read_config()
{
  MODE_BR=$(grep "^BridgeRelay" ${TORRC})
  if [ "$MODE_BR" = "BridgeRelay 1" ]; then
    TOGGLE07="IS RUNNING"
  else
    TOGGLE07="IS NOT RUNNING"
  fi
  MODE_OS=$(grep "^HiddenServiceDir" ${TORRC})
  if [ ! -z "$MODE_OS" ]; then
    TOGGLE16="IS RUNNING"
  else
    TOGGLE16="IS NOT RUNNING"
  fi
}

# This function opens the ports, after a restart if bridge relay is on
check_bridge_relay()
{
	if grep "^BridgeRelay 1" ${TORRC} ; then
  	ORPORT=$(grep "^ORPort" ${TORRC} | cut -d " " -f2)
  	OBFS4PORT=$(grep "^ServerTransportListenAddr" ${TORRC} | cut -d ":" -f2)
  	(sudo iptables -C INPUT -p tcp --dport $ORPORT -j ACCEPT) 2>/dev/null
  	RULE_CHECK=$?
  	if [ $RULE_CHECK = 1 ]; then
    	(sudo iptables -A INPUT -p tcp --dport $ORPORT -j ACCEPT) 2>/dev/null
    	(sudo iptables -A INPUT -p tcp --dport $OBFS4PORT -j ACCEPT) 2>/dev/null
  	fi
	else
		if sudo iptables-save | grep -e "-A INPUT -p tcp --dport $ORPORT -j ACCEPT" ; then (sudo /sbin/iptables -D INPUT -p tcp --dport $ORPORT -j ACCEPT) 2>/dev/null ; fi
    clear
		if sudo iptables-save | grep -e "-A INPUT -p tcp --dport $OBFS4PORT -j ACCEPT" ; then (sudo /sbin/iptables -D INPUT -p tcp --dport $OBFS4PORT -j ACCEPT) 2>/dev/null ; fi
    clear
	fi
}

# This function tests if TorBox's automatic counteractions are activated and still running
check_log_check()
{
	clear
	if pgrep -f "log_check.py"; then
  	clear
	else
  	clear
  	if grep "LOGCHECK=1" /home/torbox/torbox/run/torbox.run ; then
    	clear
    	(nohup sudo /usr/bin/python3 /home/torbox/torbox/log_check.py &) 2>/dev/null
			clear
    	sleep 1
  	fi
	fi
}

# This function asks if a Captive Portal is used and which method should be taken to pass through it
ask_captive()
{
	clear
	INPUT=$(cat text/captive_or_not-text)
	if (whiptail --title "TorBox - INFO" --defaultno --no-button "OPEN ACCESS" --yes-button "CAPTIVE PORTAL" --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
		INPUT=$(cat text/captive_portal-text)
		if (whiptail --title "TorBox - INFO" --no-button "TUNNELLING" --yes-button "SPOOFING" --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
			CAPTIVE=2
		else
			CAPTIVE=1
		fi
	else
		CAPTIVE=0
	fi
	sleep 2
}

# This function selects which Ethernet interface (ethx) the cable client is connected to. There are three possibilities:
# - there are several Ethernet interfaces, which the user has to select as a cable client connection.
# - there is only one eth-Interface, which is selected automatically.
# - there is none or the user selected that there is none: in this case, we assign the default value eth0.

select_eth_client()
{
	i=0
	readarray -t iface_array < <(ls /sys/class/net | grep '^eth')
	for iface in "${iface_array[@]}"; do
		if [ "$(cat /sys/class/net/$iface/operstate)" = "up" ]; then
			i=$((i+1));
		else
			array_number=$((i-1))
			unset 'iface_array[$arraynumber]'
		fi
	done
	iface_array=( "${iface_array[@]}" )
	if [ $i -gt 1 ]; then
		n=0
		WHIPTAIL_LINE="whiptail --nocancel --title \"TorBox v.0.5.5 - Where is your cable client?\" --radiolist \"Choose with SPACE and then press ENTER (ESC -> you don't have any cable client)\n\" 9 $MENU_WIDTH 3"
		for iface in "${iface_array[@]}"; do
			n=$((n+1))
			WHIPTAIL_LINE="$WHIPTAIL_LINE \"$n\" \"It is connected to $iface\" OFF"
		done
		CHOICE=$(eval $WHIPTAIL_LINE)
		exitstatus=$?
		# exitstatus == 1 means that the ESC key was pressed
		if [ "$exitstatus" == "1" ] || [ "$exitstatus" == "255" ]; then
			INTERFACE="${iface_array[0]}"
		else
			CHOICE=$((CHOICE-1));
			INTERFACE="${iface_array[$CHOICE]}"
		fi
	elif [ $i = 1 ]; then
		INTERFACE="${iface_array[0]}"
	else
		INTERFACE="eth0"
	fi
}


# This functions handles the configuration in torrc dependent on the present interfaces
WLAN0_exists()
{
	sudo sed -i "s/^#TransPort 192.168.42.1:9040/TransPort 192.168.42.1:9040/" ${TORRC}
	sudo sed -i "s/^#DNSPort 192.168.42.1:9053/DNSPort 192.168.42.1:9053/" ${TORRC}
	sudo sed -i "s/^#SocksPort 192.168.42.1:9050/SocksPort 192.168.42.1:9050/" ${TORRC}
	sudo sed -i "s/^#SocksPort 192.168.42.1:9052/SocksPort 192.168.42.1:9052/" ${TORRC}
	sudo sed -i "$CONTROLPORT_WLAN" ${TORRC}
	sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=wlan0/" ${RUNFILE}
}

WLAN1_exists()
{
	sudo sed -i "s/^#TransPort 192.168.42.1:9040/TransPort 192.168.42.1:9040/" ${TORRC}
	sudo sed -i "s/^#DNSPort 192.168.42.1:9053/DNSPort 192.168.42.1:9053/" ${TORRC}
	sudo sed -i "s/^#SocksPort 192.168.42.1:9050/SocksPort 192.168.42.1:9050/" ${TORRC}
	sudo sed -i "s/^#SocksPort 192.168.42.1:9052/SocksPort 192.168.42.1:9052/" ${TORRC}
	sudo sed -i "$CONTROLPORT_WLAN" ${TORRC}
	sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=wlan1/" ${RUNFILE}
}

ETH_exists()
{
	sudo sed -i "s/^#TransPort 192.168.43.1:9040/TransPort 192.168.43.1:9040/" ${TORRC}
	sudo sed -i "s/^#DNSPort 192.168.43.1:9053/DNSPort 192.168.43.1:9053/" ${TORRC}
	sudo sed -i "s/^#SocksPort 192.168.43.1:9050/SocksPort 192.168.43.1:9050/" ${TORRC}
	sudo sed -i "s/^#SocksPort 192.168.43.1:9052/SocksPort 192.168.43.1:9052/" ${TORRC}
	sudo sed -i "$CONTROLPORT_ETH" ${TORRC}
	CLIENT_IFACE=$(grep "^CLIENT_IFACE=" ${RUNFILE})
	if [ "$CLIENT_IFACE" == "CLIENT_IFACE=wlan0" ] ; then
		sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=wlan0 eth0/" ${RUNFILE}
	elif [ "$CLIENT_IFACE" == "CLIENT_IFACE=wlan1" ] ; then
		sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=wlan1 eth0/" ${RUNFILE}
	else
		sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=eth0/" ${RUNFILE}
	fi
}

ETH1_exists()
{
	sudo sed -i "s/^#TransPort 192.168.43.1:9040/TransPort 192.168.43.1:9040/" ${TORRC}
	sudo sed -i "s/^#DNSPort 192.168.43.1:9053/DNSPort 192.168.43.1:9053/" ${TORRC}
	sudo sed -i "s/^#SocksPort 192.168.43.1:9050/SocksPort 192.168.43.1:9050/" ${TORRC}
	sudo sed -i "s/^#SocksPort 192.168.43.1:9052/SocksPort 192.168.43.1:9052/" ${TORRC}
	sudo sed -i "$CONTROLPORT_ETH" ${TORRC}
	CLIENT_IFACE=$(grep "^CLIENT_IFACE=" ${RUNFILE})
	if [ "$CLIENT_IFACE" == "CLIENT_IFACE=wlan0" ] ; then
		sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=wlan0 eth1/" ${RUNFILE}
	elif [ "$CLIENT_IFACE" == "CLIENT_IFACE=wlan1" ] ; then
		sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=wlan1 eth1/" ${RUNFILE}
	else
		sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=eth1/" ${RUNFILE}
	fi
}

# This function is used for TorBox on a Cloud (tun1) and for TorBox mini (usb0)
TUN1_exists()
{
	sudo sed -i "s/^#TransPort 192.168.44.1:9040/TransPort 192.168.44.1:9040/" ${TORRC}
	sudo sed -i "s/^#DNSPort 192.168.44.1:9053/DNSPort 192.168.44.1:9053/" ${TORRC}
	sudo sed -i "s/^#SocksPort 192.168.44.1:9050/SocksPort 192.168.44.1:9050/" ${TORRC}
	sudo sed -i "s/^#SocksPort 192.168.44.1:9052/SocksPort 192.168.44.1:9052/" ${TORRC}
	sudo sed -i "$CONTROLPORT_TUN" ${TORRC}
	CLIENT_IFACE=$(grep "^CLIENT_IFACE=wlan0 " ${RUNFILE})
	if [ -n "$CLIENT_IFACE" ] ; then
		CLIENT_IFACE="${CLIENT_IFACE} $CLIENT_IFACE_TUN1_IP"
	else
		CLIENT_IFACE=$(grep "^CLIENT_IFACE=wlan1 " ${RUNFILE})
		if [ -n "$CLIENT_IFACE" ] ; then
			CLIENT_IFACE="${CLIENT_IFACE} $CLIENT_IFACE_TUN1_IP"
		else
			CLIENT_IFACE=$(grep "^CLIENT_IFACE=" ${RUNFILE})
			if [ "$CLIENT_IFACE" == "CLIENT_IFACE=wlan0" ] ; then
				sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=wlan0 $CLIENT_IFACE_TUN1_IP/" ${RUNFILE}
			elif [ "$CLIENT_IFACE" == "CLIENT_IFACE=wlan1" ] ; then
				sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=wlan1 $CLIENT_IFACE_TUN1_IP/" ${RUNFILE}
			elif [ "$CLIENT_IFACE" == "CLIENT_IFACE=eth0" ] ; then
					sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=eth0 $CLIENT_IFACE_TUN1_IP/" ${RUNFILE}
			elif [ "$CLIENT_IFACE" == "CLIENT_IFACE=eth1" ] ; then
					sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=eth1 $CLIENT_IFACE_TUN1_IP/" ${RUNFILE}
			else
				sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=$CLIENT_IFACE_TUN1_IP/" ${RUNFILE}
			fi
		fi
	fi
}

torrc_reset()
{
	sudo sed -i "s/^TransPort 192.168.42.1:9040/#TransPort 192.168.42.1:9040/" ${TORRC}
	sudo sed -i "s/^DNSPort 192.168.42.1:9053/#DNSPort 192.168.42.1:9053/" ${TORRC}
	sudo sed -i "s/^SocksPort 192.168.42.1:9050/#SocksPort 192.168.42.1:9050/" ${TORRC}
	sudo sed -i "s/^SocksPort 192.168.42.1:9052/#SocksPort 192.168.42.1:9052/" ${TORRC}
	sudo sed -i "s/^ControlPort 192.168.42.1:9051/#ControlPort 192.168.42.1:9051/" ${TORRC}
	sudo sed -i "s/^TransPort 192.168.43.1:9040/#TransPort 192.168.43.1:9040/" ${TORRC}
	sudo sed -i "s/^DNSPort 192.168.43.1:9053/#DNSPort 192.168.43.1:9053/" ${TORRC}
	sudo sed -i "s/^SocksPort 192.168.43.1:9050/#SocksPort 192.168.43.1:9050/" ${TORRC}
	sudo sed -i "s/^SocksPort 192.168.43.1:9052/#SocksPort 192.168.43.1:9052/" ${TORRC}
	sudo sed -i "s/^ControlPort 192.168.43.1:9051/#ControlPort 192.168.43.1:9051/" ${TORRC}
	sudo sed -i "s/^TransPort 192.168.44.1:9040/#TransPort 192.168.44.1:9040/" ${TORRC}
	sudo sed -i "s/^DNSPort 192.168.44.1:9053/#DNSPort 192.168.44.1:9053/" ${TORRC}
	sudo sed -i "s/^SocksPort 192.168.44.1:9050/#SocksPort 192.168.44.1:9050/" ${TORRC}
	sudo sed -i "s/^SocksPort 192.168.44.1:9052/#SocksPort 192.168.44.1:9052/" ${TORRC}
	sudo sed -i "s/^ControlPort 192.168.44.1:9051/#ControlPort 192.168.44.1:9051/" ${TORRC}
	sudo sed -i "s/^CLIENT_IFACE=.*/CLIENT_IFACE=/" ${RUNFILE}
}


######## PREPARATIONS ########
# 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}
clear
if [ -z "$INTERNETSELECTION" ]; then
  if [ $FRESHINSTALLED -gt 0 ]; then ./first_use; fi
fi
read_config
do_we_use_bridges
check_bridge_relay
check_interface_with_internet
check_log_check
check_tor
TOR_OVER_VPN=$(sudo iptables -t nat -vnL | grep tun0)

if [ ! -z "$TOR_STATUS" ] && [ ! -z "$VPN_STATUS" ] && [ ! -z "$TOR_OVER_VPN" ]; then TOR_STATUS="   TOR over VPN working"
elif [ ! -z "$TOR_STATUS" ] && [ ! -z "$VPN_STATUS" ] && [ -z "$TOR_OVER_VPN" ]; then TOR_STATUS="VPN is up & TOR working"
elif [ ! -z "$TOR_STATUS" ] && [ -z "$VPN_STATUS" ] && [ -z "$TOR_OVER_VPN" ]; then TOR_STATUS="         TOR is working"
elif [ -z "$TOR_STATUS" ] && [ ! -z "$VPN_STATUS" ] && [ -z "$TOR_OVER_VPN" ]; then TOR_STATUS="              VPN is up"
else TOR_STATUS=""
fi

if grep ^FORWARDING_ONLY=1 ${RUNFILE}; then TOR_STATUS="   FORWARDING - NO TOR!"; fi

if grep "^TOR_CONTROL_PORT_ACCESS=1" ${RUNFILE}; then
  CONTROLPORT_WLAN="s/^#ControlPort 192.168.42.1:9051/ControlPort 192.168.42.1:9051/"
  CONTROLPORT_ETH="s/^#ControlPort 192.168.43.1:9051/ControlPort 192.168.43.1:9051/"
	CONTROLPORT_TUN="s/^#ControlPort 192.168.44.1:9051/ControlPort 192.168.44.1:9051/"
else
  CONTROLPORT_WLAN="s/^ControlPort 192.168.42.1:9051/#ControlPort 192.168.42.1:9051/"
  CONTROLPORT_ETH="s/^ControlPort 192.168.43.1:9051/#ControlPort 192.168.43.1:9051/"
	CONTROLPORT_TUN="s/^ControlPort 192.168.44.1:9051/#ControlPort 192.168.44.1:9051/"
fi
CHOICE=""

#If first_use is at play, only the configuration where the Internet is coming from will be made without displaying the Main Menu
if [[ $INTERNETSELECTION -gt 4 && $INTERNETSELECTION -lt 11 ]]; then
  CHOICE=$INTERNETSELECTION
else
	#Probabaly only under Ubuntu shell output line separator fails to get reset: https://superuser.com/questions/1650758/how-does-the-shell-output-line-separator-fail-to-get-reset
	if [ "$CHECK_OS" == "Ubuntu" ]; then stty sane; fi

  ###### DISPLAY THE MAIN MENU ######
	if [ "$ON_A_CLOUD" -eq "1" ]; then
		CHOICE=$(whiptail --nocancel --title "TorBox on a cloud v.0.5.5 - MAIN MENU" --menu "Choose an option (ESC -> command prompt)           $TOR_STATUS" $MENU_HEIGHT $MENU_WIDTH $MENU_LIST_HEIGHT \
		" 1" "Tor statistics (press m for menu / q to quit)" \
		" 2" "Enforce a new exit node with a new IP" \
		" 3" "Restart Tor - check if it is working (press q to quit)"  \
		" 4" "Display the Tor log file - quick and dirty (press q to quit)" \
		"===" "===================================[Where is the Internet?]===" \
		" 5" "Ethernet cable (eth0) or TorBox on a cloud (tun1)          "$FLASH_ETH0 \
		"  " " " \
		"  " "On TorBox on a Cloud, menu entry 6-10 are not available!   " \
		"  " "For special requirements, please contact the TorBox        " \
		"  " "development team --> https://www.torbox.ch   " \
		"  " " " \
		"===" "================================================[Sub-Menus]===" \
		"11" "Go to the Countermeasure sub-menu...          ${MMENUSTRING}" \
		"12" "Go to the Configuration sub-menu..." \
		"13" "Go to run an OBFS4 Bridge Relay...            ${TOGGLE07}" \
		"14" "Go to run an Onion Service...                 ${TOGGLE16}" \
		"15" "Go to the Danger Zone..." \
		"16" "Go to the Update and Maintenance sub-menu..." \
		"==" "============================================[Informational]===" \
		"17" "HOW CAN I SUPPORT THE TOR PROJECT!" \
		"18" "HOW CAN I HELP WITH TORBOX?" \
		"===" "========================================[Reboot & Shutdown]===" \
		"19" "Reboot / Shutdown TorBox" \
		3>&1 1>&2 2>&3)
	elif [ "$TORBOX_MINI" -eq "1" ] && [ "$TORBOX_MINI_DEFAULT" -eq "1" ]; then
			CHOICE=$(whiptail --nocancel --title "TorBox on a cloud v.0.5.5 - MAIN MENU" --menu "Choose an option (ESC -> command prompt)           $TOR_STATUS" $MENU_HEIGHT $MENU_WIDTH $MENU_LIST_HEIGHT \
			" 1" "Tor statistics (press m for menu / q to quit)" \
			" 2" "Enforce a new exit node with a new IP" \
			" 3" "Restart Tor - check if it is working (press q to quit)"  \
			" 4" "Display the Tor log file - quick and dirty (press q to quit)" \
			"===" "===================================[Where is the Internet?]===" \
			"  " " " \
			"  " " " \
			" 7" "TorBox mini default (wlan0 --> usb0)   "$FLASH_USB0 \
			"  " " " \
			"  " "Go to the Danger Zone to activate other connection options!    " \
			"  " " " \
			"===" "================================================[Sub-Menus]===" \
			"11" "Go to the Countermeasure sub-menu...          ${MMENUSTRING}" \
			"12" "Go to the Configuration sub-menu..." \
			"13" "Go to run an OBFS4 Bridge Relay...            ${TOGGLE07}" \
			"14" "Go to run an Onion Service...                 ${TOGGLE16}" \
			"15" "Go to the Danger Zone..." \
			"16" "Go to the Update and Maintenance sub-menu..." \
			"==" "============================================[Informational]===" \
			"17" "HOW CAN I SUPPORT THE TOR PROJECT!" \
			"18" "HOW CAN I HELP WITH TORBOX?" \
			"===" "========================================[Reboot & Shutdown]===" \
			"19" "Reboot / Shutdown TorBox" \
			3>&1 1>&2 2>&3)
	else
  	CHOICE=$(whiptail --nocancel --title "TorBox v.0.5.5 - MAIN MENU" --menu "Choose an option (ESC -> command prompt)            $TOR_STATUS" $MENU_HEIGHT $MENU_WIDTH $MENU_LIST_HEIGHT \
  	" 1" "Tor statistics (press m for menu / q to quit)" \
  	" 2" "Enforce a new exit node with a new IP" \
  	" 3" "Restart Tor - check if it is working (press q to quit)"  \
  	" 4" "Display the Tor log file - quick and dirty (press q to quit)" \
  	"===" "===================================[Where is the Internet?]===" \
		" 5" "Ethernet cable (eth0) or TorBox on a cloud (tun1)          "$FLASH_ETH0 \
  	" 6" "Wireless network (USB adapter; wlan1)                      "$FLASH_WLAN1 \
		" 7" "Wireless network or TorBox mini (onboard chip; wlan0)      "$FLASH_WLAN0 \
  	" 8" "Cellular, USB dongle or Tethering (Android) (ppp0; usb0)   "$FLASH_USB0 \
  	" 9" "USB ethernet adapter or Tethering (iOS) (eth1)             "$FLASH_ETH1 \
  	"10" "Over a VPN connection (tun0)                               "$FLASH_TUN0 \
  	"===" "================================================[Sub-Menus]===" \
  	"11" "Go to the Countermeasure sub-menu...          ${MMENUSTRING}" \
  	"12" "Go to the Configuration sub-menu..." \
		"13" "Go to run an OBFS4 Bridge Relay...            ${TOGGLE07}" \
		"14" "Go to run an Onion Service...                 ${TOGGLE16}" \
		"15" "Go to the Danger Zone..." \
  	"16" "Go to the Update and Maintenance sub-menu..." \
		"==" "============================================[Informational]===" \
		"17" "HOW CAN I SUPPORT THE TOR PROJECT!" \
		"18" "HOW CAN I HELP WITH TORBOX?" \
  	"===" "========================================[Reboot & Shutdown]===" \
  	"19" "Reboot / Shutdown TorBox" \
  	3>&1 1>&2 2>&3)
	fi
	exitstatus=$?
	# exitstatus == 1 means that the ESC key was pressed
	if [ "$exitstatus" == "1" ]; then
		clear
		#Probabaly only under Ubuntu shell output line separator fails to get reset: https://superuser.com/questions/1650758/how-does-the-shell-output-line-separator-fail-to-get-reset
		if [ "$CHECK_OS" == "Ubuntu" ]; then stty sane; fi
		PROMPTTEXT=$(cat text/prompt-text)
		echo -e "$PROMPTTEXT"
		echo " "
		exit 0
	fi
fi

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

  # This starts nyx (Tor statistics)
  1)
    clear
    $TORSTATS
  ;;

  # This enforces a new exit node
  2)
    clear
    echo -e "${RED}[+] Requesting new identity...${YELLOW}"
    echo ""
    sleep 2
    sudo -u debian-tor tor-prompt --run 'SIGNAL NEWNYM'
    echo -e "${YELLOW}[+] DONE!${NOCOLOR}"
    sleep 4
  ;;

  # Restart Tor and check the progress
  3)
    INPUT=$(cat text/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
			exit 0
    fi
  ;;

  # This displays the tor log file (quick and dirty)
  4)
    clear
    trap "bash menu; exit 0" SIGINT
    echo -e "${RED}[+] See below the tor log file (press q to quit):${NOCOLOR}"
		stty intr q
    sudo tail -f -n 30 /var/log/tor/notices.log | sed -u 's#.*Bootstrapped 100% (done): Done#\x1b[93m&\x1b[0m#' | sed -u 's#Bootstrapped 100% (done): Done#Bootstrapped 100% (done): Done -- YOU CAN PRESS NOW Q!#' | grep -v -e "Rejecting ill-formed reverse lookup" -e "Missing mapping for virtual address" -e "You configured a non-loopback address" -e "opening log file" -e "Application request when we haven't" -e "Giving up. (waiting for circuit)" -e "New control connection opened from" -e "While fetching directory info, no running dirservers known"
		stty intr ^c
		trap
  ;;

	# Ethernet cable internet eth0 --> wlan0 / eth1 / usb0 / tun1
  5)
		if [[ "$INTERNETSELECTION" -eq "5" && "$ON_A_CLOUD" -eq "1" ]]; then
			:
		else
			INPUT=$(cat text/cable-text)
    	if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
			  if ! ip link | grep eth0 > /dev/null; then
				  INPUT=$(cat text/no_interface_try_it_anyway-text)
				  if (whiptail --title "TorBox - INFO" --yesno --defaultno --no-button "BACK!" --yes-button "PUSH IT!" "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX); then
					  exitstatus=$?
					  # exitstatus = 255 means that the ESC key was pressed / exitstatus = 1 is cancelled
					  if [ "$exitstatus" = "1" ] || [ "$exitstatus" = "255" ] ; then	exit 0 ; fi
				  else
					  trap "bash menu; exit 0" EXIT
					  exit 0
				  fi
			  fi
			else
				trap "bash menu; exit 0" EXIT
				exit 0
			fi
		fi
	  display_net_reconfiguration_info
		# NEW v.0.5.4-post
		echo -e "${YELLOW}[+] Step 1 of 2 ...${NOCOLOR}"
		if ip link | grep usb0 > /dev/null; then
			nohup bin/hostapd_fallback_komplex_new wlan0 eth1 > /dev/null 2>&1 &
		else
			nohup bin/hostapd_fallback_komplex_new wlan0 eth1 notusb0 > /dev/null 2>&1 &
		fi
		pid=$!
		wait $pid
		echo -e "${YELLOW}[+] Step 2 of 2 done${NOCOLOR}"
		sleep 2
    if ip link | grep eth0 > /dev/null; then
			ask_captive
    	if ip link | grep wlan0 | grep "state UP" ; then
      	WLAN0_IP=$(ip addr show wlan0 | grep -w 192.168.42.1)
      	clear
    	fi
    	if ip link | grep eth1 | grep "state UP" ; then
      	ETH1_IP=$(ip addr show eth1 | grep -w 192.168.43.1)
      	clear
    	fi
			# The state of tun1 is always UNKNOWN
			# TUN1_IP is used for tun1 (TorBox on a Cloud) and usb0 (TorBox mini)
			if ip link | grep tun1 | grep "state UNKNOWN" ; then
				TUN1_IP=$(ip addr show tun1 | grep -w 192.168.44.1)
        CLIENT_IFACE_TUN1_IP="tun1"
				clear
      else
        TUN1_IP=$(ip addr show usb0 | grep -w 192.168.44.1)
        CLIENT_IFACE_TUN1_IP="usb0"
        clear
			fi
			# Configure torrc depending on the present interfaces
			if [ -z "$WLAN0_IP" ] && [ -z "$ETH1_IP" ] && [ -z "$TUN1_IP" ] && [ "$ON_A_CLOUD" -eq "0" ] ; then
				sudo cp /etc/network/interfaces.bak /etc/network/interfaces
				whiptail --title "TorBox - INFO" --textbox text/no_interface-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
			# NEW v.0.5.4-post: This should avoid a error message if the OpenVPN server is not installed, yet
			# Because on a TorBox on a Cloud only this menu entry is available to use, this part of the code is
			# only here necessary and not in other parts of the menu-script.
			elif [ -z "$WLAN0_IP" ] && [ -z "$ETH1_IP" ] && [ -z "$TUN1_IP" ] && [ "$ON_A_CLOUD" -eq "1" ] ; then
				whiptail --title "TorBox - INFO" --textbox text/no_interface_yet-text $MENU_HEIGHT_15 $MENU_WIDTH
				sudo sed -i "s/^INTERNET_IFACE=.*/INTERNET_IFACE=eth0/" ${RUNFILE}
				if [ -z "$INTERNETSELECTION" ]; then
      		sudo bash bin/set_interfaces_2 eth0 wlan0 eth1 $CLIENT_IFACE_TUN1_IP $CAPTIVE
  				(sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
          clear
          restarting_tor menu
					trap
          exit 0
    		else
      		sudo bash bin/set_interfaces_2 eth0 wlan0 eth1 $CLIENT_IFACE_TUN1_IP $CAPTIVE
					(sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
          exit 0
    		fi
			else
				torrc_reset
    		if [ -n "$WLAN0_IP" ] ; then WLAN0_exists; fi
    		if [ -n "$ETH1_IP" ] ; then ETH1_exists; fi
				if [ -n "$TUN1_IP" ] ; then TUN1_exists; fi
				sudo sed -i "s/^INTERNET_IFACE=.*/INTERNET_IFACE=eth0/" ${RUNFILE}
    		# The following line should minimize those cases where a restart is required.
    		if [ "$ON_A_CLOUD" -eq "0" ]; then
					sudo systemctl restart isc-dhcp-server
				fi
    		if [ -z "$INTERNETSELECTION" ]; then
      		sudo bash bin/set_interfaces_2 eth0 wlan0 eth1 $CLIENT_IFACE_TUN1_IP $CAPTIVE
					(sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
          clear
          restarting_tor menu
          exit 0
    		else
      		sudo bash bin/set_interfaces_2 eth0 wlan0 eth1 $CLIENT_IFACE_TUN1_IP $CAPTIVE
					(sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
          exit 0
    		fi
			fi
		else
			if [ "$ON_A_CLOUD" -eq "0" ]; then
				sudo cp /etc/network/interfaces.bak /etc/network/interfaces
			fi
			whiptail --title "TorBox - INFO" --textbox text/no_eth0-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
		fi
  ;;

	# Wireless network (through an USB adapter; wlan1 --> wlan0 / eth0 / usb0 / tun1)
	# eth0 can be replaced by any other ethx
  6)
    INPUT=$(cat text/wlan1-text)
    if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
			if ! ip link | grep wlan1 > /dev/null; then
				INPUT=$(cat text/no_interface_try_it_anyway-text)
				if (whiptail --title "TorBox - INFO" --yesno --defaultno --no-button "BACK!" --yes-button "PUSH IT!" "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX); then
					exitstatus=$?
					# exitstatus = 255 means that the ESC key was pressed / exitstatus = 1 is cancelled
					if [ "$exitstatus" = "1" ] || [ "$exitstatus" = "255" ] ; then exit 0 ; fi
				else
					trap "bash menu; exit 0" EXIT
					exit 0
				fi
			fi
			select_eth_client
			clear
      display_net_reconfiguration_info
			# NEW v.0.5.4-post
			echo -e "${YELLOW}[+] Step 1 of 2 ...${NOCOLOR}"
			if ip link | grep usb0 > /dev/null; then
				nohup bin/hostapd_fallback_komplex_new wlan0 $INTERFACE > /dev/null 2>&1 &
			else
				nohup bin/hostapd_fallback_komplex_new wlan0 $INTERFACE notusb0 > /dev/null 2>&1 &
			fi
			pid=$!
			wait $pid
			echo -e "${YELLOW}[+] Step 2 of 2 done${NOCOLOR}"
			sleep 2
			if ip link | grep wlan1 > /dev/null; then
        ask_captive
        if ip link | grep wlan0 | grep "state UP" ; then
          WLAN0_IP=$(ip addr show wlan0 | grep -w 192.168.42.1)
          clear
        fi
				if ip link | grep $INTERFACE | grep "state UP" ; then
					ETH_IP=$(ip addr show $INTERFACE | grep -w 192.168.43.1)
          clear
        fi
        # The state of tun1 is always UNKNOWN
        # TUN1_IP is used for tun1 (TorBox on a Cloud) and usb0 (TorBox mini)
				if ip link | grep tun1 | grep "state UNKNOWN" ; then
					TUN1_IP=$(ip addr show tun1 | grep -w 192.168.44.1)
          CLIENT_IFACE_TUN1_IP="tun1"
					clear
        else
          TUN1_IP=$(ip addr show usb0 | grep -w 192.168.44.1)
          CLIENT_IFACE_TUN1_IP="usb0"
          clear
				fi
				# Configure torrc depending on the present interfaces
				if [ -z "$WLAN0_IP" ] && [ -z "$ETH_IP" ] && [ -z "$TUN1_IP" ] && [ "$ON_A_CLOUD" -eq "0" ]; then
					sudo cp /etc/network/interfaces.bak /etc/network/interfaces
					whiptail --title "TorBox - INFO" --textbox text/no_interface-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
				else
					torrc_reset
	      	if [ -n "$WLAN0_IP" ] ; then WLAN0_exists; fi
	      	if [ -n "$ETH_IP" ] ; then ETH_exists; fi
					if [ -n "$TUN1_IP" ] ; then TUN1_exists; fi
					sudo sed -i "s/^INTERNET_IFACE=.*/INTERNET_IFACE=wlan1/" ${RUNFILE}
        	# The following line should minimize those cases where a restart is required.
        	if [ "$ON_A_CLOUD" -eq "0" ]; then
						sudo systemctl restart isc-dhcp-server
					fi
					if [ -z "$INTERNETSELECTION" ]; then
						sudo bash bin/set_interfaces_2 wlan1 wlan0 $INTERFACE $CLIENT_IFACE_TUN1_IP $CAPTIVE
            (sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
            clear
            restarting_tor menu
						trap
            exit 0
        	else
						sudo bash bin/set_interfaces_2 wlan1 wlan0 $INTERFACE $CLIENT_IFACE_TUN1_IP $CAPTIVE
						# NEW v.0.5.4-post: Added to solve Issue #334
						(sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
            exit 0
        	fi
				fi
      else
				if [ "$ON_A_CLOUD" -eq "0" ]; then
					sudo cp /etc/network/interfaces.bak /etc/network/interfaces
				fi
        whiptail --title "TorBox - INFO" --textbox text/no_wlan1-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
      fi
    fi
  ;;

	# Wireless network (through the onboard chip; wlan0 --> wlan1 / eth0 / tun1 / usb0)
  # This menu entry temporary restores the wlan1 / eth0 configuration, which does not survive a reboot!
  # A failsave routine in rc.local takes care of it!
	# eth0 can be replaced by any other ethx
  7)
		if [ "$TORBOX_MINI" -eq "1" ]; then
			INPUT=$(cat text/wlan0_on_torbox_mini-text)
			if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
				if ! ip link | grep wlan0 > /dev/null; then
					INPUT=$(cat text/no_interface_try_it_anyway-text)
					if (whiptail --title "TorBox - INFO" --yesno --defaultno --no-button "BACK!" --yes-button "PUSH IT!" "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX); then
						exitstatus=$?
						# exitstatus = 255 means that the ESC key was pressed / exitstatus = 1 is cancelled
						if [ "$exitstatus" = "1" ] || [ "$exitstatus" = "255" ] ; then	exit 0 ; fi
					else
						trap "bash menu; exit 0" EXIT
						exit 0
					fi
				fi
			else
				trap "bash menu; exit 0" EXIT
				exit 0
			fi
		else
			INPUT=$(cat text/wlan0-text)
    	if (whiptail --title "TorBox - INFO (scroll down!)" --scrolltext --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
				if ! ip link | grep wlan0 > /dev/null; then
					INPUT=$(cat text/no_interface_try_it_anyway-text)
					if (whiptail --title "TorBox - INFO" --yesno --defaultno --no-button "BACK!" --yes-button "PUSH IT!" "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX); then
						exitstatus=$?
						# exitstatus = 255 means that the ESC key was pressed / exitstatus = 1 is cancelled
						if [ "$exitstatus" = "1" ] || [ "$exitstatus" = "255" ] ; then exit 0 ; fi
					else
						trap "bash menu; exit 0" EXIT
						exit 0
					fi
				fi
			else
				trap "bash menu; exit 0" EXIT
				exit 0
			fi
		fi
		select_eth_client
		clear
		display_net_reconfiguration_info
		# NEW v.0.5.4-post
		# We will first copy the configuration files and than try to reconfigure the network (all done in hostapd_fallback_komplex_new)
		# In case of a restart, the correct configuration should prevent a lock-out and apply the changed configuration
		# WARNING: This is the only menu entry, which needs an WiFi adapter or a Ethernet connection with the client - a lock out is possible
		echo -e "${YELLOW}[+] Step 1 of 2 ...${NOCOLOR}"
		if ip link | grep usb0  > /dev/null; then
			nohup bin/hostapd_fallback_komplex_new wlan1 $INTERFACE > /dev/null 2>&1 &
		else
			nohup bin/hostapd_fallback_komplex_new wlan1 $INTERFACE notusb0 > /dev/null 2>&1 &
		fi
		pid=$!
		wait $pid
		echo -e "${YELLOW}[+] Step 2 of 2 done${NOCOLOR}"
		sleep 2
		if ip link | grep wlan0 > /dev/null; then
			ask_captive
    	if ip link | grep wlan1 | grep "state UP" ; then
    		WLAN1_IP=$(ip addr show wlan1 | grep -w 192.168.42.1)
    		clear
    	fi
			if ip link | grep $INTERFACE | grep "state UP" ; then
				ETH_IP=$(ip addr show $INTERFACE | grep -w 192.168.43.1)
      	clear
      fi
			# The state of tun1 is always UNKNOWN
			# TUN1_IP is used for tun1 (TorBox on a Cloud) and usb0 (TorBox mini)
			if ip link | grep tun1 | grep "state UNKNOWN" ; then
				TUN1_IP=$(ip addr show tun1 | grep -w 192.168.44.1)
      	CLIENT_IFACE_TUN1_IP="tun1"
				clear
      else
      	TUN1_IP=$(ip addr show usb0 | grep -w 192.168.44.1)
      	CLIENT_IFACE_TUN1_IP="usb0"
      	clear
			fi
			# Configure torrc depending on the present interfaces
			if [ -z "$WLAN1_IP" ] && [ -z "$ETH_IP" ] && [ -z "$TUN1_IP" ] && [ "$ON_A_CLOUD" -eq "0" ]; then
				sudo cp /etc/network/interfaces.bak /etc/network/interfaces
				whiptail --title "TorBox - INFO" --textbox text/no_interface-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
			else
				torrc_reset
				if [ -n "$WLAN1_IP" ] ; then WLAN1_exists; fi
				if [ -n "$ETH_IP" ] ; then ETH_exists; fi
				if [ -n "$TUN1_IP" ] ; then TUN1_exists; fi
				sudo sed -i "s/^INTERNET_IFACE=.*/INTERNET_IFACE=wlan0/" ${RUNFILE}
      	# The following line should minimize those cases where a restart is required.
      	if [ "$ON_A_CLOUD" -eq "0" ]; then
					sudo systemctl restart isc-dhcp-server
				fi
      	if [ -z "$INTERNETSELECTION" ]; then
					sudo bash bin/set_interfaces_2 wlan0 wlan1 $INTERFACE $CLIENT_IFACE_TUN1_IP $CAPTIVE
					# NEW v.0.5.4-post: Added to solve Issue #334
					# By default, the menu script should save iptables (if not on a cloud) and let rc.local heal a lock-out situation, if "WLAN_FAILSAFE=1". It is clearly stated
					# in the DANGER ZONE that disabling the AP on wlan1 failsafe can lead to a lock-out situation. Because the setting in the DANGER ZONE was added
					# later, it might not taken into account in the menu script.
					(sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
        	clear
        	restarting_tor menu
					trap
        	exit 0
      	else
					sudo bash bin/set_interfaces_2 wlan0 wlan1 $INTERFACE $CLIENT_IFACE_TUN1_IP $CAPTIVE
					# NEW v.0.5.4-post: Added to solve Issue #334
					(sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
        	exit 0
      	fi
			fi
		else
			if [ "$ON_A_CLOUD" -eq "0" ]; then
				sudo cp /etc/network/interfaces.bak /etc/network/interfaces
			fi
			whiptail --title "TorBox - INFO" --textbox text/no_wlan0-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
		fi
  ;;

	# Cellular or other connections (ppp0; usb0 --> wlan0 / eth0 / tun1) / for Torbox mini (ppp0 --> wlan0 / eth0 / tun1 / usb0)
	# eth0 can be replaced by any other ethx
  8)
		if [ "$TORBOX_MINI" -eq "1" ]; then
			INPUT=$(cat text/ppp0_on_torbox_mini-text)
			if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
				:
			else
				trap "bash menu; exit 0" EXIT
				exit 0
			fi
		else
			INPUT=$(cat text/ppp0-text)
    	if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
				:
			else
				trap "bash menu; exit 0" EXIT
				exit 0
			fi
		fi
		NOUSB0="0"
		if [ "$TORBOX_MINI" -eq "0" ]; then
			select_eth_client
			clear
    	display_net_reconfiguration_info
			# NEW v.0.5.4-post
			echo -e "${YELLOW}[+] Step 1 of 2 ...${NOCOLOR}"
			# Because usb0 is a potential Internet ressource, usb0 cannot be a client interfaces --> notusb0
			nohup bin/hostapd_fallback_komplex_new wlan0 $INTERFACE notusb0 /dev/null 2>&1 &
			pid=$!
			wait $pid
			echo -e "${YELLOW}[+] Step 2 of 2 done${NOCOLOR}"
			sleep 2
			ask_captive
			# The interface to the Internet is called usb0
    	if ip link | grep usb0 > /dev/null; then
      	if ip link | grep wlan0 | grep "state UP" ; then
        	WLAN0_IP=$(ip addr show wlan0 | grep -w 192.168.42.1)
        	clear
      	fi
				if ip link | grep $INTERFACE | grep "state UP" ; then
					ETH_IP=$(ip addr show $INTERFACE | grep -w 192.168.43.1)
        	clear
      	fi
				# The state of tun1 is always UNKNOWN
				# TUN1_IP is used for tun1 (TorBox on a Cloud) and usb0 (TorBox mini)
				if ip link | grep tun1 | grep "state UNKNOWN" ; then
					TUN1_IP=$(ip addr show tun1 | grep -w 192.168.44.1)
        	CLIENT_IFACE_TUN1_IP="tun1"
					clear
      	else
        	TUN1_IP=$(ip addr show usb0 | grep -w 192.168.44.1)
        	CLIENT_IFACE_TUN1_IP="usb0"
        	clear
				fi
				# Configure torrc depending on the present interfaces
				if [ -z "$WLAN0_IP" ] && [ -z "$ETH_IP" ] && [ -z "$TUN1_IP" ] && [ "$ON_A_CLOUD" -eq "0" ]; then
					sudo cp /etc/network/interfaces.bak /etc/network/interfaces
					whiptail --title "TorBox - INFO" --textbox text/no_interface-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
				else
					torrc_reset
					if [ -n "$WLAN0_IP" ] ; then WLAN0_exists; fi
					if [ -n "$ETH_IP" ] ; then ETH_exists; fi
					if [ -n "$TUN1_IP" ] ; then TUN1_exists; fi
					sudo sed -i "s/^INTERNET_IFACE=.*/INTERNET_IFACE=usb0/" ${RUNFILE}
      		# The following line should minimize those cases where a restart is required.
        	if [ "$ON_A_CLOUD" -eq "0" ]; then
						sudo systemctl restart isc-dhcp-server
					fi
      		if [ -z "$INTERNETSELECTION" ]; then
						sudo bash bin/set_interfaces_2 usb0 wlan0 $INTERFACE $CLIENT_IFACE_TUN1_IP $CAPTIVE
          	(sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
          	clear
          	restarting_tor menu
						trap
          	exit 0
      		else
						sudo bash bin/set_interfaces_2 usb0 wlan0 $INTERFACE $CLIENT_IFACE_TUN1_IP $CAPTIVE
						# NEW v.0.5.4-post: Added to solve Issue #334
						(sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
          	exit 0
      		fi
				fi
			else
				NOUSB0="1"
			fi
		fi
		# The interface to the Internet is called pppd
		if [ "$TORBOX_MINI" -eq "1" ]; then
			select_eth_client
			clear
			display_net_reconfiguration_info
			# NEW v.0.5.4-post
			echo -e "${YELLOW}[+] Step 1 of 2 ...${NOCOLOR}"
			nohup bin/hostapd_fallback_komplex_new wlan0 $INTERFACE /dev/null 2>&1 &
			pid=$!
			wait $pid
			echo -e "${YELLOW}[+] Step 2 of 2 done${NOCOLOR}"
			sleep 2
			ask_captive
		fi
		if [ "$TORBOX_MINI" -eq "1" ] || [ "$NOUSB0" -eq "1" ]; then
  		if pgrep -f "pppd"; then
      	if (whiptail --title "TorBox - INFO" --no-button "CONTINUE" --yes-button "RESET" --yesno "\nThe PPP connection is already up!\n\nDo you want to reset the PPP connection or continue with the already existing one?" $MENU_HEIGHT_15 $MENU_WIDTH); then
					clear
					echo -e "${RED}[+] Reset PPP connection (you may check the output for errors)${NOCOLOR}"
					echo -e "${RED}[+] If stuck, please press q${NOCOLOR}"
					echo ""
					trap "bash menu; exit 0" SIGINT
					stty intr q
					sudo bin/poff
					sleep 5
					sudo pon
					echo ""
					sleep 2
					read -n 1 -s -r -p "Press any key to continue"
					stty intr ^c
					trap
				fi
      else
      	echo -e "${RED}[+] Activating PPP connection (you may check the output for errors)${NOCOLOR}"
      	echo -e "${RED}[+] If stuck, please press q${NOCOLOR}"
      	echo ""
				trap "bash menu; exit 0" SIGINT
				stty intr q
				if pgrep -f "pppd"; then
					sudo bin/poff
					sleep 5
				fi
      	sudo pon
      	echo ""
      	sleep 2
      	read -n 1 -s -r -p "Press any key to continue"
				stty intr ^c
				trap
			fi
      if ip link | grep ppp0 > /dev/null; then
      	if ip link | grep wlan0 | grep "state UP" ; then
        	WLAN0_IP=$(ip addr show wlan0 | grep -w 192.168.42.1)
        	clear
      	fi
				if ip link | grep $INTERFACE | grep "state UP" ; then
					ETH_IP=$(ip addr show $INTERFACE | grep -w 192.168.43.1)
        	clear
      	fi
				# The state of tun1 is always UNKNOWN
        # TUN1_IP is used for tun1 (TorBox on a Cloud) and usb0 (TorBox mini)
  			if ip link | grep tun1 | grep "state UNKNOWN" ; then
  				TUN1_IP=$(ip addr show tun1 | grep -w 192.168.44.1)
        	CLIENT_IFACE_TUN1_IP="tun1"
  				clear
      	else
        	TUN1_IP=$(ip addr show usb0 | grep -w 192.168.44.1)
        	CLIENT_IFACE_TUN1_IP="usb0"
        	clear
  			fi
				# Configure torrc depending on the present interfaces
				if [ -z "$WLAN0_IP" ] && [ -z "$ETH_IP" ] && [ -z "$TUN1_IP" ] && [ "$ON_A_CLOUD" -eq "0" ]; then
					sudo cp /etc/network/interfaces.bak /etc/network/interfaces
					whiptail --title "TorBox - INFO" --textbox text/no_interface-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
				else
					torrc_reset
					if [ -n "$WLAN0_IP" ] ; then WLAN0_exists; fi
					if [ -n "$ETH_IP" ] ; then ETH_exists; fi
					if [ -n "$TUN1_IP" ] ; then TUN1_exists; fi
					sudo sed -i "s/^INTERNET_IFACE=.*/INTERNET_IFACE=ppp0/" ${RUNFILE}
    			# The following line should minimize those cases where a restart is required.
        	if [ "$ON_A_CLOUD" -eq "0" ]; then
						sudo systemctl restart isc-dhcp-server
					fi
        	if [ -z "$INTERNETSELECTION" ]; then
						sudo bash bin/set_interfaces_2 ppp0 wlan0 $INTERFACE $CLIENT_IFACE_TUN1_IP $CAPTIVE
          	(sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
          	clear
          	restarting_tor menu
						trap
        		exit 0
      		else
						sudo bash bin/set_interfaces_2 ppp0 wlan0 $INTERFACE $CLIENT_IFACE_TUN1_IP $CAPTIVE
						# NEW v.0.5.4-post: Added to solve Issue #334
						(sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
          	exit 0
      		fi
				fi
    	else
				if [ "$ON_A_CLOUD" -eq "0" ]; then
					sudo cp /etc/network/interfaces.bak /etc/network/interfaces
				fi
      	whiptail --title "TorBox - INFO" --textbox text/no_ppp0-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
    	fi
  	fi
  ;;

	# Tethering or USB ethernet adapter (eth1 --> wlan0 / eth0 / usb0 / tun1)
  9)
    INPUT=$(cat text/tethering-text)
    if (whiptail --title "TorBox - INFO (scroll down!)" --scrolltext --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
			if ! ip link | grep eth1 > /dev/null; then
				INPUT=$(cat text/no_interface_try_it_anyway-text)
				if (whiptail --title "TorBox - INFO" --yesno --defaultno --no-button "BACK!" --yes-button "PUSH IT!" "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX); then
					exitstatus=$?
					# exitstatus = 255 means that the ESC key was pressed / exitstatus = 1 is cancelled
					if [ "$exitstatus" = "1" ] || [ "$exitstatus" = "255" ] ; then exit 0 ; fi
				else
					trap "bash menu; exit 0" EXIT
					exit 0
				fi
			fi
      display_net_reconfiguration_info
			# NEW v.0.5.4-post
			echo -e "${YELLOW}[+] Step 1 of 2 ...${NOCOLOR}"
			if ip link | grep usb0 > /dev/null; then
				nohup bin/hostapd_fallback_komplex_new wlan0 eth0 > /dev/null 2>&1 &
			else
				nohup bin/hostapd_fallback_komplex_new wlan0 eth0 notusb0 > /dev/null 2>&1 &
			fi
			pid=$!
			wait $pid
			echo -e "${YELLOW}[+] Step 2 of 2 done${NOCOLOR}"
			sleep 2
			if ip link | grep eth1 > /dev/null; then
				ask_captive
        if ip link | grep wlan0 | grep "state UP" ; then
          WLAN0_IP=$(ip addr show wlan0 | grep -w 192.168.42.1)
          clear
        fi
        if ip link | grep eth0 | grep "state UP" ; then
          ETH_IP=$(ip addr show eth0 | grep -w 192.168.43.1)
          clear
        fi
				# The state of tun1 is always UNKNOWN
				# TUN1_IP is used for tun1 (TorBox on a Cloud) and usb0 (TorBox mini)
				if ip link | grep tun1 | grep "state UNKNOWN" ; then
					TUN1_IP=$(ip addr show tun1 | grep -w 192.168.44.1)
          CLIENT_IFACE_TUN1_IP="tun1"
					clear
        else
          TUN1_IP=$(ip addr show usb0 | grep -w 192.168.44.1)
          CLIENT_IFACE_TUN1_IP="usb0"
          clear
				fi
				# Configure torrc depending on the present interfaces
				if [ -z "$WLAN0_IP" ] && [ -z "$ETH_IP" ] && [ -z "$TUN1_IP" ] && [ "$ON_A_CLOUD" -eq "0" ] ; then
					sudo cp /etc/network/interfaces.bak /etc/network/interfaces
					whiptail --title "TorBox - INFO" --textbox text/no_interface-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
				else
					torrc_reset
					if [ -n "$WLAN0_IP" ] ; then WLAN0_exists; fi
					if [ -n "$ETH_IP" ] ; then ETH_exists; fi
					if [ -n "$TUN1_IP" ] ; then TUN1_exists; fi
					sudo sed -i "s/^INTERNET_IFACE=.*/INTERNET_IFACE=eth1/" ${RUNFILE}
        	# The following line should minimize those cases where a restart is required.
					if [ "$ON_A_CLOUD" -eq "0" ]; then
						sudo systemctl restart isc-dhcp-server
					fi
        	if [ -z "$INTERNETSELECTION" ]; then
          	sudo bash bin/set_interfaces_2 eth1 wlan0 eth0 $CLIENT_IFACE_TUN1_IP $CAPTIVE
            (sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
            clear
						trap
            restarting_tor menu
            exit 0
        	else
          	sudo bash bin/set_interfaces_2 eth1 wlan0 eth0 $CLIENT_IFACE_TUN1_IP $CAPTIVE
						# NEW v.0.5.4-post: Added to solve Issue #334
						(sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
	          exit 0
        	fi
				fi
      else
				if [ "$ON_A_CLOUD" -eq "0" ]; then
					sudo cp /etc/network/interfaces.bak /etc/network/interfaces
				fi
      	whiptail --title "TorBox - INFO" --textbox text/no_eth1-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
      fi
    fi
  ;;

	# Over a VPN connection (tun0 --> wlan0 / eth0 / usb0 / tun1)
  10)
    INPUT=$(cat text/tun0-text)
    if (whiptail --title "TorBox - INFO (scroll down!)" --scrolltext --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
			tun0up=$(ip link | grep tun0)
			if [ -z "$tun0up" ] ; then
        clear
				online_check default 0 0
				if [ $OCHECK == 0 ]; then
					INPUT=$(cat text/tun0-not_connected-text)
					if (whiptail --title "TorBox - INFO" --yesno --no-button "CONTINUE" --yes-button "CONNECT WLAN" --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
						display_net_reconfiguration_info
						# NEW v.0.5.4-post
						echo -e "${YELLOW}[+] Step 1 of 2 ...${NOCOLOR}"
						if ip link | grep usb0 > /dev/null; then
							nohup bin/hostapd_fallback_komplex_new wlan0 eth0 > /dev/null 2>&1 &
						else
							nohup bin/hostapd_fallback_komplex_new wlan0 eth0 notusb0 > /dev/null 2>&1 &
						fi
						pid=$!
						wait $pid
						echo -e "${YELLOW}[+] Step 2 of 2 done${NOCOLOR}"
						sleep 2
						ask_captive
						# set_interfaces_2 will do the captive portal handling, if necessary and start TWM, but it will not restart tor
						sudo bash bin/set_interfaces_2 wlan1 wlan0 eth0 $CAPTIVE 0
			  	fi
				fi
				connecting_to_VPN
				sleep 3
			fi
			if ip link | grep tun0 > /dev/null; then
        if ip link | grep wlan0 | grep "state UP" ; then
          WLAN0_IP=$(ip addr show wlan0 | grep -w 192.168.42.1)
          clear
        fi
        if ip link | grep eth0 | grep "state UP" ; then
          ETH_IP=$(ip addr show eth0 | grep -w 192.168.43.1)
          clear
        fi
				# The state of tun1 is always UNKNOWN
				# TUN1_IP is used for tun1 (TorBox on a Cloud) and usb0 (TorBox mini)
				if ip link | grep tun1 | grep "state UNKNOWN" ; then
					TUN1_IP=$(ip addr show tun1 | grep -w 192.168.44.1)
          CLIENT_IFACE_TUN1_IP="tun1"
					clear
        else
          TUN1_IP=$(ip addr show usb0 | grep -w 192.168.44.1)
          CLIENT_IFACE_TUN1_IP="usb0"
          clear
				fi
				# Configure torrc depending on the present interfaces
				if [ -z "$WLAN0_IP" ] && [ -z "$ETH_IP" ] && [ -z "$TUN1_IP" ] && [ "$ON_A_CLOUD" -eq "0" ]; then
					sudo cp /etc/network/interfaces.bak /etc/network/interfaces
					whiptail --title "TorBox - INFO" --textbox text/no_interface-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
				else
					torrc_reset
					if [ -n "$WLAN0_IP" ] ; then WLAN0_exists; fi
					if [ -n "$ETH_IP" ] ; then ETH_exists; fi
					if [ -n "$TUN1_IP" ] ; then TUN1_exists; fi
					sudo sed -i "s/^INTERNET_IFACE=.*/INTERNET_IFACE=tun0/" ${RUNFILE}
        	# The following line should minimize those cases where a restart is required.
        	if [ "$ON_A_CLOUD" -eq "0" ]; then
						sudo systemctl restart isc-dhcp-server
					fi
        	if [ -z "$INTERNETSELECTION" ]; then
						# NEW v.0.5.4-post: Added to solve Issue #342
						sudo bash bin/set_interfaces_2 tun0 wlan0 eth0 $CLIENT_IFACE_TUN1_IP 0 1
            (sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
            clear
            restarting_tor menu
						trap
            exit 0
        	else
          	sudo bash bin/set_interfaces_2 tun0 wlan0 eth0 $CLIENT_IFACE_TUN1_IP 0 1
						# NEW v.0.5.4-post: Added to solve Issue #334
						(sudo sh -c "iptables-save > /etc/iptables.ipv4.nat") 2>/dev/null
            exit 0
        	fi
				fi
      else
				if [ "$ON_A_CLOUD" -eq "0" ]; then
					sudo cp /etc/network/interfaces.bak /etc/network/interfaces
				fi
        whiptail --title "TorBox - INFO" --textbox text/no_tun0-text $MENU_HEIGHT_15 $MENU_WIDTH_REDUX
      fi
    fi
  ;;

  # Sub-menus
  11) clear; bash menu-bridges;;
  12) clear; bash menu-config;;
	13) clear; bash menu-server;;
	14) clear; bash menu-onion;;
	15)
		clear
		INPUT=$(cat text/menu_danger-text)
		whiptail --title "TorBox - INFO" --msgbox "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH
		bash menu-danger
	;;
  16)
		clear
		screen bash menu-update
    clear
    # shellcheck disable=SC2164
    cd
    # shellcheck disable=SC2164
    cd torbox
  ;;

	# How to support Tor
	17)
		clear
		INPUT=$(cat text/support-tor-text)
		whiptail --title "TorBox - INFO" --msgbox "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH
	;;

	# How to support TorBox
	18)
		clear
		INPUT=$(cat text/support-torbox-text)
		whiptail --title "TorBox - INFO" --msgbox "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH
	;;

  # Reboot or Shotdown TorBox
  19)
		clear
    CHOICE_EXIT=$(whiptail --nocancel --title "TorBox v.0.5.5 - EXIT MENU" --menu "Reboot or Shutdown (ESC to quit)" 10 $MENU_WIDTH_REDUX 2 \
    "  1" "REBOOT   TorBox" \
    "  2" "SHUTDOWN TorBox" \
    3>&1 1>&2 2>&3)
    if [ "$CHOICE_EXIT" != " " ]; then
      if [ $CHOICE_EXIT = 1 ]; then
        clear
        echo -e "${RED}[+] Rebooting TorBox...${NOCOLOR}"
        echo -e "${YELLOW}[!] If you have activated Bridges or Bridge Relay, these services will be reactivated automatically after the restart.${NOCOLOR}"
        echo ""
        sync
        sleep 2
        sudo reboot
        exit 0
      elif [ $CHOICE_EXIT = 2 ]; then
        clear
        echo -e "${RED}[+] Shutting down TorBox...${NOCOLOR}"
        echo -e "${YELLOW}[!] If you have activated Bridges or Bridge Relay, these services will be reactivated automatically after the restart.${NOCOLOR}"
        echo -e "${YELLOW}[!] You can disconnect TorBox from the power source in about 30 second...${NOCOLOR}"
        echo ""
        sync
        sleep 2
        sudo shutdown -h now
        exit 0
      fi
    fi
  ;;

#  *)
#    clear
		#Probabaly only under Ubuntu shell output line separator fails to get reset: https://superuser.com/questions/1650758/how-does-the-shell-output-line-separator-fail-to-get-reset
#		if [ "$CHECK_OS" == "Ubuntu" ]; then stty sane; fi
#    PROMPTTEXT=$(cat text/prompt-text)
#    echo -e "$PROMPTTEXT"
#    echo " "
#    exit 0
esac
bash menu
