import asyncio, re, json
import aiohttp

SITES = ["abdatapartnerships.ca", "azleabsolutely.com", "bradyds.com", "dockon.com",
         "llc.net", "rogueklamathtransfer.com", "smileshoponline.com", "talkandhra.com",
         "nreach.com", "download.cdxnashville.com"]
SEM = asyncio.Semaphore(0) if False else asyncio.Semaphore(8)


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


def slugs(html, kind):
    pat = rf"/wp-content/{kind}/([a-z0-9\-_]+)/"
    return sorted(set(re.findall(pat, html.lower())))


async def readme_ver(sess, base, kind, slug):
    for name in ("readme.txt", "README.txt"):
        try:
            st, t = await fetch(sess, f"{base}/wp-content/{kind}/{slug}/{name}", 20000)
            if st == 200 and len(t) > 50:
                m = re.search(r"(?im)^\s*stable tag:\s*([0-9][0-9a-z.\-]*)", t)
                v = m.group(1) if m else ""
                if not v:
                    m2 = re.search(r"(?im)^\s*version:\s*([0-9][0-9a-z.\-]*)", t)
                    v = m2.group(1) if m2 else ""
                return v
        except Exception:
            pass
    return ""


async def probe(sess, dom):
    out = {"dom": dom, "plugins": {}, "themes": {}, "users": []}
    base = None
    html = ""
    for scheme in ("https", "http"):
        try:
            st, html = await fetch(sess, f"{scheme}://{dom}/")
            base = f"{scheme}://{dom}"
            break
        except Exception:
            continue
    if not base:
        out["err"] = "connect"
        return out
    pl = slugs(html, "plugins")
    th = slugs(html, "themes")
    for kind, items, key in (("plugins", pl, "plugins"), ("themes", th, "themes")):
        for slug in items[:15]:
            v = await readme_ver(sess, base, kind, slug)
            out[key][slug] = v
    # user enumeration
    try:
        st, t = await fetch(sess, base + "/wp-json/wp/v2/users?per_page=100")
        if st == 200:
            j = json.loads(t)
            out["users"] = [u.get("slug") or u.get("name") for u in j][:20]
    except Exception:
        pass
    if not out["users"]:
        try:
            st, t = await fetch(sess, base + "/?author=1")
            m = re.search(r"/author/([a-z0-9\-_.]+)/", t, re.I)
            if m:
                out["users"] = [m.group(1)]
        except Exception:
            pass
    return out


async def main():
    tout = aiohttp.ClientTimeout(total=25, connect=8, sock_read=15)
    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 SITES])
    for r in res:
        print(f"=== {r['dom']} {r.get('err','')}")
        print("   users:", r.get("users"))
        if r.get("plugins"):
            print("   plugins:", ", ".join(f"{k}={v or '?'}" for k, v in r["plugins"].items()))
        if r.get("themes"):
            print("   themes:", ", ".join(f"{k}={v or '?'}" for k, v in r["themes"].items()))
    open("/root/recon/wp_dig.json", "w").write(json.dumps(res, indent=1))

asyncio.run(main())
