Test_Reverse_Proxy / lib /clewd-superfetch.js
Moonfanz's picture
Upload 20 files
5a4f9f1 verified
raw
history blame
7.26 kB
/*
* https://gitgud.io/ahsk/clewd
* https://github.com/h-a-s-k/clewd
*/"use strict";const{spawn:e}=require("node:child_process"),{randomInt:r}=require("node:crypto"),{relative:t,resolve:s,join:n,normalize:o,basename:i,dirname:a}=require("node:path"),{writeFileSync:d,unlinkSync:c,existsSync:u,mkdirSync:m,rmdirSync:l}=require("node:fs"),{ReadableStream:f}=require("node:stream/web"),p=e=>"win32"===process.platform?".\\"+e:e,h=e=>"win32"===process.platform||e.indexOf(" ")>-1?`"${e}"`:e,w={win32:{x64:"clewd-superfetch-win-amd64.exe",ia32:"clewd-superfetch-win-ia32.exe"},darwin:{x64:"clewd-superfetch-linux-amd64",arm64:"clewd-superfetch-linux-arm64"},linux:{x64:"clewd-superfetch-linux-amd64",arm64:"clewd-superfetch-linux-arm64"},android:{x64:"clewd-superfetch-linux-amd64",arm64:"clewd-superfetch-linux-arm64"}}[process.platform]?.[process.arch],y=""+o(t("./","./bin/"+w)),b=o(s(__dirname,y,"../","../")),S=s(b,y),x=a(S),_=""+o(s(x,"./cfg")),g=""+o(s(x,"./pyld")),v=""+o(s(x,"./hdr")),O=[123,34,115,101,99,45,99,104,45,117,97,34,58,34,92,34,78,111,116,95,65,32,66,114,97,110,100,92,34,59,118,61,92,34,56,92,34,44,32,92,34,67,104,114,111,109,105,117,109,92,34,59,118,61,92,34,49,50,48,92,34,44,32,92,34,71,111,111,103,108,101,32,67,104,114,111,109,101,92,34,59,118,61,92,34,49,50,48,92,34,34,44,34,115,101,99,45,99,104,45,117,97,45,109,111,98,105,108,101,34,58,34,63,48,34,44,34,115,101,99,45,99,104,45,117,97,45,112,108,97,116,102,111,114,109,34,58,34,92,34,109,97,99,79,83,92,34,34,44,34,85,112,103,114,97,100,101,45,73,110,115,101,99,117,114,101,45,82,101,113,117,101,115,116,115,34,58,49,44,34,85,115,101,114,45,65,103,101,110,116,34,58,34,77,111,122,105,108,108,97,47,53,46,48,32,40,77,97,99,105,110,116,111,115,104,59,32,73,110,116,101,108,32,77,97,99,32,79,83,32,88,32,49,48,95,49,53,95,55,41,32,65,112,112,108,101,87,101,98,75,105,116,47,53,51,55,46,51,54,32,40,75,72,84,77,76,44,32,108,105,107,101,32,71,101,99,107,111,41,32,67,104,114,111,109,101,47,49,50,48,46,48,46,48,46,48,32,83,97,102,97,114,105,47,53,51,55,46,51,54,34,44,34,65,99,99,101,112,116,34,58,34,116,101,120,116,47,104,116,109,108,44,97,112,112,108,105,99,97,116,105,111,110,47,120,104,116,109,108,43,120,109,108,44,97,112,112,108,105,99,97,116,105,111,110,47,120,109,108,59,113,61,48,46,57,44,105,109,97,103,101,47,97,118,105,102,44,105,109,97,103,101,47,119,101,98,112,44,105,109,97,103,101,47,97,112,110,103,44,42,47,42,59,113,61,48,46,56,44,97,112,112,108,105,99,97,116,105,111,110,47,115,105,103,110,101,100,45,101,120,99,104,97,110,103,101,59,118,61,98,51,59,113,61,48,46,55,34,44,34,83,101,99,45,70,101,116,99,104,45,83,105,116,101,34,58,34,110,111,110,101,34,44,34,83,101,99,45,70,101,116,99,104,45,77,111,100,101,34,58,34,110,97,118,105,103,97,116,101,34,44,34,83,101,99,45,70,101,116,99,104,45,85,115,101,114,34,58,34,63,49,34,44,34,83,101,99,45,70,101,116,99,104,45,68,101,115,116,34,58,34,100,111,99,117,109,101,110,116,34,44,34,65,99,99,101,112,116,45,69,110,99,111,100,105,110,103,34,58,34,103,122,105,112,44,32,100,101,102,108,97,116,101,44,32,98,114,34,44,34,65,99,99,101,112,116,45,76,97,110,103,117,97,103,101,34,58,34,101,110,45,85,83,44,101,110,59,113,61,48,46,57,34,125],j=[91,34,45,45,99,105,112,104,101,114,115,32,84,76,83,95,65,69,83,95,49,50,56,95,71,67,77,95,83,72,65,50,53,54,44,84,76,83,95,65,69,83,95,50,53,54,95,71,67,77,95,83,72,65,51,56,52,44,84,76,83,95,67,72,65,67,72,65,50,48,95,80,79,76,89,49,51,48,53,95,83,72,65,50,53,54,44,69,67,68,72,69,45,69,67,68,83,65,45,65,69,83,49,50,56,45,71,67,77,45,83,72,65,50,53,54,44,69,67,68,72,69,45,82,83,65,45,65,69,83,49,50,56,45,71,67,77,45,83,72,65,50,53,54,44,69,67,68,72,69,45,69,67,68,83,65,45,65,69,83,50,53,54,45,71,67,77,45,83,72,65,51,56,52,44,69,67,68,72,69,45,82,83,65,45,65,69,83,50,53,54,45,71,67,77,45,83,72,65,51,56,52,44,69,67,68,72,69,45,69,67,68,83,65,45,67,72,65,67,72,65,50,48,45,80,79,76,89,49,51,48,53,44,69,67,68,72,69,45,82,83,65,45,67,72,65,67,72,65,50,48,45,80,79,76,89,49,51,48,53,44,69,67,68,72,69,45,82,83,65,45,65,69,83,49,50,56,45,83,72,65,44,69,67,68,72,69,45,82,83,65,45,65,69,83,50,53,54,45,83,72,65,44,65,69,83,49,50,56,45,71,67,77,45,83,72,65,50,53,54,44,65,69,83,50,53,54,45,71,67,77,45,83,72,65,51,56,52,44,65,69,83,49,50,56,45,83,72,65,44,65,69,83,50,53,54,45,83,72,65,34,44,34,45,45,104,116,116,112,50,34,44,34,45,45,104,116,116,112,50,45,115,101,116,116,105,110,103,115,32,39,49,58,54,53,53,51,54,59,50,58,48,59,52,58,54,50,57,49,52,53,54,59,54,58,50,54,50,49,52,52,39,34,44,34,45,45,104,116,116,112,50,45,119,105,110,100,111,119,45,117,112,100,97,116,101,32,49,53,54,54,51,49,48,53,34,44,34,45,45,99,111,109,112,114,101,115,115,101,100,34,44,34,45,45,101,99,104,32,71,82,69,65,83,69,34,44,34,45,45,116,108,115,118,49,46,50,34,44,34,45,45,97,108,112,115,34,44,34,45,45,116,108,115,45,112,101,114,109,117,116,101,45,101,120,116,101,110,115,105,111,110,115,34,44,34,45,45,99,101,114,116,45,99,111,109,112,114,101,115,115,105,111,110,32,98,114,111,116,108,105,34,93],$=(e=false)=>{if(!w||!u(S)){e&&console.warn(`superfetch [err] unavailable for ${process.platform}-${process.arch}, use 3.8.5 for the time being\n`);return false}e&&console.log(`superfetch [found] ${t(__dirname,S)}\n`);return true},q=(i,a)=>{a.headers??={};"string"!=typeof a.body&&(a.body=a.body?JSON.stringify(a.body):"");if(!$())return;const u=r(1,2e4).toString(),m=o(t(b,s(_,u))),l=o(t(b,s(g,u))),f=o(t(b,s(v,u))),w=o(t("./","bin/ca"));let x={...JSON.parse(Buffer.from(O).toString()),...a.headers};x=Object.entries(x).map((([e,r])=>`${e}: ${r}`));const q=h(p(m)),A=h(p(f)),L=h(p(l)),k=["-v","--http2","--cacert",""+h(p(w)),"--config",""+q,"--header","@"+A],J=[...JSON.parse(Buffer.from(j).toString()),"-X "+(a.method||"GET")];d(n(__dirname,m),J.join("\n"));d(n(__dirname,f),x.join("\n"));a.body&&d(n(__dirname,l),a.body);if("POST"===a.method||"PUT"===a.method||"PATCH"===a.method){k.push("--data");k.push("@"+L)}return new Promise((r=>{const t=e("android"===process.platform?S:y,[...k,""+i],{cwd:b,windowsHide:true,killSignal:"SIGKILL",windowsVerbatimArguments:true,detached:"win32"!==process.platform});t.superfetch=true;t.rape=function(){this.stdout?.end();this.stderr?.end()}.bind(t);t.once("spawn",(()=>{t.stream=a.stream||false;t.id=u;if(t.stream){Object.defineProperty(t,"body",{get:()=>t.stdout});return r(t)}t.body="";t.stdout.on("data",(e=>t.body+=e.toString()));t.json=async()=>JSON.parse(t.body);t.text=async()=>t.body;t.stdout.once("end",(()=>r(t)))}));t.once("error",(e=>{console.warn("superfetch [err]",e)}));t.once("close",(()=>{(e=>{try{c(n(__dirname,m));c(n(__dirname,f));a.body&&c(n(__dirname,l))}catch(e){}e.stdout.removeAllListeners();e.stderr.removeAllListeners();e.stream&&e.body.removeAllListeners()})(t)}));t.stderr.on("data",(e=>{const r=/HTTP\/2 (\d{3})+/g,s=(e=e.toString().trim()).match(r);if(!t.status&&s){const s=r.exec(e);t.status=+s[1]}const n=/(?:< )(.+?)(?:: )(.+)/g,o=e.match(n);if(o){const e={};o.forEach((r=>{const t=r.split(n);e[t?.[1]]=t?.[2]}));t.headers=e}}))}))};module.exports={SuperfetchFoldersMk:()=>{m(_,{recursive:true});m(g,{recursive:true});m(v,{recursive:true})},SuperfetchFoldersRm:()=>{l(_,{recursive:true});l(g,{recursive:true});l(v,{recursive:true})},ClewdSuperfetch:q,SuperfetchAvailable:$,SuperfetchRelative:y};