IWBH_01

nodejs RAW HTTP API proxy server

Aug 11th, 2020 (edited)
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if(!global.diid){
  2. var http = require("http"),
  3. https=require("https"),
  4. zlib=require("zlib");
  5. }
  6. var orspnd=function(sc,hed,bdy){
  7.   var hed2={"content-type":"text/plain;charset=utf-8"};
  8.   if(hed)Object.assign(hed2,hed);
  9.   hed2["Access-Control-Allow-Origin"]="*";
  10.   this.writeHead(sc||200,hed2);
  11.   this.write(bdy);
  12.   this.end();
  13. },
  14. _rcb=function(res){
  15.   var buf_=[],rspnd=this.rspnd,rURL=this.url;
  16.  res.on("data",buf_.push.bind(buf_));
  17.  res.on("end",function(){
  18.    var sc=res.statusCode,hds="HTTP "+sc+" "+rURL,hdo=res.headers,cneg=hdo["content-encoding"];
  19.    for(var p in hdo)hds+=("\r\n"+p+": "+hdo[p]);
  20.    hds+="\r\n\r\n";
  21.    if(cneg){ var coa=cneg.replace(/\x20/g,"").split(","),tbf=Buffer.concat(buf_),ec,ef;
  22.      while(coa.length){ec=coa.pop();if(ec=="gzip")ef="gunzip";else if(ec=="deflate")ef="inflate";else if(ec=="br")ef="brotliDecompress"; if(ef)tbf=zlib[ef+"Sync"](tbf);}
  23.      buf_=[tbf];
  24.    }
  25.    buf_.unshift(Buffer.from(hds));
  26.    rspnd(sc,{"content-type":"text/plain","content-encoding":"gzip"},zlib.gzipSync(Buffer.concat(buf_)));
  27.  });
  28. },
  29. errf=function(e){
  30.  this.rspnd(555,{"content-type":"application/JSON"},JSON.stringify(e));
  31. },
  32. odata=function(d){
  33.  this.bdy_dat.push(d);
  34. },
  35. oend=function(){var preq=this.preq;if(preq){
  36.   if(this.bdy_dat.length){preq.write(Buffer.concat(this.bdy_dat));} preq.end();preq.sent=!0;}this.haz_end=!0;
  37. },
  38. lstnr=function(req,rsp){
  39.   var rspnd=orspnd.bind(rsp),or_=" https://"+req.headers.host;
  40.   if(req.url=="/")rspnd(200,0,"RAW HTTP API\r\nUsage:"+or_+"/https/www.example.com/\r\nsend any request method, set any request header with \"x-a-\" prefixing the name, that prefix will be dropped and proxied request will be sent with the header you sent.\r\nAll responses include remote response headers in the response body.\nPut this page in an iframe and use to wake the repl and recieve confirmation that it is awake: "+or_+"/is_woke.html\r\nif you're a real nerd, you can find the source code of this API here: https://repl.it/@TheTranscendent/raw-http-api\r\nand here:  https://pastebin.com/Ka8Ys6jD");
  41.   else if(req.url.toLowerCase()=="/is_woke.html"){
  42.     rspnd(200,{"content-type":"text/html"},"<!Doctype html><html><head>\n\
  43. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><title>is woke</title></head><body>is woke<script type='text/javascript'>parent.postMessage('raw-http-api is woke'"+(req.headers.referer?",'"+req.headers.referer.split("/").slice(0,3).join("/")+"'":"")+");</script></body></html>");
  44.   }else if(req.method=="OPTIONS"){
  45.     var chd="",b;
  46.     if(b=req.headers["access-control-request-headers"])chd=", "+b;
  47.     rspnd(200,{"Access-Control-Allow-Headers":"Authorization, Content-Type"+chd,"Access-Control-Allow-Methods":"GET, POST, PUT, PATCH, DELETE, UPLOAD"},"someday you will run out of options");
  48.   }else{
  49.     var au=req.url.substr(1).split("/"),htp=au[0]=="http",haz_bdy;
  50.     if(htp||au[0]=="https"){
  51.       req.bdy_dat=[];
  52.       if(haz_bdy=req.headers["content-length"]){
  53.        req.on("data",odata);
  54.        req.on("end",oend);
  55.       }else req.haz_end=!0;
  56.       au[0]+=(au[0].endsWith(":")?"":":")+"/";var au2=au.join("/"),headers=Object.assign({},req.headers);
  57.       headers.host=au[1];
  58.       var del=["origin","referer","x-forwarded-for","x-replit-user-id","x-replit-user-name","x-replit-user-roles","x-real-ip"];
  59.       while(del.length)delete headers[del.pop()];
  60.       for(var p in headers) if(p.substr(0,4)=="x-a-"){ headers[p.substr(4)]=headers[p]; delete headers[p]; }
  61.  
  62.       var preq=(htp?http:https).request(au2,{"method":req.method,"headers":headers},_rcb);
  63.       preq.rspnd=rspnd;
  64.       preq.url=au2;
  65.       preq.on("error",errf);
  66.       req.preq=preq;
  67.       if((!haz_bdy)||(req.haz_end&&(!preq.sent)))oend.call(req);
  68.     }else rspnd(200,0,"invalid url");
  69.   }
  70.   global.L_req=req;
  71.   console.log("request recieved");
  72. };
  73.  
  74. if(global.diid)_svr_._events.request=lstnr;
  75. else{
  76. _svr_=http.createServer(lstnr);
  77. _svr_.listen(3000);
  78.  
  79. global.diid=!0;
  80. }
Add Comment
Please, Sign In to add comment