Binato/Binato.ts

65 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-11-16 15:25:46 +00:00
import { ChatHistory } from "./server/ChatHistory";
2022-11-16 11:59:23 +00:00
import compression from "compression";
2022-11-16 15:25:46 +00:00
import config from "./config.json";
2022-11-16 11:59:23 +00:00
import { ConsoleHelper } from "./ConsoleHelper";
import express from "express";
2022-11-16 15:25:46 +00:00
import { HandleRequest } from "./server/BanchoServer";
import { readFileSync } from "fs";
2022-11-16 11:59:23 +00:00
import { Registry, collectDefaultMetrics } from "prom-client";
2022-11-16 15:25:46 +00:00
const binatoApp:express.Application = express();
2022-11-16 11:59:23 +00:00
if (config["prometheus"]["enabled"]) {
const register:Registry = new Registry();
register.setDefaultLabels({ app: "nodejs_binato" });
collectDefaultMetrics({ register });
2022-11-16 15:25:46 +00:00
const prometheusApp:express.Application = express();
2022-11-16 11:59:23 +00:00
prometheusApp.get("/metrics", async (req, res) => {
res.end(await register.metrics());
});
prometheusApp.listen(config["prometheus"]["port"], () => ConsoleHelper.printBancho(`Prometheus metrics listening at port ${config["prometheus"]["port"]}`));
} else {
ConsoleHelper.printWarn("Prometheus is disabled!");
}
if (config["express"]["compression"]) {
binatoApp.use(compression());
ConsoleHelper.printBancho("Compression is enabled");
} else {
ConsoleHelper.printWarn("Compression is disabled");
}
2022-11-16 15:25:46 +00:00
const INDEX_PAGE:string = readFileSync("./web/serverPage.html").toString();
2022-11-16 11:59:23 +00:00
binatoApp.use((req, res) => {
let packet:Buffer = Buffer.alloc(0);
req.on("data", (chunk:Buffer) => packet = Buffer.concat([packet, chunk], packet.length + chunk.length));
req.on("end", () => {
switch (req.method) {
case "GET":
if (req.url == "/" || req.url == "/index.html" || req.url == "/index") {
2022-11-16 15:25:46 +00:00
res.send(INDEX_PAGE);
2022-11-16 11:59:23 +00:00
} else if (req.url == "/chat") {
2022-11-16 15:25:46 +00:00
res.send(ChatHistory.GenerateForWeb());
2022-11-16 11:59:23 +00:00
}
break;
case "POST":
// Make sure this address should respond to bancho requests
// Bancho addresses: c, c1, c2, c3, c4, c5, c6, ce
// Just looking for the first character being "c" *should* be enough
2022-11-16 15:25:46 +00:00
if (req.headers.host != null && req.headers.host.split(".")[0][0] == "c")
HandleRequest(req, res, packet);
2022-11-16 11:59:23 +00:00
else
res.status(400).send("400 | Bad Request!<br>Binato only accepts POST requests on Bancho subdomains.<hr>Binato");
break;
default:
res.status(405).send("405 | Method not allowed!<hr>Binato");
break;
}
});
});