forked from NullHypothesis/exitmap
/
exitselector.py
78 lines (52 loc) · 2.03 KB
/
exitselector.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
import sys
import argparse
import stem.descriptor
import ip2loc
def parseCmdArgs():
"""
Parses and returns command line arguments.
"""
parser = argparse.ArgumentParser(description="%s selects a subset of Tor "
"exit relays." % sys.argv[0])
parser.add_argument("-b", "--badexit", action="store_true",
help="Select bad exit relays.")
parser.add_argument("-c", "--countrycode", type=str, default=None,
help="Two-letter country code to select.")
parser.add_argument("-d", "--consensus", type=str, default=None,
help="Consensus document containing relays.")
parser.add_argument("-v", "--version", type=str, default=None,
help="Show relays with a specific version.")
return parser.parse_args()
def getExits( consensus, countryCode=None, badExit=False,
version=None, hosts=[] ):
exits = []
if not consensus:
return []
for desc in stem.descriptor.parse_file(open(consensus)):
# We are only interested in exit relays.
if not "Exit" in desc.flags:
continue
a = b = c = False
for (ip, port) in hosts:
if not desc.exit_policy.can_exit_to(ip, port):
continue
if (badExit and ("BadExit" in desc.flags)) or (not badExit):
a = True
if ((countryCode is not None) and \
(ip2loc.resolve(desc.address) == countryCode)) or \
(countryCode == None):
b = True
if (version and (str(desc.version) == version)) or (not version):
c = True
if a and b and c:
exits.append(desc.fingerprint)
return exits
def main():
args = parseCmdArgs()
exits = getExits(args.consensus, args.countrycode, args.badexit,
args.version)
for e in exits:
print("https://atlas.torproject.org/#details/%s" % e)
if __name__ == "__main__":
sys.exit(main())