def fight_thread(control): el = ExitList(filename = config.export_file_prefix + ".csv", status_filename = config.export_file_prefix + ".status") log.info("Initialized exit list.") dnsel = client.createResolver() torbel = client.createResolver(servers = [("localhost", 53)]) log.info("Initialized resolvers.") def makeResultChecker(router): return lambda rlist: printResult(router, rlist) def printResult((source, dest, dest_port, qstr, dnsel_ok2), result_list): global match, mismatch dnsel_ok = False torbel_ok = False torbel_el_ok = el.tor_exit_search(source, dest, dest_port) is not None query = "" for (success, value) in result_list: if success: (a_names, auth, cnames) = value assert len(a_names) == 1 for rec in a_names: dnsel_ok = dnsel_zone in str(rec.name) torbel_ok = local_zone in str(rec.name) q = "%-15s -> %-15s:%-5d - " % (IPAddress(source), IPAddress(dest), dest_port) if dnsel_ok2 and torbel_el_ok: log.info(q + "DNSEL and TorBEL agree on YES.") match += 1 elif not (dnsel_ok2 or torbel_el_ok): log.info(q + "DNSEL and TorBEL agree on NO.") match += 1 else: log.info(q + "mismatch: DNSEL = %s, TorBEL DNS = %s, TorBEL query = %s, q = %s", "yes" if dnsel_ok2 else "no", "yes" if torbel_ok else "no", "yes" if torbel_el_ok else "no", qstr) mismatch += 1
def __init__(self, zone, filename, status, *args, **kwargs): server.DNSServerFactory.__init__(self, *args, **kwargs) self.el = ExitList(filename, status) if self.el.stale: log.info("Export %s likely stale.", filename) # Set up updates. nextUpdate = timegm(self.el.next_update.timetuple()) - time.time() if nextUpdate > 0: log.debug("Scheduling first update in %.1f seconds.", nextUpdate) reactor.callLater(nextUpdate, self.update) else: log.notice("Export file is not up-to-date. Trying again in 10 minutes.") reactor.callLater(10 * 60, self.update) self.root = zone.split(".") self.root_name = zone
class TorDNSServerFactory(server.DNSServerFactory): def __init__(self, zone, filename, status, *args, **kwargs): server.DNSServerFactory.__init__(self, *args, **kwargs) self.el = ExitList(filename, status) if self.el.stale: log.info("Export %s likely stale.", filename) # Set up updates. nextUpdate = timegm(self.el.next_update.timetuple()) - time.time() if nextUpdate > 0: log.debug("Scheduling first update in %.1f seconds.", nextUpdate) reactor.callLater(nextUpdate, self.update) else: log.notice("Export file is not up-to-date. Trying again in 10 minutes.") reactor.callLater(10 * 60, self.update) self.root = zone.split(".") self.root_name = zone def update(self): next = self.el.update() nextUpdate = timegm(next.timetuple()) - time.time() if nextUpdate > 0: log.info("ExitList updated. Next update in %.1f seconds.", nextUpdate) reactor.callLater(nextUpdate, self.update) else: log.notice("Export file is not up-to-date. Trying again in 10 minutes.") reactor.callLater(10 * 60, self.update) def handleQuery(self, message, protocol, address): query = message.queries[0] ip, port = address return self.lookup(ip, query, None).addCallback( self.gotResolverResponse, protocol, message, address ).addErrback( self.gotResolverError, protocol, message, address ) def exit_search(self, name, dest_ip, dest_port, tor_ip): log.debug("query tor IP %s, dest_ip %s, dest_port %d", tor_ip, dest_ip, dest_port) router = self.el.tor_exit_search(int(tor_ip), dest_ip, dest_port) if router: log.debug("Request for %s:%d matches router %s(%s).", tor_ip, dest_port, router.idhex, router.nickname) return ([dns.RRHeader(name, dns.A, dns.IN, config.ttl, payload = dns.Record_A("127.0.0.2"), auth = True)], # Authority section [dns.RRHeader(self.root_name, dns.NS, dns.IN, config.ttl, payload = dns.Record_NS(self.root_name))], # Additional section: give the router's idhex and nickname # as CNAME records. [dns.RRHeader(self.root_name, dns.CNAME, dns.IN, config.ttl, payload = dns.Record_CNAME("id=" +router.idhex)), dns.RRHeader(self.root_name, dns.CNAME, dns.IN, config.ttl, payload = dns.Record_CNAME("nickname=" + router.nickname))] ) raise nxdomain(name) def lookup(self, address, query, timeout): try: return defer.succeed(self._lookup(address, query, timeout)) except error.DomainError, e: return defer.fail(failure.Failure(e)) # Return NXDOMAIN on any parser failure. except: