rough admin panel api and rough sending messages
This commit is contained in:
54
index.js
54
index.js
@ -3,7 +3,28 @@ const server = Bun.serve({
|
|||||||
port: 3000,
|
port: 3000,
|
||||||
routes: {
|
routes: {
|
||||||
"/admin": Response("admin panel wip"),
|
"/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: {
|
websocket: {
|
||||||
message(ws, message) {
|
message(ws, message) {
|
||||||
@ -16,19 +37,46 @@ const server = Bun.serve({
|
|||||||
} else {
|
} else {
|
||||||
serverSockets[ws.data.uuid]["remote"].send(message);
|
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) {
|
open(ws) {
|
||||||
// console.log(ws);
|
// console.log(ws);
|
||||||
let socket = new WebSocket(`wss://${ws.data.url}`);
|
let socket = new WebSocket(`wss://${ws.data.url}`);
|
||||||
|
ws.data.userinfo = {};
|
||||||
socket.addEventListener("message", event => {
|
socket.addEventListener("message", event => {
|
||||||
ws.send(event.data);
|
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 => {
|
socket.addEventListener("close", event => {
|
||||||
ws.close();
|
ws.close();
|
||||||
delete serverSockets[ws.data.uuid]["remote"];
|
delete serverSockets[ws.data.uuid]["remote"];
|
||||||
});
|
});
|
||||||
|
serverSockets[ws.data.uuid] = {};
|
||||||
serverSockets[ws.data.uuid]["remote"] = socket;
|
serverSockets[ws.data.uuid]["remote"] = socket;
|
||||||
serverSockets[ws.data.uuid]["local"] = ws;
|
serverSockets[ws.data.uuid]["local"] = ws;
|
||||||
|
serverSockets[ws.data.uuid]["sent"] = 0;
|
||||||
|
serverSockets[ws.data.uuid]["received"] = 0;
|
||||||
},
|
},
|
||||||
close(ws, code, message) {
|
close(ws, code, message) {
|
||||||
serverSockets[ws.data.uuid]["remote"].close();
|
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");
|
console.log("server up");
|
Reference in New Issue
Block a user