99 lines
4.0 KiB
JavaScript
99 lines
4.0 KiB
JavaScript
let serverSockets = {};
|
|
const server = Bun.serve({
|
|
port: 3000,
|
|
routes: {
|
|
"/admin": Response("admin panel wip"),
|
|
// 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) {
|
|
// console.log(serverSockets[ws.data.uuid]["remote"]);
|
|
// console.log(message);
|
|
if(serverSockets[ws.data.uuid]["remote"].readyState != 1) {
|
|
serverSockets[ws.data.uuid]["remote"].addEventListener("open", ()=>{
|
|
serverSockets[ws.data.uuid]["remote"].send(message);
|
|
})
|
|
} 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();
|
|
delete serverSockets[ws.data.uuid]["remote"];
|
|
},
|
|
drain(ws) {},
|
|
},
|
|
fetch(req) {
|
|
if (server.upgrade(req, { data: { url: new URL(req.url).pathname.substring(1), uuid: crypto.randomUUID() } })) {
|
|
return;
|
|
}
|
|
return Response("server up");
|
|
}
|
|
})
|
|
|
|
function parseBuffer(buf) {
|
|
return buf.toString("hex");
|
|
}
|
|
|
|
console.log("server up"); |