generateFallbackDirLine.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 urllib2
  9. import stem.descriptor.remote
  10. import stem.util.tor_tools
  11. if len(sys.argv) <= 1:
  12. print('Usage: %s fingerprint ...' % sys.argv[0])
  13. sys.exit(1)
  14. for fingerprint in sys.argv[1:]:
  15. if not stem.util.tor_tools.is_valid_fingerprint(fingerprint):
  16. print("'%s' isn't a valid relay fingerprint" % fingerprint)
  17. sys.exit(1)
  18. try:
  19. desc = stem.descriptor.remote.get_server_descriptors(fingerprint).run()[0]
  20. except urllib2.HTTPError as exc:
  21. if exc.code == 404:
  22. print('# %s not found in recent descriptors' % fingerprint)
  23. continue
  24. else:
  25. raise
  26. if not desc.dir_port:
  27. print("# %s needs a DirPort" % fingerprint)
  28. else:
  29. ipv6_addresses = [(address, port) for address, port, is_ipv6 in desc.or_addresses if is_ipv6]
  30. ipv6_field = ' ipv6=[%s]:%s' % ipv6_addresses[0] if ipv6_addresses else ''
  31. print('%s:%s orport=%s id=%s%s # %s' % (desc.address, desc.dir_port, desc.or_port, fingerprint, ipv6_field, desc.nickname))