Example #1
0
def getExits(consensus, countryCode=None, badExit=False, version=None, hosts=[]):

    exits = []

    if not consensus:
        return []

    for desc in stem.descriptor.parse_file(open(consensus)):

        # We are only interested in exit relays.
        if not "Exit" in desc.flags:
            continue

        a = b = c = False

        for (ip, port) in hosts:
            if not desc.exit_policy.can_exit_to(ip, port):
                continue

        if (badExit and ("BadExit" in desc.flags)) or (not badExit):
            a = True

        if ((countryCode is not None) and (ip2loc.resolve(desc.address) == countryCode)) or (countryCode == None):
            b = True

        if (version and (str(desc.version) == version)) or (not version):
            c = True

        if a and b and c:
            exits.append(desc.fingerprint)

    return exits
Example #2
0
def get_exits(consensus, country_code=None, bad_exit=False,
              version=None, nickname=None, address=None, hosts=[]):

    all_exits = [desc for desc in stem.descriptor.parse_file(consensus) if stem.Flag.EXIT in desc.flags]
    exits = list(all_exits)  # exits that match our given criteria

    if hosts:
        def can_exit_to(desc):
            for (ip, port) in hosts:
                if desc.exit_policy.can_exit_to(ip, port):
                    return True

            return False

        exits = filter(can_exit_to, exits)

    if address:
        exits = filter(lambda desc: address == desc.address, exits)

    if nickname:
        exits = filter(lambda desc: nickname == desc.nickname, exits)

    if bad_exit:
        exits = filter(lambda desc: stem.Flag.BADEXIT in desc.flags, exits)

    if country_code:
        exits = filter(lambda desc: ip2loc.resolve(desc.address) == country_code, exits)

    if version:
        exits = filter(lambda desc: str(desc.version) == version, exits)

    return (len(all_exits), [desc.fingerprint for desc in exits])
Example #3
0
def get_exits(consensus, country_code=None, bad_exit=False,
              version=None, nickname=None, address=None, hosts=[]):

    # Try to find descriptors for the exit policy.

    cached_descriptors = {}
    cached_descriptors_path = os.path.join(os.path.dirname(consensus),
                                           "cached-descriptors")
    if os.path.exists(cached_descriptors_path):
        for desc in stem.descriptor.parse_file(cached_descriptors_path):
            cached_descriptors[desc.fingerprint] = desc

    all_exits = [desc for desc in stem.descriptor.parse_file(consensus)
                 if stem.Flag.EXIT in desc.flags]
    exits = list(all_exits)

    if hosts:
        def can_exit_to(desc):
            for (ip, port) in hosts:

                # Check if we have the full policy.

                e = cached_descriptors.get(desc.fingerprint, None)
                if e:
                    if e.exit_policy.can_exit_to(ip, port):
                        return True
                    else:
                        continue

                # Use the summary from the consensus.

                if desc.exit_policy.can_exit_to(ip, port):
                    return True

            return False

        exits = filter(can_exit_to, exits)

    if address:
        exits = filter(lambda desc: address == desc.address, exits)

    if nickname:
        exits = filter(lambda desc: nickname == desc.nickname, exits)

    if bad_exit:
        exits = filter(lambda desc: stem.Flag.BADEXIT in desc.flags, exits)

    if country_code:
        exits = filter(lambda desc:
                       ip2loc.resolve(desc.address) == country_code, exits)

    if version:
        exits = filter(lambda desc: str(desc.version) == version, exits)

    return (len(all_exits), [desc.fingerprint for desc in exits])
Example #4
0
def getExits( consensus, countryCode=None, badExit=False,
              version=None, nickname=None, address=None, hosts=[] ):

    exits = []
    total = 0

    if not consensus:
        return []

    for desc in stem.descriptor.parse_file(open(consensus)):

        # We are only interested in exit relays.
        if not "Exit" in desc.flags:
            continue

        total += 1

        cannotExit = False
        for (ip, port) in hosts:
            if not desc.exit_policy.can_exit_to(ip, port):
                cannotExit = True
                break
        if cannotExit:
            continue

        if not ((address and address in desc.address) or (not address)):
            continue

        if not ((nickname and nickname in desc.nickname) or (not nickname)):
            continue

        # This will only yield relays which have "BadExit" as well as "Exit"
        # set.
        if not ((badExit and ("BadExit" in desc.flags)) or (not badExit)):
            continue

        if not (((countryCode is not None) and \
            (ip2loc.resolve(desc.address) == countryCode)) or \
           (countryCode == None)):
            continue

        if not ((version and (str(desc.version) == version)) or (not version)):
            continue

        exits.append(desc.fingerprint)

    return (total, exits)
Example #5
0
def get_exits(consensus,
              country_code=None,
              bad_exit=False,
              version=None,
              nickname=None,
              address=None,
              hosts=[]):

    # Try to find descriptors for the exit policy.

    cached_descriptors = {}
    cached_descriptors_path = os.path.join(os.path.dirname(consensus),
                                           "cached-descriptors")
    if os.path.exists(cached_descriptors_path):
        for desc in stem.descriptor.parse_file(cached_descriptors_path):
            cached_descriptors[desc.fingerprint] = desc

    all_exits = [
        desc for desc in stem.descriptor.parse_file(consensus)
        if stem.Flag.EXIT in desc.flags
    ]
    exits = list(all_exits)

    if hosts:

        def can_exit_to(desc):
            for (ip, port) in hosts:

                # Check if we have the full policy.

                e = cached_descriptors.get(desc.fingerprint, None)
                if e:
                    if e.exit_policy.can_exit_to(ip, port):
                        return True
                    else:
                        continue

                # Use the summary from the consensus.

                if desc.exit_policy.can_exit_to(ip, port):
                    return True

            return False

        exits = filter(can_exit_to, exits)

    if address:
        exits = filter(lambda desc: address == desc.address, exits)

    if nickname:
        exits = filter(lambda desc: nickname == desc.nickname, exits)

    if bad_exit:
        exits = filter(lambda desc: stem.Flag.BADEXIT in desc.flags, exits)

    if country_code:
        exits = filter(
            lambda desc: ip2loc.resolve(desc.address) == country_code, exits)

    if version:
        exits = filter(lambda desc: str(desc.version) == version, exits)

    return (len(all_exits), [desc.fingerprint for desc in exits])