← back to Agent Cabinet

front-page-audit/screens/crcp/repro/serve-real.cjs

21 lines

const http=require("http");
const SID=process.env.CRCP_SID||"(set CRCP_SID env var)";
const UP="http://127.0.0.1:9912";
const fs=require("fs");
http.createServer((req,res)=>{
  const p=req.url;
  if(p==="/"||p.startsWith("/?")){
    res.writeHead(200,{"Content-Type":"text/html"});
    res.end(fs.readFileSync(__dirname+"/real-index.html"));
    return;
  }
  // proxy everything else to upstream with the cookie
  const http2=require("http");
  const u=new URL(UP+p);
  const preq=http2.request({hostname:u.hostname,port:u.port,path:u.pathname+u.search,method:req.method,headers:{Cookie:"crcp_sid="+SID}},(pr)=>{
    res.writeHead(pr.statusCode,pr.headers); pr.pipe(res);
  });
  preq.on("error",e=>{res.writeHead(502);res.end("proxy err "+e.message);});
  req.pipe(preq);
}).listen(0,"127.0.0.1",function(){console.log("REALPORT="+this.address().port);});