#!/usr/bin/env python3
"""DAS: periksa berkas skrip asing di /qhuv/ — termasuk isinya, dan hubungkan dengan error console."""
import json, re, urllib.request, urllib.error

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}
SRC = [
    "https://dasindonesia.com/qhuv/",
    "https://dasindonesia.com/qhuv/K7r7ZS-kPRi9VTyy27aDH1xkTkIu7bd932YJcYopIfpPSBfD",
    "https://dasindonesia.com/qhuv/K7r7ZS-kOxu9LErPubKMezhyC1lrs_I8zH9ZeNt6LrANF8M",
]

for u in SRC:
    print(f"\n=== {u} ===")
    try:
        r = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=90)
        isi = r.read()
        print(f"   status {r.status} | {len(isi)} byte | tipe: {r.headers.get('content-type')}")
        coba = isi.decode("utf-8", "replace")
        print("   kepala:", re.sub(r"\s+", " ", coba[:300]))
        # tanda-tanda mencurigakan
        tanda = {
            "eval(": len(re.findall(r"\beval\s*\(", coba)),
            "atob(": len(re.findall(r"atob\s*\(", coba)),
            "base64 panjang": len(re.findall(r"[A-Za-z0-9+/]{200,}={0,2}", coba)),
            "document.write": len(re.findall(r"document\.write", coba)),
            "redirect ke domain lain": len(re.findall(r"(?:location\s*=|location\.replace|window\.open)\s*\(?[^)]{0,60}https?://", coba)),
            "kata kunci pertambangan": len(re.findall(r"coinhive|cryptonight|miner|webasm|Worker\(", coba, re.I)),
            "kirim data keluar": len(re.findall(r"fetch\(|XMLHttpRequest|navigator\.sendBeacon", coba)),
            "alamat luar": sorted(set(re.findall(r"https?://([a-z0-9.\-]+)", coba)))[:12],
        }
        for k, v in tanda.items():
            print(f"      {k:<26} {v}")
    except urllib.error.HTTPError as e:
        print(f"   HTTP {e.code}")
    except Exception as e:
        print(f"   GAGAL {str(e)[:80]}")

print("\n=== error console dari Lighthouse (lengkap) ===")
d = json.load(open("/home/mantapman/.hermes/profiles/atun/workspace/drafts/lh_das_beranda.json"))
for it in ((d.get("audits", {}).get("errors-in-console", {}).get("details", {}) or {}).get("items") or []):
    print("   ", it.get("source"), "|", str(it.get("description"))[:300].replace("\n", " | "))
