#!/usr/bin/env python3
"""Build a Technitium-compatible domain block list from SGB's public API.

Source: T.C. Siber Güvenlik Başkanlığı malicious-address API.
The default scope is domain records added in the last 30 days. URL records are
not converted to domains unless --include-url-hosts is explicitly selected:
one malicious URL path does not necessarily mean that its entire host is bad.
"""

from __future__ import annotations

import argparse
import datetime as dt
import ipaddress
import json
import re
import sys
import time
from pathlib import Path
from typing import Iterable
from urllib.error import HTTPError, URLError
from urllib.parse import urlencode, urlparse
from urllib.request import Request, urlopen

API_URL = "https://siberguvenlik.gov.tr/api/address/index"
USER_AGENT = "Technitium-SGB-Domain-Sync/1.0 (defensive threat-intelligence import)"
DOMAIN_RE = re.compile(r"^(?=.{1,253}$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$")


def normalise_domain(value: str) -> str | None:
    """Return a safe ASCII FQDN, or None for IPs and malformed input."""
    candidate = value.strip().lower().rstrip(".")
    if not candidate or "/" in candidate or " " in candidate:
        return None
    try:
        ipaddress.ip_address(candidate)
        return None
    except ValueError:
        pass
    try:
        candidate = candidate.encode("idna").decode("ascii")
    except UnicodeError:
        return None
    return candidate if DOMAIN_RE.fullmatch(candidate) else None


def url_host(value: str) -> str | None:
    parsed = urlparse(value if "://" in value else f"https://{value}")
    return normalise_domain(parsed.hostname or "")


def fetch_json(url: str, attempts: int = 4) -> dict:
    for attempt in range(1, attempts + 1):
        request = Request(url, headers={"Accept": "application/json", "User-Agent": USER_AGENT})
        try:
            with urlopen(request, timeout=45) as response:
                return json.load(response)
        except (HTTPError, URLError, TimeoutError, json.JSONDecodeError) as exc:
            if attempt == attempts:
                raise RuntimeError(f"API request failed after {attempts} attempts: {exc}") from exc
            delay = min(30, 2**attempt)
            print(f"Warning: {exc}; retrying in {delay}s", file=sys.stderr)
            time.sleep(delay)
    raise AssertionError("unreachable")


def endpoint(record_type: str, page: int, per_page: int, since: str | None) -> str:
    query = {"type": record_type, "page": page, "per-page": per_page}
    if since:
        query["date_gte"] = since
    return f"{API_URL}?{urlencode(query)}"


def records(record_type: str, per_page: int, since: str | None, max_pages: int) -> Iterable[dict]:
    first = fetch_json(endpoint(record_type, 1, per_page, since))
    page_count = int(first.get("pageCount", 0))
    if page_count < 1:
        raise RuntimeError("The API response did not contain a valid pageCount.")
    if page_count > max_pages:
        raise RuntimeError(f"Refusing to fetch {page_count} pages; increase --max-pages after reviewing scope.")
    for page in range(1, page_count + 1):
        payload = first if page == 1 else fetch_json(endpoint(record_type, page, per_page, since))
        models = payload.get("models", [])
        if not isinstance(models, list):
            raise RuntimeError(f"Unexpected models field on page {page}.")
        print(f"{record_type}: page {page}/{page_count} ({len(models)} records)", file=sys.stderr)
        yield from models
        if page < page_count:
            time.sleep(0.5)  # Respect the public service; no parallel page requests.


def main() -> int:
    parser = argparse.ArgumentParser(description="Create a one-domain-per-line list for Technitium DNS Server.")
    scope = parser.add_mutually_exclusive_group()
    scope.add_argument("--days", type=int, default=30, help="Only fetch records from the last N UTC days (default: 30).")
    scope.add_argument("--full", action="store_true", help="Fetch all published domain records; use sparingly.")
    parser.add_argument("--include-url-hosts", action="store_true", help="Also block hosts extracted from URL records (higher false-positive risk).")
    parser.add_argument("--output", type=Path, default=Path("sgb-domains.txt"), help="Output list path (default: sgb-domains.txt).")
    parser.add_argument("--per-page", type=int, default=9000, choices=range(1, 10000), metavar="1-9999", help="API page size (default: 9000).")
    parser.add_argument("--max-pages", type=int, default=100, help="Safety cap for fetched pages (default: 100).")
    args = parser.parse_args()
    if args.days is not None and args.days < 1:
        parser.error("--days must be at least 1")

    since = None if args.full else (dt.datetime.now(dt.timezone.utc) - dt.timedelta(days=args.days)).strftime("%Y-%m-%d")
    domains: set[str] = set()
    domain_records = 0
    for item in records("domain", args.per_page, since, args.max_pages):
        domain_records += 1
        domain = normalise_domain(str(item.get("url", "")))
        if domain:
            domains.add(domain)

    url_records = 0
    if args.include_url_hosts:
        for item in records("url", args.per_page, since, args.max_pages):
            url_records += 1
            domain = url_host(str(item.get("url", "")))
            if domain:
                domains.add(domain)

    if not domains:
        raise RuntimeError("No valid domains were produced; existing block list was left untouched.")

    args.output.parent.mkdir(parents=True, exist_ok=True)
    temporary = args.output.with_suffix(args.output.suffix + ".tmp")
    header = (
        "# Generated from T.C. Siber Güvenlik Başkanlığı public malicious-address API\n"
        f"# Scope: {'full history' if args.full else f'last {args.days} UTC days'}; URL hosts: {'included' if args.include_url_hosts else 'excluded'}\n"
        "# One normalized domain per line; suitable for a Technitium DNS block-list source.\n"
    )
    temporary.write_text(header + "\n".join(sorted(domains)) + "\n", encoding="utf-8", newline="\n")
    temporary.replace(args.output)
    print(f"Wrote {len(domains)} unique domains to {args.output} (source records: domain={domain_records}, url={url_records}).")
    return 0


if __name__ == "__main__":
    try:
        raise SystemExit(main())
    except RuntimeError as exc:
        print(f"ERROR: {exc}", file=sys.stderr)
        raise SystemExit(1)
