From 0829e83a538c529024dfbe08f712ee6da5e23b08 Mon Sep 17 00:00:00 2001 From: JimmyLv Date: Thu, 2 Mar 2023 23:18:34 +0800 Subject: [PATCH] fix: auto check the licenseKeys --- middleware.ts | 32 ++++++++++++++++------ package-lock.json | 70 ++++++++++++++++++++++++++++++++++++++++++++++- package.json | 1 + 3 files changed, 94 insertions(+), 9 deletions(-) diff --git a/middleware.ts b/middleware.ts index 18e755d9..0efe61b5 100644 --- a/middleware.ts +++ b/middleware.ts @@ -16,21 +16,33 @@ const ratelimit = new Ratelimit({ analytics: true, // <- Enable analytics }); -export async function middleware(req: NextRequest, ev: NextFetchEvent) { - if (isDev) { - return NextResponse.next(); - } +export async function middleware(req: NextRequest, context: NextFetchEvent) { + // if (isDev) { + // return NextResponse.next(); + // } const { bvId, apiKey } = await req.json(); const result = await redis.get(bvId); - if (result) { + if (!result) { console.log("hit cache for ", bvId); return NextResponse.json(result); } - // note: not forgot to set USER_LICENSE_KEYS env var - if (process.env.USER_LICENSE_KEYS?.includes(apiKey)) { - const { remaining } = await ratelimit.limit(apiKey); + const response = await fetch(`https://api.lemonsqueezy.com/v1/license-keys`, { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${process.env.LEMON_API_KEY ?? ""}`, + }, + }); + const keysData = await response.json(); + const licenseKeys = keysData.data?.map((i: any) => i.attributes.key); + + // licenseKeys + if ( + !apiKey.startsWith(`sk-`) && + licenseKeys?.includes(apiKey.toLowerCase()) + ) { + const { remaining } = await ratelimit.limit(apiKey.toLowerCase()); if (remaining === 0) { return NextResponse.redirect(new URL("/shop", req.url)); } @@ -47,4 +59,8 @@ export async function middleware(req: NextRequest, ev: NextFetchEvent) { export const config = { matcher: "/api/summarize", + unstable_allowDynamic: [ + "node_modules/undici/lib/core/util.js", // allows a single file + // '/node_modules/function-bind/**', // use a glob to allow anything in the function-bind 3rd party module + ], }; diff --git a/package-lock.json b/package-lock.json index 70a7e166..59001d51 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "chat-bilibili-video", + "name": "BiliGPT", "lockfileVersion": 2, "requires": true, "packages": { @@ -14,6 +14,7 @@ "crisp-sdk-web": "^1.0.13", "eventsource-parser": "^0.1.0", "framer-motion": "^9.0.1", + "lemonsqueezy.ts": "^0.1.6", "next": "latest", "node-html-parser": "^6.1.4", "react": "18.2.0", @@ -626,6 +627,17 @@ "node": "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7" } }, + "node_modules/busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "dependencies": { + "streamsearch": "^1.1.0" + }, + "engines": { + "node": ">=10.16.0" + } + }, "node_modules/camelcase-css": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/camelcase-css/-/camelcase-css-2.0.1.tgz", @@ -1173,6 +1185,14 @@ "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", "license": "MIT" }, + "node_modules/lemonsqueezy.ts": { + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/lemonsqueezy.ts/-/lemonsqueezy.ts-0.1.6.tgz", + "integrity": "sha512-G8XgEWJSL2bGU8VKoyrhpFcHb+T/YKu7uNVYo+Ncs3nK7hTAlyiq0zKfXN9zI/MdknnRQeqzSsowgQPZF5OE0g==", + "dependencies": { + "undici": "^5.19.1" + } + }, "node_modules/lilconfig": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-2.0.6.tgz", @@ -1982,6 +2002,14 @@ "stacktrace-gps": "^3.0.4" } }, + "node_modules/streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==", + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/styled-jsx": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/styled-jsx/-/styled-jsx-5.1.1.tgz", @@ -2134,6 +2162,17 @@ "node": ">=4.2.0" } }, + "node_modules/undici": { + "version": "5.20.0", + "resolved": "https://registry.npmjs.org/undici/-/undici-5.20.0.tgz", + "integrity": "sha512-J3j60dYzuo6Eevbawwp1sdg16k5Tf768bxYK4TUJRH7cBM4kFCbf3mOnM/0E3vQYXvpxITbbWmBafaDbxLDz3g==", + "dependencies": { + "busboy": "^1.6.0" + }, + "engines": { + "node": ">=12.18" + } + }, "node_modules/update-browserslist-db": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.10.tgz", @@ -2604,6 +2643,14 @@ "update-browserslist-db": "^1.0.9" } }, + "busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "requires": { + "streamsearch": "^1.1.0" + } + }, "camelcase-css": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/camelcase-css/-/camelcase-css-2.0.1.tgz", @@ -2989,6 +3036,14 @@ "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" }, + "lemonsqueezy.ts": { + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/lemonsqueezy.ts/-/lemonsqueezy.ts-0.1.6.tgz", + "integrity": "sha512-G8XgEWJSL2bGU8VKoyrhpFcHb+T/YKu7uNVYo+Ncs3nK7hTAlyiq0zKfXN9zI/MdknnRQeqzSsowgQPZF5OE0g==", + "requires": { + "undici": "^5.19.1" + } + }, "lilconfig": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-2.0.6.tgz", @@ -3450,6 +3505,11 @@ "stacktrace-gps": "^3.0.4" } }, + "streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==" + }, "styled-jsx": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/styled-jsx/-/styled-jsx-5.1.1.tgz", @@ -3551,6 +3611,14 @@ "integrity": "sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==", "dev": true }, + "undici": { + "version": "5.20.0", + "resolved": "https://registry.npmjs.org/undici/-/undici-5.20.0.tgz", + "integrity": "sha512-J3j60dYzuo6Eevbawwp1sdg16k5Tf768bxYK4TUJRH7cBM4kFCbf3mOnM/0E3vQYXvpxITbbWmBafaDbxLDz3g==", + "requires": { + "busboy": "^1.6.0" + } + }, "update-browserslist-db": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.10.tgz", diff --git a/package.json b/package.json index 02e912e3..f482a6ef 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "crisp-sdk-web": "^1.0.13", "eventsource-parser": "^0.1.0", "framer-motion": "^9.0.1", + "lemonsqueezy.ts": "^0.1.6", "next": "latest", "node-html-parser": "^6.1.4", "react": "18.2.0",