#!/bin/bash

## Copyright (C) 2017 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

#### meta start
#### project Whonix
#### category networking and apps
#### workstation_only yes
#### description
## dummy Tor wrapper doing nothing but wait forever and
##
## OnionShare support for configuration option "bundled Tor".
##
## Bisq support.
#### meta end

## Bisq runs Tor which it installs to /home/user/.local/share/Bisq2/tor/tor
## with the following command line options:
## -f /home/user/.local/share/Bisq2/tor/torrc __OwningControllerProcess 8581
##
# cat -- /home/user/.local/share/Bisq2/tor/torrc
# HashedControlPassword 16:9A4EA39F06ADB2F5609AFF1EC4784C1F03AA25713D0E828D731E657B16
# ControlPort 127.0.0.1:43649
# Log notice file /home/user/.local/share/Bisq2/tor/debug.log
# DataDirectory /home/user/.local/share/Bisq2/tor
# SocksPort 43677
# DisableNetwork 1
##
# sudo cat -- /run/tor/control.txt
# PORT=127.0.0.1:9052
# PORT=127.0.0.1:44343
# UNIX_PORT=/run/tor/control

source /usr/libexec/helper-scripts/light_sleep.bsh

exit_handler() {
   if [ ! "$socat_pid_list" = "" ]; then
      for socat_pid_item in $socat_pid_list ; do
         kill -s sigterm -- "$socat_pid_item" || true
      done
   fi
   ## OnionShare will do that after Tor got killed by OnionShare.
   ## So we will not do it here, otherwise OnionShare would show an error
   ## during shutdown.
   #if [ ! "$DataDirectory" = "" ]; then
      #rm -rf "$DataDirectory"
   #fi
}

set -e

verify_config() {
   /usr/bin/tor.anondist-orig "${args[@]}"
   exit "$?"
}

## Debugging.
## This can cause an issue during Qubes boot process.
## tor[807]: /usr/bin/tor: line 59: /root/toroutput: Read-only file system
#set -x
#exec 5>&1 1>> ~/toroutput
#exec 6>&2 2>> ~/toroutput

args=("$@")
true "args: ${args[@]}"

file_name=""
DataDirectory=""
ControlSocket=""
CookieAuthFile=""
SocksPort=""
socat_pid=""

## Thanks to:
## http://mywiki.wooledge.org/BashFAQ/035

while :
do
      case $1 in
         -f)
            file_name="$2"
            shift 2
            ;;
         ## tor-ctrl uses 'tor --verify-config'
         ## https://github.com/nyxnor/tor-ctrl/issues/9
         --verify-config)
            verify_config
            shift
            ;;
         --version)
            exec /usr/bin/tor.anondist-orig --version
            shift
            ;;
         --)
            shift
            break
            ;;
         -*)
            true "$0 unknown option: $1" >&2
            shift
            continue
            ;;
         *)
            break
            ;;
      esac
done

## If there are input files (for example) that follow the options, they
## will remain in the "$@" positional parameters.

## OnionShare support for configuration option "bundled Tor".
## Bisq 2 support.
if [ ! "$file_name" = "" ]; then
   DataDirectory="$(cat -- "$file_name" | grep -- DataDirectory | awk '{print $2}')"
   ControlSocket="$(cat -- "$file_name" | grep -- ControlSocket | awk '{print $2}')"
   CookieAuthFile="$(cat -- "$file_name" | grep -- CookieAuthFile | awk '{print $2}')"
   SocksPort="$(cat -- "$file_name" | grep -- SocksPort | awk '{print $2}')"
   ## Bisq example Tor config:
   ## ControlPort 127.0.0.1:43649
   ControlPort="$(cat -- "$file_name" | grep -- ControlPort | awk '{print $2}')"

   ## Example SocksPort:
   ## SocksPort=24883
   SocksPort=${SocksPort#"SocksPort="}

   ControlPort=$(printf '%s\n' "$ControlPort" | cut -d ':' -f 2)

   mkdir -p "$DataDirectory"

   if [ ! "$ControlSocket" = "" ]; then
      ln -s /run/tor/control "$ControlSocket"
      ln -s /run/tor/control.authcookie "$CookieAuthFile"
   fi

   if [ ! "$ControlPort" = "" ]; then
      socat TCP-LISTEN:"$ControlPort",fork,bind=127.0.0.1 TCP:127.0.0.1:9051 &
      socat_pid_list+=" $!"
   fi

   trap exit_handler EXIT

   if [ ! "$SocksPort" = "" ]; then
      socat TCP-LISTEN:"$SocksPort",fork,bind=127.0.0.1 TCP:127.0.0.1:9050 &
      socat_pid_list+=" $!"
   fi
fi

if printf '%s\n' "$file_name" | grep --quiet --ignore-case -- "Bisq" ; then
   ## Emulate creation of Tor debug.log that Bisq is waiting for.
   ## https://github.com/bisq-network/bisq2/issues/1894#issuecomment-2016924749
   mkdir --parents ~/.local/share/Bisq2/tor/
   printf '%s\n' "
Emulated log file created by: $0

Mar 24 20:12:35.477 [notice] Opened Control listener connection (ready) on 127.0.0.1:${ControlPort}
" | tee ~/.local/share/Bisq2/tor/debug.log >/dev/null
fi

light_sleep infinity
exit 0
