-
Notifications
You must be signed in to change notification settings - Fork 6
/
generateFallbackDirLine.py
executable file
·59 lines (49 loc) · 1.88 KB
/
generateFallbackDirLine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# Generate a fallback directory whitelist/blacklist line for every fingerprint
# passed as an argument.
#
# Usage:
# generateFallbackDirLine.py fingerprint ...
# Future imports for Python 2.7, mandatory in 3.0
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import sys
import stem.descriptor.remote as remote
import stem.util.tor_tools as tor_tools
if len(sys.argv) <= 1:
print('Usage: %s fingerprint ...' % sys.argv[0])
sys.exit(1)
input_list = sys.argv[1:]
for fingerprint in input_list:
if not tor_tools.is_valid_fingerprint(fingerprint):
print("'%s' isn't a valid relay fingerprint" % fingerprint)
sys.exit(2)
found_list = []
desc_query = remote.get_server_descriptors(input_list,
retries=3,
timeout=30)
for desc in desc_query.run():
assert desc.fingerprint in input_list
# Skip duplicates on retries
if desc.fingerprint in found_list:
continue
found_list.append(desc.fingerprint)
if not desc.dir_port:
print("# %s needs a DirPort" % desc.fingerprint)
else:
ipv6_addresses = [(address, port)
for address, port, is_ipv6 in desc.or_addresses
if is_ipv6]
ipv6_field = (' ipv6=[%s]:%s' % ipv6_addresses[0]
if ipv6_addresses
else '')
print('%s:%s orport=%s id=%s%s # %s' % (desc.address,
desc.dir_port,
desc.or_port,
desc.fingerprint,
ipv6_field,
desc.nickname))
for fingerprint in input_list:
if fingerprint not in found_list:
print("# {} not found in current descriptors".format(fingerprint))