import os,sys,json,urllib.request,concurrent.futures,random
UA='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36'
BASE='https://h5cq.cqgame.games/h5/'
codes=sorted(os.listdir('shells'))
codes=[c.split('.')[0] for c in codes]
random.seed(7)
sel=sorted(random.sample(codes,25),key=lambda x:int(x))
print('SEL',len(sel))

def get(url,ref,timeout=20):
    r=urllib.request.Request(url,headers={'User-Agent':UA,'Referer':ref})
    try:
        with urllib.request.urlopen(r,timeout=timeout) as f:
            return f.status,f.read()
    except urllib.error.HTTPError as e:
        return e.code,e.read(4096)
    except Exception as e:
        return 0,str(e).encode()

def probe(c):
    ref=BASE+c+'/index.html'
    st,body=get(BASE+c+'/assets/json/setting.json',ref)
    ok = st==200 and body.lstrip()[:1]==b'{'
    return c,st,len(body),ok,body if ok else b''

os.makedirs('assets/json',exist_ok=True)
hits=[]
with concurrent.futures.ThreadPoolExecutor(10) as ex:
    for c,st,n,ok,body in ex.map(probe,sel):
        print(c,st,n,'OK' if ok else '')
        if ok:
            open('assets/json/'+c+'.json','wb').write(body)
            hits.append(c)
print('HITS',len(hits),' '.join(hits))
open('assets/hits.txt','w').write('\n'.join(hits)+'\n')
