def test_hex_converters(self): self.assertEqual(hexIdFromHash('AHhuQ8zFQJdT8l42Axxc6m6kNwI'), '$00786E43CCC5409753F25E36031C5CEA6EA43702') self.assertEqual( hashFromHexId('$00786E43CCC5409753F25E36031C5CEA6EA43702'), 'AHhuQ8zFQJdT8l42Axxc6m6kNwI') # should work with or without leading $ self.assertEqual( hexIdFromHash( hashFromHexId('00786E43CCC5409753F25E36031C5CEA6EA43702')), '$00786E43CCC5409753F25E36031C5CEA6EA43702')
def test_hex_converters(self): self.assertEqual( hexIdFromHash('AHhuQ8zFQJdT8l42Axxc6m6kNwI'), '$00786E43CCC5409753F25E36031C5CEA6EA43702' ) self.assertEqual( hashFromHexId('$00786E43CCC5409753F25E36031C5CEA6EA43702'), 'AHhuQ8zFQJdT8l42Axxc6m6kNwI' ) # should work with or without leading $ self.assertEqual( hexIdFromHash(hashFromHexId('00786E43CCC5409753F25E36031C5CEA6EA43702')), '$00786E43CCC5409753F25E36031C5CEA6EA43702' )
async def _print_router_info(router, agent=None): loc = await router.get_location() print(u" name: {}".format(router.name)) print(u" hex id: {}".format(router.id_hex)) print(u"id hash (base64): {}".format(hashFromHexId(router.id_hex))) print(u" location: {}".format("unknown" if loc.countrycode is None else loc.countrycode)) print(u" address: {}:{} (DirPort={})".format(router.ip, router.or_port, router.dir_port)) print(u" flags: {}".format(" ".join(router.flags))) diff = datetime.datetime.utcnow() - router.modified print(u" last published: {} ago ({})".format(humanize.naturaldelta(diff), router.modified)) if agent: print(util.colors.italic("Extended information from" + util.colors.green(" onionoo.torproject.org") + ":")) details = await router.get_onionoo_details(agent) details.setdefault('dir_address', '<none>') details.setdefault('city_name', 'unknown') details.setdefault('region_name', 'unknown') details.setdefault('country_name', 'unknown') details['or_addresses'] = ', '.join(details.get('or_addresses', [])) details['verified_host_names_formatted'] = ', '.join(details['verified_host_names']) print( u" platform: {platform}\n" u" runnning: {running}\n" u" dir_address: {dir_address}\n" u" OR addresses: {or_addresses}\n" u" location: {city_name}, {region_name}, {country_name}\n" u" host names: {verified_host_names_formatted}\n" u" AS: {as} ({as_name})\n" u" last restarted: {last_restarted}\n" u" last changed: {last_changed_address_or_port}\n" u" last seen: {last_seen}\n" u" probabilities: guard={guard_probability} middle={middle_probability} exit={exit_probability}\n" u"".format(**details) )
def _print_router_info(router, agent=None): # loc = yield router.get_location() loc = yield router.location print(u" name: {}".format(router.name)) print(u" hex id: {}".format(router.id_hex)) print(u"id hash (base64): {}".format(hashFromHexId(router.id_hex))) print(u" location: {}".format("unknown" if loc.countrycode is None else loc.countrycode)) print(u" address: {}:{} (DirPort={})".format(router.ip, router.or_port, router.dir_port)) print(u" flags: {}".format(" ".join(router.flags))) diff = datetime.datetime.utcnow() - router.modified print(u" last published: {} ago ({})".format(humanize.naturaldelta(diff), router.modified)) if agent: print(util.colors.italic("Extended information from" + util.colors.green(" onionoo.torproject.org") + ":")) details = yield router.get_onionoo_details(agent) details.setdefault('dir_address', '<none>') details.setdefault('city_name', 'unknown') details.setdefault('region_name', 'unknown') details.setdefault('country_name', 'unknown') details['or_addresses'] = ', '.join(details.get('or_addresses', [])) print( u" platform: {platform}\n" u" runnning: {running}\n" u" dir_address: {dir_address}\n" u" OR addresses: {or_addresses}\n" u" location: {city_name}, {region_name}, {country_name}\n" u" host name: {host_name}\n" u" AS: {as_number} ({as_name})\n" u" last restarted: {last_restarted}\n" u" last changed: {last_changed_address_or_port}\n" u" last seen: {last_seen}\n" u" probabilities: guard={guard_probability} middle={middle_probability} exit={exit_probability}\n" u"".format(**details) )
def build_circuit(self, routers=None, using_guards=True, purpose=None): """ Builds a circuit consisting of exactly the routers specified, in order. This issues an EXTENDCIRCUIT call to Tor with all the routers specified. :param routers: a list of Router instances which is the path desired. To allow Tor to choose the routers itself, pass None (the default) for routers. :param using_guards: A warning is issued if the first router isn't in self.entry_guards. :return: A Deferred that will callback with a Circuit instance (with the .id member being valid, and probably nothing else). """ if routers is None or routers == []: cmd = "EXTENDCIRCUIT 0" else: if using_guards and routers[0] not in self.entry_guards.values(): warnings.warn( "Circuit doesn't start with a guard: %s" % routers, RuntimeWarning ) cmd = "EXTENDCIRCUIT 0 " first = True for router in routers: if first: first = False else: cmd += ',' # XXX should we really accept bytes here? if isinstance(router, bytes) and len(router) == 40 \ and hashFromHexId(router): cmd += router.decode('utf8') else: cmd += router.id_hex[1:] if purpose is not None: cmd += " purpose={}".format(purpose) d = self.protocol.queue_command(cmd) d.addCallback(self._find_circuit_after_extend) return d
def build_circuit(self, routers=None, using_guards=True, purpose=None): """ Builds a circuit consisting of exactly the routers specified, in order. This issues an EXTENDCIRCUIT call to Tor with all the routers specified. :param routers: a list of Router instances which is the path desired. To allow Tor to choose the routers itself, pass None (the default) for routers. :param using_guards: A warning is issued if the first router isn't in self.entry_guards. :return: A Deferred that will callback with a Circuit instance (with the .id member being valid, and probably nothing else). """ if routers is None or routers == []: cmd = "EXTENDCIRCUIT 0" else: if using_guards and routers[0] not in self.entry_guards.values(): warnings.warn( "Circuit doesn't start with a guard: %s" % routers, RuntimeWarning) cmd = "EXTENDCIRCUIT 0 " first = True for router in routers: if first: first = False else: cmd += ',' # XXX should we really accept bytes here? if isinstance(router, bytes) and len(router) == 40 \ and hashFromHexId(router): cmd += router.decode('utf8') else: cmd += router.id_hex[1:] if purpose is not None: cmd += " purpose={}".format(purpose) d = self.protocol.queue_command(cmd) d.addCallback(self._find_circuit_after_extend) return d
def build_circuit(self, routers=None, using_guards=True): print "build circuit" cmd = "EXTENDCIRCUIT 0 " first = True for router in routers: if first: first = False else: cmd += ',' if isinstance(router, basestring) and len(router) == 40 \ and hashFromHexId(router): cmd += router else: cmd += router.id_hex[1:] d = self.protocol.queue_command(cmd) d.addCallback(self._find_circuit_after_extend) return d
def build_circuit(self, routers=None, using_guards=True): cmd = "EXTENDCIRCUIT 0 " first = True for router in routers: if first: first = False else: cmd += ',' if isinstance(router, basestring) and len(router) == 40 \ and hashFromHexId(router): cmd += router else: cmd += router.id_hex[1:] d = self.protocol.queue_command(cmd) print "d %r" % (d,) d.addCallback(self._find_circuit_after_extend) return d
def router_from_id(self, routerid): """IRouterContainer API""" try: return self.routers[routerid] except KeyError: router = Router(self.protocol) if routerid[0] != '$': raise # just re-raise the KeyError idhash = routerid[1:41] nick = '' is_named = False if len(routerid) > 41: nick = routerid[42:] is_named = routerid[42] is '=' router.update(nick, hashFromHexId(idhash), '0'*27, 'unknown', 'unknown', '0', '0') return router
def build_circuit(self, routers=None, using_guards=True): """ Builds a circuit consisting of exactly the routers specified, in order. This issues an EXTENDCIRCUIT call to Tor with all the routers specified. :param routers: a list of Router instances which is the path desired. To allow Tor to choose the routers itself, pass None (the default) for routers. :param using_guards: A warning is issued if the first router isn't in self.entry_guards. :return: A Deferred that will callback with a Circuit instance (with the .id member being valid, and probably nothing else). """ if routers is None or routers == []: cmd = "EXTENDCIRCUIT 0" else: if using_guards and routers[0] not in self.entry_guards.values(): warnings.warn( "Building a circuit not starting with a guard: %s" % (str(routers), ), RuntimeWarning) cmd = "EXTENDCIRCUIT 0 " first = True for router in routers: if first: first = False else: cmd += ',' if isinstance(router, types.StringType) and len( router) == 40 and hashFromHexId(router): cmd += router else: cmd += router.id_hex[1:] d = self.protocol.queue_command(cmd) d.addCallback(self._find_circuit_after_extend) return d
def router_from_id(self, routerid): """IRouterContainer API""" try: return self.routers[routerid] except KeyError: router = Router(self.protocol) if routerid[0] != "$": raise # just re-raise the KeyError idhash = routerid[1:41] nick = "" is_named = False if len(routerid) > 41: nick = routerid[42:] is_named = routerid[41] is "=" router.update(nick, hashFromHexId(idhash), "0" * 27, "unknown", "unknown", "0", "0") router.name_is_unique = is_named return router
def router_from_id(self, routerid): """IRouterContainer API""" try: return self.routers[routerid] except KeyError: router = Router(self.protocol) if routerid[0] != '$': raise # just re-raise the KeyError idhash = routerid[1:41] nick = '' is_named = False if len(routerid) > 41: nick = routerid[42:] is_named = routerid[41] == '=' router.update(nick, hashFromHexId(idhash), '0' * 27, 'unknown', 'unknown', '0', '0') router.name_is_unique = is_named return router
def build_circuit(self, routers=None, using_guards=True): """ Builds a circuit consisting of exactly the routers specified, in order. This issues an EXTENDCIRCUIT call to Tor with all the routers specified. :param routers: a list of Router instances which is the path desired. To allow Tor to choose the routers itself, pass None (the default) for routers. :param using_guards: A warning is issued if the first router isn't in self.entry_guards. :return: A Deferred that will callback with a Circuit instance (with the .id member being valid, and probably nothing else). """ if routers is None or routers == []: cmd = "EXTENDCIRCUIT 0" else: if using_guards and routers[0] not in self.entry_guards.values(): warnings.warn("Building a circuit not starting with a guard: %s" % (str(routers),), RuntimeWarning) cmd = "EXTENDCIRCUIT 0 " first = True for router in routers: if first: first = False else: cmd += ',' if isinstance(router, types.StringType) and len(router) == 40 and hashFromHexId(router): cmd += router else: cmd += router.id_hex[1:] d = self.protocol.queue_command(cmd) d.addCallback(self._find_circuit_after_extend) return d