From 83573bb42600b3606c56f094a5980e1105aea471 Mon Sep 17 00:00:00 2001 From: Vedal Date: Sat, 15 Jun 2024 03:28:19 +0100 Subject: [PATCH] Add webhook refresh --- ebs/package.json | 1 + ebs/src/modules/config.ts | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/ebs/package.json b/ebs/package.json index d38b6da..f2ddd87 100644 --- a/ebs/package.json +++ b/ebs/package.json @@ -7,6 +7,7 @@ "build": "esbuild --bundle --minify --platform=node --sourcemap=inline --outfile=dist/index.js src/index.ts" }, "dependencies": { + "@octokit/webhooks": "^13.2.7", "@twurple/api": "^7.1.0", "@twurple/ebs-helper": "^7.1.0", "body-parser": "^1.20.2", diff --git a/ebs/src/modules/config.ts b/ebs/src/modules/config.ts index 411cb41..5e577d3 100644 --- a/ebs/src/modules/config.ts +++ b/ebs/src/modules/config.ts @@ -4,6 +4,7 @@ import { sendPubSubMessage } from "../util/pubsub"; import { strToU8, compressSync, strFromU8 } from "fflate"; import { getBannedUsers } from "../util/db"; import { asyncCatch } from "../util/middleware"; +import { Webhooks } from "@octokit/webhooks"; let activeConfig: Config | undefined; let configData: Config | undefined; @@ -84,6 +85,26 @@ app.get("/private/refresh", asyncCatch(async (_, res) => { res.sendStatus(200); })); +const webhooks = new Webhooks({ + secret: process.env.PRIVATE_API_KEY!, +}); + +app.post("/webhook/refresh", asyncCatch(async (req, res) => { + // github webhook + const signature = req.headers["x-hub-signature-256"] as string; + const body = req.body as string; + + if(!(await webhooks.verify(body, signature))) { + res.sendStatus(403); + return; + } + + await refreshConfig(); + console.log("Refreshed config, new config version is ", activeConfig!.version); + await broadcastConfigRefresh(activeConfig!); + res.sendStatus(200); +})); + app.get("/public/config", asyncCatch(async (req, res) => { const config = await getConfig(); res.send(JSON.stringify(config));