Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedal987 committed Jun 15, 2024
2 parents 1aa97a4 + 98759b2 commit 66e588c
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 296 deletions.
68 changes: 44 additions & 24 deletions ebs/src/modules/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Config } from "common/types";
import { app } from "../index";
import { app } from "..";
import { sendPubSubMessage } from "../util/pubsub";
import { strToU8, compressSync, strFromU8 } from "fflate";
import { compressSync, strFromU8, strToU8 } from "fflate";
import { getBannedUsers } from "../util/db";
import { asyncCatch } from "../util/middleware";
import { Webhooks } from "@octokit/webhooks";
import { sendToLogger } from "../util/logger";

let activeConfig: Config | undefined;
let configData: Config | undefined;
Expand All @@ -27,6 +28,18 @@ async function fetchConfig(): Promise<Config> {
console.error("Error when fetching config");
console.error(e);

sendToLogger({
transactionToken: null,
userIdInsecure: null,
important: true,
fields: [
{
header: "Error when fetching config",
content: e.toString(),
},
],
}).then();

return {
version: -1,
message: "Error when fetching config",
Expand All @@ -37,8 +50,7 @@ async function fetchConfig(): Promise<Config> {
function processConfig(data: Config) {
const config: Config = JSON.parse(JSON.stringify(data));
if (!ingameState) {
Object.values(config.redeems!)
.forEach((redeem) => (redeem.disabled = true));
Object.values(config.redeems!).forEach((redeem) => (redeem.disabled = true));
}
return config;
}
Expand All @@ -53,7 +65,7 @@ export async function getConfig(): Promise<Config> {

export async function setActiveConfig(data: Config) {
activeConfig = processConfig(data);
broadcastConfigRefresh(activeConfig);
await broadcastConfigRefresh(activeConfig);
}

export async function broadcastConfigRefresh(config: Config) {
Expand All @@ -72,34 +84,39 @@ export function isIngame() {
export function setIngame(newIngame: boolean) {
if (ingameState == newIngame) return;
ingameState = newIngame;
setActiveConfig(configData!);
setActiveConfig(configData!).then();
}

async function refreshConfig() {
configData = await fetchConfig();
activeConfig = processConfig(configData);
}

app.get("/private/refresh", asyncCatch(async (_, res) => {
await refreshConfig();
console.log("Refreshed config, new config version is ", activeConfig!.version);
await broadcastConfigRefresh(activeConfig!);
res.sendStatus(200);
}));
app.get(
"/private/refresh",
asyncCatch(async (_, res) => {
await refreshConfig();
console.log("Refreshed config, new config version is ", activeConfig!.version);
await broadcastConfigRefresh(activeConfig!);
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 = JSON.stringify(req.body);
app.post(
"/webhook/refresh",
asyncCatch(async (req, res) => {
// github webhook
const signature = req.headers["x-hub-signature-256"] as string;
const body = JSON.stringify(req.body);

if(!(await webhooks.verify(body, signature))) {
res.sendStatus(403);
return;
}
if (!(await webhooks.verify(body, signature))) {
res.sendStatus(403);
return;
}

// only refresh if the config.json file was changed
if(req.body.commits.some((commit: any) => commit.modified.includes("config.json"))) {
Expand All @@ -113,10 +130,13 @@ app.post("/webhook/refresh", asyncCatch(async (req, res) => {
}
}));

app.get("/public/config", asyncCatch(async (req, res) => {
const config = await getConfig();
res.send(JSON.stringify(config));
}));
app.get(
"/public/config",
asyncCatch(async (req, res) => {
const config = await getConfig();
res.send(JSON.stringify(config));
})
);

(async () => {
const config = await getConfig();
Expand Down
51 changes: 27 additions & 24 deletions ebs/src/modules/game/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import { app } from "../../index";
import { app } from "../..";
import { asyncCatch } from "../../util/middleware";
import { GameConnection } from "./connection";
import { MessageType } from "./messages";
import { ResultMessage } from "./messages.game";
import { CommandInvocationSource, RedeemMessage } from "./messages.server";
import { StressTestRequest, isStressTesting, startStressTest } from "./stresstest";
import { isStressTesting, startStressTest, StressTestRequest } from "./stresstest";

export let connection: GameConnection = new GameConnection();

app.ws("/private/socket", (ws, req) => {
connection.setSocket(ws);
});

app.post("/private/redeem", asyncCatch(async (req, res) => {
//console.log(req.body);
const msg = {
...connection.makeMessage(MessageType.Redeem),
source: CommandInvocationSource.Dev,
...req.body,
} as RedeemMessage;
if (!connection.isConnected()) {
res.status(500).send("Not connected");
return;
}
app.post(
"/private/redeem",
asyncCatch(async (req, res) => {
//console.log(req.body);
const msg = {
...connection.makeMessage(MessageType.Redeem),
source: CommandInvocationSource.Dev,
...req.body,
} as RedeemMessage;
if (!connection.isConnected()) {
res.status(500).send("Not connected");
return;
}

try {
await connection.sendMessage(msg);
res.status(201).send(JSON.stringify(msg));
} catch (e) {
res.status(500).send(e);
}
}));
try {
await connection.sendMessage(msg);
res.status(201).send(JSON.stringify(msg));
} catch (e) {
res.status(500).send(e);
}
})
);

app.post("/private/setresult", (req, res) => {
//console.log(req.body);
Expand Down Expand Up @@ -62,7 +65,7 @@ app.post("/private/stress", (req, res) => {
res.status(500).send("Not connected");
return;
}

const reqObj = req.body as StressTestRequest;
if (reqObj.type === undefined || reqObj.duration === undefined || reqObj.interval === undefined) {
res.status(400).send("Must have type, duration, and interval");
Expand All @@ -71,14 +74,14 @@ app.post("/private/stress", (req, res) => {
console.log(reqObj);
startStressTest(reqObj.type, reqObj.duration, reqObj.interval);
res.sendStatus(200);
})
});

app.get("/private/unsent", (req, res) => {
const unsent = connection.getUnsent();
res.send(JSON.stringify(unsent));
})
});

app.get("/private/outstanding", (req, res) => {
const outstanding = connection.getOutstanding();
res.send(JSON.stringify(outstanding));
})
});
Loading

0 comments on commit 66e588c

Please sign in to comment.