diff --git a/index.js b/index.js index 60bf8c2..1afeb3b 100755 --- a/index.js +++ b/index.js @@ -9,11 +9,11 @@ import mime from "mime-types"; import compression from "compression"; // import { accs, infdb, polytrack } from "./database.js"; import { accs, top } from "./database.js"; -import { } from "./accounts/friend.js"; +import { } from "./accounts/friend.js"; import { findClientID, search, download } from "./accounts/music.js"; import { minoProxy } from "./games/osu.js"; import { banUser, removeAccount, verifyCookie, getUserFromCookie, createAccount, resetPassword, loginAccount, addBadge } from "./accounts/manage.js"; -import { } from "./accounts/misc.js"; +import { } from "./accounts/misc.js"; import { getRawData, generateAccountPage, editProfile, saveData, getUsers, isAdmin, retrieveData } from "./accounts/profile.js"; import { callAI } from "./ai.js"; import { Readable } from 'stream'; @@ -32,24 +32,24 @@ const port = process.env.PORT || 3000; const app = express(); // why the fuck does this have to exist? -app.use("/resources/semag/hotline-miami/", (req,res,next) => { - if(req.method == "HEAD") { - req.socket.destroy(); +app.use("/resources/semag/hotline-miami/", (req, res, next) => { + if (req.method == "HEAD") { + req.socket.destroy(); } next(); }); app.use("/osu/", await minoProxy) -app.use("/resources/semag/portal/", (req,res,next) => { - res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp'); - res.setHeader('Cross-Origin-Opener-Policy', 'same-origin'); +app.use("/resources/semag/portal/", (req, res, next) => { + res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp'); + res.setHeader('Cross-Origin-Opener-Policy', 'same-origin'); next(); }); -app.use("/resources/semag/terraria/", (req,res,next) => { - res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp'); - res.setHeader('Cross-Origin-Opener-Policy', 'same-origin'); +app.use("/resources/semag/terraria/", (req, res, next) => { + res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp'); + res.setHeader('Cross-Origin-Opener-Policy', 'same-origin'); next(); }); app.use(compression()); @@ -75,9 +75,9 @@ wss.on("connection", function connection(ws, req, res) { server.on("message", async function message(data) { let message = Buffer.from(data).toString(); - if(message.startsWith("online")) { + if (message.startsWith("online")) { ws.send(message); - } else if(message.startsWith("annc")) { + } else if (message.startsWith("annc")) { ws.send(message); } }) @@ -98,7 +98,7 @@ wss.on("connection", function connection(ws, req, res) { } } else if (message.startsWith("annc")) { let splitMessage = message.split(";;"); - if(await isAdmin(splitMessage[1])) { + if (await isAdmin(splitMessage[1])) { // wss.clients.forEach(client => { // client.send(`annc;;${splitMessage[2]};;${splitMessage[3]}`); // }) @@ -107,24 +107,24 @@ wss.on("connection", function connection(ws, req, res) { } }); - ws.on("close", () => {server.close()}); + ws.on("close", () => { server.close() }); }); app.post( - "/api/event", - (req, res) => { - fetch("https://analytics.skysthelimit.dev/api/event", { - method: "post", - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - - body: req.body - }).then(async (response) => { - res.send(response.status); - }) - } - ); + "/api/event", + (req, res) => { + fetch("https://analytics.skysthelimit.dev/api/event", { + method: "post", + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + + body: req.body + }).then(async (response) => { + res.send(response.status); + }) + } +); app.post("/register", async (req, res) => { let status = await createAccount(req.body.username, req.body.password, req.body["h-captcha-response"]); if (status["success"]) { @@ -178,7 +178,7 @@ app.post("/api/account/upload", async (req, res, next) => { }); app.post("/api/analytics/game", async (req, res) => { - if(req.body && req.body.path.length < 16) { + if (req.body && req.body.path.length < 16) { const query = top.prepare(` INSERT INTO stats (game, plays) VALUES ($id, 1) @@ -193,6 +193,13 @@ app.post("/api/analytics/game", async (req, res) => { return; }) +app.use("/api/analytics/get", async(req, res) => { + const query = top.prepare(`SELECT * FROM stats ORDER BY plays DESC`); + let data = query.all(); + res.send(data); + return; +}) + app.post("/api/ai/sendMessage", async (req, res) => { // take in chat id and message // stream back reply @@ -208,21 +215,21 @@ app.use("/api/music/download", async (req, res, next) => { let song = req.query.url; song = song.replaceAll(" ", ""); const processor = Bun.spawn([ - "yt-dlp", - "bestaudio[abr<=160]", - "-o", `-`, - "https://soundcloud.com/" + req.query.url + "yt-dlp", + "bestaudio[abr<=160]", + "-o", `-`, + "https://soundcloud.com/" + req.query.url ]); for await (const chunk of processor.stdout) { - res.write(chunk); - } + res.write(chunk); + } res.end(); }); app.post("/api/recat", async (req, res) => { - if(isAdmin(req.cookies.token)) { + if (isAdmin(req.cookies.token)) { let domain = JSON.parse(req.body)["domain"]; let message; - if(recatters.includes(domain)) { + if (recatters.includes(domain)) { recatters.pop(domain); message = `Successfully set ${domain} to Selenite.` } else { @@ -307,7 +314,7 @@ app.use("/api/stats", async (req, res, next) => { .send({ "users": accs.query(`SELECT COUNT(*) FROM accounts`).get()["COUNT(*)"], // "cpu": os.cpus(), - "ram": `${(os.totalmem()-os.freemem())/1000000000}GB / ${os.totalmem()/1000000000}GB`, + "ram": `${(os.totalmem() - os.freemem()) / 1000000000}GB / ${os.totalmem() / 1000000000}GB`, "cpuUsage": os.loadavg(), "uptime": `${os.uptime()}s` });