import asyncio, base64, re, sys, os
import aiohttp

LIMIT = int(sys.argv[1]) if len(sys.argv) > 1 else 0
ips = sorted({l.split('\t')[0] for f in os.listdir('.') if f.startswith('forti_hits_') and f.endswith('.tsv')
              for l in open(f, errors='replace') if l.strip()})
if LIMIT:
    ips = ips[:LIMIT]
print("ips", len(ips), flush=True)
SEM = asyncio.Semaphore(150)
OUT = open('saml_issuers.tsv', 'w')
LOCK = asyncio.Lock()
CNT = {'ok': 0, 'err': 0}

async def one(s, ip):
    url = "https://%s/saml/login-forticloud?fallback=https%%3A%%2F%%2F%s%%2Flogin&host=%s&redir=%%2F" % (ip, ip, ip)
    try:
        async with SEM:
            async with s.get(url, ssl=False, timeout=aiohttp.ClientTimeout(total=12)) as r:
                if r.status != 200:
                    body = ''
                else:
                    body = await r.text(errors='replace')
    except Exception:
        CNT['err'] += 1
        return
    m = re.search(r'name="SAMLRequest" value="([^"]+)"', body)
    if not m:
        CNT['err'] += 1
        return
    try:
        x = base64.b64decode(m.group(1)).decode('utf-8', 'replace')
    except Exception:
        CNT['err'] += 1
        return
    iss = re.search(r'<saml:Issuer>([^<]+)</saml:Issuer>', x)
    iss = iss.group(1) if iss else 'unknown'
    CNT['ok'] += 1
    async with LOCK:
        OUT.write("%s\t%s\n" % (ip, iss))
        OUT.flush()

async def main():
    conn = aiohttp.TCPConnector(limit=150, force_close=True)
    async with aiohttp.ClientSession(connector=conn) as s:
        await asyncio.gather(*[one(s, ip) for ip in ips])
    OUT.close()
    print("done", CNT, flush=True)

asyncio.run(main())
