From 4d10b10d9ec7a64956e164bc39cec2a1abdf8adb Mon Sep 17 00:00:00 2001 From: Pawan Osman Date: Sat, 20 Apr 2024 23:33:22 +0300 Subject: [PATCH] Add: verification client api_key access function set API_KEY in .env file to enable this function Unsetting or setting API_KEY to null will keep the default of allowing anonymous access. Changes to be committed: modified: .env.example modified: src/app.ts --- .env.example | 2 ++ src/app.ts | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index e11d37f..8aea50f 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,7 @@ SERVER_PORT=3040 USER_AGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" +NEW_SESSION_RETRIES=5 +API_KEY= CLOUDFLARED=true PROXY=false diff --git a/src/app.ts b/src/app.ts index 22ebe56..409e311 100644 --- a/src/app.ts +++ b/src/app.ts @@ -18,9 +18,13 @@ const baseUrl = "https://chat.openai.com"; const apiUrl = `${baseUrl}/backend-anon/conversation`; const refreshInterval = 60000; // Interval to refresh token in ms const errorWait = 120000; // Wait time in ms after an error +const newSessionRetries: number = + parseInt(process.env.NEW_SESSION_RETRIES) || 5; // Number of retries to get a new session const userAgent = process.env.USER_AGENT || "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"; +const authKey: string = + process.env.API_KEY || null; // Authorized client apiKey let cloudflared: ChildProcessWithoutNullStreams; @@ -162,7 +166,7 @@ function GenerateProofToken( } // Function to get a new session ID and token from the OpenAI API -async function getNewSession(): Promise { +async function getNewSession(retries: number = 0): Promise { let newDeviceId = randomUUID(); try { const response = await axiosInstance.post( @@ -178,7 +182,8 @@ async function getNewSession(): Promise { return session; } catch (error) { - return null; + await wait(500); + return retries < newSessionRetries ? getNewSession(retries + 1) : null; } } @@ -195,6 +200,32 @@ function enableCORS(req: Request, res: Response, next: NextFunction) { // Middleware to handle chat completions async function handleChatCompletion(req: Request, res: Response) { + // If .env sets API_KEY and is not empty, the apiKey of req.headers will be verified. + if (authKey) { + const clientApiKey = req.headers.authorization?.split(' ')[1] ?? "null"; + if (!clientApiKey || clientApiKey != authKey) { + console.log( + "Request:", + `${req.method} ${req.originalUrl}`, + `${req.body?.messages?.length ?? 0} messages`, + `ClientKey: ${clientApiKey} Verify Failed!` + ); + + res.write( + JSON.stringify({ + status: false, + error: { + message: `Incorrect API key provided: ${clientApiKey}, Authorized access only!`, + type: "invalid_request_error", + code: "invalid_api_key" + }, + support: "https://discord.pawan.krd", + }) + ); + return res.end(); + } + } + console.log( "Request:", `${req.method} ${req.originalUrl}`, @@ -216,7 +247,7 @@ async function handleChatCompletion(req: Request, res: Response) { }) ); - res.end(); + return res.end(); } let proofToken = GenerateProofToken( @@ -399,7 +430,7 @@ async function handleChatCompletion(req: Request, res: Response) { status: false, error: { message: - "An error occurred. Please check the server console to confirm it is ready and free of errors. Additionally, ensure that your request complies with OpenAI's policy.", + "An error occurred. please try again. Additionally, ensure that your request complies with OpenAI's policy.", type: "invalid_request_error", }, support: "https://discord.pawan.krd",