import asyncio
import os
import re
import sys
import time

import aiohttp

LIST = "/root/recon/scan_p443.list"
SHARD = int(sys.argv[1]) if len(sys.argv) > 1 else 0
NSHARD = int(sys.argv[2]) if len(sys.argv) > 2 else 1
LIMIT = int(os.environ.get("LIMIT", "0"))
CONC = int(os.environ.get("CONC", "1500"))
TOT = float(os.environ.get("TOT", "8"))
CONNT = float(os.environ.get("CONNT", "4"))
READ = float(os.environ.get("READ", "6"))
OUT = "/root/recon/forti_hits_%d.tsv" % SHARD
LOG = "/root/recon/forti_shard_%d.log" % SHARD

TITLE = re.compile(r"<title>([^<]{0,90})", re.I)
stats = {"done": 0, "hit": 0, "err": 0, "code": {}}
sem = None


def log(msg):
    with open(LOG, "a") as f:
        f.write("%s %s\n" % (time.strftime("%H:%M:%S"), msg))


def match(txt, url, server):
    t = txt.lower()
    if "fortiweb" in t:
        return "fortiweb"
    if "fortigate" in t or "fortissl" in t or "/remote/logincheck" in t:
        return "fortigate"
    if "forti" in t and ("sslvpn" in t or "vpn" in t):
        return "forti-other"
    if "fortinet" in server.lower():
        return "server-fortinet"
    if "/remote/login" in url.lower():
        return "redirect-forti"
    return ""


async def one(sess, ip):
    try:
        async with sem:
            async with sess.get("https://%s/" % ip, ssl=False, allow_redirects=True,
                                max_redirects=2) as r:
                body = await r.content.read(6144)
                txt = body.decode("utf-8", "ignore")
                url = str(r.url)
                k = str(r.status)
                stats["code"][k] = stats["code"].get(k, 0) + 1
                kind = match(txt, url, r.headers.get("Server", ""))
                if kind:
                    t = TITLE.search(txt)
                    with open(OUT, "a") as f:
                        f.write("%s\t%d\t%s\t%s\t%s\n" % (
                            ip, r.status, kind, url[:120],
                            (t.group(1).strip().replace("\t", " ") if t else "")))
                    stats["hit"] += 1
    except Exception as e:
        stats["err"] += 1
        n = type(e).__name__
        if n == "TypeError":
            n = "TypeError:" + str(e)[:60]
        stats["code"][n] = stats["code"].get(n, 0) + 1
    finally:
        stats["done"] += 1


async def reporter(t0):
    while True:
        await asyncio.sleep(30)
        el = time.time() - t0
        log("done=%d hits=%d err=%d rate=%.0f/s %s" % (
            stats["done"], stats["hit"], stats["err"],
            stats["done"] / el if el else 0, stats["code"]))
        stats["code"] = {}


def ips():
    idx = 0
    with open(LIST, "r") as f:
        for line in f:
            if line.startswith("#"):
                continue
            idx += 1
            if LIMIT and idx > LIMIT:
                return
            if idx % NSHARD != SHARD:
                continue
            p = line.split()
            if len(p) >= 4:
                yield p[3]


async def worker(sess, it):
    for ip in it:
        await one(sess, ip)


async def main():
    global sem
    sem = asyncio.Semaphore(CONC)
    t0 = time.time()
    to = aiohttp.ClientTimeout(total=TOT, connect=CONNT, sock_read=READ)
    conn = aiohttp.TCPConnector(limit=0, ssl=False, force_close=True)
    rep = asyncio.ensure_future(reporter(t0))
    async with aiohttp.ClientSession(timeout=to, connector=conn,
                                     headers={"User-Agent": "Mozilla/5.0"}) as sess:
        it = ips()
        await asyncio.gather(*[worker(sess, it) for _ in range(CONC)])
    rep.cancel()
    el = time.time() - t0
    log("FINISH done=%d hits=%d err=%d secs=%.0f rate=%.0f/s" % (
        stats["done"], stats["hit"], stats["err"], el,
        stats["done"] / el if el else 0))


asyncio.run(main())
