[Solved] help_check_tor_bootstrap.py python help

troubadour, could you help with https://github.com/Whonix/Whonix/blob/master/whonix_shared/usr/lib/whonixcheck/help_check_tor_bootstrap.py please?

Damian Johnson recently made a suggestion.

> b = bootstrap_status.split( ) > progress = b[2] > progress_percent = ( progress.split( "=" ) )[1] > exit_code=int(progress_percent)

Operating by the positional argument is fine, but a little risky since
Tor is perfectly allowed to rearrange those. I’d suggest the following
instead…

match = re.match(‘.* PROGRESS=([0-9]+).*’, line)

if match:
exit_code = int(match.group(1))

Could you implement his suggestion in help_check_tor_bootstrap.py please?

New suggestion.

I would definitely suggest the progress parsing change, otherwise tor upgrades might accidentally break you. Minor mistake on my part though...

match = re.match(‘.* PROGRESS=([0-9]+).*’, line)

… should be…

match = re.match(‘.* PROGRESS=([0-9]+).*’, bootstrap_status)

The suggestion from Damian works, after some modifications.

#!/usr/bin/python

## This file is part of Whonix.
## Copyright (C) 2012 - 2014 Patrick Schleizer <adrelanos@riseup.net>
## See the file COPYING for copying conditions.

import sys
import os.path
import re
import stem

from stem.control import Controller

if os.path.exists("/usr/share/whonix/whonix_workstation"):
  p=9151
elif os.path.exists("/usr/share/whonix/whonix_gateway"):
  p=9051
else:
  exit_code=254
  sys.exit(exit_code)

try:
  with Controller.from_port(port = p) as controller:

    if os.path.exists("/usr/share/whonix/whonix_gateway"):
      controller.authenticate("password")

    bootstrap_status = controller.get_info("status/bootstrap-phase")

    ## 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.

    #print "%s" % (bootstrap_status)

    progress_percent = re.match('.* PROGRESS=([0-9]+).*', bootstrap_status)

    exit_code = int(progress_percent.group(1))

except:
  exit_code=255

sys.exit(exit_code)

Using the ‘re’ module is fine, until Tor decides to modify the “NOTICE BOOTSTRAP PROGRESS=” string.

I’m discovering stem. It could be useful for other scripts (help_tbbversion for example).

Done:
https://github.com/Whonix/Whonix/commit/af46b55166fddac959c00d55557c1dd2a2408e0f

I left the ‘print “%s” % (bootstrap_status)’ intact, because whonixcheck reads help_check_tor_bootstrap.py’s stdout.

Works well, thanks!

Yeah, I forgot to import it.

until Tor decides to modify the "NOTICE BOOTSTRAP PROGRESS=" string.
I don't know if they are going to, if they have a policy of not breaking the existing protocol.
I'm discovering stem. It could be useful for other scripts (help_tbbversion for example).
Yes.

New home is:
https://github.com/Whonix/anon-shared-helper-scripts/blob/master/usr/lib/anon-shared-helper-scripts/tor_bootstrap_check.py

I noticed, when tor_bootstrap_check.py catches an exception, then no error message such as “can not access /var/lib/tor/control.authcookies” is written to stdout.

Example to force an error for testing.

usr/lib/anon-shared-helper-scripts/tor_bootstrap_check.py 127.0.0.1 9055 1 ; echo $?

Can we somehow write the error message to stdout?

Trying to push a revised version of tor_bootstrap_check.py.

In troubadoour (troubadour) · GitHub, after forking and cloning https://github.com/Whonix/anon-shared-helper-scripts:

$ git push troubadoour master
19:06:08 libtorsocks(7650): SOCKS V4 connect rejected:
19:06:08 libtorsocks(7650): SOCKS server refused connection
ssh: connect to host git.com port 22: Connection refused
fatal: The remote end hung up unexpectedly

The output of the command takes ~ 2 minutes.
I did push the updated open-link-confirmation to check there was no problem with github.

In github.com/Whonix:
(to make sure)
git reset --hard 0c407726f8a # your latest commit
git checkout origin/master
git branch exceptions_handling
git checkout exceptions_handling

I make the edits, git add, git commit.

$ git remote add troubadoour git@github.com:Whonix/anon-shared-helper-scripts.git
fatal: remote troubadoour already exists.  

Probably OK, or is that the cause of the problem? I did ‘git remote rm troubadoour’ and ‘git remote add’ again, Same error.

Tried to push.

$ git push origin exceptions_handling
Username for 'https://github.com': troubadoour
Password for 'https://troubadoour@github.com': 
error: The requested URL returned error: 403 while accessing https://github.com/Whonix/anon-shared-helper-scripts.git/info/refs
fatal: HTTP request failed

