import asyncio
import aiohttp

SEM = asyncio.Semaphore(25)

def load():
    out = []
    for line in open("/root/recon/lists/zimbra.txt"):
        p = line.rstrip("\n").split("\t")
        if len(p) >= 3:
            out.append((p[0], int(p[1]), p[2]))
    return out

async def one(sess, h):
    ip, port, sc = h
    for base in (f"{sc}://{ip}:{port}", f"{'https' if sc == 'http' else 'http'}://{ip}:{port}"):
        try:
            async with sess.get(base + "/login_up.php", ssl=False) as r:
                t = await r.text()
            return ip, port, base, r.status, len(t), "incorrect" in t.lower()
        except Exception:
            continue
    return ip, port, "", 0, 0, False

async def main():
    hosts = load()
    tout = aiohttp.ClientTimeout(total=15, connect=6, sock_read=10)
    out = []
    async with aiohttp.ClientSession(timeout=tout, headers={"User-Agent": "Mozilla/5.0"}) as s:
        async def g(h):
            async with SEM:
                return await one(s, h)
        res = await asyncio.gather(*[g(h) for h in hosts])
    for r in res:
        out.append("\t".join(map(str, r)))
    open("/root/recon/plesk_login_sizes.tsv", "w").write("\n".join(out))
    big = [r for r in res if r[4] > 20000]
    mid = [r for r in res if 8000 < r[4] <= 20000]
    small = [r for r in res if 0 < r[4] <= 8000]
    err = [r for r in res if r[4] == 0]
    print("total", len(res), "SPA>20k", len(big), "8-20k", len(mid), "<=8k", len(small), "err", len(err))
    for r in big[:10]:
        print("  big", r[0], r[3], r[4])

asyncio.run(main())
