#!/bin/bash
# shellcheck disable=SC2001,SC2010,SC2012,SC2016,SC2062,SC2128,SC2178

# 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
#
# The code in this script is an adaption from the incredible OnionJuggler project.
# Copyright (C) 2022 OnionJuggler developers
# Github: https://github.com/nyxnor/onionjuggler
#
# 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 will set up a public .onion site, optionally manage the client
# access authorization and help to share resources over Onion services.
#
# SYNTAX
# ./menu-onion [<SELECTION>]
# <SELECTION> 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=13
#
# How many lines are only for decoration and spaces?
NO_SPACER=5
#
#Set the the variables for the menu
MENU_WIDTH=80
MENU_WIDTH_REDUX=60
MENU_HEIGHT_25=25
MENU_HEIGHT_20=20
MENU_HEIGHT_15=15
# 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'

#Other variables
TORRC="/etc/tor/torrc"
BAK="/etc/tor/torrc.bak"
TOR_USER="debian-tor"
DATA_DIR_ROOT="/var/lib"
DATA_DIR="${DATA_DIR_ROOT}/tor"
DATA_DIR_OS="${DATA_DIR}/services"
CLIENT_ONION_AUTH_DIR="${DATA_DIR}/onion_auth"
WEBSERVER="nginx"
WEBSITE_DIR="/var/www"
NGINX_DIR="/etc/nginx"
TORBOX_PATH="/home/torbox/torbox"
RUNFILE="${TORBOX_PATH}/run/torbox.run"
SELECTION=$1
ENTRY_NUMBERS=0

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

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

# This function imports the configuration and makes some preparations
# TOGGLE16 / TOGGLE17 represents the status of the Onion Servcie mode
read_config()
{
  MODE_OS=$(grep "^HiddenServiceDir" ${TORRC})
  if [ -n "$MODE_OS" ]; then
    TOGGLE16="Onion Service ON!"
    TOGGLE16b="ON"
    TOGGLE17b="OFF"
  else
    TOGGLE16="Onion Service OFF!"
    TOGGLE16b="OFF"
    TOGGLE17b="ON"
  fi
}

# This function creates a list of all onione services
# Syntax create_service_list [<only_with_clients>]
# [<only_with_clients>] will provide a list with services that have at least one client
# Following variables can be used:
# $SERVICE_NAME_LIST is a list with the onion service names (can be separate by newlines (/n))
create_service_list()
{
  ONLY_WITH_CLIENTS=$1
  SERVICE_NAME_LIST=$(sudo -u "${TOR_USER}" ls "${DATA_DIR_OS}")
  if [ "$ONLY_WITH_CLIENTS" == "only_with_clients" ]; then
    i=0
    for SERVICE_NAME in $SERVICE_NAME_LIST; do
      CLIENT_EXIST="$(sudo -u "${TOR_USER}" ls "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/" | sed "s/\.auth//g")"
      if [ -n "${CLIENT_EXIST}" ]; then
        i=$((i+1))
        ONION_HOSTNAME="$(sudo -u "${TOR_USER}" grep -s ".onion" "${DATA_DIR_OS}/${SERVICE_NAME}/hostname")"
        ONION_HOSTNAME_WITHOUT_ONION=${ONION_HOSTNAME%.onion}
        if [ $i == 1 ]; then SERVICE_LIST="$(printf "%s\n" "${SERVICE_NAME}")"
        else SERVICE_LIST="$(printf "%s\n%s\n" "${SERVICE_LIST}" "${SERVICE_NAME}")"; fi
      fi
    done
    SERVICE_NAME_LIST=${SERVICE_LIST}
  fi
  clear
}

# This function saves the clients names that are inside the <HiddenServiceDir>/authorized_clients/ in list format (CLIENT1,CLIENT2,...)
# Syntax create_client_list [<SERVICE_NAME>]
# Following variables can be used:
# $CLIENT_NAME_LIST is a list with the onion service names (SERV1,SERV2,...)
# $CLIENT_COUNT number of items in the $CLIENT_NAME_LIST
create_client_list()
{
  SERVICE_NAME="${1}"
  CLIENT_COUNT=0
  CLIENT_NAME_LIST="$(printf %s"$(sudo -u "${TOR_USER}" ls "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/")" | sed "s/\.auth//g" | tr "\n" ", ")"
  CLIENT_COUNT="$(printf %s"${CLIENT_NAME_LIST}" | tr -dc "," | wc -c)"
  CLIENT_COUNT=$((CLIENT_COUNT+1))
}

# This function lists the services which have an nginx configuration
# Syntax create_web_service_list <TFS>
# <TFS> 0 -> only shared folders; 1 -> only TFS; 2 -> only TCS; else -> shared folders, TFS and TCS
# Following variables can be used:
# WEB_SERVICE_NAME_LIST is a list with the service names which shares a folder through Nginx (SERV1,SERV2,...)
# VIRTPORT_WEB_SERVICE_LIST is a list with shows on which port the folder is shared through Nginx
# WEB_SERVICE_NAME_FOR_MENU is a list which combines the service name with the port number, which is used by the menu

create_web_service_list(){
	TFS="${1}"
	WEB_SERVICE_NAME_LIST_WITH_PORT=""
	if [ "$TFS" == "0" ]; then WEB_SERVICE_NAME_LIST_WITH_PORT=$(sudo ls ${NGINX_DIR}/sites-enabled/ | grep "\-onion.conf" | sed "s/-onion.conf//g")
	elif [ "$TFS" == "1" ]; then WEB_SERVICE_NAME_LIST_WITH_PORT=$(sudo ls ${NGINX_DIR}/sites-enabled/ | grep "\-onion-filesharing.conf" | sed "s/-onion-filesharing.conf//g")
	elif [ "$TFS" == "2" ]; then WEB_SERVICE_NAME_LIST_WITH_PORT=$(sudo ls ${NGINX_DIR}/sites-enabled/ | grep "\-onion-chatsecure.conf" | sed "s/-onion-chatsecure.conf//g")
	else WEB_SERVICE_NAME_LIST_WITH_PORT=$(sudo ls ${NGINX_DIR}/sites-enabled/ | grep "\-onion.conf" | sed "s/-onion.conf//g"; sudo ls ${NGINX_DIR}/sites-enabled/ | grep "\-onion-filesharing.conf" | sed "s/-onion-filesharing.conf//g"; sudo ls ${NGINX_DIR}/sites-enabled/ | grep "\-onion-chatsecure.conf" | sed "s/-onion-chatsecure.conf//g"); fi
	i=0
	for WEB_SERVICE_NAME in $WEB_SERVICE_NAME_LIST_WITH_PORT; do
		i=$((i+1))
		WEB_SERVICE_NAME=$(rev <<< ${WEB_SERVICE_NAME})
		VIRTPORT_WEB_SERVICE=$(cut -d '-' -f1 <<< ${WEB_SERVICE_NAME} | rev)
		WEB_SERVICE_NAME=$(cut -d '-' -f2- <<< ${WEB_SERVICE_NAME} | rev)
		if [ $i == 1 ]; then
			# % represent a space
			WEB_SERVICE_NAME_FOR_MENU="$(printf "%s\n" "${WEB_SERVICE_NAME}%Port:%$VIRTPORT_WEB_SERVICE")"
			WEB_SERVICE_NAME_LIST="$(printf "%s\n" "${WEB_SERVICE_NAME}")"
			VIRTPORT_WEB_SERVICE_LIST="$(printf "%s\n" "${VIRTPORT_WEB_SERVICE}")"
		else
			WEB_SERVICE_NAME_FOR_MENU="$(printf "%s\n%s\n" "${WEB_SERVICE_NAME_FOR_MENU}" "${WEB_SERVICE_NAME}%Port:%$VIRTPORT_WEB_SERVICE")"
			WEB_SERVICE_NAME_LIST="$(printf "%s\n%s\n" "${WEB_SERVICE_NAME_LIST}" "${WEB_SERVICE_NAME}")"
			VIRTPORT_WEB_SERVICE_LIST="$(printf "%s\n%s\n" "${VIRTPORT_WEB_SERVICE_LIST}" "${VIRTPORT_WEB_SERVICE}")"
		fi
	done
}

# This function will generate a new key pair (public and private key) for an operator).
# As a client of an Onion Service, generate a key pair, from which you can give the public key to an Onion Operator.
# Syntax client_auth_on <ONION_HOSTNAME> [<CLIENT_PRIV_KEY>]
client_auth_on()
{
  ONION_HOSTNAME="${1}"
  CLIENT_PRIV_KEY="${2}"
	## removes protocol such as http(s)://, ssh:// and git:// from the front of the address and trailing / at the end of the onion to clean it and only show the hostname (address.onion)
	ONION_HOSTNAME="$(printf %s"${ONION_HOSTNAME}\n" | sed "s|.*://||" | sed "s|/$||")"
	ONION_HOSTNAME_WITHOUT_ONION="${ONION_HOSTNAME%.onion}"
	[ "${ONION_HOSTNAME_WITHOUT_ONION%%*[^a-z2-7]*}" ] || error_onion_invalid 1
	[ "${#ONION_HOSTNAME_WITHOUT_ONION}" = "56" ] || error_onion_invalid 2
	echo -e "${RED}[+] Activating client Onion Service authorization... ${SERVICE_NAME}...${NOCOLOR}"
	(sudo sed -i "s/^#ClientOnionAuthDir/ClientOnionAuthDir/g" ${TORRC}) 2>/dev/null
	[ -d "${CLIENT_ONION_AUTH_DIR}" ] || sudo -u "${TOR_USER}" mkdir -p "${CLIENT_ONION_AUTH_DIR}"
	if [ -z "${CLIENT_PRIV_KEY}" ]; then
		## Generate pem and derive pub and priv keys
		openssl genpkey -algorithm x25519 -out /tmp/k1.prv.pem
		grep -v "PRIVATE KEY" /tmp/k1.prv.pem | base64pem -d | tail --bytes=32 | base32 | sed 's/=//g' > /tmp/k1.prv.key
		openssl pkey -in /tmp/k1.prv.pem -pubout | grep -v "PUBLIC KEY" | base64pem -d | tail -c 32 | base32 | sed 's/=//g' > /tmp/k1.pub.key
		## save variables
		CLIENT_PUB_KEY=$(cat /tmp/k1.pub.key)
    CLIENT_PRIV_KEY=$(cat /tmp/k1.prv.key)
		CLIENT_PRIV_KEY_CONFIG="${ONION_HOSTNAME_WITHOUT_ONION}:descriptor:x25519:${CLIENT_PRIV_KEY}"
		CLIENT_PUB_KEY_CONFIG="descriptor:x25519:${CLIENT_PUB_KEY}"
		## Delete pem and keys
		sudo rm -f /tmp/k1.pub.key /tmp/k1.prv.key /tmp/k1.prv.pem
		# Client side configuration
		printf %s"${CLIENT_PRIV_KEY_CONFIG}\n" | sudo tee "${CLIENT_ONION_AUTH_DIR}/${ONION_HOSTNAME_WITHOUT_ONION}.auth_private" >/dev/null
		sudo chown debian-tor:debian-tor "${CLIENT_ONION_AUTH_DIR}/${ONION_HOSTNAME_WITHOUT_ONION}.auth_private"
		# Output
		echo -e "${YELLOW}[!] Client side authorization is now configured!${NOCOLOR}"
		echo -e "${RED}[+] Below are the generated keys:${NOCOLOR}"
		echo ""
		echo -e "${YELLOW}Service address                       = ${RED}${ONION_HOSTNAME}${NOCOLOR}"
		echo -e "${YELLOW}Private key (saved)                   = ${RED}${CLIENT_PRIV_KEY}${NOCOLOR}"
		echo -e "${YELLOW}Private key configuration             = ${RED}${CLIENT_PRIV_KEY_CONFIG}${NOCOLOR}"
		echo -e "${YELLOW}Public key (for the service operator) = ${RED}${CLIENT_PUB_KEY}${NOCOLOR}"
		echo -e "${YELLOW}Public key configuration              = ${RED}${CLIENT_PUB_KEY_CONFIG}${NOCOLOR}"
		echo ""
		echo -e "${NOCOLOR}Now it depends on the service operator to authorize your client public key"
		echo -e "${NOCOLOR}Send the public key to the onion service operator of ${YELLOW}${ONION_HOSTNAME}${NOCOLOR} with following instruction:"
		echo ""
		echo -e ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
		echo -e "Please, create a file with the client name (eg. alice) using the suffix '.auth' (eg. alice.auth) inside the folder"
		echo -e "'<HiddenServiceDir>/authorized_clients/' where the service hostname is ${YELLOW}${ONION_HOSTNAME}${NOCOLOR} and restart tor by using this command:"
		echo -e "${YELLOW}printf '${CLIENT_PUB_KEY_CONFIG}' | sudo tee /var/lib/tor/${ONION_HOSTNAME}/authorized_clients/alice.auth${NOCOLOR}"
		echo -e "${YELLOW}sudo chown -R debian-tor:debian-tor /var/lib/tor/${ONION_HOSTNAME}/authorized_clients/alice.auth${NOCOLOR}"
		echo -e "${YELLOW}sudo systemctl restart tor${NOCOLOR}"
		printf "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
		echo ""
		read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
  else
		# Client side configuration
		CLIENT_PRIV_KEY_CONFIG="${ONION_HOSTNAME_WITHOUT_ONION}:descriptor:x25519:${CLIENT_PRIV_KEY}"
		printf %s"${CLIENT_PRIV_KEY_CONFIG}\n" | sudo tee "${CLIENT_ONION_AUTH_DIR}/${ONION_HOSTNAME_WITHOUT_ONION}.auth_private" >/dev/null
		sudo chown debian-tor:debian-tor "${CLIENT_ONION_AUTH_DIR}/${ONION_HOSTNAME_WITHOUT_ONION}.auth_private"
		# Output
		echo -e "${YELLOW}[!] Client side authorization is now configured!${NOCOLOR}"
		echo ""
		echo -e "${YELLOW}Service address                       = ${RED}${ONION_HOSTNAME}${NOCOLOR}"
		echo -e "${YELLOW}Private key (saved)                   = ${RED}${CLIENT_PRIV_KEY}${NOCOLOR}"
		echo -e "${YELLOW}Private key configuration             = ${RED}${CLIENT_PRIV_KEY_CONFIG}${NOCOLOR}"
		echo ""
		echo -e "${NOCOLOR}As you inserted the private key manually, it ise expected that you have already sent/received the public key to/from the Onion Service operator."
		echo ""
		read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
  fi
	clear
	echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
	sudo systemctl restart tor
	echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
	sleep 2
}

