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

@ -7,7 +7,7 @@ import config_utils
import sanitize import sanitize
import mod_validation as validate 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 _PAGE = Path(__file__).parent.name

View file

@ -4,7 +4,7 @@ from datetime import datetime, timezone
import config_utils import config_utils
import factory 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 DNS_LOG_MAX = 50
BL_TYPE_LABELS = {'community': 'Community', 'local': 'Local'} BL_TYPE_LABELS = {'community': 'Community', 'local': 'Local'}

View file

@ -13,6 +13,7 @@ Usage:
""" """
import json import json
import logging
import os import os
import sys import sys
import urllib.request import urllib.request
@ -21,6 +22,8 @@ from pathlib import Path
SCRIPT_DIR = Path(__file__).parent SCRIPT_DIR = Path(__file__).parent
CONFIG_FILE = SCRIPT_DIR / "config.json" CONFIG_FILE = SCRIPT_DIR / "config.json"
BLOCKLIST_DIR = SCRIPT_DIR / "blocklists" BLOCKLIST_DIR = SCRIPT_DIR / "blocklists"
LOG_FILE = BLOCKLIST_DIR / ".log"
LAST_DL_FILE = BLOCKLIST_DIR / ".last-dl"
def die(msg): def die(msg):
@ -40,9 +43,27 @@ def load_config():
return json.load(f) 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): def download_blocklists(data):
any_fail = False any_fail = False
BLOCKLIST_DIR.mkdir(exist_ok=True)
for bl in data.get("dns_blocking", {}).get("blocklists", []): for bl in data.get("dns_blocking", {}).get("blocklists", []):
if bl.get("bl_type") == "local": if bl.get("bl_type") == "local":
continue continue
@ -50,32 +71,29 @@ def download_blocklists(data):
save_as = bl.get("save_as", "") save_as = bl.get("save_as", "")
name = bl.get("name", "?") name = bl.get("name", "?")
if not url or not save_as: 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 continue
try: try:
req = urllib.request.Request(url, headers={"User-Agent": "routlin/1.0"}) req = urllib.request.Request(url, headers={"User-Agent": "routlin/1.0"})
with urllib.request.urlopen(req, timeout=30) as r: with urllib.request.urlopen(req, timeout=30) as r:
content = r.read() content = r.read()
(BLOCKLIST_DIR / save_as).write_bytes(content) (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: except Exception as e:
print(f" ERROR: Failed to download '{name}': {e}") logging.error("Failed to download '%s': %s", name, e)
any_fail = True any_fail = True
return not any_fail return not any_fail
LAST_DL_FILE = BLOCKLIST_DIR / ".last-dl"
def main(): def main():
check_root() check_root()
setup_logging()
data = load_config() data = load_config()
print("Downloading blocklists ==============================================") logging.info("Downloading blocklists ==========================================")
success = download_blocklists(data) success = download_blocklists(data)
BLOCKLIST_DIR.mkdir(exist_ok=True)
LAST_DL_FILE.write_text(str(int(__import__('time').time()))) LAST_DL_FILE.write_text(str(int(__import__('time').time())))
if not success: if not success:
print("WARNING: One or more downloads failed.") logging.warning("One or more downloads failed.")
sys.exit(1) sys.exit(1)

View file

@ -20,7 +20,7 @@ import mod_validation as validation
BLOCKLIST_DIR = shared.SCRIPT_DIR / "blocklists" BLOCKLIST_DIR = shared.SCRIPT_DIR / "blocklists"
DB_FILE = BLOCKLIST_DIR / "domains.db" DB_FILE = BLOCKLIST_DIR / "domains.db"
LOG_FILE = shared.SCRIPT_DIR / "blocklists.log" LOG_FILE = BLOCKLIST_DIR / ".log"
RESOLV_CONF = Path("/etc/resolv.conf") RESOLV_CONF = Path("/etc/resolv.conf")
_log = logging.getLogger("blocklists") _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([ service_content = "\n".join([
"# Generated by core.py -- do not edit manually.", "# Generated by core.py -- do not edit manually.",
"", "",
@ -80,7 +81,8 @@ def install_timer(data):
"", "",
"[Service]", "[Service]",
"Type=oneshot", "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",
"", "",
]) ])