diff --git a/docker/routlin-dash/app/pages/dnsblocking/action.py b/docker/routlin-dash/app/pages/dnsblocking/action.py index 141435e..6493106 100644 --- a/docker/routlin-dash/app/pages/dnsblocking/action.py +++ b/docker/routlin-dash/app/pages/dnsblocking/action.py @@ -7,7 +7,7 @@ import config_utils import sanitize import mod_validation as validate -DNS_LOG_FILE = Path(config_utils.CONFIGS_DIR) / 'blocklists.log' +DNS_LOG_FILE = Path(config_utils.BLOCKLISTS_DIR) / '.log' _PAGE = Path(__file__).parent.name diff --git a/docker/routlin-dash/app/pages/dnsblocking/view.py b/docker/routlin-dash/app/pages/dnsblocking/view.py index 3cb4016..db74473 100644 --- a/docker/routlin-dash/app/pages/dnsblocking/view.py +++ b/docker/routlin-dash/app/pages/dnsblocking/view.py @@ -4,7 +4,7 @@ from datetime import datetime, timezone import config_utils import factory -DNS_LOG_FILE = f'{config_utils.CONFIGS_DIR}/blocklists.log' +DNS_LOG_FILE = f'{config_utils.BLOCKLISTS_DIR}/.log' DNS_LOG_MAX = 50 BL_TYPE_LABELS = {'community': 'Community', 'local': 'Local'} diff --git a/routlin/dl_blocklists.py b/routlin/dl_blocklists.py index d4d1cc7..016776f 100644 --- a/routlin/dl_blocklists.py +++ b/routlin/dl_blocklists.py @@ -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) diff --git a/routlin/mod_dnsmasq.py b/routlin/mod_dnsmasq.py index fed82f2..9a607d6 100644 --- a/routlin/mod_dnsmasq.py +++ b/routlin/mod_dnsmasq.py @@ -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") diff --git a/routlin/mod_timers.py b/routlin/mod_timers.py index 482c7c2..423550f 100644 --- a/routlin/mod_timers.py +++ b/routlin/mod_timers.py @@ -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", "", ])