import asyncio, re, json
import aiohttp

SRC = "/root/recon/plesk_stats.tsv"
OUT = "/root/recon/sites.tsv"
SEM = asyncio.Semaphore(15)
JUNK = re.compile(r"(?i)^(plesk|parallels panel|web server|localhost|ip-|vps|server\.|ns\d|mail\.|webmail\.|www\.plesk|plesk\.page|secureserver\.net|amazonaws\.com|contaboserver|vultrusercontent|hostway|ip-1|ip-2)")


def domains():
    d = set()
    for line in open(SRC):
        p = line.rstrip("\n").split("\t")
        if len(p) < 5:
            continue
        for field in (p[4], p[5] if len(p) > 5 else ""):
            for name in field.split(","):
                name = name.strip().lower().rstrip(".")
                if not name or "." not in name:
                    continue
                if JUNK.search(name) or name.endswith((".local", ".lan", ".internal")):
                    continue
                if name.startswith("*"):
                    name = name[2:]
                d.add(name)
    return sorted(d)


async def fetch(sess, url, read=40000):
    async with sess.get(url, ssl=False, allow_redirects=True) as r:
        raw = await r.content.read(read)
        return r.status, str(r.url), dict(r.headers), raw.decode("utf-8", "replace")


async def probe(sess, dom):
    rec = {"dom": dom}
    base = None
    for scheme in ("https", "http"):
        try:
            st, url, h, t = await fetch(sess, f"{scheme}://{dom}/")
            rec.update(scheme=scheme, st=st, url=url[:80], server=h.get("Server", "")[:30])
            base = f"{scheme}://{dom}"
            break
        except Exception:
            continue
    if base is None:
        rec["err"] = "connect"
        return rec
    m = re.search(r"(?is)<title[^>]*>\s*(.*?)\s*</title>", t)
    rec["title"] = re.sub(r"\s+", " ", m.group(1))[:50] if m else ""
    g = re.search(r'(?i)<meta[^>]+name=["\']generator["\'][^>]+content=["\']([^"\']+)', t)
    rec["gen"] = g.group(1)[:40] if g else ""
    low = t.lower()
    rec["wp"] = "wp-content" in low or "wp-includes" in low
    rec["cms"] = ("wordpress" if rec["wp"] else
                  "joomla" if "/media/system/js/" in low else
                  "drupal" if "drupal" in low else
                  "bitrix" if "bitrix" in low else "")
    if rec["wp"]:
        for path, key in (("/wp-json/", "wpjson"), ("/wp-json/wp/v2/users?per_page=5", "wpusers"),
                          ("/readme.html", "readme"), ("/xmlrpc.php", "xmlrpc"),
                          ("/.env", "env"), ("/wp-config.php.bak", "baks")):
            try:
                st2, _, h2, t2 = await fetch(sess, base + path, 8000)
                rec[key] = st2
                if key == "readme" and st2 == 200:
                    v = re.search(r"(?i)version\s*([0-9.]+)", t2)
                    rec["wpver"] = v.group(1) if v else ""
                if key == "wpusers" and st2 == 200:
                    try:
                        rec["usern"] = len(json.loads(t2))
                    except Exception:
                        rec["usern"] = "?"
                if key == "env" and st2 == 200:
                    rec["env_leak"] = "yes" if ("APP_" in t2 or "DB_" in t2) else ""
            except Exception:
                rec[key] = 0
    return rec


async def main():
    doms = domains()
    print("domains:", len(doms))
    tout = aiohttp.ClientTimeout(total=20, connect=8, sock_read=12)
    rows = []
    async with aiohttp.ClientSession(timeout=tout, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}) as s:
        async def g(d):
            async with SEM:
                try:
                    return await probe(s, d)
                except Exception:
                    return {"dom": d, "err": "probe"}
        res = await asyncio.gather(*[g(d) for d in doms])
    for r in res:
        rows.append("\t".join(f"{k}={v}" for k, v in r.items()))
        flag = ""
        if r.get("wp"):
            flag = f"WP ver={r.get('wpver','')} users={r.get('usern','')} json={r.get('wpjson','')} env={r.get('env','')} bak={r.get('baks','')}"
        print(f"{r['dom'][:40]:40s} {r.get('st','')} {r.get('cms',''):9s} {r.get('gen','')[:22]:22s} {flag}")
    open(OUT, "w").write("\n".join(rows))
    print("saved", OUT)

asyncio.run(main())
