Development
This commit is contained in:
parent
114da3cd1c
commit
20061872d7
6 changed files with 216 additions and 68 deletions
|
|
@ -561,23 +561,23 @@
|
|||
{
|
||||
"name": "oisd-big",
|
||||
"description": "OISD Big (ads, phishing, malware, telemetry)",
|
||||
"bl_type": "community"
|
||||
"save_as": "oisd-big.conf",
|
||||
"url": "https://big.oisd.nl/dnsmasq2",
|
||||
"format": "dnsmasq"
|
||||
},
|
||||
{
|
||||
"name": "hagezi-light",
|
||||
"description": "Hagezi Light (ads, tracking, metrics, badware)",
|
||||
"bl_type": "community"
|
||||
"save_as": "hagezi-light.conf",
|
||||
"url": "https://raw.githubusercontent.com/hagezi/dns-blocklists/main/dnsmasq/light.txt",
|
||||
"format": "dnsmasq"
|
||||
},
|
||||
{
|
||||
"name": "hagezi-pro-plus",
|
||||
"description": "Hagezi Pro Plus (ads, tracking, porn, gambling)",
|
||||
"bl_type": "community"
|
||||
"save_as": "hagezi-pro-plus.conf",
|
||||
"url": "https://raw.githubusercontent.com/hagezi/dns-blocklists/main/dnsmasq/pro.plus.txt",
|
||||
"format": "dnsmasq"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
|
|||
|
|
@ -122,7 +122,21 @@ def parse_hosts_format(content):
|
|||
return domains
|
||||
|
||||
|
||||
def parse_blocklist(content, fmt):
|
||||
def detect_format(content):
|
||||
for ln in content.splitlines():
|
||||
ln = ln.strip()
|
||||
if not ln or ln.startswith("#"):
|
||||
continue
|
||||
if ln.startswith("local=/") or ln.startswith("address=/"):
|
||||
return "dnsmasq"
|
||||
if ln[0].isdigit():
|
||||
return "hosts"
|
||||
return "dnsmasq"
|
||||
|
||||
|
||||
def parse_blocklist(content, fmt=None):
|
||||
if fmt is None:
|
||||
fmt = detect_format(content)
|
||||
if fmt == "dnsmasq":
|
||||
return parse_dnsmasq_format(content)
|
||||
return parse_hosts_format(content)
|
||||
|
|
@ -151,7 +165,10 @@ def download_all_blocklists(data):
|
|||
results = {}
|
||||
for name in needed:
|
||||
entry = bl_library[name]
|
||||
url = entry["url"]
|
||||
if entry.get("bl_type") == "local":
|
||||
results[name] = (None, entry)
|
||||
continue
|
||||
url = entry["url"]
|
||||
try:
|
||||
req = urllib.request.Request(url, headers={"User-Agent": "dns-blocklists.py/1.0"})
|
||||
with urllib.request.urlopen(req, timeout=30) as r:
|
||||
|
|
@ -164,6 +181,15 @@ def download_all_blocklists(data):
|
|||
return results
|
||||
|
||||
|
||||
def _parse_local_domains(content):
|
||||
domains = set()
|
||||
for ln in content.splitlines():
|
||||
ln = ln.strip()
|
||||
if ln and not ln.startswith("#"):
|
||||
domains.add(ln)
|
||||
return domains
|
||||
|
||||
|
||||
def update_blocklists(data):
|
||||
BLOCKLIST_DIR.mkdir(exist_ok=True)
|
||||
|
||||
|
|
@ -172,12 +198,23 @@ def update_blocklists(data):
|
|||
|
||||
domains_by_name = {}
|
||||
for name, (content, entry) in downloaded.items():
|
||||
if content is None:
|
||||
if entry.get("bl_type") == "local":
|
||||
save_as = entry.get("save_as", "")
|
||||
local_file = BLOCKLIST_DIR / save_as if save_as else None
|
||||
try:
|
||||
local_content = local_file.read_text() if local_file else ""
|
||||
domains = _parse_local_domains(local_content)
|
||||
log.info(f"Local blocklist '{name}': {len(domains):,} domains")
|
||||
except Exception as e:
|
||||
log.error(f"Local blocklist '{name}' could not be read: {e}")
|
||||
domains = set()
|
||||
domains_by_name[name] = domains
|
||||
elif content is None:
|
||||
log.error(f"Blocklist '{name}' failed to download -- it will be skipped.")
|
||||
domains_by_name[name] = set()
|
||||
else:
|
||||
(BLOCKLIST_DIR / entry["save_as"]).write_text(content)
|
||||
domains = parse_blocklist(content, entry.get("format", "dnsmasq"))
|
||||
domains = parse_blocklist(content)
|
||||
log.info(f"Parsed {len(domains):,} domains from '{name}'")
|
||||
domains_by_name[name] = domains
|
||||
|
||||
|
|
@ -208,7 +245,10 @@ def update_blocklists(data):
|
|||
f.unlink()
|
||||
log.info(f"Removed stale merged file: {f.name}")
|
||||
|
||||
any_failed = any(content is None for content, _ in downloaded.values())
|
||||
any_failed = any(
|
||||
content is None and entry.get("bl_type") != "local"
|
||||
for content, entry in downloaded.values()
|
||||
)
|
||||
return not any_failed
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue