#!/bin/bash

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

check_tor_socks_port_reachability() {
   ## Small check if one of Tor's SocksPorts is functional.

   if test -f /run/qubes/this-is-templatevm ; then
      return 0
   fi

   local SOCKS_PORT_SYSTEMCHECK
   SOCKS_PORT_SYSTEMCHECK="9110"

   true "FUNCNAME: CURL: $CURL"

   $CURL \
      --fail \
      $CURL_VERBOSE \
      --max-time 10 \
      --output "$TEMP_DIR/socks_port_open_file" \
      --unix-socket "/run/tor/socks" \
      "file//run/tor/socks" \
      &

   ## Example OK:
   ## curl: (22) The requested URL returned error: 501 Tor is not an HTTP Proxy

   ## Example Failure:
   ## curl: (56) Recv failure: Connection reset by peer

   lastpid="$!"
   wait "$lastpid" || { check_socks_port_open_test="$?" ; true; };

   local curl_status_message
   curl_status_message="$(/usr/libexec/helper-scripts/curl_exit_codes "$check_socks_port_open_test")"

   if [ "$check_socks_port_open_test" = "22" ]; then
      local MSG="<p>Tor SocksPort Reachability Test Result: Reachable. \
(curl exit code: $check_socks_port_open_test | curl status message: $curl_status_message)</p>"
      if [ "$verbose" -ge "1" ]; then
         $output_x ${output_opts[@]} --messagex --typex "info" --message "$MSG"
         $output_cli ${output_opts[@]} --messagecli --typecli "info" --message "$MSG"
      fi
      return 0
   fi

   if [ "$check_socks_port_open_test" = "7" ]; then
      local MSG="<p>Tor SocksPort Reachability Test Result: Unreachable! \
Failed to connect to host. Gateway likely reachable, but Tor service likely not started. \
Either user has run <code>sudo service tor@default stop</code> or Gateway is still booting and has not started Tor service yet. \
(curl exit code: $check_socks_port_open_test | curl status message: $curl_status_message)</p>"
      if [ "$verbose" -ge "1" ]; then
         $output_x ${output_opts[@]} --messagex --typex "warning" --message "$MSG"
         $output_cli ${output_opts[@]} --messagecli --typecli "warning" --message "$MSG"
         EXIT_CODE="1"
      fi
   elif [ "$check_socks_port_open_test" = "28" ]; then
      if [ "$VM" = "Whonix-Gateway" ]; then
         local MSG="<p>Tor SocksPort Reachability Test Result: Unreachable! \
Gateway likely powered off. \
(curl exit code: $check_socks_port_open_test | curl status message: $curl_status_message)</p>"
      else
         local MSG="<p>Tor SocksPort Reachability Test Result: Unreachable! \
(curl exit code: $check_socks_port_open_test | curl status message: $curl_status_message)</p>"
      fi
      if [ "$verbose" -ge "1" ]; then
         $output_x ${output_opts[@]} --messagex --typex "warning" --message "$MSG"
         $output_cli ${output_opts[@]} --messagecli --typecli "warning" --message "$MSG"
         EXIT_CODE="1"
      fi
   else
      local MSG="<p>Tor SocksPort Reachability Test Result: Unreachable! \
Unexpected error! \
(curl exit code: $check_socks_port_open_test | curl status message: $curl_status_message)</p>"
      if [ "$verbose" -ge "1" ]; then
         $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
         $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
         EXIT_CODE="1"
      fi
   fi
}
