view analytics

This commit is contained in:
sky
2026-02-07 23:47:35 -05:00
parent 71f377ac18
commit 09cb2c6198

View File

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