def iter_exit_relays(exit_relays, controller, stats, args): """ Invoke circuits for all selected exit relays. """ before = datetime.datetime.now() cached_consensus_path = os.path.join(args.tor_dir, "cached-consensus") fingerprints = relayselector.get_fingerprints(cached_consensus_path) count = len(exit_relays) # Start building a circuit for every exit relay we got. for i, exit_relay in enumerate(exit_relays): # Determine the hops in our next circuit. if args.first_hop: hops = [args.first_hop, exit_relay] else: all_hops = list(fingerprints) try: all_hops.remove(exit_relay) except ValueError: # Catch exception when exit is not in the cached_consensus pass first_hop = random.choice(all_hops) log.debug("Using random first hop %s for circuit." % first_hop) hops = [first_hop, exit_relay] assert len(hops) > 1 try: controller.new_circuit(hops) except stem.ControllerError as err: stats.failed_circuits += 1 log.debug("Circuit with exit relay \"%s\" could not be " "created: %s" % (exit_relay, err)) if i != (count - 1): sleep(args.build_delay, args.delay_noise) log.info("Done triggering circuit creations after %s." % str(datetime.datetime.now() - before))
def run_module(module_name, args, controller, stats): """ Run an exitmap module over all available exit relays. """ logger.info("Running module '%s'." % module_name) stats.modules_run += 1 try: module = __import__("modules.%s" % module_name, fromlist=[module_name]) except ImportError as err: logger.error("Failed to load module because: %s" % err) return exit_relays = select_exits(args, module) count = len(exit_relays) stats.total_circuits += count if count < 1: raise error.ExitSelectionError("Exit selection yielded %d exits " "but need at least one." % count) handler = EventHandler(controller, module.probe, stats) controller.add_event_listener(handler.new_event, EventType.CIRC, EventType.STREAM) logger.debug("Circuit creation delay of %.3f seconds will account for " "total delay of %.3f seconds." % (args.build_delay, count * args.build_delay)) before = datetime.datetime.now() logger.debug("Beginning to trigger %d circuit creation(s)." % count) consensus = util.get_consensus_path(args) fingerprints = relayselector.get_fingerprints(consensus) # Start building a circuit for every exit relay we got. for i, exit_relay in enumerate(exit_relays): # Determine the hops in our next circuit. if args.first_hop: hops = [args.first_hop, exit_relay] else: all_hops = list(fingerprints) all_hops.remove(exit_relay) first_hop = random.choice(all_hops) logger.debug("Using random first hop %s for circuit." % first_hop) hops = [first_hop, exit_relay] assert len(hops) > 1 try: controller.new_circuit(hops) except stem.ControllerError as err: stats.failed_circuits += 1 logger.debug("Circuit with exit relay \"%s\" could not be " "created: %s" % (exit_relay, err)) if i != (count - 1): time.sleep(args.build_delay) logger.info("Done triggering circuit creations after %s." % str(datetime.datetime.now() - before))