I do not understand what’s going on it troubadoour. In whonix, either I am missing an extra step to be able to push, or something is wrong in my flow. “error 403” means forbidden, I believe.

$ git push troubadoour exceptions_handling
Enter passphrase for key '/home/user/.ssh/id_rsa': 
ERROR: Permission to Whonix/anon-shared-helper-scripts.git denied to troubadoour.
fatal: The remote end hung up unexpectedly

Normal, I guess.

OK, eventually, it’s in GitHub - troubadoour/anon-shared-helper-scripts: Helper scripts useful for Anonymizing Linux Distributions. I probably forgot to remote add troubadoour to the repository. Sometimes, I’m amazed (of myself).

Added some exceptions catching that should take care of most of the situations.

Examples (in Gateway).

user@host:~$ ./tor_bootstrap_check.py 127.0.01 9051 1
Invalid IP address: 127.0.01

user@host:~$ ./tor_bootstrap_check.py 127.0.0.1 90051 1
Invalid port: 90051
or
user@host:~$ ./tor_bootstrap_check.py 127.0.0.1 9o51 1
invalid literal for int() with base 10: '9o51'

user@host:~$ ./tor_bootstrap_check.py 127.0.0.1 9150 1
Unable to authenticate: unable to use the control socket

user@host:~$ ./tor_bootstrap_check.py 127.0.0.1 9155 1
Socket error: [Errno 111] Connection refused

user@host:~$ ./tor_bootstrap_check.py 127.0.0.1 9051 1
NOTICE BOOTSTRAP PROGRESS=100 TAG=done SUMMARY="Done"

All the exceptions except “invalid literal” are raised by stem. I do not think we can catch an exception such as “can not access /var/lib/tor/control.authcookies”.

[quote=“troubadour, post:7, topic:264”] $ git push troubadoour exceptions_handling Enter passphrase for key '/home/user/.ssh/id_rsa': ERROR: Permission to Whonix/anon-shared-helper-scripts.git denied to troubadoour. fatal: The remote end hung up unexpectedly

Normal, I guess.[/quote]
No, not normal. It’s a contradiction. When you push to “troubadoour”, you shouldn’t get permission denied to “Whonix”. Something is wrong with your git remotes. Check them out.

git remote -v

git remote add troubadoour git@github.com:Whonix/anon-shared-helper-scripts.git

This is a contradiction as well.

Should be either.

[code]git remote add origin git@github.com:Whonix/anon-shared-helper-scripts.git/code]

Or.

[code]git remote add troubadoour git@github.com:troubadoour/anon-shared-helper-scripts.git/code]

Username for 'https://github.com': troubadoour Password for 'https://troubadoour@github.com':

This means, you used the https instead of the ssh link on github. https asks for github user/pass. ssh asks for ssh keyfile password. For that reason, I prefer ssh.

You can also have a look into your .git/config file. It includes all remotes.

ssh: connect to host git.com port 22: Connection refused

Something wrong with git removes. Otherwise it wouldn’t say “git.com”. Check git remove -v and/or .git/config file.
I suggest to never try to manually write things like “git@github.com:Whonix/anon-shared-helper-scripts.git/” by hand. Mostly small mistakes cause trouble. Always copy and paste it from github. Always make sure you use ssh github links. Imho, simpler. Github only shows ssh links when you’re logged in.

Merged GitHub - troubadoour/anon-shared-helper-scripts: Helper scripts useful for Anonymizing Linux Distributions. Thanks!

That already works! Stem tells us.

user@host:~$ sudo su clearnet [sudo] password for user: clearnet@host:/home/user$ /usr/lib/anon-shared-helper-scripts/tor_bootstrap_check.py 127.0.0.1 9051 1 Unable to authenticate: Authentication failed: '/var/run/tor/control.authcookie' doesn't exist

(This is expected, because user “clearnet” is not member of the group “debian-tor”. Only user “user” is.)
(And by the way, this is really useful for debugging. Because I needed to add a small fix to https://github.com/Whonix/sdwdate-plugin-anon-shared-con-check. User sdwdate wasn’t part of group “debian:tor”. Needed some time to figure out. Now, that we have better error messages, debugging will be simpler in future.)

There is a small remaining problem. When running.

/usr/lib/anon-shared-helper-scripts/tor_bootstrap_check.py

Or.

/usr/lib/anon-shared-helper-scripts/tor_bootstrap_check.py 1

It exits 1. This is a problem, because whonixcheck would interpret this as “bootstrap is 1% done”. Can you make it exit 255 as well please?

It exits 1. This is a problem, because whonixcheck would interpret this as "bootstrap is 1% done". Can you make it exit 255 as well please?

Pushed “exit code 255 if invalid number of arguments”.

Merged! Added two minor commits on top. Please review.