mirror of
https://github.com/sern-handler/frontpage-bot
synced 2026-06-06 01:16:54 +00:00
feat: redeploy on switch and ratelimiter
This commit is contained in:
@@ -25,7 +25,7 @@
|
||||
"discord-api-types": "^0.37.84",
|
||||
"lucia": "^3.1.1",
|
||||
"lucide-react": "^0.368.0",
|
||||
"next": "^14.2.3",
|
||||
"next": "^14.2.5",
|
||||
"next-themes": "^0.3.0",
|
||||
"oslo": "^1.2.0",
|
||||
"react": "^18",
|
||||
|
||||
@@ -1,14 +1,30 @@
|
||||
import prisma from "@/lib/db";
|
||||
import { RateLimiter } from '@/lib/ratelimiter'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
export async function GET() {
|
||||
// 5 requests per minute
|
||||
const rateLimiter = new RateLimiter({
|
||||
windowSize: 60 * 1000,
|
||||
maxRequests: 5,
|
||||
})
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const ip = req.headers.get('X-Forwarded-For') ?? 'unknown';
|
||||
const isRateLimited = rateLimiter.limit(ip);
|
||||
|
||||
if (isRateLimited) {
|
||||
return new Response(JSON.stringify({ success: false, message: 'rate limited!' }), {
|
||||
status: 429,
|
||||
});
|
||||
}
|
||||
|
||||
const dbFetch = await prisma.bot.findMany({
|
||||
where: {
|
||||
verified: true
|
||||
}
|
||||
})
|
||||
return new Response(JSON.stringify(dbFetch), {
|
||||
return new Response(JSON.stringify({ success: true, ...dbFetch }), {
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
}
|
||||
|
||||
@@ -86,9 +86,17 @@ export async function handleBotVerificationSwitch(data: { id: string, verified:
|
||||
verified: parsedData.data.verified,
|
||||
},
|
||||
});
|
||||
|
||||
fetch('https://automata.sern.dev/web/updateWebsite', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': process.env.UPDATEWEB_TOKEN!,
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Bot verification status for ${botUpdate.name} updated`,
|
||||
message: `Bot verification status for ${botUpdate.name} updated, redeploying website...`,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
35
src/lib/ratelimiter.ts
Normal file
35
src/lib/ratelimiter.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
// credit https://blog.stackademic.com/rate-limiting-serverless-functions-1dde0adf9a47
|
||||
|
||||
export class RateLimiter {
|
||||
windowStart: number;
|
||||
windowSize: number;
|
||||
maxRequests: number;
|
||||
idToRequestCount: Map<string, number>;
|
||||
|
||||
constructor(config: { windowSize: number; maxRequests: number }) {
|
||||
this.windowStart = Date.now();
|
||||
this.windowSize = config.windowSize;
|
||||
this.maxRequests = config.maxRequests;
|
||||
this.idToRequestCount = new Map<string, number>();
|
||||
}
|
||||
|
||||
limit(id: string) {
|
||||
const now = Date.now();
|
||||
|
||||
// Check and update current window
|
||||
const isNewWindow = now - this.windowStart > this.windowSize;
|
||||
if (isNewWindow) {
|
||||
this.windowStart = now;
|
||||
this.idToRequestCount.set(id, 0);
|
||||
}
|
||||
|
||||
// Check and update current request limits
|
||||
const currentRequestCount = this.idToRequestCount.get(id) ?? 0;
|
||||
|
||||
if (currentRequestCount >= this.maxRequests) return true;
|
||||
|
||||
this.idToRequestCount.set(id, currentRequestCount + 1);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
108
yarn.lock
108
yarn.lock
@@ -280,10 +280,10 @@
|
||||
resolved "https://registry.yarnpkg.com/@lucia-auth/adapter-prisma/-/adapter-prisma-4.0.1.tgz#8f8aa95bf0ac266fd2fb8ac3dc6dd9faec53342f"
|
||||
integrity sha512-3SztRhj1RAHbbhI/0aB7YC5zl6Z6aktPhkWpn2CHhiB03B9x/+A+M6pqJuAt1usU8PzkjVilgRPhrPymMar66A==
|
||||
|
||||
"@next/env@14.2.3":
|
||||
version "14.2.3"
|
||||
resolved "https://registry.yarnpkg.com/@next/env/-/env-14.2.3.tgz#d6def29d1c763c0afb397343a15a82e7d92353a0"
|
||||
integrity sha512-W7fd7IbkfmeeY2gXrzJYDx8D2lWKbVoTIj1o1ScPHNzvp30s1AuoEFSdr39bC5sjxJaxTtq3OTCZboNp0lNWHA==
|
||||
"@next/env@14.2.5":
|
||||
version "14.2.5"
|
||||
resolved "https://registry.yarnpkg.com/@next/env/-/env-14.2.5.tgz#1d9328ab828711d3517d0a1d505acb55e5ef7ad0"
|
||||
integrity sha512-/zZGkrTOsraVfYjGP8uM0p6r0BDT6xWpkjdVbcz66PJVSpwXX3yNiRycxAuDfBKGWBrZBXRuK/YVlkNgxHGwmA==
|
||||
|
||||
"@next/eslint-plugin-next@14.2.0":
|
||||
version "14.2.0"
|
||||
@@ -292,50 +292,50 @@
|
||||
dependencies:
|
||||
glob "10.3.10"
|
||||
|
||||
"@next/swc-darwin-arm64@14.2.3":
|
||||
version "14.2.3"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.2.3.tgz#db1a05eb88c0224089b815ad10ac128ec79c2cdb"
|
||||
integrity sha512-3pEYo/RaGqPP0YzwnlmPN2puaF2WMLM3apt5jLW2fFdXD9+pqcoTzRk+iZsf8ta7+quAe4Q6Ms0nR0SFGFdS1A==
|
||||
"@next/swc-darwin-arm64@14.2.5":
|
||||
version "14.2.5"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.2.5.tgz#d0a160cf78c18731c51cc0bff131c706b3e9bb05"
|
||||
integrity sha512-/9zVxJ+K9lrzSGli1///ujyRfon/ZneeZ+v4ptpiPoOU+GKZnm8Wj8ELWU1Pm7GHltYRBklmXMTUqM/DqQ99FQ==
|
||||
|
||||
"@next/swc-darwin-x64@14.2.3":
|
||||
version "14.2.3"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-darwin-x64/-/swc-darwin-x64-14.2.3.tgz#a3f8af05b5f9a52ac3082e66ac29e125ab1d7b9c"
|
||||
integrity sha512-6adp7waE6P1TYFSXpY366xwsOnEXM+y1kgRpjSRVI2CBDOcbRjsJ67Z6EgKIqWIue52d2q/Mx8g9MszARj8IEA==
|
||||
"@next/swc-darwin-x64@14.2.5":
|
||||
version "14.2.5"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-darwin-x64/-/swc-darwin-x64-14.2.5.tgz#eb832a992407f6e6352eed05a073379f1ce0589c"
|
||||
integrity sha512-vXHOPCwfDe9qLDuq7U1OYM2wUY+KQ4Ex6ozwsKxp26BlJ6XXbHleOUldenM67JRyBfVjv371oneEvYd3H2gNSA==
|
||||
|
||||
"@next/swc-linux-arm64-gnu@14.2.3":
|
||||
version "14.2.3"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-14.2.3.tgz#4e63f43879285b52554bfd39e6e0cc78a9b27bbf"
|
||||
integrity sha512-cuzCE/1G0ZSnTAHJPUT1rPgQx1w5tzSX7POXSLaS7w2nIUJUD+e25QoXD/hMfxbsT9rslEXugWypJMILBj/QsA==
|
||||
"@next/swc-linux-arm64-gnu@14.2.5":
|
||||
version "14.2.5"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-14.2.5.tgz#098fdab57a4664969bc905f5801ef5a89582c689"
|
||||
integrity sha512-vlhB8wI+lj8q1ExFW8lbWutA4M2ZazQNvMWuEDqZcuJJc78iUnLdPPunBPX8rC4IgT6lIx/adB+Cwrl99MzNaA==
|
||||
|
||||
"@next/swc-linux-arm64-musl@14.2.3":
|
||||
version "14.2.3"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-14.2.3.tgz#ebdaed26214448b1e6f2c3e8b3cd29bfba387990"
|
||||
integrity sha512-0D4/oMM2Y9Ta3nGuCcQN8jjJjmDPYpHX9OJzqk42NZGJocU2MqhBq5tWkJrUQOQY9N+In9xOdymzapM09GeiZw==
|
||||
"@next/swc-linux-arm64-musl@14.2.5":
|
||||
version "14.2.5"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-14.2.5.tgz#243a1cc1087fb75481726dd289c7b219fa01f2b5"
|
||||
integrity sha512-NpDB9NUR2t0hXzJJwQSGu1IAOYybsfeB+LxpGsXrRIb7QOrYmidJz3shzY8cM6+rO4Aojuef0N/PEaX18pi9OA==
|
||||
|
||||
"@next/swc-linux-x64-gnu@14.2.3":
|
||||
version "14.2.3"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-14.2.3.tgz#19e3bcc137c3b582a1ab867106817e5c90a20593"
|
||||
integrity sha512-ENPiNnBNDInBLyUU5ii8PMQh+4XLr4pG51tOp6aJ9xqFQ2iRI6IH0Ds2yJkAzNV1CfyagcyzPfROMViS2wOZ9w==
|
||||
"@next/swc-linux-x64-gnu@14.2.5":
|
||||
version "14.2.5"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-14.2.5.tgz#b8a2e436387ee4a52aa9719b718992e0330c4953"
|
||||
integrity sha512-8XFikMSxWleYNryWIjiCX+gU201YS+erTUidKdyOVYi5qUQo/gRxv/3N1oZFCgqpesN6FPeqGM72Zve+nReVXQ==
|
||||
|
||||
"@next/swc-linux-x64-musl@14.2.3":
|
||||
version "14.2.3"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-14.2.3.tgz#794a539b98e064169cf0ff7741b2a4fb16adec7d"
|
||||
integrity sha512-BTAbq0LnCbF5MtoM7I/9UeUu/8ZBY0i8SFjUMCbPDOLv+un67e2JgyN4pmgfXBwy/I+RHu8q+k+MCkDN6P9ViQ==
|
||||
"@next/swc-linux-x64-musl@14.2.5":
|
||||
version "14.2.5"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-14.2.5.tgz#cb8a9adad5fb8df86112cfbd363aab5c6d32757b"
|
||||
integrity sha512-6QLwi7RaYiQDcRDSU/os40r5o06b5ue7Jsk5JgdRBGGp8l37RZEh9JsLSM8QF0YDsgcosSeHjglgqi25+m04IQ==
|
||||
|
||||
"@next/swc-win32-arm64-msvc@14.2.3":
|
||||
version "14.2.3"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-14.2.3.tgz#eda9fa0fbf1ff9113e87ac2668ee67ce9e5add5a"
|
||||
integrity sha512-AEHIw/dhAMLNFJFJIJIyOFDzrzI5bAjI9J26gbO5xhAKHYTZ9Or04BesFPXiAYXDNdrwTP2dQceYA4dL1geu8A==
|
||||
"@next/swc-win32-arm64-msvc@14.2.5":
|
||||
version "14.2.5"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-14.2.5.tgz#81f996c1c38ea0900d4e7719cc8814be8a835da0"
|
||||
integrity sha512-1GpG2VhbspO+aYoMOQPQiqc/tG3LzmsdBH0LhnDS3JrtDx2QmzXe0B6mSZZiN3Bq7IOMXxv1nlsjzoS1+9mzZw==
|
||||
|
||||
"@next/swc-win32-ia32-msvc@14.2.3":
|
||||
version "14.2.3"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.2.3.tgz#7c1190e3f640ab16580c6bdbd7d0e766b9920457"
|
||||
integrity sha512-vga40n1q6aYb0CLrM+eEmisfKCR45ixQYXuBXxOOmmoV8sYST9k7E3US32FsY+CkkF7NtzdcebiFT4CHuMSyZw==
|
||||
"@next/swc-win32-ia32-msvc@14.2.5":
|
||||
version "14.2.5"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.2.5.tgz#f61c74ce823e10b2bc150e648fc192a7056422e0"
|
||||
integrity sha512-Igh9ZlxwvCDsu6438FXlQTHlRno4gFpJzqPjSIBZooD22tKeI4fE/YMRoHVJHmrQ2P5YL1DoZ0qaOKkbeFWeMg==
|
||||
|
||||
"@next/swc-win32-x64-msvc@14.2.3":
|
||||
version "14.2.3"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-14.2.3.tgz#2be4e39ee25bfbd85be78eea17c0e7751dc4323c"
|
||||
integrity sha512-Q1/zm43RWynxrO7lW4ehciQVj+5ePBhOK+/K2P7pLFX3JaJ/IZVC69SHidrmZSOkqz7ECIOhhy7XhAFG4JYyHA==
|
||||
"@next/swc-win32-x64-msvc@14.2.5":
|
||||
version "14.2.5"
|
||||
resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-14.2.5.tgz#ed199a920efb510cfe941cd75ed38a7be21e756f"
|
||||
integrity sha512-tEQ7oinq1/CjSG9uSTerca3v4AZ+dFa+4Yu6ihaG8Ud8ddqLQgFGcnwYls13H5X5CPDPZJdYxyeMui6muOLd4g==
|
||||
|
||||
"@node-rs/argon2-android-arm-eabi@1.7.0":
|
||||
version "1.7.0"
|
||||
@@ -2617,12 +2617,12 @@ next-themes@^0.3.0:
|
||||
resolved "https://registry.yarnpkg.com/next-themes/-/next-themes-0.3.0.tgz#b4d2a866137a67d42564b07f3a3e720e2ff3871a"
|
||||
integrity sha512-/QHIrsYpd6Kfk7xakK4svpDI5mmXP0gfvCoJdGpZQ2TOrQZmsW0QxjaiLn8wbIKjtm4BTSqLoix4lxYYOnLJ/w==
|
||||
|
||||
next@^14.2.3:
|
||||
version "14.2.3"
|
||||
resolved "https://registry.yarnpkg.com/next/-/next-14.2.3.tgz#f117dd5d5f20c307e7b8e4f9c1c97d961008925d"
|
||||
integrity sha512-dowFkFTR8v79NPJO4QsBUtxv0g9BrS/phluVpMAt2ku7H+cbcBJlopXjkWlwxrk/xGqMemr7JkGPGemPrLLX7A==
|
||||
next@^14.2.5:
|
||||
version "14.2.5"
|
||||
resolved "https://registry.yarnpkg.com/next/-/next-14.2.5.tgz#afe4022bb0b752962e2205836587a289270efbea"
|
||||
integrity sha512-0f8aRfBVL+mpzfBjYfQuLWh2WyAwtJXCRfkPF4UJ5qd2YwrHczsrSzXU4tRMV0OAxR8ZJZWPFn6uhSC56UTsLA==
|
||||
dependencies:
|
||||
"@next/env" "14.2.3"
|
||||
"@next/env" "14.2.5"
|
||||
"@swc/helpers" "0.5.5"
|
||||
busboy "1.6.0"
|
||||
caniuse-lite "^1.0.30001579"
|
||||
@@ -2630,15 +2630,15 @@ next@^14.2.3:
|
||||
postcss "8.4.31"
|
||||
styled-jsx "5.1.1"
|
||||
optionalDependencies:
|
||||
"@next/swc-darwin-arm64" "14.2.3"
|
||||
"@next/swc-darwin-x64" "14.2.3"
|
||||
"@next/swc-linux-arm64-gnu" "14.2.3"
|
||||
"@next/swc-linux-arm64-musl" "14.2.3"
|
||||
"@next/swc-linux-x64-gnu" "14.2.3"
|
||||
"@next/swc-linux-x64-musl" "14.2.3"
|
||||
"@next/swc-win32-arm64-msvc" "14.2.3"
|
||||
"@next/swc-win32-ia32-msvc" "14.2.3"
|
||||
"@next/swc-win32-x64-msvc" "14.2.3"
|
||||
"@next/swc-darwin-arm64" "14.2.5"
|
||||
"@next/swc-darwin-x64" "14.2.5"
|
||||
"@next/swc-linux-arm64-gnu" "14.2.5"
|
||||
"@next/swc-linux-arm64-musl" "14.2.5"
|
||||
"@next/swc-linux-x64-gnu" "14.2.5"
|
||||
"@next/swc-linux-x64-musl" "14.2.5"
|
||||
"@next/swc-win32-arm64-msvc" "14.2.5"
|
||||
"@next/swc-win32-ia32-msvc" "14.2.5"
|
||||
"@next/swc-win32-x64-msvc" "14.2.5"
|
||||
|
||||
normalize-path@^3.0.0, normalize-path@~3.0.0:
|
||||
version "3.0.0"
|
||||
|
||||
Reference in New Issue
Block a user