2020-08-27 13:09:35 +01:00
|
|
|
console.clear();
|
|
|
|
|
2021-09-22 19:15:01 +01:00
|
|
|
// Globals
|
|
|
|
global.protocolVersion = 19;
|
|
|
|
|
2020-08-27 13:09:35 +01:00
|
|
|
const app = require("express")(),
|
2022-01-04 03:39:53 +00:00
|
|
|
consoleHelper = require("./consoleHelper.js"),
|
|
|
|
prometheusApp = require("express")(),
|
|
|
|
fs = require("fs"),
|
|
|
|
serverHandler = require("./server/serverHandler.js"),
|
|
|
|
config = require("./config.json");
|
2020-08-27 13:09:35 +01:00
|
|
|
|
2021-09-22 19:17:25 +01:00
|
|
|
if (config.prometheusEnabled) {
|
2022-01-04 03:39:53 +00:00
|
|
|
// We only need to require this if prom metrics are on.
|
|
|
|
const prom = require("prom-client");
|
2021-02-05 03:14:53 +00:00
|
|
|
|
2022-01-04 03:39:53 +00:00
|
|
|
const register = new prom.Registry();
|
|
|
|
|
|
|
|
register.setDefaultLabels({ app: "nodejs_binato" });
|
|
|
|
|
|
|
|
prom.collectDefaultMetrics({ register });
|
|
|
|
|
|
|
|
prometheusApp.get("*", async (req, res) => {
|
|
|
|
if (req.url.split("?")[0] != "/metrics") return res.status(404).end("");
|
|
|
|
|
|
|
|
res.end(await register.metrics());
|
|
|
|
});
|
|
|
|
|
|
|
|
prometheusApp.listen(config.prometheusPort, () => consoleHelper.printBancho(`Prometheus metrics listening at port ${config.prometheusPort}`));
|
|
|
|
} else consoleHelper.printWarn("Prometheus is disabled!");
|
2020-08-27 13:09:35 +01:00
|
|
|
|
2021-09-22 19:17:25 +01:00
|
|
|
if (config.compression) {
|
2022-01-04 03:39:53 +00:00
|
|
|
app.use(require("compression")());
|
|
|
|
consoleHelper.printBancho("Gzip Compression is enabled.");
|
|
|
|
} else consoleHelper.printWarn("Gzip Compression is disabled!");
|
2020-08-27 13:09:35 +01:00
|
|
|
|
|
|
|
app.use((req, res) => {
|
2022-01-04 03:39:53 +00:00
|
|
|
req.packet = new Buffer.alloc(0);
|
|
|
|
req.on("data", (chunk) => req.packet = Buffer.concat([req.packet, chunk], req.packet.length + chunk.length));
|
|
|
|
req.on("end", () => {
|
|
|
|
switch (req.method) {
|
|
|
|
case "GET":
|
2022-02-22 10:02:30 +00:00
|
|
|
if (req.url == "/" || req.url == "/index.html" || req.url == "/index") {
|
|
|
|
res.sendFile(`${__dirname}/web/serverPage.html`);
|
2022-01-04 03:39:53 +00:00
|
|
|
} else if (req.url == "/chat") {
|
|
|
|
fs.readFile("./web/chatPageTemplate.html", (err, data) => {
|
|
|
|
if (err) throw err;
|
2021-01-26 12:29:15 +00:00
|
|
|
|
2022-01-04 03:39:53 +00:00
|
|
|
let lines = "", flip = false;
|
|
|
|
const limit = global.chatHistory.length < 10 ? 10 : global.chatHistory.length;
|
|
|
|
for (let i = global.chatHistory.length - 10; i < limit; i++) {
|
|
|
|
if (i < 0) i = 0;
|
|
|
|
lines += `<div class="line line${flip ? 1 : 0}">${global.chatHistory[i] == null ? "<hidden>blank</hidden>" : global.chatHistory[i]}</div>`
|
|
|
|
flip = !flip;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.send(data.toString().replace("|content|", lines));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
2020-08-27 13:09:35 +01:00
|
|
|
|
2022-01-04 03:39:53 +00:00
|
|
|
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
|
|
|
|
if (req.headers["host"].split(".")[0][0] == "c")
|
|
|
|
serverHandler(req, res);
|
|
|
|
else
|
|
|
|
res.status(400).send("400 | Bad Request!<br>Binato only accepts post requests on bancho subdomains.<hr>Binato");
|
|
|
|
break;
|
2020-08-27 13:09:35 +01:00
|
|
|
|
2022-01-04 03:39:53 +00:00
|
|
|
default:
|
|
|
|
res.status(405).send("405 | Method not allowed!<hr>Binato");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2020-08-27 13:09:35 +01:00
|
|
|
});
|
|
|
|
|
2022-01-04 03:39:53 +00:00
|
|
|
app.listen(config.port, () => consoleHelper.printBancho(`Binato is up! Listening at port ${config.port}`));
|