# This function shows the .onion address and all necessary information to use it.
# Syntax show_onion_address <service_name>
show_onion_address()
{
  SERVICE_NAME="${1}"
  test_service_exists "${SERVICE_NAME}"
  echo ""
  echo -e "${YELLOW}Service name    = ${RED}${SERVICE_NAME}${NOCOLOR}"
  echo ""
  qrencode -m 2 -t ANSIUTF8 "${ONION_HOSTNAME}"
  echo ""
  echo -e "${YELLOW}Service address = ${RED}${ONION_HOSTNAME}${NOCOLOR}"
  find_virtport "${SERVICE_NAME}"
  echo -e "${YELLOW}Virtual port    = ${RED}${VIRTPORT}${NOCOLOR}"
  if sudo grep -qc "^HiddenServiceDir .*/${SERVICE_NAME}$" "${TORRC}"; then
    echo -e "${YELLOW}Status          = ${RED}active${NOCOLOR}"
  else
    echo -e "${YELLOW}Status          = ${YELLOW}inactive${NOCOLOR}"
  fi
  create_client_list "${SERVICE_NAME}"
  echo ""
  echo -e "${YELLOW}Client access information${NOCOLOR}"
  if [ ! -z "${CLIENT_NAME_LIST}" ]; then
    echo -e "${YELLOW}Clients         = ${RED}${CLIENT_NAME_LIST} (${CLIENT_COUNT})${NOCOLOR}"
    for AUTH in $(sudo -u "${TOR_USER}" ls "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/"); do
      echo -e "${YELLOW}File name       : ${RED}${AUTH}${NOCOLOR}"
      echo -e "${YELLOW}Content         : ${RED}$(sudo -u "${TOR_USER}" grep "descriptor:x25519:" "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/${AUTH}")${NOCOLOR}"
    done
  else
    echo -e "${RED}If activated, currently, this Onion Service will be ${YELLOW}PUBLIC${RED} available.${NOCOLOR}"
    echo "If you want to restrict client's access, you have to control the client access by"
    echo "generating a key pair (menu entry 7), sending the client his private key or"
    echo "registering a client's public key (menu entry 8) if he is providing you with it."
  fi
	# This produces WEB_SERVICE_NAME_LIST with all available shared folder and TFS locations
	create_web_service_list
  echo ""
  echo -e "${YELLOW}Shared folder / TFS and TCS information${NOCOLOR}"
	if [[ ! -z "$WEB_SERVICE_NAME_LIST" && ( -L "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf" || -L "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" || -L "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf" ) ]]; then
		[ -L "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf" ] && SHARED_FOLDER=$(grep "root" "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf" | sed -e 's/^[[:space:]]*//' | cut -d " " -f2 | cut -d  ";" -f1)
		[ -L "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" ] && SHARED_FOLDER=$(grep "# Path to shared dir" "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" | sed -e 's/^[[:space:]]*//' | cut -d " " -f2 | cut -d  ";" -f1)
		[ ! -z "$SHARED_FOLDER" ] && echo -e "${RED}Shared folder   = ${YELLOW}$SHARED_FOLDER${NOCOLOR}"
		if grep -q -c "^TFS-${SERVICE_NAME}" ${RUNFILE}; then echo -e "${YELLOW}TFS is configured ${RED}for this service address. You can change that with menu entry 18. If started, it is accessible${NOCOLOR}"
		elif grep -q -c "^TCS-${SERVICE_NAME}" ${RUNFILE}; then echo -e "${YELLOW}TCS is configured ${RED}for this service address. You can change that with menu entry 19. If started, it is accessible${NOCOLOR}"
		else echo -e "${RED}The folder is shared by Nginx. You can change that with menu entry 17. If started, it is accessible${NOCOLOR}"; fi
		if [ "$VIRTPORT" == "80" ]; then echo -e "${RED}through the service address ${YELLOW}${ONION_HOSTNAME}${NOCOLOR}"
		else echo -e "${RED}through the service address ${YELLOW}${ONION_HOSTNAME}:${VIRTPORT}${NOCOLOR}"; fi
	else
    echo -e "${RED}Currently, the Onion Service above is ${YELLOW}not accessible${NOCOLOR}."
    echo -e "To change that, you have to start sharing a folder, configuring TFS or TCS (menu entry 17, 18 or 19)."
  fi
  echo ""
  read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
  clear
}

