#!/usr/bin/env python3
"""Refresh the bundled datacenter-CIDR list used by ngsite/datacenter.py to flag
cloud-origin traffic on the /jump affiliate redirect (so scraper bots wearing a
real-browser UA skip the click tally + affiliate wrap, like declared bots do).

Pulls the providers' OWN published ranges:
  - Google Cloud: https://www.gstatic.com/ipranges/cloud.json
  - AWS:          https://ip-ranges.amazonaws.com/ip-ranges.json

Writes ngsite/data/datacenter_cidrs.txt (one CIDR per line, IPv4 + IPv6, sorted,
provenance header). This is a BUILD/MAINTENANCE step, not runtime — the web app
never fetches anything; it only reads the bundled file. Ranges change slowly, so
re-run at deploy time or periodically (e.g. weekly) and commit the result.

  python scripts/refresh-datacenter-cidrs.py        # rewrites the data file
  python scripts/refresh-datacenter-cidrs.py --check # report counts, no write
"""
import argparse
import ipaddress
import json
import os
import sys
import urllib.request
from datetime import datetime, timezone

HERE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(HERE, "..", "ngsite", "data", "datacenter_cidrs.txt")

GCP_URL = "https://www.gstatic.com/ipranges/cloud.json"
AWS_URL = "https://ip-ranges.amazonaws.com/ip-ranges.json"


def _get(url):
    req = urllib.request.Request(url, headers={"User-Agent": "arpf-cidr-refresh"})
    with urllib.request.urlopen(req, timeout=30) as r:
        return json.loads(r.read().decode("utf-8"))


def _existing_cidrs(path):
    """The CIDR lines already in the file (comments ignored), or None if absent.
    Lets the refresh be idempotent: rewrite only when the SET changes, so the
    timestamp header alone never churns a commit in the weekly job."""
    try:
        with open(path) as f:
            return {ln.strip() for ln in f
                    if ln.strip() and not ln.startswith("#")}
    except OSError:
        return None


def gcp_prefixes(doc):
    for p in doc.get("prefixes", []):
        cidr = p.get("ipv4Prefix") or p.get("ipv6Prefix")
        if cidr:
            yield cidr


def aws_prefixes(doc):
    for p in doc.get("prefixes", []):
        if p.get("ip_prefix"):
            yield p["ip_prefix"]
    for p in doc.get("ipv6_prefixes", []):
        if p.get("ipv6_prefix"):
            yield p["ipv6_prefix"]


def collect():
    nets = {}  # str(net) -> (sort_key, net) ; dedup by normalized form
    counts = {}
    for label, url, extract in (("GCP", GCP_URL, gcp_prefixes),
                                ("AWS", AWS_URL, aws_prefixes)):
        doc = _get(url)
        n = 0
        for cidr in extract(doc):
            try:
                net = ipaddress.ip_network(cidr, strict=False)
            except ValueError:
                continue
            key = (net.version, int(net.network_address), net.prefixlen)
            nets[str(net)] = (key, net)
            n += 1
        counts[label] = n
    ordered = [s for s, _ in sorted(nets.items(), key=lambda kv: kv[1][0])]
    return ordered, counts


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--check", action="store_true",
                    help="fetch + report counts, don't write the file")
    args = ap.parse_args()

    try:
        cidrs, counts = collect()
    except Exception as e:  # network/parse failure must not write a partial file
        print("refresh FAILED (file left unchanged):", e, file=sys.stderr)
        return 1

    v4 = sum(1 for c in cidrs if ":" not in c)
    v6 = len(cidrs) - v4
    print("fetched: GCP=%d AWS=%d  ->  %d unique CIDRs (%d v4, %d v6)"
          % (counts.get("GCP", 0), counts.get("AWS", 0), len(cidrs), v4, v6))
    if args.check:
        return 0

    # Idempotent: skip the write (and its new timestamp) when the CIDR set is
    # unchanged, so the weekly refresh job only commits on a real change.
    if _existing_cidrs(OUT) == set(cidrs):
        print("unchanged (%d CIDRs); file left as-is" % len(cidrs))
        return 0

    now = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")
    os.makedirs(os.path.dirname(OUT), exist_ok=True)
    with open(OUT, "w") as f:
        f.write("# Datacenter (cloud) CIDR ranges for ngsite/datacenter.py.\n")
        f.write("# Sources: Google Cloud (gstatic cloud.json) + AWS ip-ranges.json\n")
        f.write("# Generated: %s by scripts/refresh-datacenter-cidrs.py\n" % now)
        f.write("# %d CIDRs (%d IPv4, %d IPv6). Do not hand-edit; re-run the script.\n"
                % (len(cidrs), v4, v6))
        for c in cidrs:
            f.write(c + "\n")
    print("wrote", os.path.relpath(OUT, HERE), "(%d CIDRs)" % len(cidrs))
    return 0


if __name__ == "__main__":
    sys.exit(main())
