#!/bin/bash

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

check_tor_bootstrap_helper_variables() {
   if command -v qubesdb-read >/dev/null 2>&1 ; then
      if [ -f /run/qubes/this-is-templatevm ] || [ -f "/usr/share/anon-ws-base-files/workstation" ]; then
         ## 'qubesdb-read /qubes-gateway' could fail if NetVM is set to 'none'.
         if [ "$GATEWAY_IP" = "" ]; then
            gateway_ip_error=""
            qubesdb_read_qubes_gateway_result="$(qubesdb-read /qubes-gateway 2>/dev/null)" || { gateway_ip_error="qubesdb_read_failed" ; qubesdb_read_qubes_gateway_result="127.0.0.1" ; };
            GATEWAY_IP="$qubesdb_read_qubes_gateway_result"
         fi
         if [ "$GATEWAY_IP6" = "" ]; then
            gateway_ip_error=""
            qubesdb_read_qubes_gateway_result="$(qubesdb-read /qubes-gateway6 2>/dev/null)" || { gateway_ip_error="qubesdb_read_failed" ; qubesdb_read_qubes_gateway_result="::1" ; };
            GATEWAY_IP6="$qubesdb_read_qubes_gateway_result"
         fi
         if [ "$gateway_control_port" = "" ]; then
            gateway_control_port="9051"
         fi
      fi
   fi

   if [ -f "/usr/share/anon-ws-base-files/workstation" ]; then
      if [ "$GATEWAY_IP" = "" ]; then
         ## IP HARDCODED. Set GATEWAY_IP through the following configuration folder instead.
         ## (Replace IP "10.152.152.10" with the actual IP intended to use.)
         ##
         ## systemcheck:
         ## /etc/systemcheck.d/50_user.conf
         ##
         ## tb-updater:
         ## /etc/torbrowser.d/50_user.conf
         ##
         ## Legacy for Whonix below version 15.0.1.6.9:
         ## whonixcheck:
         ## /etc/whonix.d/50_user.conf
         ##
         ## With the following contents:
         ## GATEWAY_IP="10.152.152.10"
         ##
         ## sdwdate:
         ## config file:
         ## /etc/sdwdate.d/50_user.conf
         ## contents
         ## PROXY_IP="10.152.152.10"
         GATEWAY_IP="10.152.152.10"
      fi
      if [ "$GATEWAY_IP6" = "" ]; then
         ## IP HARDCODED. See above comment
         GATEWAY_IP6="fd19:c33d:88bc::10"
      fi
      if [ "$gateway_control_port" = "" ]; then
         gateway_control_port="9051"
      fi
   fi

   if [ -f "/usr/share/anon-gw-base-files/gateway" ]; then
      if [ "$GATEWAY_IP" = "" ]; then
         GATEWAY_IP="127.0.0.1"
      fi
      if [ "$GATEWAY_IP6" = "" ]; then
         GATEWAY_IP6="::1"
      fi
   fi

   if [ "$gateway_control_port" = "" ]; then
      gateway_control_port="9051"
   fi
   if [ "$GATEWAY_IP" = "" ]; then
      GATEWAY_IP="127.0.0.1"
   fi
   if [ "$GATEWAY_IP6" = "" ]; then
      GATEWAY_IP6="::1"
   fi
}

check_tor_bootstrap_helper_run_helper_script() {
   check_tor_bootstrap_helper_variables

   check_tor_bootstrap_helper_kill_after="5s"
   check_tor_bootstrap_helper_timeout_after="10s"

   check_bootstrap_helper_script_exit_code="0"

   ## TODO: Consider moving the privleap configuration that allows this to work
   ##   into this package (right now it's part of systemcheck)
   check_bootstrap_helper_script_output=$( \
      timeout \
      --kill-after="$check_tor_bootstrap_helper_kill_after" \
      "$check_tor_bootstrap_helper_timeout_after" \
         leaprun \
            "$check_bootstrap_helper_script" \
               2>&1 \
               ) \
               || { check_bootstrap_helper_script_exit_code="$?" ; true; };
}

check_tor_bootstrap_helper() {
   check_tor_bootstrap_status
   check_tor_circuit_established
}

run_signal_newnym() {
   check_bootstrap_helper_script="tor-signal-newnym"
   check_tor_bootstrap_helper_run_helper_script
}