# This function configures the TFS in the run file and starts TFS.
# Syntax configure_tfs
# <choice>: from the selection if downloads and/or uploads are allowed
# This function needs the following special variables:
# CHOICE, TFS_PATH, SERVICE_NAME
configure_tfs()
{
	allow_ud=0
	if [ ! -z "$CHOICE3" ]; then
		mapfile -t CHOICE3 <<< "$CHOICE3"
		for ARGUMENT in "${CHOICE3[@]}"; do
			if [ $ARGUMENT = 1 ]; then allow_ud=$((allow_ud+1))
			elif [ $ARGUMENT = 2 ]; then allow_ud=$((allow_ud+2))
			fi
		done
	fi
	# This is necessary to work with special characters in sed
	TFS_PATH_STRING="$(<<< "$TFS_PATH" sed -e 's`[][\\/.*^$]`\\&`g')"
	# Using the Anchor in torbox.run
	REPLACE_STRING=$(grep -m 1 "This will configure the TFS program" ${RUNFILE})
	#This is necessary to work with special characters in sed
	REPLACE_STRING="$(<<< "$REPLACE_STRING" sed -e 's`[][\\/.*^$]`\\&`g')"
	# This will give $ONION_HOSTNAME
	test_service_exists $SERVICE_NAME
	# This will give VIRTPORT and HIDDENSSERVICEPORT
	find_virtport $SERVICE_NAME
	# allow_ud = 0 --> neither up- nor download
	# allow_ud = 1 --> only upload
	# allow_ud = 2 --> only download
	# allow_ud = 3 --> up- and download
	if [ "$allow_ud" == "0" ]; then
		echo -e "${YELLOW}[!] NEITHER UP- NOR DOWNLOAD ALLOWED!${NOCOLOR}"
		echo -e "${RED}[+] We will configure TFS for $SERVICE_NAME, anyway.${NOCOLOR}"
		echo -e "${RED}[+] You can change the configuration later with menu entry 2.${NOCOLOR}"
		echo ""
		echo -e "${RED}[+] Configuring TFS...${NOCOLOR}"
    TFS_STRING="\nTFS-$SERVICE_NAME=lib\/fileshare\/tfs -n $SERVICE_NAME -fp $TFS_PATH_STRING -od $ONION_HOSTNAME"
		sudo sed -E -i "s/# This will configure the TFS program.*/$REPLACE_STRING$TFS_STRING/g" "${RUNFILE}"
		echo -e "${RED}[+] Starting TFS (up- and downloading prohibited)...${NOCOLOR}"
		(nohup sudo ./lib/fileshare/tfs -n $SERVICE_NAME -fp $TFS_PATH -od $ONION_HOSTNAME &) &>/dev/null
    printf "${TFS_PATH};x" > "${TFS_PATH}/.access"
	elif [ "$allow_ud" == "1" ]; then
		echo -e "${RED}[+] Configuring TFS...${NOCOLOR}"
		TFS_STRING="\nTFS-$SERVICE_NAME=lib\/fileshare\/tfs -n $SERVICE_NAME -fp $TFS_PATH_STRING -od $ONION_HOSTNAME"
		sudo sed -E -i "s/# This will configure the TFS program.*/$REPLACE_STRING$TFS_STRING/g" "${RUNFILE}"
		echo -e "${RED}[+] Starting TFS (only for uploading)...${NOCOLOR}"
		(nohup sudo ./lib/fileshare/tfs -n $SERVICE_NAME -fp $TFS_PATH -od $ONION_HOSTNAME &) &>/dev/null
    printf "${TFS_PATH};xw" > "${TFS_PATH}/.access"
	elif [ "$allow_ud" == "2" ]; then
		echo -e "${RED}[+] Configuring TFS...${NOCOLOR}"
		TFS_STRING="\nTFS-$SERVICE_NAME=lib\/fileshare\/tfs -n $SERVICE_NAME -fp $TFS_PATH_STRING -od $ONION_HOSTNAME"
		sudo sed -E -i "s/# This will configure the TFS program.*/$REPLACE_STRING$TFS_STRING/g" "${RUNFILE}"
		echo -e "${RED}[+] Starting TFS (only for downloading)...${NOCOLOR}"
		(nohup sudo ./lib/fileshare/tfs -n $SERVICE_NAME -fp $TFS_PATH -od $ONION_HOSTNAME &) &>/dev/null
		printf "${TFS_PATH};xr" > "${TFS_PATH}/.access"
	elif [ "$allow_ud" == "3" ]; then
		echo -e "${RED}[+] Configuring TFS...${NOCOLOR}"
		TFS_STRING="\nTFS-$SERVICE_NAME=lib\/fileshare\/tfs -n $SERVICE_NAME -fp $TFS_PATH_STRING -od $ONION_HOSTNAME"
		sudo sed -E -i "s/# This will configure the TFS program.*/$REPLACE_STRING$TFS_STRING/g" "${RUNFILE}"
		echo -e "${RED}[+] Starting TFS (only for up- and downloading)...${NOCOLOR}"
		(nohup sudo ./lib/fileshare/tfs -n $SERVICE_NAME -fp $TFS_PATH -od $ONION_HOSTNAME &) &>/dev/null
    printf "${TFS_PATH};xrw" > "${TFS_PATH}/.access"
	fi
	#Configure Nginx
	echo ""
	echo -e "${RED}[+] Sharing the folder ${YELLOW}$TFS_PATH${RED} for the Onion Service named ${YELLOW}$SERVICE_NAME${RED} on port ${YELLOW}$VIRTPORT${RED}"
	(cp "etc/nginx/sites-available/sample-onion-filesharing.conf" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf") >/dev/null
	UNIX_PATH="unix:/var/run/${SERVICE_NAME}-onion"
	HIDDENSSERVICEPORT=$(grep -m 1 "$UNIX_PATH" ${TORRC})
	#Bash specific, but should also work with other shells
	HIDDENSSERVICEPORT=${HIDDENSSERVICEPORT//#}
	TARGET=$(cut -d ' ' -f3 <<< $HIDDENSSERVICEPORT)
	sed -i'' "s|TARGET|${TARGET}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf"
	sed -i'' "s|ONION_HOSTNAME|${ONION_HOSTNAME}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf"
  sed -i'' "s|SERVICE|${SERVICE_NAME}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf"
  # NEW - TorBox v.0.5.5: Not necessary with TFS v.2
  # sed -i'' "s|FOLDER|$TFS_PATH|" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf"
	(sudo mv "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" "/etc/${WEBSERVER}/sites-available/") >/dev/null
	(sudo ln -sf "${NGINX_DIR}/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" ${NGINX_DIR}/sites-enabled/) >/dev/null
	echo -e "${RED}[+] Reloading Nginx to apply new configuration...${NOCOLOR}"
  # sudo systemctl reload nginx will not create new socks !!
	# sudo ls /var/run/ | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
	sudo systemctl reload nginx
	(sudo rm -f "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf") &>/dev/null
}

# This function configures the TCS in the run file and starts TCS.
# Syntax configure_tcs
# This function needs the following special variables:
# SERVICE_NAME
configure_tcs()
{
	# Using the Anchor in torbox.run
	REPLACE_STRING=$(grep -m 1 "This will configure the TCS program" ${RUNFILE})
	#This is necessary to work with special characters in sed
	REPLACE_STRING="$(<<< "$REPLACE_STRING" sed -e 's`[][\\/.*^$]`\\&`g')"
	# This will give $ONION_HOSTNAME
	test_service_exists $SERVICE_NAME
	# This will give VIRTPORT and HIDDENSSERVICEPORT
	find_virtport $SERVICE_NAME
	echo -e "${RED}[+] Configuring TCS...${NOCOLOR}"
	# NEW - TorBox v.0.5.5
	PID_PATH="$TORBOX_PATH/lib/chatsecure/pid"
	(sudo rm $PID_PATH/$SERVICE_NAME.pid) &>/dev/null
	(sudo rm $DB_PATH/$SERVICE_NAME.*) &>/dev/null
	(sudo rm /var/run/tcs_$SERVICE_NAME.sock) &>/dev/null
	#
	TCS_STRING="\nTCS-$SERVICE_NAME=lib\/chatsecure\/tcs -n $SERVICE_NAME -od $ONION_HOSTNAME"
	sudo sed -E -i "s/# This will configure the TCS program.*/$REPLACE_STRING$TCS_STRING/g" "${RUNFILE}"
	echo -e "${RED}[+] Starting TCS...${NOCOLOR}"
	(nohup sudo ./lib/chatsecure/tcs -n $SERVICE_NAME -od $ONION_HOSTNAME &) &>/dev/null
	# Configure Nginx
	echo ""
	echo -e "${RED}[+] Starting TCS for the Onion Service named ${YELLOW}$SERVICE_NAME${RED} on port ${YELLOW}$VIRTPORT${RED}"
	(cp "etc/nginx/sites-available/sample-onion-chatsecure.conf" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf") >/dev/null
	UNIX_PATH="unix:/var/run/${SERVICE_NAME}-onion"
	HIDDENSSERVICEPORT=$(grep -m 1 "$UNIX_PATH" ${TORRC})
	#Bash specific, but should also work with other shells
	HIDDENSSERVICEPORT=${HIDDENSSERVICEPORT//#}
	TARGET=$(cut -d ' ' -f3 <<< $HIDDENSSERVICEPORT)
	sed -i'' "s|TARGET|${TARGET}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf"
	sed -i'' "s|ONION_HOSTNAME|${ONION_HOSTNAME}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf"
	sed -i'' "s|SERVICE|${SERVICE_NAME}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf"
	(sudo mv "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf" "/etc/${WEBSERVER}/sites-available/") >/dev/null
	(sudo ln -sf "${NGINX_DIR}/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf" ${NGINX_DIR}/sites-enabled/) >/dev/null
	echo -e "${RED}[+] Reloading Nginx to apply new configuration...${NOCOLOR}"
  # sudo systemctl reload nginx will not create new socks !!
	# sudo ls /var/run/ | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
	sudo systemctl reload nginx
	(sudo rm -f "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf") &>/dev/null
}

# This function creates WEB_SERVICE_NAME_LIST with all available shared folder, TFS and TCS configurations for Nginx
# config_shared_folders_TFS_and_TCS
config_shared_folders_TFS_and_TCS()
{
	WEB_SERVICE_NAME_LIST=""
	create_web_service_list 0
	if [ ! -z "$WEB_SERVICE_NAME_LIST" ]; then
		echo -e "${RED}[+] ${YELLOW}Found shared folder(s)...    ${RED}Nginx reloaded to make them available.${NOCOLOR}"
		# sudo systemctl reload nginx will not create new socks !!
		# sudo ls /var/run/ | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
		sudo systemctl reload nginx
	fi
	WEB_SERVICE_NAME_LIST=""
	create_web_service_list 1
	if [ ! -z "$WEB_SERVICE_NAME_LIST" ]; then
		sudo ./bin/start_tfs initial
		echo -e "${RED}[+] ${YELLOW}Found a TFS configuration... ${RED}Nginx reloaded to make them available.${NOCOLOR}"
	fi
	WEB_SERVICE_NAME_LIST=""
	create_web_service_list 2
	if [ ! -z "$WEB_SERVICE_NAME_LIST" ]; then
		sudo ./bin/start_tcs initial
		echo -e "${RED}[+] ${YELLOW}Found a TCS configuration... ${RED}Nginx reloaded to make them available.${NOCOLOR}"
	fi
	echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
	sudo systemctl restart tor
	echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
	echo ""
	read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
	clear
}

# This function checks if an Onion Service is configured and exit when this is not the case
# Syntax error_no_onion_service
error_no_onion_service()
{
	if [ -z "${SERVICE_NAME_LIST}" ]; then
		echo -e "${YELLOW}[!] NO ONION SERVICES AVAILABLE!!${NOCOLOR}"
		echo -e "${RED}[+] There are no onion services configured, yet. You have to create them first (menu entry 2).${NOCOLOR}"
		echo ""
		read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
		clear
		trap "bash menu-onion; exit 0" EXIT
		exit 1
	fi
}

# This function checks if there is an Onion Service with client authorization and exit when this is not the case
# Syntax error_no_authorization
error_no_authorization()
{
	if [ -z "${SERVICE_NAME_LIST}" ]; then
		echo -e "${YELLOW}[!] NO ONION SERVICES WITH CLIENT AUTHORIZATION AVAILABLE!!${NOCOLOR}"
		echo -e "${RED}[+] There are no onion services with client authorization configured, yet.${NOCOLOR}"
		echo ""
		read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
		clear
		trap "bash menu-onion; exit 0" EXIT
		exit 1
	fi
}

# This function displays an error message and exit if the Onion domain is already used
# Syntax error_already_used
error_already_used()
{
	echo -e "${YELLOW}[!] THE ONION DOMAIN IS ALREADY USED!${NOCOLOR}"
	echo -e "${RED}[+] You cannot use the same Onion domain for two different sharing options!${NOCOLOR}"
	echo -e "${RED}[+] You have to create another Onion domain.${NOCOLOR}"
	echo ""
	read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
	clear
	trap "bash menu-onion; exit 0" EXIT
	exit 1
}

# This function displays an error message and exit if the Onion domain is invalid
# Syntax error_onion_invalid <REASON>
# <REASON>: 1 -> it is not within base32 alphabet lower-case encoding
#           2 -> is different than 56 characters
error_onion_invalid()
{
	REASON=$1
	echo -e "${YELLOW}[!] THE ONION DOMAIN IS INVALID!${NOCOLOR}"
	[ "$REASON" = "1" ] && echo -e "${RED}[+] The entered Onion domain is invalid, it is not within base32 alphabet lower-case encoding [a-z][2-7]${NOCOLOR}"
	[ "$REASON" = "2" ] && echo -e "${RED}[+] Onion domain is invalid, LENGTH=${#ONION_HOSTNAME_WITHOUT_ONION} is different than 56 characters (<56-char-base32>.onion)${NOCOLOR}"
	echo ""
	read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
	clear
	trap "bash menu-onion; exit 0" EXIT
	exit 1
}

# This function displays an error message and exit if there is no client authorization files
# Syntax error_no_client_authorization
error_no_client_authorization()
{
	echo -e "${YELLOW}[!] NO CLIENT AUTHORIZATION AVAILABLE!!${NOCOLOR}"
	echo ""
	read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
	clear
	trap "bash menu-onion; exit 0" EXIT
	exit 1
}

# This function displays an error message and exit if there is no folder actively shared
# Syntax error_no_folder_shared
error_no_folder_shared()
{
	echo -e "${YELLOW}[!] There are no folders actively shared, yet!${NOCOLOR}"
	echo -e "${RED}[+] Start sharing a folder on an Onion domain, first (entry 1) ${NOCOLOR}"
	echo ""
	read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
	clear
	trap "bash menu-onion; exit 0" EXIT
	exit 1
}

# This function displays an error message and exit if TFS is not running
# Syntax error_no_folder_shared
error_no_tfs_running()
{
	echo -e "${YELLOW}[!] TORBOX FILESHARING (TFS) IS NOT RUNNING!!!${NOCOLOR}"
	echo ""
	read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
	clear
	trap "bash menu-onion; exit 0" EXIT
	exit 1
}

# This function displays an error message and exit if TCS is not running
# Syntax error_no_folder_shared
error_no_tcs_running()
{
	echo -e "${YELLOW}[!] There is no TCS started on an Onion domain, yet!${NOCOLOR}"
	echo -e "${RED}[+] Start TCS on an Onion domain, first (entry 1) ${NOCOLOR}"
	echo ""
	read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
	clear
	trap "bash menu-onion; exit 0" EXIT
	exit 1
}


######## PREPARATIONS ########
read_config
clear

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

# NEW v.0.5.4: To be sure that the necessary directories exist
if ! sudo test -d /var/lib/tor/services; then
  (sudo mkdir /var/lib/tor/services) 2>/dev/null
  sudo chown -R debian-tor:debian-tor /var/lib/tor/services
  sudo chmod -R go-rwx /var/lib/tor/services
fi
if ! sudo test -d /var/lib/tor/onion_auth; then
  (mkdir /var/lib/tor/onion_auth) 2>/dev/null
  chown -R debian-tor:debian-tor /var/lib/tor/onion_auth
  chmod -R go-rwx /var/lib/tor/onion_auth
fi

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

#This is used, if the script is started with an defined menu selection
CHOICE=()
if [ ! -z "$SELECTION" ]; then
  CHOICE=$SELECTION
else
  CHOICE=$(whiptail --cancel-button "Back" --title "TorBox v.0.5.5 - ONION SERVICES" --menu "Choose an option (ESC -> back to the main menu)         ${TOGGLE16}" $MENU_HEIGHT $MENU_WIDTH $MENU_LIST_HEIGHT \
  "==" "=============================================[Informational]===" \
  " 1" "RUN AN ONION SERVICE - READ ME FIRST"  \
  "==" "============================================[Onion Services]==="  \
  " 2" "Toggle Onion Service Mode from $TOGGLE16b to $TOGGLE17b"  \
  " 3" "Create or reactivate an Onion Service"  \
  " 4" "List all Onion Services"  \
  " 5" "Delete or deactivate an Onion Service"  \
  " 6" "Enter the advanced tor configuration editor"  \
  "==" "===================================[Control client's access]==="  \
  " 7" "Generate a new key pair (public and private key) for a client"  \
  " 8" "Register a client with its public key"  \
  " 9" "Edit a client's authorization"  \
  "10" "List all clients for a particular Onion Service"  \
  "11" "Remove a client's authorization"  \
	"==" "===============================[Manage my access to a server]==="  \
	"12" "Generate a new key pair (public and private key) for a server"  \
	"13" "Register a server with its private key"  \
	"14" "Edit a server access authorization"  \
	"15" "List all server access authorizations"  \
	"16" "Remove a server access authorization"  \
  "==" "=================================================[Share it!]==="  \
  "17" "Start/stop sharing a folder on an Onion domain and list them"  \
	"18" "Start/stop upload or/and download files (TorBox File Sharing)"  \
	"19" "Start/stop TorBox Chat Secure" \
  3>&1 1>&2 2>&3)
	exitstatus=$?
	# exitstatus == 255 means that the ESC key was pressed
	[ "$exitstatus" == "255" ] && exit 0
fi

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

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

  # Toggle Onion Service Mode  ON or OFF
  2)
    if [ -z "$MODE_OS" ]; then
      INPUT=$(cat text/activate-onion_service-text)
      if (whiptail --title "TorBox - INFO" --defaultno --no-button "DON'T CHANGE" --yes-button "ACTIVATE" --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
        if grep -q "#HiddenServiceDir" ${TORRC}; then
					SERVICE_NAME_LIST=$(grep "^#HiddenServiceDir" ${TORRC} | sed "s/^#HiddenServiceDir \/var\/lib\/tor\/services\///g")
          echo -e "${RED}[+] Activating Onion Service Mode...${NOCOLOR}"
          (sudo sed -i "s/^#HiddenServiceDir/HiddenServiceDir/g" ${TORRC}) &>/dev/null
          (sudo sed -i "s/^#HiddenServicePort/HiddenServicePort/g" ${TORRC}) &>/dev/null
          (sudo sed -i "s/^##HiddenServiceDir/#HiddenServiceDir/g" ${TORRC}) &>/dev/null
          (sudo sed -i "s/^##HiddenServicePort/#HiddenServicePort/g" ${TORRC}) &>/dev/null
					echo -e "${RED}[+] Reactivating following Onion Service name(s):\n\n${YELLOW}${SERVICE_NAME_LIST}\n${NOCOLOR}"
					config_shared_folders_TFS_and_TCS
					i=0
					#This is necessary to work with special characters in sed
					DATA_DIR_OS_STRING="$(<<< "${DATA_DIR_OS}" sed -e 's`[][\\/.*^$]`\\&`g')"
					SERVICE_NAME_LIST=$(grep "HiddenServiceDir" ${TORRC} | sed "s/^#HiddenServiceDir $DATA_DIR_OS_STRING\///g" | sed "s/^HiddenServiceDir $DATA_DIR_OS_STRING\///g")
					for SERVICE_NAME in $(printf %s"${SERVICE_NAME_LIST}" | tr "," " "); do
						i=$((i+1))
						clear
						if [ "$i" -gt "1" ]; then
							echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included (page $i)${NOCOLOR}"
						else
							echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included${NOCOLOR}"
						fi
						show_onion_address $SERVICE_NAME
					done
        else
          trap "bash menu-onion; exit 0" SIGINT
          echo -e "${YELLOW}[!] THERE ARE NO ONION SERVICES CONFIGURED!!${NOCOLOR}"
					stty intr q
          read -n 1 -s -r -p $'\e[1;31m[+] We will create now an Onion Service for you. If you agree, press ENTER, otherwise press q. \e[0m'
					stty intr ^c
          clear
          bash menu-onion 3; exit 0
        fi
      fi
    else
      INPUT=$(cat text/deactivate-onion_server-text)
      if (whiptail --title "TorBox - INFO" --defaultno --no-button "DON'T CHANGE" --yes-button "DEACTIVATE" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
        clear
				HIDDENSERVICENAMES=$(grep "^HiddenServiceDir" ${TORRC} | sed "s/^HiddenServiceDir \/var\/lib\/tor\/services\///g")
        echo -e "${RED}[+] Deactivating Onion Service Mode...${NOCOLOR}"
        (sudo sed -i "s/^#HiddenServiceDir/##HiddenServiceDir/g" ${TORRC}) 2>/dev/null
        (sudo sed -i "s/^#HiddenServicePort/##HiddenServicePort/g" ${TORRC}) 2>/dev/null
        (sudo sed -i "s/^HiddenServiceDir/#HiddenServiceDir/g" ${TORRC}) 2>/dev/null
        (sudo sed -i "s/^HiddenServicePort/#HiddenServicePort/g" ${TORRC}) 2>/dev/null
				if [ ! -z "$HIDDENSERVICENAMES" ]; then echo -e "${RED}[+] Deactivating following Onion Service name(s):\n\n${YELLOW}${HIDDENSERVICENAMES}\n${NOCOLOR}"; fi
				# This will only kill the TFS/TCS process, but not touch torbox.run and the Nginx configuration for later reactivation
				TFS_NAME_LIST=$(ls lib/fileshare/pid/ | sed "s/.pid//")
				if [ ! -z "$TFS_NAME_LIST" ]; then
					for TFS_NAME in $TFS_NAME_LIST; do
						echo -e "${RED}[+] Stopping TFS named ${YELLOW}${TFS_NAME}${RED} on Onion domain...${NOCOLOR}"
						PID=$(cat /home/torbox/torbox/lib/fileshare/pid/${TFS_NAME}.pid)
						(sudo kill $PID) &>/dev/null
					done
				fi
				TCS_NAME_LIST=$(ls lib/chatsecure/pid/ | sed "s/.pid//")
				if [ ! -z "$TCS_NAME_LIST" ]; then
					for TCS_NAME in $TCS_NAME_LIST; do
						echo -e "${RED}[+] Stopping TCS named ${YELLOW}${TCS_NAME}${RED} on Onion domain...${NOCOLOR}"
						PID=$(cat /home/torbox/torbox/lib/chatsecure/pid/${TCS_NAME}.pid)
						(sudo kill $PID) &>/dev/null
					done
				fi
        echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
        sudo systemctl restart tor
        echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
				echo ""
				read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
				clear
      fi
    fi
    read_config
  ;;

  # Create or reactivate an Onion Service
  3)
    INPUT=$(cat text/enable-onion_service-text)
    if (whiptail --title "TorBox - INFO (scroll down!)" --scrolltext --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
			#This is necessary to work with special characters in sed
			DATA_DIR_OS_STRING="$(<<< "${DATA_DIR_OS}" sed -e 's`[][\\/.*^$]`\\&`g')"
			SERVICE_NAME_LIST=$(grep "#HiddenServiceDir" ${TORRC} | sed "s/^##HiddenServiceDir $DATA_DIR_OS_STRING\///g" | sed "s/^#HiddenServiceDir $DATA_DIR_OS_STRING\///g")
			if [ ! -z "$SERVICE_NAME_LIST" ]; then
				if (whiptail --title "TorBox - INFO" --yesno --no-button "CREATE A NEW ONE" --yes-button "REACTIVATE" "There are deactivated Onion Services configured.\n\nDo you want to reactivate an already configured Onion Service or create a new one?" $MENU_HEIGHT_15 $MENU_WIDTH); then
					exitstatus=$?
					service_menu checklist "$SERVICE_NAME_LIST"
					if [ "$exitstatus" = "0" ]; then
						i=0
						for SERVICE_NAME in $SERVICE_NAME_LIST; do
							i=$((i+1))
							for SERVICE_NUMBER in $ENTRY_NUMBERS; do
								if [ "$i" = "$SERVICE_NUMBER" ]; then
									echo -e "${RED}[+] Reactivating ${YELLOW}${SERVICE_NAME}${NOCOLOR}"
									SERVICE_NAME_STRING="$(<<< "${SERVICE_NAME}" sed -e 's`[][\\/.*^$]`\\&`g')"
									sudo sed -i "s/^##HiddenServiceDir/#HiddenServiceDir/g" ${TORRC} 2>/dev/null
		              sudo sed -i "s/^##HiddenServicePort/#HiddenServicePort/g" ${TORRC} 2>/dev/null
		              sudo sed -i "s/^#HiddenServiceDir ${DATA_DIR_OS_STRING}\/${SERVICE_NAME_STRING}/HiddenServiceDir ${DATA_DIR_OS_STRING}\/${SERVICE_NAME_STRING}/g" ${TORRC} 2>/dev/null
									# This gives VIRTPORT, HIDDENSSERVICEPORT
									find_virtport $SERVICE_NAME
									#This is necessary to work with special characters in sed
									HIDDENSSERVICEPORT_STRING="$(<<< "${HIDDENSSERVICEPORT}" sed -e 's`[][\\/.*^$]`\\&`g')"
									sudo sed -i "s/^#$HIDDENSSERVICEPORT_STRING/$HIDDENSSERVICEPORT_STRING/g" ${TORRC}
								fi
							done
						done
						config_shared_folders_TFS_and_TCS
						i=0
						for SERVICE_NAME in $(printf %s"${SERVICE_NAME_LIST}" | tr "," " "); do
							i=$((i+1))
							clear
							if [ "$i" -gt "1" ]; then
								echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included (page $i)${NOCOLOR}"
							else
								echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included${NOCOLOR}"
							fi
							show_onion_address $SERVICE_NAME
						done
						trap "bash menu-onion; exit 0" EXIT
						exit 0
					else
						trap "bash menu-onion; exit 0" EXIT
						exit 0
					fi
				fi
			fi
      SERVICE_NAME=$(whiptail --title "TorBox - INFO" --inputbox "\n\nName your onion service directory in one string and no space (e.g.: torbox.ch):" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 3>&1 1>&2 2>&3)
			exitstatus=$?
			if [ ! -z "${SERVICE_NAME}" ]; then
				if [ ! "${SERVICE_NAME%%*[^a-z0-9_.]*}" ]; then
					clear
					echo -e "${YELLOW}[!] THE SERVICE NAME IS INVALID!!${NOCOLOR}"
					echo -e "${RED}[+] The name can only contain characters in range of: 'a-z', 'A-Z', '_', '.'.${NOCOLOR}"
					sleep 5
					clear
					trap "bash menu-onion; exit 0" EXIT
					exit 1
				fi
        if (grep "^HiddenServiceDir ${DATA_DIR_OS}/${SERVICE_NAME}" "${TORRC}"); then
          clear
          echo -e "${YELLOW}[!] THE SERVICE NAME IS ALREADY USED AND ACTIVATED!!${NOCOLOR}"
          echo -e "${RED}[+] If you want to replace the particular Onion Service, you must first delete it with menu entry 4.${NOCOLOR}"
          sleep 5
          clear
          trap "bash menu-onion; exit 0" EXIT
          exit 1
        fi
        if (grep "#HiddenServiceDir ${DATA_DIR_OS}/${SERVICE_NAME}" "${TORRC}"); then
          clear
          INPUT=$(cat text/reactivate-onion_service-text)
          if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH); then
            exitstatus=$?
            if [ "$exitstatus" = "0" ]; then
              clear
              echo -e "${RED}[+] Reactivating ${YELLOW}${SERVICE_NAME}${NOCOLOR}"
              #This is necessary to work with special characters in sed
              SERVICE_NAME_STRING="$(<<< "${SERVICE_NAME}" sed -e 's`[][\\/.*^$]`\\&`g')"
              sudo sed -i "s/^##HiddenServiceDir/#HiddenServiceDir/g" ${TORRC} 2>/dev/null
              sudo sed -i "s/^##HiddenServicePort/#HiddenServicePort/g" ${TORRC} 2>/dev/null
              sudo sed -i "s/^#HiddenServiceDir ${DATA_DIR_OS_STRING}\/${SERVICE_NAME_STRING}/HiddenServiceDir ${DATA_DIR_OS_STRING}\/${SERVICE_NAME_STRING}/g" ${TORRC} 2>/dev/null
							# This gives VIRTPORT, HIDDENSSERVICEPORT
							find_virtport $SERVICE_NAME
              #This is necessary to work with special characters in sed
              HIDDENSSERVICEPORT_STRING="$(<<< "${HIDDENSSERVICEPORT}" sed -e 's`[][\\/.*^$]`\\&`g')"
              sudo sed -i "s/^#$HIDDENSSERVICEPORT_STRING/$HIDDENSSERVICEPORT_STRING/g" ${TORRC}
							echo -e "${RED}[+] Reactivating following Onion Service name:\n\n${YELLOW}${SERVICE_NAME}\n${NOCOLOR}"
							config_shared_folders_TFS_and_TCS
							echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included${NOCOLOR}"
							show_onion_address $SERVICE_NAME
              trap "bash menu-onion; exit 0" EXIT
              exit 0
            fi
          else
            clear
            echo -e "${YELLOW}[!] THE SERVICE NAME IS ALREADY USED!!${NOCOLOR}"
            echo -e "${RED}[+] If you want to replace the particular Onion Service, you must first delete it with menu entry 4.${NOCOLOR}"
            sleep 5
            clear
            trap "bash menu-onion; exit 0" EXIT
            exit 1
          fi
        fi
        SERVICE_PORTS=$(whiptail --title "Onion Service -- virtual ports" --inputbox "\n\nPlease, configure a virtual port. Usually 80 is just good enough: " $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 80 3>&1 1>&2 2>&3)
				exitstatus=$?
				if [ ! -z "${SERVICE_PORTS}" ]; then
          clear
          echo -e "${RED}[+] Configuring torrc...${NOCOLOR}"
          VIRTPORT=$(cut -d ' ' -f1 <<< $SERVICE_PORTS)
          is_integer "${VIRTPORT}"
					exitstatus=$?
					# exitstatus == 1 means that $VIRTPORT is not an integer
					if [ "$exitstatus" = "1" ]; then
	        	clear
	        	echo -e "${YELLOW}[!] WRONG INPUT - THIS IS NOT AN INTEGER!${NOCOLOR}"
	        	sleep 5
	        	clear
	        	trap "bash menu-onion; exit 0" EXIT
	        	exit 1
					fi
					# Using the Anchor in torrc
          REPLACE_STRING=$(grep -m 1 "This will configure the Onion Services" ${TORRC})
					#This is necessary to work with special characters in sed
					REPLACE_STRING="$(<<< "$REPLACE_STRING" sed -e 's`[][\\/.*^$]`\\&`g')"
					UNIX_PATH="unix:/var/run/${SERVICE_NAME}-onion"
          SOCK_FILE="${UNIX_PATH}-${VIRTPORT}.sock"
          #This is necessary to work with special characters in sed
          DATA_DIR_OS_STRING="$(<<< "${DATA_DIR_OS}" sed -e 's`[][\\/.*^$]`\\&`g')"
          SERVICE_NAME_STRING="$(<<< "${SERVICE_NAME}" sed -e 's`[][\\/.*^$]`\\&`g')"
          SOCK_FILE_STRING="$(<<< "${SOCK_FILE}" sed -e 's`[][\\/.*^$]`\\&`g')"
					NEW_STRING="\nHiddenServiceDir $DATA_DIR_OS_STRING\/$SERVICE_NAME_STRING\nHiddenServicePort ${VIRTPORT} $SOCK_FILE_STRING"
          #Writing the necessary entries on the right place in torrc (not at the end, because there are already the bridges)
          sudo sed -E -i "s/## This will configure the Onion Services.*/$REPLACE_STRING$NEW_STRING/g" "${TORRC}"
          #Create a folder, which can be published through the Onion Services
          echo -e "${RED}[+] Creating ${YELLOW}$WEBSITE_DIR/$SERVICE_NAME ${RED}which can be shared through the Onion domain.${NOCOLOR}"
          sudo mkdir "$WEBSITE_DIR/$SERVICE_NAME"
					sudo chown torbox:torbox "$WEBSITE_DIR/$SERVICE_NAME"
          echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
          sudo systemctl restart tor
          echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
					echo ""
					read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
					clear
          echo -e "${RED}[+] This is the information for your newly created Onion Service${NOCOLOR}"
          show_onion_address $SERVICE_NAME
        fi
      else
				# exitstatus == 255 means that the ESC key was pressed
				if [ $exitstatus != 255 ]; then
        	clear
        	echo -e "${YELLOW}[!] NO SERVICE NAME GIVEN!!${NOCOLOR}"
        	echo -e "${RED}[+] You didn't define a service name! You have to define a service name!${NOCOLOR}"
        	sleep 5
        	clear
        	trap "bash menu-onion; exit 0" EXIT
        	exit 1
				fi
      fi
    fi
  ;;

  # List all Onion Services
  4)
    create_service_list
		error_no_onion_service
    i=0
    for SERVICE_NAME in $(printf %s"${SERVICE_NAME_LIST}" | tr "," " "); do
      i=$((i+1))
      clear
			if [ "$i" -gt "1" ]; then
				echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included (page $i)${NOCOLOR}"
			else
				echo -e "${RED}[+] This is the list of all existing Onion Services on your system - shared folders, TFS and TCS included${NOCOLOR}"
			fi
      show_onion_address $SERVICE_NAME
    done
  ;;

  # Delete or deactivate Onion Service
  5)
    create_service_list
		error_no_onion_service
    INPUT=$(cat text/disable-onion_service-text)
    if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH); then
      exitstatus=$?
      service_menu checklist "$SERVICE_NAME_LIST"
      if [ "$exitstatus" = "0" ]; then
        i=0
        for SERVICE_NAME in $SERVICE_NAME_LIST; do
          i=$((i+1))
          for SERVICE_NUMBER in $ENTRY_NUMBERS; do
            if [ "$i" = "$SERVICE_NUMBER" ]; then
              INPUT="Would you DEACTIVATE or DELETE the Onion Service named ->$SERVICE_NAME<-?\n\nDEACTIVATE means that nothing will be deleted and the Onion Service can be reactivatet later.\n\nDELETE means that the Onion Service, client authorizations, configuration for TFS & TCS and the HOSTED DATA IN THE SHARED FOLDER will be deleted PERMANENTLY! The service CANNOT be reactivated and the same onion address CANNOT be used anymore.\n"
              if (whiptail --title "TorBox - INFO" --defaultno --no-button "DEACTIVATE" --yes-button "DELETE" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
                exitstatus=$?
              else
                exitstatus=$?
              fi
              clear
							#This is necessary to work with special characters in sed
							DATA_DIR_OS_STRING="$(<<< "${DATA_DIR_OS}" sed -e 's`[][\\/.*^$]`\\&`g')"
							SERVICE_NAME_STRING="$(<<< "${SERVICE_NAME}" sed -e 's`[][\\/.*^$]`\\&`g')"
							# This gives VIRTPORT, HIDDENSSERVICEPORT and UNIX_PATH
							find_virtport $SERVICE_NAME
              if [ "$exitstatus" = "0" ]; then
								echo -e "${RED}[+] Stopping TFS and TCS, if active${NOCOLOR}"
								(TFS_NAME_LIST=$(ls lib/fileshare/pid/$SERVICE_NAME.pid)) >/dev/null
								(TCS_NAME_LIST=$(ls lib/chatsecure/pid/$SERVICE_NAME.pid)) >/dev/null
								if [ ! -z "$TFS_NAME_LIST" ]; then stopping_tfs $SERVICE_NAME; fi
								if [ ! -z "$TCS_NAME_LIST" ]; then stopping_tcs $SERVICE_NAME; fi
                echo -e "${RED}[+] Delete Onion Service named ${YELLOW}$SERVICE_NAME${NOCOLOR}"
                (sudo rm -rfv "${DATA_DIR_OS}/${SERVICE_NAME}") &>/dev/null
                sudo sed -i "/HiddenServiceDir ${DATA_DIR_OS_STRING}\/${SERVICE_NAME_STRING}/d" ${TORRC}
                #This is necessary to work with special characters in sed
                HIDDENSSERVICEPORT_STRING="$(<<< "${HIDDENSSERVICEPORT}" sed -e 's`[][\\/.*^$]`\\&`g')"
                sudo sed -i "/$HIDDENSSERVICEPORT_STRING/d" ${TORRC}
								echo -e "${RED}[+] Removing Nginx configuration for shared folders...${NOCOLOR}"
                if [ -f "/etc/nginx/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf" ]; then sudo rm "/etc/nginx/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf"; fi
                if [ -f "/etc/nginx/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion.conf" ]; then sudo rm "/etc/nginx/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion.conf"; fi
                echo ""
								if [ -d "${WEBSITE_DIR}/${SERVICE_NAME}" ]; then
                	echo -e "${RED}[+] Delete shared folder $WEBSITE_DIR/$SERVICE_NAME in 5 seconds...${NOCOLOR}"
                	echo -e "${YELLOW}    Last chance to abbort with q!${NOCOLOR}"
                	trap "bash menu-onion; exit 0" SIGINT
                  stty intr q
                	sleep 1
                	echo -e "1"
                	sleep 1
                	echo -e "2"
                	sleep 1
                	echo -e "3"
                	sleep 1
                	echo -e "4"
                	sleep 1
                	echo -e "5"
                	sleep 1
                  (sudo rm -r $WEBSITE_DIR/$SERVICE_NAME) &>/dev/null
                	echo -e "${RED}[+] Shared folder $WEBSITE_DIR/$SERVICE_NAME is deleted!${NOCOLOR}"
                  sleep 3
									stty intr ^c
									trap
                  clear
								fi
              elif [ "$exitstatus" = "1" ]; then
                echo -e "${RED}[+] Deactivate Onion Service named ${YELLOW}$SERVICE_NAME${NOCOLOR}"
                sudo sed -i "s/^HiddenServiceDir ${DATA_DIR_OS_STRING}\/${SERVICE_NAME_STRING}/#HiddenServiceDir ${DATA_DIR_OS_STRING}\/${SERVICE_NAME_STRING}/g" ${TORRC}
                #This is necessary to work with special characters in sed
                HIDDENSSERVICEPORT_STRING="$(<<< "${HIDDENSSERVICEPORT}" sed -e 's`[][\\/.*^$]`\\&`g')"
                sudo sed -i "s/^$HIDDENSSERVICEPORT_STRING/#$HIDDENSSERVICEPORT_STRING/g" ${TORRC}
								# This will only kill the TFS/TCS process, but not touch torbox.run and the Nginx configuration for later reactivation
								echo -e "${RED}[+] Stopping TFS and TCS, if active${NOCOLOR}"
								TFS_NAME_LIST=$(ls lib/fileshare/pid/$SERVICE_NAME.pid)
								if [ ! -z "$TFS_NAME_LIST" ]; then
									PID=$(cat /home/torbox/torbox/lib/fileshare/pid/${SERVICE_NAME}.pid)
									(sudo kill $PID) &>/dev/null
								fi
								TCS_NAME_LIST=$(ls lib/chatsecure/pid/$SERVICE_NAME.pid)
								if [ ! -z "$TCS_NAME_LIST" ]; then
									PID=$(cat /home/torbox/torbox/lib/chatsecure/pid/${SERVICE_NAME}.pid)
									(sudo kill $PID) &>/dev/null
								fi
              fi
              sleep 2
            fi
          done
        done
        clear
				echo -e "${RED}[+] Reloading Nginx to apply new configuration...${NOCOLOR}"
        # sudo systemctl reload nginx will not create new socks !!
				# sudo ls /var/run/ | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
				sudo systemctl reload nginx
        echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
        sudo systemctl restart tor
        echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
				sleep 5
				read_config
				clear
      fi
    fi
  ;;

  # Enter the advanced configuration editor
  6)
    INPUT=$(cat text/advanced-OS-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 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-onion
      fi
    fi
  ;;

  # Generate a new key pair (public and private key) for a client
  # As the onion service operator, make your Onion Service authenticated by generating a pair or public and private keys
  # The client's pub key is automatically saved inside <HiddenServiceDir>/authorized_clients/alice.auth
  # The client's private key is shown in the screen and the key file deleted
  # The onion service operator should send the private key to the desired client
  7)
    create_service_list
		error_no_onion_service
    INPUT=$(cat text/generate-new_key_pair-text)
    if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
      exitstatus=$?
      service_menu checklist "$SERVICE_NAME_LIST"
      if [ "$exitstatus" = "0" ]; then
        i=0
        for SERVICE_NAME in $SERVICE_NAME_LIST; do
          i=$((i+1))
          for SERVICE_NUMBER in $ENTRY_NUMBERS; do
            if [ "$i" = "$SERVICE_NUMBER" ]; then
              CLIENT_NAME_LIST="$(whiptail --title "TorBox - INFO" --inputbox "\n\nType in the client(s) name(s) which you want authorize to acces the onion service(s), delimited by space or/and comma (e.g.: alice, bob, carol):" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 3>&1 1>&2 2>&3)"
              CLIENT_NAME_LIST="$(printf %s"${CLIENT_NAME_LIST}" | tr -s ' ' ',' | tr -s ',' ',')"
              if [ -n "${CLIENT_NAME_LIST}" ]; then
                clear
                for CLIENT_NAME in $(printf %s"${CLIENT_NAME_LIST}" | tr "," " "); do
                  test_service_exists "${SERVICE_NAME}"
                  # Generate pem and derive pub and priv keys
                  openssl genpkey -algorithm x25519 -out /tmp/k1.prv.pem
                  # basez has to be installed
                  grep -v " PRIVATE KEY" /tmp/k1.prv.pem | base64pem -d | tail --bytes=32 | base32 | sed "s/=//g" > /tmp/k1.prv.key
                  openssl pkey -in /tmp/k1.prv.pem -pubout | grep -v " PUBLIC KEY" | base64pem -d | tail --bytes=32 | base32 | sed "s/=//g" > /tmp/k1.pub.key
                  ## save variables
                  CLIENT_PUB_KEY=$(cat /tmp/k1.pub.key)
                  CLIENT_PRIV_KEY=$(cat /tmp/k1.prv.key)
                  ONION_HOSTNAME_WITHOUT_ONION=${ONION_HOSTNAME%.onion}
                  CLIENT_PRIV_KEY_CONFIG="${ONION_HOSTNAME_WITHOUT_ONION}:descriptor:x25519:${CLIENT_PRIV_KEY}"
                  CLIENT_PUB_KEY_CONFIG="descriptor:x25519:${CLIENT_PUB_KEY}"
                  # Server side configuration
                  printf %s"${CLIENT_PUB_KEY_CONFIG}\n" | sudo tee "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/${CLIENT_NAME}.auth" >/dev/null
                  echo -e "${RED}[+] Below are the generated keys:${NOCOLOR}"
                  echo ""
                  echo -e "${YELLOW}Service name                 = ${RED}${SERVICE_NAME}${NOCOLOR}"
                  echo -e "${YELLOW}Client Name                  = ${RED}${CLIENT_NAME}${NOCOLOR}"
                  echo -e "${YELLOW}Service address              = ${RED}${ONION_HOSTNAME}${NOCOLOR}"
                  echo -e "${YELLOW}Public key (saved)           = ${RED}${CLIENT_PUB_KEY}${NOCOLOR}"
                  echo -e "${YELLOW}Public key configuration     = ${RED}${CLIENT_PUB_KEY_CONFIG}${NOCOLOR}"
                  echo -e "${YELLOW}Private key (for the client) = ${RED}${CLIENT_PRIV_KEY}${NOCOLOR}"
                  echo -e "${YELLOW}Private key configuration    = ${RED}${CLIENT_PRIV_KEY_CONFIG}${NOCOLOR}"
                  echo ""
                  echo -e "${YELLOW}Remember:${NOCOLOR} TorBox is now configured with the public key and the client can only access it with the private key."
                  echo -e "          To give him acces, you have to give him his private key. ${YELLOW}No key - no access!${NOCOLOR}"
                  echo ""
                  read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
                  clear
                  ## Delete pem and keys
                  (sudo rm -f /tmp/k1.pub.key /tmp/k1.prv.key /tmp/k1.prv.pem) &>/dev/null
                done
              fi
            fi
          done
        done
				clear
				echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
				sudo systemctl restart tor
				echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
				sleep 2
      fi
    fi
  ;;

  # Register the client with its public key
  # The client sends to the onion service operator a public key, which can be register for a Onion Service
  # The onion service operator doesn't have to send to the client anything - the client has the private key already
  8)
    create_service_list
		error_no_onion_service
    INPUT=$(cat text/register-client_public_key-text)
    if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
      exitstatus=$?
      service_menu menu "$SERVICE_NAME_LIST"
      if [ "$exitstatus" = "0" ]; then
        i=0
        for SERVICE_NAME in $SERVICE_NAME_LIST; do
          i=$((i+1))
          for SERVICE_NUMBER in $ENTRY_NUMBERS; do
            if [ "$i" = "$SERVICE_NUMBER" ]; then
              CLIENT_NAME=$(whiptail --title "TorBox - INFO" --inputbox "\n\nType in the client name which you want authorize to acces the onion service (e.g.: alice):" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 3>&1 1>&2 2>&3)
              exitstatus=$?
              # exitstatus == 255 means that the ESC key was pressed
              if [ "$exitstatus" = "0" ]; then
                CLIENT_PUB_KEY=$(whiptail --title "TorBox - INFO" --inputbox "\n\nType in the client's public key in base32 (for example: BAFYBEICZSSCDSBS7FFQZ55ASQDF3SMV6KLCW3GOFSZVWLYARCI47BGF354):" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 3>&1 1>&2 2>&3)
                if { [ -n "${SERVICE_NAME}" ] && [ -n "${CLIENT_NAME}" ] && [ -n "${CLIENT_PUB_KEY}" ]; }; then
                  clear
                  test_service_exists "${SERVICE_NAME}"
                  ONION_HOSTNAME_WITHOUT_ONION=${ONION_HOSTNAME%.onion}
                  CLIENT_PUB_KEY_CONFIG="descriptor:x25519:${CLIENT_PUB_KEY}"
                  printf %s"${CLIENT_PUB_KEY_CONFIG}" | sudo tee "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/${CLIENT_NAME}".auth >/dev/null
                  echo -e "${RED}[+] Client's public key is now registered for ${YELLOW}$SERVICE_NAME${NOCOLOR}"
                  echo -e "${RED}[+] There is not more to do due the fact that the client has the private key already.${NOCOLOR}"
                  echo ""
                  read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
                  clear
                fi
              fi
            fi
          done
        done
				clear
				echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
				sudo systemctl restart tor
				echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
				sleep 2
      fi
    fi
  ;;

  # Edit a client's authorization
  9)
    create_service_list only_with_clients
		error_no_authorization
    INPUT=$(cat text/edit_clients_authorisation-text)
    if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
      exitstatus=$?
      service_menu menu "$SERVICE_NAME_LIST"
      if [ "$exitstatus" = "0" ]; then
        i=0
        for SERVICE_NAME in $SERVICE_NAME_LIST; do
          i=$((i+1))
          for SERVICE_NUMBER in $ENTRY_NUMBERS; do
            if [ "$i" = "$SERVICE_NUMBER" ]; then
              n=0
              CLIENT_NAME_LIST=$(sudo -u "${TOR_USER}" ls "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/" | sed "s/\.auth//g")
              for CLIENT_NAME in $CLIENT_NAME_LIST; do
                n=$((n+1))
                if [ $n == 1 ]; then CLIENT_LIST="\"==\" \"===============================================================\""; fi
                CLIENT_LIST="$CLIENT_LIST \"${n}\" \"${CLIENT_NAME} \""
              done
              CLIENT_LIST="$CLIENT_LIST \"==\" \"===============================================================\"";
              clear
              if [ $n -gt 11 ]; then n=11; fi
              WHIPTAIL_LINE="whiptail --nocancel --title \"TorBox - INFO\" --menu --separate-output \"\nChoose with SPACE and then press ENTER (ESC -> go back)\" $((n+10)) 80 $((n+2)) ${CLIENT_LIST} 3>&1 1>&2 2>&3"
              CHOICE_CLIENT=$(eval $WHIPTAIL_LINE)
              exitstatus=$?
              CLIENT_NAME_LIST_NUMBERS=$(printf %s"${CHOICE_CLIENT}")
              if [ "$exitstatus" = "0" ]; then
                n=0
                for CLIENT_NAME in $CLIENT_NAME_LIST; do
                  n=$((n+1))
                  for CLIENT_NUMBER in $CLIENT_NAME_LIST_NUMBERS; do
                    if [ "$n" = "$CLIENT_NUMBER" ]; then
                      sudo nano "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/${CLIENT_NAME}.auth"
                    fi
                  done
                done
              fi
            fi
          done
        done
        clear
        echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
        sudo systemctl restart tor
        echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
        sleep 2
      fi
    fi
  ;;

  # List all clients for a particular Onion Service
  10)
    create_service_list only_with_clients
		error_no_authorization
    service_menu checklist "$SERVICE_NAME_LIST" "\nNot listed services don't have any client authorization."
    exitstatus=$?
    if [ "$exitstatus" = "0" ]; then
      i=0
      for SERVICE_NAME in $SERVICE_NAME_LIST; do
        i=$((i+1))
        for SERVICE_NUMBER in $ENTRY_NUMBERS; do
          if [ "$i" = "$SERVICE_NUMBER" ]; then
            clear
            echo -e "${RED}[+] This are the clients for ${YELLOW}$SERVICE_NAME${RED}:${NOCOLOR}"
            echo ""
            create_client_list "${SERVICE_NAME}"
            echo -e "${YELLOW}Clients   = ${RED}${CLIENT_NAME_LIST} (${CLIENT_COUNT})${NOCOLOR}"
            FILE_NAME_LIST=$(sudo -u "${TOR_USER}" ls "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/")
            for FILE_NAME in $FILE_NAME_LIST; do
              echo -e "${YELLOW}File name = ${RED}${FILE_NAME}${NOCOLOR}"
              CONTENT=$(sudo -u "${TOR_USER}" grep "descriptor:x25519:" "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/${FILE_NAME}")
              echo -e "${YELLOW}Content   = ${RED}$CONTENT${NOCOLOR}"
            done
            echo ""
            read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
            clear
          fi
        done
      done
    fi
  ;;

  # Remove a client's authorization
  11)
		# Gives $SERVICE_NAME_LIST
		create_service_list only_with_clients
		error_no_authorization
    INPUT=$(cat text/remove_clients_authorization-text)
    if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
      exitstatus=$?
      service_menu checklist "$SERVICE_NAME_LIST"
      if [ "$exitstatus" = "0" ]; then
        i=0
        for SERVICE_NAME in $SERVICE_NAME_LIST; do
          i=$((i+1))
          for SERVICE_NUMBER in $ENTRY_NUMBERS; do
            if [ "$i" = "$SERVICE_NUMBER" ]; then
              n=0
              CLIENT_NAME_LIST=$(sudo -u "${TOR_USER}" ls "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/" | sed "s/\.auth//g")
              for CLIENT_NAME in $CLIENT_NAME_LIST; do
                n=$((n+1))
                if [ $n == 1 ]; then CLIENT_LIST="\"${n}\" \"${CLIENT_NAME}\" OFF"
              else CLIENT_LIST="$CLIENT_LIST \"${n}\" \"${CLIENT_NAME}\" OFF"; fi
              done
              clear
              if [ $n -gt 11 ]; then n=11; fi
							WHIPTAIL_LINE="whiptail --nocancel --title \"TorBox - INFO\" --checklist --separate-output \"Clients for the Onion Service named $SERVICE_NAME\n\nPlease, use SPACE to select/deselect, ENTER to apply, ESC to go back.\" $((n+11)) 80 $n ${CLIENT_LIST} 3>&1 1>&2 2>&3"
              CHOICE_CLIENT=$(eval $WHIPTAIL_LINE)
              exitstatus=$?
              CLIENT_NAME_LIST_NUMBERS=$(printf %s"${CHOICE_CLIENT}")
              if [ "$exitstatus" = "0" ]; then
                n=0
                for CLIENT_NAME in $CLIENT_NAME_LIST; do
                  n=$((n+1))
                  for CLIENT_NUMBER in $CLIENT_NAME_LIST_NUMBERS; do
                    if [ "$n" = "$CLIENT_NUMBER" ]; then
                      (sudo rm -f "${DATA_DIR_OS}/${SERVICE_NAME}/authorized_clients/${CLIENT_NAME}.auth") &>/dev/null
                      echo -e "${RED}[+] Client named ${YELLOW}$CLIENT_NAME${RED} has been removed from the Onion Service named ${YELLOW}$SERVICE_NAME${NOCOLOR}"
                      sleep 2
                    fi
                  done
                done
                # Show the status of the service after removing the selected authorization(s)
                # This is important to show the user, if the service has to become public
								# This gives VIRTPORT and HIDDENSSERVICEPORT
								find_virtport $SERVICE_NAME
                create_client_list $SERVICE_NAME
                clear
                echo -e "${RED}[+] This is the status of ${YELLOW}$SERVICE_NAME${RED} after altering the client aithorization.${NOCOLOR}"
                echo -e "${RED}[+] Be careful - removing all client authorizations makes the service public. Check below!${NOCOLOR}"
                echo ""
                show_onion_address $SERVICE_NAME
              fi
            fi
          done
        done
        clear
        echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
        sudo systemctl restart tor
        echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
        sleep 2
      fi
    fi
  ;;

	# Generate a new key pair (public and private key) for an operator
	# As a client of an Onion Service, generate a key pair, from which you can give the public key to an Onion Operator.
	12)
		INPUT=$(cat text/generate-new_key_pair_client_auth-text)
		if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
			ONION_HOSTNAME=$(whiptail --title "TorBox - INFO" --inputbox "\n\nEnter the Onion Domain of the service you want to authenticate as a client\n(e.g.: duckduckgogg42xjoc72x3sjasowoarfbgcmvfimaftt6twagswzczad.onion):" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 3>&1 1>&2 2>&3)
			if [ -n "${ONION_HOSTNAME}" ]; then
				clear
				client_auth_on "${ONION_HOSTNAME}"
			fi
		fi
	;;

	# Register an Onion Service with its private key
	13)
		INPUT=$(cat text/register-client_private_key-text)
		if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
			ONION_HOSTNAME=$(whiptail --title "TorBox - INFO" --inputbox "\n\nEnter the Onion Domain of the service you want to authenticate as a client\n(e.g.: duckduckgogg42xjoc72x3sjasowoarfbgcmvfimaftt6twagswzczad.onion):" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 3>&1 1>&2 2>&3)
			CLIENT_PRIV_KEY=$(whiptail --title "TorBox - INFO" --inputbox "\n\nType in the client's private key (e.g: GBJWTYNY5OIQT4BSBP7OY4I7NSTLRDOZNMKSVXEXKGMWPUDPLJRA):" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 3>&1 1>&2 2>&3)
			if { [ -n "${ONION_HOSTNAME}" ] && [ -n "${CLIENT_PRIV_KEY}" ]; }; then
				clear
				client_auth_on "${ONION_HOSTNAME}" "${CLIENT_PRIV_KEY}"
			fi
		fi
	;;

	# Edit a Onion Service access authorization
	14)
		ONION_AUTH_LIST=$(sudo -u "${TOR_USER}" ls "${CLIENT_ONION_AUTH_DIR}"/ | cut -d "." -f1)
		[ -z "$ONION_AUTH_LIST" ] && error_no_client_authorization
		INPUT=$(cat text/edit_clients_authorisation-text)
		if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
			exitstatus=$?
			service_menu menu "$ONION_AUTH_LIST" "Choose an authorization of the Onion Service listed below to edit. (ESC -> go back)"
			# shellcheck disable=SC2154
			if [ "$exitstatus" = "0" ]; then
				i=0
				for ONION_AUTH in $ONION_AUTH_LIST; do
					i=$((i+1))
					for SERVICE_NUMBER in $ENTRY_NUMBERS; do
						if [ "$i" = "$SERVICE_NUMBER" ]; then sudo nano "${CLIENT_ONION_AUTH_DIR}/${ONION_AUTH}.auth_private"; fi
					done
				done
				clear
				echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
				sudo systemctl restart tor
				echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
				sleep 2
			fi
		fi
	;;

	# List all server access authorizations
  15)
		ONION_AUTH_LIST=$(sudo -u "${TOR_USER}" ls "${CLIENT_ONION_AUTH_DIR}"/ | cut -d "." -f1)
		[ -z "$ONION_AUTH_LIST" ] && error_no_client_authorization
		echo -e "${RED}[+] This are your authorizations as a client:${NOCOLOR}"
		echo ""
		for ONION_AUTH in $ONION_AUTH_LIST; do
			echo -e "${YELLOW}Onion Service   = ${RED}${ONION_AUTH}.onion${NOCOLOR}"
			#printf %s"# Content:   $(sudo -u "${TOR_USER}" cat "${CLIENT_ONION_AUTH_DIR}"/"${AUTH}")"
			CONTENT=$(sudo grep "descriptor:x25519:" "${CLIENT_ONION_AUTH_DIR}/${ONION_AUTH}.auth_private")
			echo -e "${YELLOW}Content   = ${RED}$CONTENT${NOCOLOR}"
			echo ""
		done
		echo ""
		read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
		clear
  ;;

	#Remove a server access authorization
	16)
		ONION_AUTH_LIST=$(sudo -u "${TOR_USER}" ls "${CLIENT_ONION_AUTH_DIR}"/ | cut -d "." -f1)
		[ -z "$ONION_AUTH_LIST" ] && error_no_client_authorization
		INPUT=$(cat text/remove_clients_authorization_2-text)
		if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
			exitstatus=$?
			service_menu checklist "$ONION_AUTH_LIST" "Choose the client authorization of the Onion Service(s) listed below to remove. (ESC -> go back)"
			# shellcheck disable=SC2154
			if [ "$exitstatus" = "0" ]; then
				i=0
				for ONION_AUTH in $ONION_AUTH_LIST; do
					i=$((i+1))
					for SERVICE_NUMBER in $ENTRY_NUMBERS; do
						if [ "$i" = "$SERVICE_NUMBER" ]; then
							echo -e "${RED}[+] Remove client authorization for $ONION_AUTH${NOCOLOR}"
							sudo rm -fv "${CLIENT_ONION_AUTH_DIR}/${ONION_AUTH}.auth_private"
						fi
					done
				done
				ONION_AUTH_LIST=$(sudo -u "${TOR_USER}" ls "${CLIENT_ONION_AUTH_DIR}"/ | cut -d "." -f1)
				[ -z "$ONION_AUTH_LIST" ] && error_no_client_authorization
				(sudo sed -i "s/^ClientOnionAuthDir/#ClientOnionAuthDir/g" ${TORRC}) 2>/dev/null
				echo -e "${RED}[+] Restarting Tor!${NOCOLOR}"
				sudo systemctl restart tor
				echo -e "${RED}[+] Restarted Tor succesfully!${NOCOLOR}"
				sleep 2
			fi
		fi
  ;;

  # Serve a website folder on onion domain
  17)
  	INPUT=$(cat text/web_server-text)
  	if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH); then
    	create_service_list
		 	error_no_onion_service
     	clear
      CHOICE2=$(whiptail --cancel-button "Back" --title "TorBox v.0.5.5 - Serve a website folder on onion domain" --menu "Choose an option (ESC -> go back)" 13 $MENU_WIDTH 6  \
      "==" "===============================================================" \
      " 1" "Start sharing a folder on an Onion domain"  \
			" 2" "Change the shared folder of a certain Onion domain" \
      " 3" "Stop sharing a folder on an Onion domain"  \
      " 4" "List of all configured shared folders"  \
      "==" "===============================================================" \
			3>&1 1>&2 2>&3)
			exitstatus=$?
			if [ "$exitstatus" = "0" ]; then
      	 CHOICE2=$(echo "$CHOICE2" | tr -d ' ')
      	 case "$CHOICE2" in

         #Start sharing a folder on onion domain
       	 1)
        	clear
        	service_menu menu "$SERVICE_NAME_LIST"
        	if [ "$exitstatus" = "0" ]; then
          	i=0
          	for SERVICE_NAME in $SERVICE_NAME_LIST; do
            	i=$((i+1))
            	for SERVICE_NUMBER in $ENTRY_NUMBERS; do
              	if [ "$i" = "$SERVICE_NUMBER" ]; then
									# This will give ONION_HOSTNAME
              		test_service_exists $SERVICE_NAME
									# This will give VIRTPORT and HIDDENSSERVICEPORT
									find_virtport $SERVICE_NAME
									# Check if the service/port pair is already used
									[ -f "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf" ] && error_already_used
									[ -f "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" ] && error_already_used
									[ -f "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf" ] && error_already_used
									# Which folder should be shared?
									SUB_FOLDER=$(whiptail --title "TorBox v.0.5.5 - TorBox's File Sharing capabilities" --inputbox "\nWhich folder inside of $WEBSITE_DIR should be shared? (default is $WEBSITE_DIR/${SERVICE_NAME}, however you can chose another folder, e.g ${SERVICE_NAME}/shared or $WEBSITE_DIR/${SERVICE_NAME}/shared):" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 3>&1 1>&2 2>&3)
									if grep "^$WEBSITE_DIR/" <<< "${SUB_FOLDER}"; then SUB_FOLDER=$(grep "^$WEBSITE_DIR/" <<< "${SUB_FOLDER}" | sed 's|^/var/www/||g'); fi
									if grep "^/" <<< "${SUB_FOLDER}"; then SUB_FOLDER=$(grep "^/" <<< "${SUB_FOLDER}" | sed 's|^/||g'); fi
									clear
									# If the user doesn't write something into the dialogue, the default is valid
									if [[ "$SUB_FOLDER" = "" || "$SUB_FOLDER" = " " ]]; then
										SHARE_PATH="$WEBSITE_DIR/$SERVICE_NAME"
									else
										if [[ $SUB_FOLDER == */ ]]; then
												SHARE_PATH="$WEBSITE_DIR/$SUB_FOLDER"
											else
												SHARE_PATH="$WEBSITE_DIR/$SUB_FOLDER/"
											fi
									fi
									if [ ! -d $SHARE_PATH ]; then
										sudo chown torbox:torbox $WEBSITE_DIR
										sudo -utorbox mkdir -p $SHARE_PATH
									fi
									echo -e "${RED}[+] Sharing the folder ${YELLOW}$SHARE_PATH${RED} for the Onion Service named ${YELLOW}$SERVICE_NAME${RED} on port ${YELLOW}$VIRTPORT${RED}"
									(cp "etc/nginx/sites-available/sample-onion.conf" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion.conf") >/dev/null
									UNIX_PATH="unix:/var/run/${SERVICE_NAME}-onion"
									HIDDENSSERVICEPORT=$(grep -m 1 "$UNIX_PATH" ${TORRC})
									#Bash specific, but should also work with other shells
	               	HIDDENSSERVICEPORT=${HIDDENSSERVICEPORT//#}
	               	TARGET=$(cut -d ' ' -f3 <<< $HIDDENSSERVICEPORT)
	               	sed -i'' "s|TARGET|${TARGET}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion.conf"
	               	sed -i'' "s|ONION_HOSTNAME|${ONION_HOSTNAME}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion.conf"
	               	sed -i'' "s|SERVICE|${SERVICE_NAME}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion.conf"
	               	sed -i'' "s|FOLDER|$SHARE_PATH|" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion.conf"
              		(sudo mv "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion.conf" "/etc/${WEBSERVER}/sites-available/") >/dev/null
              		(sudo ln -sf "${NGINX_DIR}/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion.conf" ${NGINX_DIR}/sites-enabled/) >/dev/null
									# Check if the shared folder is empty. If this is the case, the splash screen will be copied into the folder
	               	EMPTY_FOLDER=$(ls -A "$SHARE_PATH")
	               	if [ -z "$EMPTY_FOLDER" ]; then
	                 	(sudo cp "etc/nginx/shared-folder/index.html" "$SHARE_PATH") >/dev/null
	                 	(sudo cp "etc/nginx/shared-folder/tb-001-logo.png" "$SHARE_PATH") >/dev/null
	               	fi
									echo -e "${RED}[+] Reloading Nginx to apply new configuration...${NOCOLOR}"
                  # sudo systemctl reload nginx will not create new socks !!
									# sudo ls /var/run/ | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
									sudo systemctl reload nginx
									(sudo rm -f "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion.conf") &>/dev/null
									echo ""
									if [ "$VIRTPORT" == "80" ]; then echo -e "${YELLOW}Service address (http) = ${RED}${ONION_HOSTNAME}${NOCOLOR}"; else
										echo -e "${YELLOW}Service address (http) = ${RED}${ONION_HOSTNAME}:${VIRTPORT}${NOCOLOR}"
									fi
									echo ""
									read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
									clear
              	fi
            	done
          	done
        	fi
       	;;

				# Change the shared folder of a certain Onion domain
       	2)
					clear
					# This produces WEB_SERVICE_NAME_LIST with all available shared folder and TFS locations
					create_web_service_list 0
					[ -z $WEB_SERVICE_NAME_LIST ] && error_no_folder_shared
					service_menu menu "$WEB_SERVICE_NAME_LIST"
					exitstatus=$?
					if [ "$exitstatus" = "0" ]; then
						i=0
						for SERVICE_NAME in $WEB_SERVICE_NAME_LIST; do
							i=$((i+1))
							for SERVICE_NUMBER in $ENTRY_NUMBERS; do
								if [ "$i" = "$SERVICE_NUMBER" ]; then
									# This will give VIRTPORT and HIDDENSSERVICEPORT
									find_virtport $SERVICE_NAME
									# Old path
									SHARE_PATH=$(grep "root" "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf" | sed -e 's/^[[:space:]]*//' | cut -d " " -f2 | cut -d ";" -f1)
									# Which folder should be shared?
									SUB_FOLDER=$(whiptail --title "TorBox v.0.5.5 - TorBox's File Sharing capabilities" --inputbox "\nWhich folder inside of $WEBSITE_DIR should be shared? Currently: $SHARE_PATH\n\nPress enter or specify a new folder:" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 3>&1 1>&2 2>&3)
									if grep "^$WEBSITE_DIR/" <<< "${SUB_FOLDER}"; then SUB_FOLDER=$(grep "^$WEBSITE_DIR/" <<< "${SUB_FOLDER}" | sed 's|^/var/www/||g'); fi
									if grep "^/" <<< "${SUB_FOLDER}"; then SUB_FOLDER=$(grep "^/" <<< "${SUB_FOLDER}" | sed 's|^/||g'); fi
									clear
									# If the user doesn't write something into the dialogue, the old path is still valid
									if [[ $SUB_FOLDER = "" || $SUB_FOLDER = " " ]]; then
										sleep 1
									else
										SHARE_PATH_OLD=$SHARE_PATH
										if [[ $SUB_FOLDER == */ ]]; then
											SHARE_PATH="$WEBSITE_DIR/$SUB_FOLDER"
										else
											SHARE_PATH="$WEBSITE_DIR/$SUB_FOLDER/"
										fi
									fi
									if [ ! -d $SHARE_PATH ]; then
										sudo chown torbox:torbox $WEBSITE_DIR
										sudo -u torbox mkdir -p $SHARE_PATH
									fi
									if [[ $SUB_FOLDER = "" || $SUB_FOLDER = " " ]]; then
										sleep 1
									else
										#Configure Nginx
										echo ""
										echo -e "${RED}[+] Sharing the folder ${YELLOW}$SHARE_PATH${RED} for the Onion Service named ${YELLOW}$SERVICE_NAME${RED} on port ${YELLOW}$VIRTPORT${RED}"
										sudo rm "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf"
                    # sudo systemctl reload nginx will not create new socks !!
										# sudo ls /var/run/ | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
										sudo systemctl reload nginx
										sudo sed -i "s|$SHARE_PATH_OLD|$SHARE_PATH|g" "${NGINX_DIR}/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion.conf"
										(sudo ln -sf "${NGINX_DIR}/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion.conf" ${NGINX_DIR}/sites-enabled/) >/dev/null
										# Check if the shared folder is empty. If this is the case, the splash screen will be copied into the folder
	              		EMPTY_FOLDER=$(ls -A "$SHARE_PATH")
	              		if [ -z "$EMPTY_FOLDER" ]; then
	               			(sudo cp "etc/nginx/shared-folder/index.html" "$SHARE_PATH") >/dev/null
	               			(sudo cp "etc/nginx/shared-folder/tb-001-logo.png" "$SHARE_PATH") >/dev/null
	             			fi
									fi
									echo ""
									echo -e "${RED}[+] Reloading Nginx to apply new configuration...${NOCOLOR}"
                  # sudo systemctl reload nginx will not create new socks !!
									# sudo ls /var/run/ | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
									sudo systemctl reload nginx
									echo ""
									# This will give ONION_HOSTNAME
									test_service_exists $SERVICE_NAME
									if [ "$VIRTPORT" == "80" ]; then echo -e "${YELLOW}Service address (http) = ${RED}${ONION_HOSTNAME}${NOCOLOR}"; else
										echo -e "${YELLOW}Service address (http) = ${RED}${ONION_HOSTNAME}:${VIRTPORT}${NOCOLOR}"
									fi
									echo ""
									read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
									clear
								fi
							done
						done
					fi
				;;

       	# Stop sharing folder on onion domain
       	3)
         	clear
					# This produces WEB_SERVICE_NAME_LIST with all available shared folder and TFS locations
         	create_web_service_list 0
					[ -z $WEB_SERVICE_NAME_LIST ] && error_no_folder_shared
         	service_menu checklist "$WEB_SERVICE_NAME_LIST"
         	if [ "$exitstatus" = "0" ]; then
         		if [ ! -z "$SERVICE_NAME_LIST" ]; then
           		i=0
           		for SERVICE_NAME in $SERVICE_NAME_LIST; do
             		i=$((i+1))
               	for SERVICE_NUMBER in $ENTRY_NUMBERS; do
               		if [ "$i" = "$SERVICE_NUMBER" ]; then
										x=0
										for VIRTPORT_WEB_SERVICE in $VIRTPORT_WEB_SERVICE_LIST; do
											x=$((x+1))
											if [ "$x" = "$i" ]; then
                 				echo -e "${RED}[+] Stopping sharing the folder ${YELLOW}$WEBSITE_DIR/$SERVICE_NAME${RED} for the Onion Service named ${YELLOW}$SERVICE_NAME${RED} on port ${YELLOW}$VIRTPORT_WEB_SERVICE${NOCOLOR}."
                 				sudo rm -f "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT_WEB_SERVICE}-onion.conf"
                 				sudo rm -f "${NGINX_DIR}/sites-available/${SERVICE_NAME}-${VIRTPORT_WEB_SERVICE}-onion.conf"
											fi
										done
                 	fi
               	done
             	done
             	echo ""
             	echo -e "${RED}[+] Reloading Nginx to apply new configuration...${NOCOLOR}"
              # sudo systemctl reload nginx will not create new socks !!
							# sudo ls /var/run/ | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
							sudo systemctl reload nginx
           		echo ""
           		read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
           	fi
         	fi
       	;;

       	#List of actively shared folders
       	4)
         	clear
					# This produces WEB_SERVICE_NAME_LIST
					create_web_service_list 0
					[ -z $WEB_SERVICE_NAME_LIST ] && error_no_folder_shared
          i=0;
          for WEB_SERVICE_NAME in $WEB_SERVICE_NAME_LIST; do
            i=$((i+1))
						if [ "$i" -gt "1" ]; then
            	echo -e "${YELLOW}[+] This is the list of configured shared folders (page $i):${NOCOLOR}"
						else
							echo -e "${YELLOW}[+] This is the list of configured shared folders:${NOCOLOR}"
						fi
            show_onion_address $WEB_SERVICE_NAME
          done
       	;;
     	esac
		fi
 	fi
	;;

	# Start/stop upload or/and download files (TorBox File Sharing)
	18)
		INPUT=$(cat text/tfs-text)
		if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH); then
			create_service_list
			error_no_onion_service
			clear
			CHOICE2=$(whiptail --cancel-button "Back" --title "TorBox v.0.5.5 - TorBox's File Sharing capabilities" --menu "Choose an option (ESC -> go back)" 13 $MENU_WIDTH 6 \
			"==" "===============================================================" \
			" 1" "Start or restart TFS on Onion domains" \
			" 2" "Change the configuration of a certain TFS" \
			" 3" "Stop TFS on an Onion domain" \
			" 4" "List all Onion domains with TFS" \
			"==" "===============================================================" \
			3>&1 1>&2 2>&3)
			exitstatus=$?
			if [ "$exitstatus" = "0" ]; then
				CHOICE2=$(echo "$CHOICE2" | tr -d ' ')
				case "$CHOICE2" in

					# Restart or Start TFS on Onion domains
					1)
						clear
						# Restart
						sudo ./bin/start_tfs
            # NEW - TorBox v.0.5.5: If n > 0 then TFS was restartet and there is no need to create a new one
            n=$(grep -c "^TFS-" "${RUNFILE}")
            if [ $n -eq 0 ] ; then
						  service_menu menu "$SERVICE_NAME_LIST"
						  if [ "$exitstatus" = "0" ]; then
							  i=0
							  for SERVICE_NAME in $SERVICE_NAME_LIST; do
								  i=$((i+1))
								  for SERVICE_NUMBER in $ENTRY_NUMBERS; do
									  if [ "$i" = "$SERVICE_NUMBER" ]; then
										  # This will give ONION_HOSTNAME
										  test_service_exists $SERVICE_NAME
										  # This will give VIRTPORT and HIDDENSSERVICEPORT
										  find_virtport $SERVICE_NAME
										  # Check if the service/port pair is already used
										  [ -f "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf" ] && error_already_used
										  [ -f "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion-chatsecure.conf" ] && error_already_used
										  # Where should the files be stored?
										  SUB_FOLDER=$(whiptail --title "TorBox v.0.5.5 - TorBox's File Sharing capabilities" --inputbox "\nWhere inside of $WEBSITE_DIR do you want store the shared files for TFS? (default is $WEBSITE_DIR/${SERVICE_NAME}, however you can chose another folder, e.g ${SERVICE_NAME}/tfsfolder or $WEBSITE_DIR/${SERVICE_NAME}/tfsfolder):" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 3>&1 1>&2 2>&3)
										  if grep "^$WEBSITE_DIR/" <<< "${SUB_FOLDER}"; then SUB_FOLDER=$(grep "^$WEBSITE_DIR/" <<< "${SUB_FOLDER}" | sed 's|^/var/www/||g'); fi
										  if grep "^/" <<< "${SUB_FOLDER}"; then SUB_FOLDER=$(grep "^/" <<< "${SUB_FOLDER}" | sed 's|^/||g'); fi
										  clear
										  # If the user doesn't write something into the dialogue, the default is valid
										  if [[ "$SUB_FOLDER" = "" || "$SUB_FOLDER" = " " ]]; then
											  TFS_PATH="$WEBSITE_DIR/$SERVICE_NAME/"
										  else
											  if [[ $SUB_FOLDER == */ ]]; then
												  TFS_PATH="$WEBSITE_DIR/$SUB_FOLDER"
											  else
												  TFS_PATH="$WEBSITE_DIR/$SUB_FOLDER/"
											  fi
										  fi
										  if [ ! -d $TFS_PATH ]; then
											  sudo chown torbox:torbox $WEBSITE_DIR
											  sudo -utorbox mkdir -p $TFS_PATH
										  fi
										  ##### DISPLAY THE AVAILABLE OPTIONS ######
											CHOICE3=$(whiptail --nocancel --title "TorBox v.0.5.5 - TorBox's File Sharing capabilities" --checklist --separate-output "Choose if you want to allow uploading or/and downloading.\n\nPlease, use SPACE to select/deselect, ENTER to apply, ESC to go back." $MENU_HEIGHT_20 $MENU_WIDTH 2 \
					     			  " 1" "Allow uploading" ON \
						     		  " 2" "Allow downloading" ON \
						     		  3>&1 1>&2 2>&3)
										  clear
										  CHOICE3=$(echo "$CHOICE3" | tr -d ' ')
					     			  configure_tfs
										  echo ""
										  if [ "$VIRTPORT" == "80" ]; then echo -e "${YELLOW}Service address (http) = ${RED}${ONION_HOSTNAME}${NOCOLOR}"; else
											  echo -e "${YELLOW}Service address (http) = ${RED}${ONION_HOSTNAME}:${VIRTPORT}${NOCOLOR}"
										  fi
										  echo ""
										  read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
										  clear
									  fi
								  done
							  done
						  fi
            fi
					;;

					#Change the configuration of a certain TFS
					2)
						clear
						SERVICE_NAME_LIST=$(grep "^TFS-" ${RUNFILE} | sed "s/^TFS-//" | cut -d "=" -f1)
            [ -z $SERVICE_NAME_LIST ] && error_no_tfs_running
						TFS_CONFIG_LIST=$(grep "^TFS-" ${RUNFILE} | cut -d "=" -f2- | sed "s|lib/fileshare/tfs ||")
						service_menu menu "$SERVICE_NAME_LIST"
						exitstatus=$?
						if [ "$exitstatus" = "0" ]; then
							i=0
							for SERVICE_NAME in $SERVICE_NAME_LIST; do
								i=$((i+1))
								for SERVICE_NUMBER in $ENTRY_NUMBERS; do
									if [ "$i" = "$SERVICE_NUMBER" ]; then
										# Creating an array on an easy way
										# shellcheck disable=SC2207
										TFS_CONFIG_LIST_ARRAY=($(echo "$TFS_CONFIG_LIST" | tr " " "\n"))
										#	Distribute the elemwnts of the config line to variables
										TFS_PATH="${TFS_CONFIG_LIST_ARRAY[1]}"
										SUB_FOLDER=$(whiptail --title "TorBox v.0.5.5 - TorBox's File Sharing capabilities" --inputbox "\nWhere inside of $WEBSITE_DIR do you want store the shared files for TFS? Currently: $TFS_PATH\n\nPress enter or specify a new folder:" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX 3>&1 1>&2 2>&3)
										if grep "^$WEBSITE_DIR/" <<< "${SUB_FOLDER}"; then SUB_FOLDER=$(grep "^$WEBSITE_DIR/" <<< "${SUB_FOLDER}" | sed 's|^/var/www/||g'); fi
										if grep "^/" <<< "${SUB_FOLDER}"; then SUB_FOLDER=$(grep "^/" <<< "${SUB_FOLDER}" | sed 's|^/||g'); fi
										clear
										# If the user doesn't write something into the dialogue, the old path is still valid
										if [[ $SUB_FOLDER = "" || $SUB_FOLDER = " " ]]; then
											if [[ $TFS_PATH == */ ]]; then
												TFS_PATH="$WEBSITE_DIR/$TFS_PATH"
											else
												TFS_PATH="$WEBSITE_DIR/$TFS_PATH/"
											fi
										else
											if [[ $SUB_FOLDER == */ ]]; then
												TFS_PATH="$WEBSITE_DIR/$SUB_FOLDER"
											else
												TFS_PATH="$WEBSITE_DIR/$SUB_FOLDER/"
											fi
										fi
										if [ ! -d $TFS_PATH ]; then
											sudo chown torbox:torbox $WEBSITE_DIR
											sudo -utorbox mkdir -p $TFS_PATH
										fi
										##### DISPLAY THE AVAILABLE OPTIONS ######
										TFS_PATH_ACCESS="${TFS_PATH}.access"
										if grep ";x" "${TFS_PATH_ACCESS}"; then
											CHOICE3=$(whiptail --nocancel --title "TorBox v.0.5.5 - TorBox's File Sharing capabilities" --checklist --separate-output "Choose if you want to allow uploading or/and downloading.\n\nPlease, use SPACE to select/deselect, ENTER to apply, ESC to go back." $MENU_HEIGHT_20 $MENU_WIDTH 2 \
											" 1" "Allow uploading" OFF \
											" 2" "Allow downloading" OFF \
											3>&1 1>&2 2>&3)
										elif grep ";xw" "${TFS_PATH_ACCESS}"; then
											CHOICE3=$(whiptail --nocancel --title "TorBox v.0.5.5 - TorBox's File Sharing capabilities" --checklist --separate-output "Choose if you want to allow uploading or/and downloading.\n\nPlease, use SPACE to select/deselect, ENTER to apply, ESC to go back." $MENU_HEIGHT_20 $MENU_WIDTH 2 \
											" 1" "Allow uploading" ON \
											" 2" "Allow downloading" OFF \
											3>&1 1>&2 2>&3)
										elif grep ";xr" "${TFS_PATH_ACCESS}"; then
											CHOICE3=$(whiptail --nocancel --title "TorBox v.0.5.5 - TorBox's File Sharing capabilities" --checklist --separate-output "Choose if you want to allow uploading or/and downloading.\n\nPlease, use SPACE to select/deselect, ENTER to apply, ESC to go back." $MENU_HEIGHT_20 $MENU_WIDTH 2 \
											" 1" "Allow uploading" OFF \
											" 2" "Allow downloading" ON \
											3>&1 1>&2 2>&3)
										elif grep ";xrw" "${TFS_PATH_ACCESS}"; then
											CHOICE3=$(whiptail --nocancel --title "TorBox v.0.5.5 - TorBox's File Sharing capabilities" --checklist --separate-output "Choose if you want to allow uploading or/and downloading.\n\nPlease, use SPACE to select/deselect, ENTER to apply, ESC to go back." $MENU_HEIGHT_20 $MENU_WIDTH 2 \
											" 1" "Allow uploading" ON \
											" 2" "Allow downloading" ON \
											3>&1 1>&2 2>&3)
										fi
										clear
										CHOICE3=$(echo "$CHOICE3" | tr -d ' ')
										sudo sed -E -i "/^TFS-$SERVICE_NAME=lib\/fileshare\/tfs/d" "${RUNFILE}"
										clear
										[ -f "lib/fileshare/pid/$SERVICE_NAME.pid" ] && stopping_tfs $SERVICE_NAME
										configure_tfs
										echo ""
										# This will give ONION_HOSTNAME
										test_service_exists $SERVICE_NAME
										if [ "$VIRTPORT" == "80" ]; then echo -e "${YELLOW}Service address (http) = ${RED}${ONION_HOSTNAME}${NOCOLOR}"; else
											echo -e "${YELLOW}Service address (http) = ${RED}${ONION_HOSTNAME}:${VIRTPORT}${NOCOLOR}"
										fi
										echo ""
										read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
										clear
									fi
								done
							done
						fi
					;;

					#Stop TFS on Onion domain
					3)
						SERVICE_NAME_LIST_001=$(grep "^TFS-" ${RUNFILE} | sed "s/^TFS-//" | cut -d "=" -f1)
						[ -z "$SERVICE_NAME_LIST_001" ] && error_no_tfs_running
						clear
						SERVICE_NAME_LIST_002=$(ls lib/fileshare/pid/ | sed "s/.pid//")
						if [ -z "$SERVICE_NAME_LIST_002" ]; then
							echo -e "${RED}[+] Removing TFS configuration...${NOCOLOR}"
              (sudo pkill -f "fileshare") &>/dev/null
							sudo sed -i "/^TFS-/d" "${RUNFILE}"
							echo -e "${RED}[+] Removing Nginx configuration...${NOCOLOR}"
							sudo ls /etc/nginx/sites-enabled | grep .*-onion-filesharing.conf | xargs -I {} -d"\n" sudo rm /etc/nginx/sites-enabled/{}
							sudo ls /etc/nginx/sites-available | grep .*-onion-filesharing.conf | xargs -I {} -d"\n" sudo rm /etc/nginx/sites-available/{}
						else
							service_menu checklist "$SERVICE_NAME_LIST_002"
	       			if [ "$exitstatus" = "0" ]; then
	         			if [ ! -z "$SERVICE_NAME_LIST_002" ]; then
	           			i=0
	             		for SERVICE_NAME in $SERVICE_NAME_LIST_002; do
	             			i=$((i+1))
	             			for SERVICE_NUMBER in $ENTRY_NUMBERS; do
	               			if [ "$i" = "$SERVICE_NUMBER" ]; then
												stopping_tfs $SERVICE_NAME
											fi
	             			done
	             		done
								fi
							fi
						fi
						echo -e "${RED}[+] Reloading Nginx...${NOCOLOR}"
            # sudo systemctl reload nginx will not create new socks !!
						# sudo ls /var/run/ | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
						sudo systemctl reload nginx
						echo ""
						read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
					;;

					#List all Onion domains with TFS
					4)
            if ! pgrep -f "fileshare/tfs"; then error_no_tfs_running; fi
            clear
						SERVICE_NAME_LIST=$(grep "^TFS-" ${RUNFILE} | sed "s/^TFS-//" | cut -d "=" -f1)
						if [ ! -z "$SERVICE_NAME_LIST" ]; then
							i=0;
							for SERVICE_NAME in $SERVICE_NAME_LIST; do
								i=$((i+1))
								echo -e "${YELLOW}[+] This is the information about the running TFS:${NOCOLOR}"
								show_onion_address $SERVICE_NAME
							done
						else
							clear
							echo -e "${YELLOW}[!] NO FOLDERS ARE ACTIVELY SHARED!!${NOCOLOR}"
							sleep 5
						fi
					;;
				esac
			fi
		fi
	;;

	# Start/stop TorBox Chat Secure
	19)
		INPUT=$(cat text/secure_chat-text)
		if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_20 $MENU_WIDTH); then
			create_service_list
			error_no_onion_service
			clear
			CHOICE2=$(whiptail --cancel-button "Back" --title "TorBox v.0.5.5 - TorBox's Chat Secure" --menu "Choose an option (ESC -> go back)" 12 $MENU_WIDTH 5 \
			"==" "===============================================================" \
			" 1" "Start or restart TCS on Onion domains" \
			" 2" "Stop TCS on an Onion domain" \
			" 3" "List all Onion domains with TCS" \
			"==" "===============================================================" \
			3>&1 1>&2 2>&3)
			exitstatus=$?
			if [ "$exitstatus" = "0" ]; then
				CHOICE2=$(echo "$CHOICE2" | tr -d ' ')
				case "$CHOICE2" in

					# Restart or Start TCS on Onion domains
					1)
						clear
						# Restart
						sudo ./bin/start_tcs
						# New Start (always executed!)
						service_menu menu "$SERVICE_NAME_LIST"
						if [ "$exitstatus" = "0" ]; then
							i=0
							for SERVICE_NAME in $SERVICE_NAME_LIST; do
								i=$((i+1))
								for SERVICE_NUMBER in $ENTRY_NUMBERS; do
									if [ "$i" = "$SERVICE_NUMBER" ]; then
										# This will give ONION_HOSTNAME
										test_service_exists $SERVICE_NAME
										# This will give VIRTPORT and HIDDENSSERVICEPORT
										find_virtport $SERVICE_NAME
										# Check if the service/port pair is already used
										[ -f "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion.conf" ] && error_already_used
										[ -f "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" ] && error_already_used
										configure_tcs
										echo ""
										if [ "$VIRTPORT" == "80" ]; then echo -e "${YELLOW}Service address (http) = ${RED}${ONION_HOSTNAME}${NOCOLOR}"; else
											echo -e "${YELLOW}Service address (http) = ${RED}${ONION_HOSTNAME}:${VIRTPORT}${NOCOLOR}"
										fi
										echo ""
										read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
										clear
									fi
								done
							done
						fi
					;;

					#Stop TCS on Onion domain
					2)
						SERVICE_NAME_LIST_001=$(grep "^TCS-" ${RUNFILE} | sed "s/^TCS-//" | cut -d "=" -f1)
						[ -z "$SERVICE_NAME_LIST_001" ] && error_no_tcs_running
						clear
						SERVICE_NAME_LIST_002=$(ls lib/chatsecure/pid/ | sed "s/.pid//")
						if [ -z "$SERVICE_NAME_LIST_002" ]; then
							echo -e "${RED}[+] Removing TCS configuration...${NOCOLOR}"
							(sudo pkill -f "chatsecure") &>/dev/null
							sudo sed -i "/^TCS-/d" "${RUNFILE}"
							# NEW - TorBox v.0.5.5
							sleep 2
							PID_PATH="$TORBOX_PATH/lib/fileshare/pid/"
							(sudo rm $PID_PATH/$SERVICE_NAME.pid) &>/dev/null
							(sudo rm $DB_PATH/$SERVICE_NAME.*) &>/dev/null
							(sudo rm /var/run/tcs_$SERVICE_NAME.sock) &>/dev/null
							#
							echo -e "${RED}[+] Removing Nginx configuration...${NOCOLOR}"
							sudo ls /etc/nginx/sites-enabled | grep .*-onion-chatsecure.conf | xargs -I {} -d"\n" sudo rm /etc/nginx/sites-enabled/{}
							sudo ls /etc/nginx/sites-available | grep .*-onion-chatsecure.conf | xargs -I {} -d"\n" sudo rm /etc/nginx/sites-available/{}
						else
							service_menu checklist "$SERVICE_NAME_LIST_002"
							if [ "$exitstatus" = "0" ]; then
								if [ ! -z "$SERVICE_NAME_LIST_002" ]; then
									i=0
									for SERVICE_NAME in $SERVICE_NAME_LIST_002; do
										i=$((i+1))
										for SERVICE_NUMBER in $ENTRY_NUMBERS; do
											if [ "$i" = "$SERVICE_NUMBER" ]; then
												stopping_tcs $SERVICE_NAME
											fi
										done
									done
								fi
							fi
						fi
						echo -e "${RED}[+] Reloading Nginx...${NOCOLOR}"
            # sudo systemctl reload nginx will not create new socks !!
						# sudo ls /var/run/ | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
						sudo systemctl reload nginx
						echo ""
						read -n 1 -s -r -p $'\e[1;31mPlease press any key to continue... \e[0m'
						;;

					#List all Onion domains with TCS
					3)
            if ! pgrep -f "./tcs"; then error_no_tcs_running; fi
						clear
						SERVICE_NAME_LIST=$(grep "^TCS-" ${RUNFILE} | sed "s/^TCS-//" | cut -d "=" -f1)
						if [ ! -z "$SERVICE_NAME_LIST" ]; then
							i=0;
							for SERVICE_NAME in $SERVICE_NAME_LIST; do
								i=$((i+1))
								echo -e "${YELLOW}[+] This is the information about the running TCS:${NOCOLOR}"
								show_onion_address $SERVICE_NAME
							done
						else
							clear
							echo -e "${YELLOW}[!] NO FOLDERS ARE ACTIVELY SHARED!!${NOCOLOR}"
							sleep 5
						fi
					;;
				esac
			fi
		fi
	;;

	*)
    clear
    exit 0

esac
bash menu-onion
