Skip to content

Instantly share code, notes, and snippets.

@NSkelsey
Last active February 14, 2017 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NSkelsey/cea16bd652c5bfddbc59c3706522562d to your computer and use it in GitHub Desktop.
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
#!/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