-
Notifications
You must be signed in to change notification settings - Fork 8
/
production.js
9 lines (6 loc) · 8.04 KB
/
production.js
1
2
3
4
5
6
7
8
9
addEventListener("fetch",e=>{e.respondWith(handleRequest(e.request).catch(({stack:e})=>new Response(e,{headers:{"Content-Type":"text/plain;charset=utf8","Access-Control-Allow-Origin":"*","Access-Control-Allow-Headers":"*"},status:500})))});async function handleRequest(e){let t={"Content-Type":"application/json","Access-Control-Allow-Origin":"*","Access-Control-Allow-Headers":"*"};if(e.url===`${new URL(e.url).origin}/`){let a=await e.clone().text(),n={url:e.url,method:e.method,data:a,headers:Object.fromEntries(e.headers),cf:Object.fromEntries(Object.entries(e.cf))};return new Response(JSON.stringify(n,null,2),{headers:t})}function o(e){var a;let n={400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",408:"Request Timeout",410:"Gone",413:"Payload Too Large",429:"Too Many Requests",500:"Internal Server Error",502:"Bad Gateway",503:"Service Unavailable"};return n[e]?(a=n[e],new Response(JSON.stringify({error:{message:`${e} ${a}`,code:e}},null,2),{headers:t,status:e})):null}if("OPTIONS"==e.method)return new Response(null,{headers:{"Content-Type":"text/plain;charset=utf8","Access-Control-Allow-Origin":"*","Access-Control-Allow-Headers":"*","Access-Control-Allow-Methods":"GET,POST,OPTIONS"},status:204});if("POST"!==e.method&&"GET"!==e.method)return o(405);let s=new URL(e.url),r=["completions","generations","transcriptions","edits","embeddings","translations","variations","files","fine-tunes","moderations","models","listBaseModel","openai","kamiya","generate","conversation","3p","bjq","ato"],i=await e.clone().text(),h=s.pathname.split("?")[0].split("/").pop(),p="undefined"!=typeof API_KEY&&""!==API_KEY?API_KEY.split(","):["sk-xxxxxxxxxx"],l="undefined"!=typeof GPTAPI_KEY&&""!==GPTAPI_KEY?GPTAPI_KEY:"sk-xxxxxxxxxx",c="undefined"!=typeof BJQ_KEY&&""!==BJQ_KEY?BJQ_KEY:"sk-xxxxxxxxxx",d="undefined"!=typeof OMG_KEY&&""!==OMG_KEY?OMG_KEY:"sk-xxxxxxxxxx",m="undefined"!=typeof GPTAPI_TOKEN&&""!==GPTAPI_TOKEN?GPTAPI_TOKEN:"abcdefg",u="undefined"!=typeof KAMIYA_TOKEN&&""!==KAMIYA_TOKEN?KAMIYA_TOKEN.split(","):["sk-xxxxxxxxxx"],g="undefined"!=typeof PASSWORD&&""!==PASSWORD?PASSWORD.split(","):["cpcw"],f=`Bearer ${p[Math.floor(Math.random()*p.length)]}`,x=`Bearer ${u[Math.floor(Math.random()*u.length)]}`;if(r.includes(h)&&("POST"===e.method||"GET"===e.method)){let w=e.headers.get("Authorization");if(!w)return o(401);let y=w.substring(7);g.includes(y)||(f=w,x=w,l=w,c=w,d=w)}else if(!r.includes(h)&&!["usage","getDetails","history","subscription","info"].includes(h))return o(403);let b=Object.fromEntries(e.headers);switch(Object.assign(b,{"user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36","sec-ch-ua":'"Chromium";v="116", "Google Chrome";v="116", "Not:A-Brand";v="99"',"sec-ch-ua-mobile":"?0","sec-ch-ua-platform":'"macOS"',"sec-fetch-dest":"empty","sec-fetch-mode":"cors","sec-fetch-site":"same-site",Pragma:"no-cache","Cache-Control":"no-cache","content-type":"application/json","X-Forwarded-For":"1.1.1.1","X-Real-IP":"1.1.1.1"}),!0){case s.pathname.startsWith("/ato"):case s.pathname.startsWith("/v1/"):if(!s.pathname.endsWith("/ato")&&!s.pathname.endsWith("/completions")&&!s.pathname.endsWith("/models"))return o(404);if(s.pathname=s.pathname.replace(/^\/ato\//,"/"),s.pathname=s.pathname.replace(/^\/ato$/,"/v1/chat/completions"),"POST"===e.method&&s.pathname.endsWith("/completions")){let $=await e.clone().json(),_=$.model;["gpt-3.5-turbo-16k","gpt-3.5-turbo","gpt-4","gpt-4-32k"].includes(_)?(s.host="api.gptapi.us",Object.assign(b,{authorization:l,origin:"https://bettergpt.chat",referer:"https://bettergpt.chat/",authority:s.host})):(s.host="api.ohmygpt.com",Object.assign(b,{authorization:d,origin:"https://bettergpt.chat",referer:"https://bettergpt.chat/",authority:s.host}))}else s.host="api.ohmygpt.com",Object.assign(b,{authorization:d,origin:"https://bettergpt.chat",referer:"https://bettergpt.chat/",authority:s.host});break;case s.pathname.startsWith("/3p"):if(s.host="api.gptapi.us",s.pathname=s.pathname.replace(/^\/3p\//,"/"),s.pathname=s.pathname.replace(/^\/3p$/,"/v1/chat/completions"),s.pathname.endsWith("/info")){let A=await fetch("https://www.gptapi.us/api/user/self",{headers:{Authorization:`Bearer ${m}`}}),v=await A.json(),k=v.data.quota/5e5;return new Response(k.toFixed(4),{headers:t})}Object.assign(b,{authorization:l,origin:"https://bettergpt.chat",referer:"https://bettergpt.chat/",authority:s.host});break;case s.pathname.startsWith("/omg"):if(s.host="api.ohmygpt.com",s.pathname=s.pathname.replace(/^\/omg\//,"/"),s.pathname=s.pathname.replace(/^\/omg$/,"/v1/chat/completions"),s.pathname.endsWith("/info")){let W=await fetch("https://api.ohmygpt.com/api/v1/user/admin/balance",{headers:{Authorization:`Bearer ${d}`},method:"POST",body:null}),C=await W.json(),S=parseFloat(C.data.balance)/25e4;return new Response(S.toFixed(4),{headers:t})}Object.assign(b,{authorization:d,origin:"https://bettergpt.chat",referer:"https://bettergpt.chat/",authority:s.host});break;case s.pathname.startsWith("/bjq"):if(s.host="api.openai.one",s.pathname=s.pathname.replace(/^\/bjq\//,"/"),s.pathname=s.pathname.replace(/^\/bjq$/,"/v1/chat/completions"),s.pathname.endsWith("/info")){let T=await fetch(`https://price.openai.one/api.php?GetData&Getkeyinfo&key=${c}`),O=await T.json();return new Response(Number(O.remain_quota).toFixed(4),{headers:t})}Object.assign(b,{authorization:c,origin:"https://bettergpt.chat",referer:"https://bettergpt.chat/",authority:s.host});break;case s.pathname.startsWith("/openai"):s.host="api.openai.com",s.pathname=s.pathname.replace(/^\/openai\//,"/"),s.pathname=s.pathname.replace(/^\/openai$/,"/v1/chat/completions"),Object.assign(b,{authorization:f,origin:"https://bettergpt.chat",referer:"https://bettergpt.chat/",authority:s.host});break;case s.pathname.startsWith("/kamiya"):switch(s.host="p0.kamiya.dev",s.pathname=s.pathname.replace(/^\/kamiya\//,"/"),!0){case s.pathname.endsWith("/models"):let q=await fetch("https://gist.githubusercontent.com/DoingDog/5d9f8228d02645bb2ace999de796e5b9/raw/fakeModels.json");return new Response(q.body,{headers:t});case s.pathname.endsWith("/info"):let j=await fetch(`https://${s.host}/api/session/getDetails`,{headers:{Authorization:x}}),z=await j.json();return new Response(z.data.credit.toString(),{headers:t});default:s.pathname=s.pathname.replace(/^\/kamiya$/,"/v1/chat/completions"),s.pathname=s.pathname.replace(/^\/v1\//,"/api/openai/"),Object.assign(b,{authorization:x,origin:"https://chat.kamiya.dev",referer:"https://chat.kamiya.dev/",authority:s.host})}break;default:return o(404)}let P=new Headers;Object.entries(b).forEach(([e,t])=>{P.append(e,t)});let G;try{G=new Request(s.toString(),{headers:P,body:e.body,method:e.method,redirect:"follow"})}catch(E){return o(400)}if("POST"===e.method&&s.pathname.endsWith("/completions")&&i)try{let B=JSON.parse(i),M=B.messages,F=M[M.length-1],I=F.content,R=M[M.length-3]?.content||"";if(I.includes("WS[")||R.includes("WS[")){let H=[];function D(e,t){let a=0,n=e.match(/WS\[[^\]]+\]/g);return n&&n.forEach(e=>{let n=e.slice(3,-1);a<2&&(t.push(n),a++)}),null}if(D(I,H),R&&D(R,H),H.length>=1){let N=[],U=H.length<=2?10:5;for(let L of H)try{let X=await fetch(`https://s0.awsl.app/search?q=${L}&max_results=${U}`),K=await X.json(),J=K.map(({title:e,body:t,href:a})=>`'${e}' : ${t} ; (${a})`).join("\n");N.push(`
[${L}]
${J}`)}catch(Q){return o(502)}F.content=`${I.replace(/WS\[[^\]]*\]/g,"")}
Current date:${new Date().toLocaleString()} UTC
Instructions: Reply to me in the language of my request or question above. Give a comprehensive answer to the question or request I have made above. Below are some results from a web search. Use them if necessary.
${N}`,B.messages[M.length-1]=F,G=new Request(G,{body:JSON.stringify(B)})}}}catch(V){return o(400)}try{let Y=await fetch(G),Z=o(Y.status);if(("POST"===e.method||"GET"===e.method)&&s.pathname.endsWith("/completions")&&Z)return Z;let ee=new Response(Y.body,Y);return ee.headers.set("Access-Control-Allow-Origin",e.headers.get("Access-Control-Allow-Origin")||"*"),ee.headers.set("Access-Control-Allow-Headers",e.headers.get("Access-Control-Allow-Headers")||"*"),ee}catch(et){return o(502)}}