def router_is_excluded(self, r): if r.fingerprint in self.idhexes: return True if r.nickname in self.nicks: return True if "or_addresses" in r.__dict__: # Stem 1.7.0 only addresses = r.or_addresses else: addresses = [(r.address, 9001, False)] for addr in addresses: is_ipv6 = addr[2] if len(self.countries): country = None if is_ipv6 and \ self.controller.get_info("ip-to-country/ipv6-available", "0") == "1": country = self.controller.get_info("ip-to-country/"+addr[0]) if not is_ipv6 and \ self.controller.get_info("ip-to-country/ipv4-available", "0") == "1": country = self.controller.get_info("ip-to-country/"+addr[0]) if country != None and country.lower() in self.countries: return True for network in self.networks: if is_ipv6: if network.version == 6 and net(addr[0]+"/128").overlaps(network): return True else: if network.version == 4 and net(addr[0]+"/32").overlaps(network): return True return False
def _parse_line(self, conf_line): # We assume Tor has validated the line already. So this parsing # is very dumb+simple (except for semantics of valid data). # See routerset_parse() in tor for parser semantic ordering. if self.exclude_unknowns == "1": self.countries.add("??") self.countries.add("a1") if conf_line == None: return parts = conf_line.split(",") for p in parts: if p[0] == "$": p = p[1:] if "~" in p: p = p.split("~")[0] if "=" in p: p = p.split("=")[0] if len(p) == 40 and all(c in string.hexdigits for c in p): self.idhexes.add(p) elif p[0] == "{" and p[-1] == "}": self.countries.add(p[1:-1].lower()) elif ":" in p or "." in p: self.networks.append(net(unicode(p), strict=False)) else: self.nicks.add(p) plog("INFO", "Honoring ExcludeNodes line: " + conf_line) if len(self.networks): plog("INFO", "Excluding networks " + str(self.networks)) if len(self.idhexes): plog("INFO", "Excluding idhexes " + str(self.idhexes)) if len(self.nicks): plog("INFO", "Excluding nicks " + str(self.nicks)) if len(self.countries): if self.exclude_unknowns == "auto": self.countries.add("??") self.countries.add("a1") if self.controller.get_info("ip-to-country/ipv4-available", "0") == "0": plog( "WARN", "ExcludeNodes contains countries, but Tor has no GeoIP file! " + "Tor is not excluding countries!") else: plog("INFO", "Excluding countries " + str(self.countries))
#!/usr/bin/env pytest -vs """Tests for lcgit.""" import pytest from ipaddress import ip_network as net from lcgit import lcg, lcgit sequences = [ [], [1], "foobar", range(10), net("192.168.1.0/32"), net("192.168.1.0/31"), net("192.168.1.0/30"), net("192.168.1.0/29"), net("192.168.1.0/28"), net("192.168.1.0/24"), net("192.168.0.0/20"), pytest.param(net("172.16.0.0/12"), marks=pytest.mark.slow), pytest.param(net("10.0.0.0/8"), marks=pytest.mark.slow), ] @pytest.mark.parametrize("sequence", sequences) def test_counts_and_dups(sequence): """Verify LCG output integrity.""" answer = sorted([i for i in sequence]) my_lcg = lcg(sequence) accumulated = [] count = 0