From a3173609dd84541e97749a86dad5ff4fa8fa3683 Mon Sep 17 00:00:00 2001 From: SKINMAKER Date: Sun, 16 Apr 2023 15:02:14 +0900 Subject: [PATCH] fix: remove outdated webhook client (#555) * fix: remove outdated webhook client * feat: destroy client --- pages/api/v2/applications/bots/[id]/index.ts | 8 +++++++- pages/api/v2/applications/servers/[id]/index.ts | 8 +++++++- utils/Webhook.ts | 8 ++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pages/api/v2/applications/bots/[id]/index.ts b/pages/api/v2/applications/bots/[id]/index.ts index d8dadba..86a9af2 100644 --- a/pages/api/v2/applications/bots/[id]/index.ts +++ b/pages/api/v2/applications/bots/[id]/index.ts @@ -8,7 +8,8 @@ import RequestHandler from '@utils/RequestHandler' import { User, WebhookStatus } from '@types' import { parseWebhookURL } from 'discord.js' -import { verifyWebhook } from '@utils/Webhook' +import { destroyWebhookClient, verifyWebhook } from '@utils/Webhook' +import { webhookClients } from '@utils/DiscordBot' const BotApplications = RequestHandler().patch(async (req: ApiRequest, res) => { const user = await get.Authorization(req.cookies.token) @@ -31,6 +32,10 @@ const BotApplications = RequestHandler().patch(async (req: ApiRequest, res) => { if(key === false) { return ResponseWrapper(res, { code: 400, message: '웹후크 주소를 검증할 수 없습니다.', errors: ['웹후크 주소가 올바른지 확인해주세요.\n웹후크 주소 검증에 대한 자세한 내용은 API 문서를 참고해주세요.'] }) } + const client = webhookClients.bot.get(req.query.id) + if(client && validated.webhookURL !== client.url) { + destroyWebhookClient(req.query.id, 'bot') + } await update.webhook(req.query.id, 'bots', { url: validated.webhookURL, status: parseWebhookURL(validated.webhookURL) ? WebhookStatus.Discord : WebhookStatus.HTTP, @@ -38,6 +43,7 @@ const BotApplications = RequestHandler().patch(async (req: ApiRequest, res) => { secret: key, }) } else { + destroyWebhookClient(req.query.id, 'bot') await update.webhook(req.query.id, 'bots', { url: null, status: WebhookStatus.None, diff --git a/pages/api/v2/applications/servers/[id]/index.ts b/pages/api/v2/applications/servers/[id]/index.ts index ea0c07f..746d045 100644 --- a/pages/api/v2/applications/servers/[id]/index.ts +++ b/pages/api/v2/applications/servers/[id]/index.ts @@ -8,7 +8,8 @@ import RequestHandler from '@utils/RequestHandler' import { WebhookStatus } from '@types' import { parseWebhookURL } from 'discord.js' -import { verifyWebhook } from '@utils/Webhook' +import { destroyWebhookClient, verifyWebhook } from '@utils/Webhook' +import { webhookClients } from '@utils/DiscordBot' const ServerApplications = RequestHandler().patch(async (req: ApiRequest, res) => { const user = await get.Authorization(req.cookies.token) @@ -31,6 +32,10 @@ const ServerApplications = RequestHandler().patch(async (req: ApiRequest, res) = if(key === false) { return ResponseWrapper(res, { code: 400, message: '웹후크 주소를 검증할 수 없습니다.', errors: ['웹후크 주소가 올바른지 확인해주세요.\n웹후크 주소 검증에 대한 자세한 내용은 API 문서를 참고해주세요.'] }) } + const client = webhookClients.server.get(req.query.id) + if(client && validated.webhookURL !== client.url) { + destroyWebhookClient(req.query.id, 'server') + } await update.webhook(req.query.id, 'servers', { url: validated.webhookURL, status: parseWebhookURL(validated.webhookURL) ? WebhookStatus.Discord : WebhookStatus.HTTP, @@ -38,6 +43,7 @@ const ServerApplications = RequestHandler().patch(async (req: ApiRequest, res) = secret: key, }) } else { + destroyWebhookClient(req.query.id, 'server') await update.webhook(req.query.id, 'servers', { url: null, status: WebhookStatus.None, diff --git a/utils/Webhook.ts b/utils/Webhook.ts index 9932488..f668623 100644 --- a/utils/Webhook.ts +++ b/utils/Webhook.ts @@ -15,6 +15,14 @@ type RelayOptions = { secret: string, } +export function destroyWebhookClient(id: string, type: 'bot' | 'server') { + const client = webhookClients[type].get(id) + if(client) { + client.destroy() + webhookClients[type].delete(id) + } +} + function relayedFetch(options: RelayOptions): Promise { return fetch(process.env.WEBHOOK_RELAY_URL, { method: 'POST',