generateFallbackDirLine.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python
  2. # Generate a fallback directory whitelist/blacklist line for every fingerprint
  3. # passed as an argument.
  4. #
  5. # Usage:
  6. # generateFallbackDirLine.py fingerprint ...
  7. import sys
  8. import stem.descriptor.remote as remote
  9. if len(sys.argv) <= 1:
  10. print "Usage: {} fingerprint ...".format(sys.argv[0])
  11. sys.exit(-1)
  12. # we need the full consensus, because it has IPv6 ORPorts
  13. # and we want a fingerprint to router mapping in routers
  14. #
  15. # stem returns document_handler='DOCUMENT' as a list of consensuses
  16. # with one entry
  17. consensus = remote.get_consensus(document_handler='DOCUMENT').run()[0]
  18. for fingerprint in sys.argv[1:]:
  19. if fingerprint in consensus.routers:
  20. r = consensus.routers[fingerprint]
  21. # Tor clients don't use DirPorts, but old code requires one for fallbacks
  22. if r.dir_port is not None:
  23. # IPv4:DirPort orport=ORPort id=Fingerprint ipv6=[IPv6]:IPv6ORPort # nick
  24. ipv6_or_ap_list = [ apv for apv in r.or_addresses if apv[2] ]
  25. ipv6_str = ""
  26. if len(ipv6_or_ap_list) > 0:
  27. ipv6_or_ap = ipv6_or_ap_list[0]
  28. ipv6_str = " ipv6=[{}]:{}".format(ipv6_or_ap[0], ipv6_or_ap[1])
  29. print ("{}:{} orport={} id={}{} # {}"
  30. .format(r.address, r.dir_port, r.or_port, r.fingerprint,
  31. ipv6_str, r.nickname))
  32. else:
  33. print "# {} needs a DirPort".format(fingerprint)
  34. else:
  35. print "# {} not found in current consensus".format(fingerprint)