check_tor_dormant_status() {
   check_bootstrap_helper_script="tor-dormant-check"

   ## sets: check_bootstrap_helper_script_exit_code
   ## sets: check_bootstrap_helper_script_output
   check_tor_bootstrap_helper_run_helper_script

   tor_dormant_status="$check_bootstrap_helper_script_output"

   ## `timeout` returns:
   ## - 124 if sigterm was sufficient
   ## - 137 if needed to use kill.
   if [ "$check_bootstrap_helper_script_exit_code" = "124" ]; then
      tor_dormant_type="sigterm"
   elif [ "$check_bootstrap_helper_script_exit_code" = "137" ]; then
      tor_dormant_type="sigkill"
   elif [ "$check_bootstrap_helper_script_exit_code" = "254" ]; then
      tor_dormant_type="invalid-exit-code-please-report-this-bug"
   else
      tor_dormant_type="check-ok"
   fi

}

check_tor_bootstrap_status() {
   check_bootstrap_helper_script="tor-bootstrap-check"

   ## sets: check_bootstrap_helper_script_exit_code
   ## sets: check_bootstrap_helper_script_output
   check_tor_bootstrap_helper_run_helper_script

   tor_bootstrap_percent="$check_bootstrap_helper_script_exit_code"
   tor_bootstrap_status="$check_bootstrap_helper_script_output"

   ## `timeout` returns:
   ## - 124 if sigterm was sufficient
   ## - 137 if needed to use kill.
   if [ "$check_bootstrap_helper_script_exit_code" = "124" ]; then
      tor_bootstrap_timeout_type="sigterm"
   elif [ "$check_bootstrap_helper_script_exit_code" = "137" ]; then
      tor_bootstrap_timeout_type="sigkill"
   elif [ "$check_bootstrap_helper_script_exit_code" = "254" ]; then
      tor_bootstrap_timeout_type="invalid-exit-code-please-report-this-bug"
   else
      tor_bootstrap_timeout_type="none"
   fi
}

check_tor_circuit_established() {
   check_bootstrap_helper_script="tor-circuit-established-check"

   ## sets: check_bootstrap_helper_script_exit_code
   ## sets: check_bootstrap_helper_script_output
   ## sets: tor_bootstrap_timeout_type
   check_tor_bootstrap_helper_run_helper_script

   tor_circuit_established_check_exit_code="$check_bootstrap_helper_script_exit_code"

   if [ "$check_bootstrap_helper_script_exit_code" = "0" ]; then
      tor_circuit_established="$check_bootstrap_helper_script_output"
      if [ "$tor_circuit_established" = "1" ]; then
         tor_circuit_established_word="established"
      else
         tor_circuit_established_word="not established"
      fi
   else
      tor_circuit_established="0"
      tor_circuit_established_word="not established"
   fi
}

tor_consensus_valid-after() {
   check_bootstrap_helper_script="tor-consensus-valid-after"

   ## sets: check_bootstrap_helper_script_exit_code
   ## sets: check_bootstrap_helper_script_output
   check_tor_bootstrap_helper_run_helper_script

   tor_consensus_valid_after_exit_code="$check_bootstrap_helper_script_exit_code"
   tor_consensus_valid_after_output="$check_bootstrap_helper_script_output"
   tor_consensus_valid_after_unixtime=""
   if [ ! "$check_bootstrap_helper_script_exit_code" = "0" ]; then
      true "check_bootstrap_helper_script_exit_code is non-zero $check_bootstrap_helper_script_exit_code."
      return 0
   fi
   if [ "$tor_consensus_valid_after_output" = "" ]; then
      true "tor_consensus_valid_after_output is empty."
      return 0
   fi
   tor_consensus_valid_after_unixtime="$(date --utc --date="$tor_consensus_valid_after_output" +"%s")" || true
}

tor_consensus_valid-until() {
   check_bootstrap_helper_script="tor-consensus-valid-until"

   ## sets: check_bootstrap_helper_script_exit_code
   ## sets: check_bootstrap_helper_script_output
   check_tor_bootstrap_helper_run_helper_script

   tor_consensus_valid_until_exit_code="$check_bootstrap_helper_script_exit_code"
   tor_consensus_valid_until_output="$check_bootstrap_helper_script_output"
   tor_consensus_valid_until_unixtime=""
   if [ ! "$check_bootstrap_helper_script_exit_code" = "0" ]; then
      true "check_bootstrap_helper_script_exit_code is non-zero $check_bootstrap_helper_script_exit_code."
      return 0
   fi
   if [ "$tor_consensus_valid_until_output" = "" ]; then
      true "tor_consensus_valid_until_output is empty."
      return 0
   fi
   tor_consensus_valid_until_unixtime="$(date --utc --date="$tor_consensus_valid_until_output" +"%s")" || true
}
