Last active
February 14, 2017 16:42
-
-
Save NSkelsey/cea16bd652c5bfddbc59c3706522562d to your computer and use it in GitHub Desktop.
Retrieves the list of exit relays from a local tor daemon. Requires twisted and txtorcon. Adapted from txtorsocks/examples/get_info.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import io | |
from twisted.internet import reactor, defer | |
from txtorcon import TorInfo, build_local_tor_connection, Router | |
def error(x): | |
print "ERROR", x | |
return x | |
@defer.inlineCallbacks | |
def setup_complete(info): | |
ns_all = yield info.ns.all() | |
lines = ns_all.split('\n') | |
exit_routers = [] | |
for i in range(0, len(lines), 3): | |
r = lines[i] | |
s = lines[i+1] | |
w = lines[i+2] | |
router = Router(None) | |
router.update(*r.split(' ')[1:-1]) | |
router.from_consensus = True | |
router.flags = s.split(' ')[1:] | |
if 'exit' in router.flags: | |
exit_routers.append(router) | |
print 'Num of relays: %d' % (len(lines)/3) | |
print 'Num of exit routers: %d' % len(exit_routers) | |
reactor.stop() | |
def setup_failed(arg): | |
print "SETUP FAILED", arg | |
reactor.stop() | |
def bootstrap(c): | |
info = TorInfo(c) | |
info.post_bootstrap.addCallback(setup_complete).addErrback(setup_failed) | |
d = build_local_tor_connection(reactor, build_state=False) | |
d.addCallback(bootstrap).addErrback(setup_failed) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment