From c4fe3f8a17e7a69ae7be5b2395142b634c579859 Mon Sep 17 00:00:00 2001 From: sky Date: Mon, 23 Jun 2025 18:04:11 -0400 Subject: [PATCH] rough admin panel api and rough sending messages --- index.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 5e8720b..b958567 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,28 @@ const server = Bun.serve({ port: 3000, routes: { "/admin": Response("admin panel wip"), - "/api/*": Response.json({"success": false, "reason": "API has not been implemented."}) + // wip + "/api/getAllConnections": ()=>{ + let response = {"data": []}; + Object.keys(serverSockets).forEach(item => { + console.log(serverSockets[item]); + response.data.push({ + "server": serverSockets[item]["local"]["data"]["userinfo"]["url"], + "username": serverSockets[item]["local"]["data"]["userinfo"]["username"], + "client": serverSockets[item]["local"]["data"]["userinfo"]["client"], + "server_brand": serverSockets[item]["local"]["data"]["userinfo"]["server_brand"], + }) + }); + return Response.json(response); + }, + "/api/sendMessage": ()=>{ + // let goodMessage = Buffer.from(`0f1c7b2274657874223a22746869732069732061206d657373616765227d00`, "hex") + Object.keys(serverSockets).forEach(item => { + serverSockets[item]["local"].send(goodMessage); + }); + return Response("good") + }, + "/api/*": Response.json({"success": false, "reason": "Endpoint does not exist."}) }, websocket: { message(ws, message) { @@ -16,19 +37,46 @@ const server = Bun.serve({ } else { serverSockets[ws.data.uuid]["remote"].send(message); } + if(serverSockets[ws.data.uuid]["sent"] == 0) { + let hex = parseBuffer(message); + hex = hex.substring(30); + hex = hex.split("000e"); + hex[0] = Buffer.from(hex[0], "hex"); + hex[0] = hex[0].toString("utf8") + hex[1] = Buffer.from(hex[1], "hex"); + hex[1] = hex[1].toString("utf8") + console.log(hex[0]); + console.log(hex[1]); + ws.data.userinfo.username = hex[1]; + ws.data.userinfo.client = hex[0]; + }; + serverSockets[ws.data.uuid]["sent"]++; }, open(ws) { // console.log(ws); let socket = new WebSocket(`wss://${ws.data.url}`); + ws.data.userinfo = {}; socket.addEventListener("message", event => { ws.send(event.data); + if(serverSockets[ws.data.uuid]["received"] == 0) { + let hex = parseBuffer(event.data); + hex = hex.substring(12); + hex = Buffer.from(hex, "hex"); + hex = hex.toString("utf8") + ws.data.userinfo.server_brand = hex; + console.log(hex); + }; + serverSockets[ws.data.uuid]["received"]++; }); socket.addEventListener("close", event => { ws.close(); delete serverSockets[ws.data.uuid]["remote"]; }); + serverSockets[ws.data.uuid] = {}; serverSockets[ws.data.uuid]["remote"] = socket; serverSockets[ws.data.uuid]["local"] = ws; + serverSockets[ws.data.uuid]["sent"] = 0; + serverSockets[ws.data.uuid]["received"] = 0; }, close(ws, code, message) { serverSockets[ws.data.uuid]["remote"].close(); @@ -44,4 +92,8 @@ const server = Bun.serve({ } }) +function parseBuffer(buf) { + return buf.toString("hex"); +} + console.log("server up"); \ No newline at end of file