#!/bin/bash

# This file is part of TorBox, an easy to use anonymizing router based on Raspberry Pi.
# Copyright (C) 2026 radio_24
# Contact: anonym@torbox.ch
#
# 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 activate or deactivate Meek-Azure to circumvent censorship.
# Following variables can be used:
# $SNOWSTRING -> represents the status of the Snowflake bridging mode
# $MEEKSTRING -> represents the status of the Meek-Azure bridging mode
#
# SYNTAX
# ./meek-azure <MEEKSTRING> <SNOWSTRING>
#
# <MEEKSTRING> <SNOWSTRING> give the status of Snowflake and Meek-Azure.
#
# Possible values for <MEEKSTRING> <SNOWSTRING>: "ON!" or "OFF".
#
#
#
##### SET VARIABLES ######
#
#Set the the variables for the menu
MENU_WIDTH=80
MENU_WIDTH_REDUX=60
MENU_HEIGHT_15=15
MENU_HEIGHT_25=25

#Colors
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;93m'
NOCOLOR='\033[0m'

#Other variables
BRIDGE_DB_URL="https://onionoo.torproject.org"
TORRC="/etc/tor/torrc"
TORBOX_PATH="/home/torbox/torbox"
TXT_DIR="/home/torbox/torbox/text"
RUNFILE="$TORBOX_PATH/run/torbox.run"
MEEKSTRING=$1
SNOWSTRING=$2
EXITID=$(grep "^EXITID=" ${RUNFILE}) 2>/dev/null
i=0


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

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


######## PREPARATIONS ########
###########################

if [ $EXITID = "EXITID=1" ]; then
  sudo sed -i "s/^EXITID=.*/EXITID=0/" ${RUNFILE}
  exit 0
fi

