示例#1
0
def run_module(module_name, args, controller, socks_port, 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

    # Let module perform one-off setup tasks.

    if hasattr(module, "setup"):
        logger.debug("Calling module's setup() function.")
        module.setup()

    exit_destinations = select_exits(args, module)

    exit_relays = list(exit_destinations.keys())
    random.shuffle(exit_relays)

    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,
                           socks_port,
                           stats,
                           exit_destinations=exit_destinations)

    controller.add_event_listener(handler.new_event, EventType.CIRC,
                                  EventType.STREAM)

    duration = count * args.build_delay
    logger.info("Scan is estimated to take around %s." %
                datetime.timedelta(seconds=duration))

    logger.info("Beginning to trigger %d circuit creation(s)." % count)

    iter_exit_relays(exit_relays, controller, stats, args)
示例#2
0
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))