#!/bin/bash

## Copyright (C) 2012 - 2020 ENCRYPTED SUPPORT LP <adrelanos@riseup.net>
## See the file COPYING for copying conditions.

check_tor_bootstrap_helper_variables() {
   if command -v qubesdb-read >/dev/null 2>&1 ; then
      local qubes_vm_type
      qubes_vm_type="$(qubesdb-read /qubes-vm-type)" || true
      if [ "$qubes_vm_type" = "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_control_port" = "" ]; then
            gateway_control_port="9051"
         fi
      fi
   fi

   if [ -f "/usr/share/anon-ws-base-files/workstation" ]; then
      if [ "$GATEWAY_IP" = "" ]; then
         GATEWAY_IP="10.152.152.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
   fi

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

check_tor_bootstrap_helper_run_helper_script() {
   if [ "$TEMP_DIR" = "" ]; then
      echo "Variable TEMP_DIR was empty." >&2
      TEMP_DIR="$(mktemp --directory)"
   fi

   check_tor_bootstrap_helper_variables

   check_tor_bootstrap_helper_kill_after="5s"
   check_tor_bootstrap_helper_timeout_after="10s"
   check_bootstrap_helper_bootstrap_file="$TEMP_DIR/tor_check_bootstrap_helper_bootstrap_file"

   rm --force "$check_bootstrap_helper_bootstrap_file"
   check_bootstrap_helper_script_exit_code="0"
   timeout \
      --kill-after="$check_tor_bootstrap_helper_kill_after" \
      "$check_tor_bootstrap_helper_timeout_after" \
      $check_bootstrap_helper_script \
      > "$check_bootstrap_helper_bootstrap_file" \
      2>&1 \
      &
   lastpid="$!"
   wait "$lastpid" || { check_bootstrap_helper_script_exit_code="$?" ; true; };

   if [ -f "$check_bootstrap_helper_bootstrap_file" ]; then
      check_bootstrap_helper_script_output="$(cat "$check_bootstrap_helper_bootstrap_file")"
      if [ "$check_bootstrap_helper_script_output" = "" ]; then
         check_bootstrap_helper_script_output="Variable check_bootstrap_helper_script_output is empty."
         check_bootstrap_helper_script_exit_code="277"
      fi
   else
      check_bootstrap_helper_script_output="ERROR: File '$check_bootstrap_helper_bootstrap_file' does not exist. check_bootstrap_helper_script: '$check_bootstrap_helper_script' Please report this Whonix bug!"
   fi
}

check_tor_bootstrap_helper() {
   check_tor_bootstrap_status
   check_tor_circuit_established
}

check_tor_bootstrap_status() {
   check_bootstrap_helper_script="/usr/local/lib64/helper-scripts/tor_bootstrap_check.py"

   ## 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"
   else
      tor_bootstrap_timeout_type="none"
   fi
}

check_tor_circuit_established() {
   check_bootstrap_helper_script="/usr/local/lib64/helper-scripts/tor_circuit_established_check.py"

   ## sets: check_bootstrap_helper_script_exit_code
   ## sets: check_bootstrap_helper_script_output
   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="/usr/local/lib64/helper-scripts/tor_consensus_valid-after.py"

   ## 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"
   if [ "$check_bootstrap_helper_script_exit_code" = "0" ]; then
      tor_consensus_valid_after_unixtime="$(date --date="$tor_consensus_valid_after_output" +"%s")" || true
   fi
}

tor_consensus_valid-until() {
   check_bootstrap_helper_script="/usr/local/lib64/helper-scripts/tor_consensus_valid-until.py"

   ## 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"
   if [ "$check_bootstrap_helper_script_exit_code" = "0" ]; then
      tor_consensus_valid_until_unixtime="$(date --date="$tor_consensus_valid_until_output" +"%s")" || true
   fi
}