case $MEEKSTRING in

  "OFF")
		if grep "^ReachableAddresses" ${TORRC} ; then
			INPUT=$(cat $TXT_DIR/no_antifirewall-please-text)
			if (whiptail --title "TorBox - INFO" --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH); then
				sudo sed -i "s/^ReachableAddresses /#ReachableAddresses /g" ${TORRC}
			fi
		fi
    clear
		trap "bash meek-azure; exit 0" SIGINT
		echo -e "${RED}[+] Let's check, if MEEK-AZURE could work for you!${NOCOLOR}"
		readarray -t configured_meekazure_deactivated < <(grep "^#Bridge meek_lite " ${TORRC})
		if [ ${#configured_meekazure_deactivated[0]} = 0 ]; then
			echo " "
			echo -e "${YELLOW}[!] There is no MEEK-AZURE configured! Did you change /etc/tor/torrc ?${NOCOLOR}"
			echo -e "${YELLOW}[!] We cannot activate MEEK-AZURE! Contact anonym@torbox.ch for help!${NOCOLOR}"
			read -n 1 -s -r -p "Press any key to continue"
			exit 1
		else
			echo -e "${RED}[+] MEEK-AZURE is configured and ready!${NOCOLOR}"
			number_configured_meekazure_deactivated=${#configured_meekazure_deactivated[*]}
			sleep 5
		fi
		echo " "
    clear
		online_check $BRIDGE_DB_URL 0 1
		# IMORTANT: the support to show the ONLINE/OFFLINE status of Meek-Azure bridge is currently disabled because it is not reliable.
		OCHECK=0
		# if [ $OCHECK == 1 ]; then
		#	echo " "
		#	echo -e "${YELLOW}[+] OK - we are connected with the bridge database${NOCOLOR}"
		#	echo -e "${RED}[+] Checking next the MEEK-AZURE SERVER - this could take a moment!${NOCOLOR}"
		#	echo -e "${RED}[+] You can only use MEEK-AZURE, if the server is ONLINE!${NOCOLOR}"
		#	sleep 2
		#	echo " "
		#	MEEK_ON=0
		#	i=0
		#	while [ $i -lt $number_configured_meekazure_deactivated ]
		#	do
		#		bridge_address=$(cut -d ' ' -f3,4 <<< ${configured_meekazure_deactivated[$i]})
	  #		bridge_hash=$(cut -d ' ' -f2 <<< $bridge_address)
				#Meek works only through clearnet
		#		bridge_status=$($TORBOX_PATH/bin/bridges_check.py -f $bridge_hash)
    #    case $bridge_status in
    #      0) bridge_status_txt="${RED}- OFFLINE${NOCOLOR}";;
    #      1)
		#			  bridge_status_txt="${GREEN}- ONLINE${NOCOLOR}"
		#			  MEEK_ON=$((MEEK_ON+1))
    #      ;;
    #      2) bridge_status_txt="- DOESN'T EXIST";;
    #    esac
		#		i=$((i+1))
		#		bridge_address="$i : $bridge_address $bridge_status_txt"
		#		echo -e "$bridge_address"
		#	done
		#	if [ $MEEK_ON == 0 ] ; then
		#		echo " "
		#		echo -e "${YELLOW}[!] SORRY! - the MEEK-AZURE SERVER seems to be OFFLINE!${NOCOLOR}"
		#		echo -e "${RED}[+] We try to use it anyway, but most likely it will not work :(${NOCOLOR}"
		#	fi
		#	echo " "
		#	read -n 1 -s -r -p "Press any key to continue"
		# else
		#	echo " "
		#	echo -e "${YELLOW}[!] SORRY! - no connection with the bridge database!${NOCOLOR}"
		#	echo -e "${RED}[!] We'll go ahead anyway.${NOCOLOR}"
		#	echo " "
		#	sleep 3
		# fi
		clear
		INPUT=$(cat $TXT_DIR/activate-meek-azure-text)
		if (whiptail --title "TorBox - INFO (scroll down!)" --scrolltext --defaultno --yesno "$INPUT" $MENU_HEIGHT_25 $MENU_WIDTH); then
			clear
			deactivating_bridge_relay
			deactivate_obfs4_bridges NORESTART
			sudo sed -i "s/^Bridge obfs4 /#Bridge obfs4 /g" ${TORRC}
		  sudo sed -i "s/^Bridge snowflake /#Bridge snowflake /g" ${TORRC}
			sudo sed -i "s/^ClientTransportPlugin snowflake /#ClientTransportPlugin snowflake /g" ${TORRC}
			sudo sed -i "s/^#UseBridges/UseBridges/g" ${TORRC}
			sudo sed -i "s/^#UpdateBridgesFromAuthority/UpdateBridgesFromAuthority/g" ${TORRC}
			sudo sed -i "s/^#ClientTransportPlugin meek_lite,obfs4/ClientTransportPlugin meek_lite,obfs4/g" ${TORRC}
			sudo sed -i "s/^#Bridge meek_lite /Bridge meek_lite /g" ${TORRC}
			sudo sed -i "s/^EXITID=.*/EXITID=1/" ${RUNFILE}
			clear
			restarting_tor meek-azure
		fi
  ;;

	"ON!")
    INPUT=$(cat $TXT_DIR/deactivate-meek-azure-text)
    if (whiptail --title "TorBox - INFO" --defaultno --yesno "$INPUT" $MENU_HEIGHT_15 $MENU_WIDTH_REDUX); then
      clear
      sudo sed -i "s/^Bridge snowflake /#Bridge snowflake /g" ${TORRC}
      sudo sed -i "s/^Bridge meek_lite /#Bridge meek_lite /g" ${TORRC}
      sudo sed -i "s/^UseBridges/#UseBridges/g" ${TORRC}
      sudo sed -i "s/^UpdateBridgesFromAuthority/#UpdateBridgesFromAuthority/g" ${TORRC}
      sudo sed -i "s/^ClientTransport/#ClientTransport/g" ${TORRC}
      sudo sed -i "s/^Bridge obfs4 /#Bridge obfs4 /g" ${TORRC}
      sudo sed -i "s/^EXITID=.*/EXITID=1/" ${RUNFILE}
      restarting_tor meek-azure
    fi
  ;;
esac
