2024-01-14 14:28:53 +00:00
#!/usr/local/bin/python3.sh -u
# -*-mode: python; py-indent-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
__doc__ = """
A script by adrelanos @riseup.net to check what percentage of boostrapping
tor is at .
"""
2024-01-14 11:55:48 +00:00
## Copyright (C) 2012 - 2020 ENCRYPTED SUPPORT LP <adrelanos@riseup.net>
## See the file COPYING for copying conditions.
import sys
import os
import re
2024-01-17 20:35:20 +00:00
import logging
import socket
2024-01-14 11:55:48 +00:00
from stem . connection import connect
2024-01-16 14:35:29 +00:00
from stem_examples . tor_controller import get_controller
2024-01-14 11:55:48 +00:00
2024-01-17 20:35:20 +00:00
LOG = logging . getLogger ( )
2024-01-14 11:55:48 +00:00
def iMain ( lArgs = None ) :
2024-02-21 08:03:10 +00:00
password = os . environ . get ( ' TOR_CONTROLLER_PASSWORD ' , ' ' )
2024-01-14 11:55:48 +00:00
if os . path . exists ( ' /run/tor/control ' ) :
controller = get_controller ( password = password , unix = ' /run/tor/control ' )
else :
controller = get_controller ( password = password , port = 9051 )
## Possible answer, if network cable has been removed:
## 250-status/bootstrap-phase=WARN BOOTSTRAP PROGRESS=80 TAG=conn_or SUMMARY="Connecting to the Tor network" WARNING="No route to host" REASON=NOROUTE COUNT=26 RECOMMENDATION=warn
## Possible answer:
## 250-status/bootstrap-phase=NOTICE BOOTSTRAP PROGRESS=85 TAG=handshake_or SUMMARY="Finishing handshake with first hop"
## Possible answer, when done:
## 250-status/bootstrap-phase=NOTICE BOOTSTRAP PROGRESS=100 TAG=done SUMMARY="Done"
## TODO: parse the messages above.
2024-01-17 20:35:20 +00:00
try :
bootstrap_status = controller . get_info ( " status/bootstrap-phase " )
LOG . info ( format ( bootstrap_status ) )
2024-01-14 11:55:48 +00:00
2024-01-17 20:35:20 +00:00
progress_percent = re . match ( ' .* PROGRESS=([0-9]+).* ' , bootstrap_status )
exit_code = int ( progress_percent . group ( 1 ) )
2024-01-14 11:55:48 +00:00
2024-01-17 20:35:20 +00:00
controller . close ( )
except socket . error as e :
# Error while receiving a control message (SocketClosed): received exception "read of closed file"
return 0
except Exception as e :
raise
return 0
2024-01-14 11:55:48 +00:00
if __name__ == ' __main__ ' :
2024-01-17 16:30:48 +00:00
from stem_examples . stem_utils import vsetup_logging
if os . environ . get ( ' DEBUG ' , ' ' ) :
log_level = 10
else :
log_level = 20
vsetup_logging ( LOG , log_level )
try :
2024-02-04 03:29:52 +00:00
i = iMain ( [ ] )
2024-01-17 16:30:48 +00:00
except KeyboardInterrupt as e :
i = 0
except Exception as e :
2024-01-17 20:35:20 +00:00
LOG . exception ( f " Exception { e } " )
2024-01-17 16:30:48 +00:00
i = 1
sys . exit ( i )