"""Print each profile's expected blocked share (by query weight) via /api/lookup, and every domain that does not resolve on the host.""" import json import socket import sys import urllib.request sys.path.insert(0, sys.argv[1]) from profiles import PROFILES # noqa: E402 COOKIE = open(sys.argv[2]).read().split("nxdns_session")[1].split()[0] BASE = "http://127.0.0.1:18080" GROUP = {"kids": 2} def lookup(domain, group_id): req = urllib.request.Request(f"{BASE}/api/lookup?domain={domain}&group_id={group_id}") req.add_header("Cookie", f"nxdns_session={COOKIE}") return json.load(urllib.request.urlopen(req)) cache = {} for name, prof in PROFILES.items(): gid = GROUP.get(name, 1) total = blocked = 0 for domain, weight in prof["domains"]: res = lookup(domain, gid) total += weight if sys.argv[3:] == ["-v"]: print(name, domain, json.dumps(res)[:200]) is_blocked = res.get("blocked") or (res.get("decision") or {}).get("action") == "block" if is_blocked: blocked += weight elif domain not in cache and not domain.endswith(".lan"): try: socket.getaddrinfo(domain, None) cache[domain] = True except OSError: cache[domain] = False print("UNRESOLVED", name, domain) print(f"{name}: blocked weight share {blocked / total:.1%}")