import concurrent.futures, json, subprocess, sys, time

hosts = json.loads(sys.argv[1])
def check(row):
    host, expected = row
    r = subprocess.run(['curl', '--noproxy', '*', '-sS', '-I', '--connect-timeout', '4', '--max-time', '9', '-o', '/dev/null', '-w', '%{http_code}|%{time_total}|%{remote_ip}', 'https://' + host], capture_output=True, text=True)
    return dict(host=host, expected=expected, result=r.stdout.strip(), code=r.returncode, error=r.stderr.strip()[:180])

results=[]
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as ex:
    for row in ex.map(check, hosts):
        results.append(row)
        print(json.dumps(row), flush=True)
print('AUDIT_COMPLETE '+str(len(results)), flush=True)
