#!/usr/bin/python3 -Bsu

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

import sys
from stem.connection import connect

controller = connect()

if not controller:
    sys.exit(255)

circuit_established = controller.get_info("status/circuit-established")

## Possible answer, if established:
## 1

## Possible answer, if not established:
## 0

## Caveat: status/circuit-established is C-Tor's sticky
## have_completed_a_circuit() flag - set on the first multi-hop circuit
## built and reset only on clock-jump/idle, stale dir-info, or teardown
## (never on a failed fetch). So "1" can be a false positive with paths
## actually down, even for general (not just onion) circuits: treat it
## as "worth trying", not "usable now" - usability is only proven by an
## actual connection.
## https://gitlab.torproject.org/tpo/core/tor/-/issues/28027
## https://gitlab.torproject.org/tpo/core/tor/-/issues/21422

print(format(circuit_established))

controller.close()

if circuit_established == "1":
    sys.exit(0)

sys.exit(1)
