stem_examples/src/stem_examples/compare_flags.py

73 lines
2.4 KiB
Python
Raw Normal View History

2024-01-14 11:55:48 +00:00
# -*-mode: python; py-indent-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
2024-01-14 14:28:53 +00:00
#
2024-01-16 14:35:29 +00:00
__doc__ = """
Compares the votes of two directory authorities, in this case moria1
and maatuska, with a special interest in the 'Running' flag.
2024-01-16 18:10:43 +00:00
broken because this site fails:
http://128.31.0.39:9131/tor/status-vote
2024-01-16 14:35:29 +00:00
https://stem.torproject.org/tutorials/examples/compare_flags.html
"""
2024-01-14 11:55:48 +00:00
import sys
import collections
import stem.descriptor
import stem.descriptor.remote
import stem.directory
from tor_controller import set_socks_proxy
def iMain():
# Query all authority votes asynchronously.
downloader = stem.descriptor.remote.DescriptorDownloader(
document_handler = stem.descriptor.DocumentHandler.DOCUMENT,
)
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
# An ordered dictionary ensures queries are finished in the order they were
# added.
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
queries = collections.OrderedDict()
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
for name, authority in stem.directory.Authority.from_cache().items():
if authority.v3ident is None:
continue # authority doesn't vote if it lacks a v3ident
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
queries[name] = downloader.get_vote(authority)
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
# Wait for the votes to finish being downloaded, this produces a dictionary of
# authority nicknames to their vote.
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
votes = dict((name, query.run()[0]) for (name, query) in queries.items())
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
# Get a superset of all the fingerprints in all the votes.
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
all_fingerprints = set()
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
for vote in votes.values():
all_fingerprints.update(vote.routers.keys())
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
# Finally, compare moria1's votes to maatuska's votes.
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
for fingerprint in all_fingerprints:
moria1_vote = votes['moria1'].routers.get(fingerprint)
maatuska_vote = votes['maatuska'].routers.get(fingerprint)
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
if not moria1_vote and not maatuska_vote:
print("both moria1 and maatuska haven't voted about %s" % fingerprint)
elif not moria1_vote:
print("moria1 hasn't voted about %s" % fingerprint)
elif not maatuska_vote:
print("maatuska hasn't voted about %s" % fingerprint)
elif 'Running' in moria1_vote.flags and 'Running' not in maatuska_vote.flags:
print("moria1 has the Running flag but maatuska doesn't: %s" % fingerprint)
elif 'Running' in maatuska_vote.flags and 'Running' not in moria1_vote.flags:
print("maatuska has the Running flag but moria1 doesn't: %s" % fingerprint)
2024-01-14 14:28:53 +00:00
2024-01-14 11:55:48 +00:00
return 0
if __name__ == '__main__':
set_socks_proxy()
sys.exit(iMain())