#!/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 can report that a circuit is
## established, and Tor connectivity can still be down, even for
## general (not just onion) circuits. It means that Tor successfully
## built a circuit, and nothing has changed to make Tor think that it
## maybe can't, not that connections are guaranteed or even likely to
## succeed, see
## https://gitlab.torproject.org/tpo/core/tor/-/blob/892dc508dbbbcacf583eb879325c97f36d468c99/src/core/mainloop/mainloop.c#L218.
## Treat it as "worth trying", not "usable now" - usability is only
## proven by an actual connection.

print(format(circuit_established))

controller.close()

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

sys.exit(1)
