Development

This commit is contained in:
Matthew Grotke 2026-06-09 11:32:33 -04:00
parent f794314f28
commit e4a9d5c038
5 changed files with 35 additions and 15 deletions

View file

@ -13,6 +13,7 @@ Usage:
"""
import json
import logging
import os
import sys
import urllib.request
@ -21,6 +22,8 @@ from pathlib import Path
SCRIPT_DIR = Path(__file__).parent
CONFIG_FILE = SCRIPT_DIR / "config.json"
BLOCKLIST_DIR = SCRIPT_DIR / "blocklists"
LOG_FILE = BLOCKLIST_DIR / ".log"
LAST_DL_FILE = BLOCKLIST_DIR / ".last-dl"
def die(msg):
@ -40,9 +43,27 @@ def load_config():
return json.load(f)
def setup_logging():
BLOCKLIST_DIR.mkdir(exist_ok=True)
try:
file_handler = logging.FileHandler(LOG_FILE, mode='a')
except PermissionError:
print(f"WARNING: Cannot write to {LOG_FILE} -- run with sudo.")
file_handler = None
handlers = [logging.StreamHandler()]
if file_handler:
handlers.insert(0, file_handler)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
handlers=handlers,
force=True,
)
def download_blocklists(data):
any_fail = False
BLOCKLIST_DIR.mkdir(exist_ok=True)
for bl in data.get("dns_blocking", {}).get("blocklists", []):
if bl.get("bl_type") == "local":
continue
@ -50,32 +71,29 @@ def download_blocklists(data):
save_as = bl.get("save_as", "")
name = bl.get("name", "?")
if not url or not save_as:
print(f" Skipped '{name}': missing url or save_as")
logging.warning("Skipped '%s': missing url or save_as", name)
continue
try:
req = urllib.request.Request(url, headers={"User-Agent": "routlin/1.0"})
with urllib.request.urlopen(req, timeout=30) as r:
content = r.read()
(BLOCKLIST_DIR / save_as).write_bytes(content)
print(f" Downloaded: {name} ({len(content):,} bytes)")
logging.info("Downloaded: %s (%s bytes)", name, f"{len(content):,}")
except Exception as e:
print(f" ERROR: Failed to download '{name}': {e}")
logging.error("Failed to download '%s': %s", name, e)
any_fail = True
return not any_fail
LAST_DL_FILE = BLOCKLIST_DIR / ".last-dl"
def main():
check_root()
setup_logging()
data = load_config()
print("Downloading blocklists ==============================================")
logging.info("Downloading blocklists ==========================================")
success = download_blocklists(data)
BLOCKLIST_DIR.mkdir(exist_ok=True)
LAST_DL_FILE.write_text(str(int(__import__('time').time())))
if not success:
print("WARNING: One or more downloads failed.")
logging.warning("One or more downloads failed.")
sys.exit(1)

View file

@ -20,7 +20,7 @@ import mod_validation as validation
BLOCKLIST_DIR = shared.SCRIPT_DIR / "blocklists"
DB_FILE = BLOCKLIST_DIR / "domains.db"
LOG_FILE = shared.SCRIPT_DIR / "blocklists.log"
LOG_FILE = BLOCKLIST_DIR / ".log"
RESOLV_CONF = Path("/etc/resolv.conf")
_log = logging.getLogger("blocklists")

View file

@ -69,7 +69,8 @@ def install_timer(data):
"",
])
blocklist_script = shared.SCRIPT_DIR / "dl_blocklists.py"
dl_script = shared.SCRIPT_DIR / "dl_blocklists.py"
merge_script = shared.SCRIPT_DIR / "core.py"
service_content = "\n".join([
"# Generated by core.py -- do not edit manually.",
"",
@ -80,7 +81,8 @@ def install_timer(data):
"",
"[Service]",
"Type=oneshot",
f"ExecStart=/usr/bin/python3 {blocklist_script}",
f"ExecStart=/usr/bin/python3 {dl_script}",
f"ExecStart=/usr/bin/python3 {merge_script} --merge-blocklists",
"",
])