Skip to content

Commit

Permalink
Fixed some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueBurger committed Nov 9, 2023
1 parent 1bd1144 commit 04f33e4
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public void onEnable() {
@Override
public void run() {
JSONParser parser = new JSONParser();
ByteBuffer bb = ByteBuffer.allocate(10_000);
int byteCount;
while(true) {
int byteCount;
ByteBuffer bb = ByteBuffer.allocate(10_000);
try {
byteCount = client.read(bb);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected void execute(JSONObject data, String id) throws IOException {
locationObj.put("y", location.getY());
locationObj.put("z", location.getZ());
locationObj.put("world", location.getWorld().getName());
playerObject.put("position", locationObj);
playerObject.put("location", locationObj);
playerArray.add(playerObject);
}
responseObj.put("players", playerArray);
Expand Down
4 changes: 4 additions & 0 deletions Server/Gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ async function buildWeb() {
export async function packetsToFile() {
let fileData = "";
let files = fs.readdirSync("src/packets");
files = files.filter(f => {
if(fs.statSync("src/packets/" + f).isDirectory()) return false;
return true;
});
if(process.env.BURGERPANEL_SKIP_SERVER != "1") {
files.forEach((f) => {
fileData += `import ${f.replace(".ts", "")} from "./dist/Server/src/packets/${f.replace(".ts", ".js")}";\n`;
Expand Down
4 changes: 4 additions & 0 deletions Server/buildTools/packetsToFile.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import fs from "node:fs";
let files = fs.readdirSync("src/packets");
files = files.filter(f => {
if(fs.statSync("src/packets/" + f).isDirectory()) return false;
return true;
});
let shareTextData = `// Do not touch this file, it is automatically changed.\nexport default ${JSON.stringify(files.map(f => f.replace(".ts", "")))} as const`;
fs.writeFileSync("../Share/Packets.ts", shareTextData);
29 changes: 29 additions & 0 deletions Server/src/packets/integrator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { OurClient, Packet, ServerPacketResponse } from "../index.js";
import { servers } from "../db.js";
import serverManager, { userHasAccessToServer } from "../serverManager.js";
import { Permission, hasServerPermission } from "../../../Share/Permission.js";
import { Request } from "../../../Share/Requests.js";

import IntegratorInstall from "./integrator/IntegratorInstall.js"
import serverIntegrator from "../serverIntegrator.js";

export default class IntegratorPacket extends Packet {
name: Request = "integrator";
requiresAuth: boolean = true;
async handle(client: OurClient, data: any): ServerPacketResponse<"integrator"> {
const server = await servers.findById(data.id);
if(!server || !userHasAccessToServer(client.data.auth.user, server.toJSON())) return "Server not found!";
switch(data.action) {
case "install":
if(!hasServerPermission(client.data.auth.user, server.toJSON(), "integrator.install")) return "You can't do that!";
return await IntegratorInstall(server.toJSON(), client, data);
case "status":
const status = await serverIntegrator.request(server.toJSON(), "status");
return {
type: "status",
status
}
}
return;
}
}
14 changes: 14 additions & 0 deletions Server/src/packets/integrator/IntegratorInstall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { OurClient } from "../..";
import { Server } from "../../../../Share/Server";
import fs from "node:fs/promises";
import { buildInfo } from "../../../../Share/BuildInfo.js";
import { integratorJarPath } from "../../serverIntegrator.js";
import logger from "../../logger.js";

export default async function install(server: Server, client: OurClient, data: any): Promise<{type: "install-success"}> {
logger.log(`${client.data.auth.user?.username} is installing the BurgerPanel integrator on server ${server.name}`, "server.integrator");
await fs.copyFile(integratorJarPath, server.path + `/plugins/BurgerPanelIntegrator-${buildInfo.version}.jar`);
return {
type: "install-success"
}
}
71 changes: 68 additions & 3 deletions Server/src/serverIntegrator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const isProd = process.env.NODE_ENV == "production";
import fsSync from "node:fs";
import fs from "node:fs/promises";
import net from "node:net";
import path from "node:path";
import logger, { LogLevel } from "./logger.js";
import type { Server } from "../../Share/Server.js";
import { buildInfo } from "../../Share/BuildInfo.js";
import nodeCrypto from "node:crypto";
import url from "node:url";
let __dirname = url.fileURLToPath(new URL('.', import.meta.url));
export let integratorJarPath: string;
if(process.env.NODE_ENV == "production") integratorJarPath = __dirname + "/Integrator.jar";
else integratorJarPath = __dirname + "/../../../../Integrator/build/libs/BurgerPanelIntegrator-1.0-SNAPSHOT.jar";
interface OurIntegratorClient extends net.Socket {
burgerpanelData: {
server?: string
Expand All @@ -13,7 +21,9 @@ export default new class ServerIntegrator {
path: string | undefined;
filename = "connector.burgerpanelsock";
requestCallbacks: {
[id: string]: (resp: any) => void
[serverID: string]: {
[id: string]: (resp: any) => void
}
} = {};
server: net.Server = undefined as any as net.Server;
servers: {
Expand All @@ -28,6 +38,20 @@ export default new class ServerIntegrator {
if(fsSync.existsSync(this.path)) fsSync.rmSync(this.path);
this.listen();
}
async updateIntegratorIfNeeded(server: Server) {
let files: string[];
try {
files = await fs.readdir(server.path + "/plugins/");
} catch {
return;
}
let currentIntegratorFilename = files.find(f => f.startsWith("BurgerPanelIntegrator-") && f.endsWith(".jar"));
if(!currentIntegratorFilename) return;
if(currentIntegratorFilename == `BurgerPanelIntegrator-${buildInfo.gitHash}.jar`) return;
logger.log(`Autoupdating BurgerPanel integrator on server ${server.name}`, "server.integrator")
await fs.unlink(server.path + "/plugins/" + currentIntegratorFilename);
await fs.copyFile(integratorJarPath, server.path + "/plugins/" + `BurgerPanelIntegrator-${buildInfo.gitHash}.jar`);
}
getPath() {
if(process.platform == "win32") {
logger.log("Integrator cannot be used because microsoft", "debug", LogLevel.DEBUG);
Expand All @@ -40,6 +64,9 @@ export default new class ServerIntegrator {
let server = net.createServer(_c => {
let c = _c as OurIntegratorClient;
c.burgerpanelData = {};
c.on("close", () => {
if(c.burgerpanelData.server) delete this.servers[c.burgerpanelData.server];
});
c.on("data", d => {
let json;
try {
Expand All @@ -52,8 +79,9 @@ export default new class ServerIntegrator {
if(!["request", "response"].includes(json.dataType)) return;
if(json.dataType == "response") {
console.log(JSON.stringify(json, null, 2));
if(!this.requestCallbacks[json.id]) return;
this.requestCallbacks[json.id](json.data);
if(!c.burgerpanelData.server) return;
if(!this.requestCallbacks[c.burgerpanelData.server] || !this.requestCallbacks[c.burgerpanelData.server][json.id]) return;
this.requestCallbacks[c.burgerpanelData.server][json.id](json.data);
return;
}
// todo: proper handler
Expand Down Expand Up @@ -81,5 +109,42 @@ export default new class ServerIntegrator {
this.servers[server._id] = {
server
};
this.requestCallbacks[server._id] = {};
}
findUnusedID(server: Server): string {
for(let i = 0; i < 100; i++) {
let str = nodeCrypto.randomBytes(30).toString("base64url");
if(this.requestCallbacks[server._id][str]) continue;
return str;
}
throw new Error("somehow didnt find a id in 100 tries");
}
request(server: Server, packet: string, data: any = {}): Promise<any> {
if(!this.isReadyForRequests(server)) throw new Error("Server isn't ready for requests!");
const client = this.servers[server._id].client;
if(!client) throw new Error("bad");
let id = this.findUnusedID(server);
let toSend = JSON.stringify({
id,
packet,
data
});
console.log("Sending", toSend);
client.write(toSend);
return new Promise((res, rej) => {
let timeout = setTimeout(() => {
delete this.requestCallbacks[server._id][id];
rej("Request timed out");
}, 10_000);
this.requestCallbacks[server._id][id] = (d) => {
clearTimeout(timeout);
res(d);
}
});
}
isReadyForRequests(server: Server) {
const client = this.servers[server._id]?.client;
if(!client) return false;
return true;
}
}
1 change: 1 addition & 0 deletions Server/src/serverManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ enforce-secure-profile=false
args = ["-Dnojline=true", "-Xms" + server.mem + "M", "-Xmx" + server.mem + "M", ...jvmArgs];
}
serverIntegrator.prepareServer(server);
await serverIntegrator.updateIntegratorIfNeeded(server);
let childProcess = spawn("java", args, {
cwd: server.path,
stdio: "pipe",
Expand Down
14 changes: 14 additions & 0 deletions Share/Integrator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type IntegratorPlayerInformation = {
name: string,
uuid: string,
location: {
world: string,
x: number,
y: number,
z: number
}
};
export type IntegratorServerInformation = {
tps: number,
players: IntegratorPlayerInformation[]
};
2 changes: 1 addition & 1 deletion Share/Packets.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Do not touch this file, it is automatically changed.
export default ["attachToServer","auth","createServer","createUser","deleteServer","deleteUser","detachFromServer","editUser","getAllServers","getAllSettings","getServer","getSetting","getUserData","getUserToken","getUsers","importServer","killServer","listSessions","logging","logout","ping","plugins","serverFiles","serverLogs","setServerOption","setSetting","startServer","stopServer","systemInformation","writeToConsole"] as const
export default ["attachToServer","auth","createServer","createUser","deleteServer","deleteUser","detachFromServer","editUser","getAllServers","getAllSettings","getServer","getSetting","getUserData","getUserToken","getUsers","importServer","integrator","killServer","listSessions","logging","logout","ping","plugins","serverFiles","serverLogs","setServerOption","setSetting","startServer","stopServer","systemInformation","writeToConsole"] as const
2 changes: 1 addition & 1 deletion Share/Permission.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Server } from "./Server.js";
import { User } from "./User.js";
export const _ServerPermissions = ["set.autostart", "set.autorestart", "set.port", "set.jvmArgs", "set.usejvmargs", "set.software", "set.version", "set.mem", "set.name", "set.allowedUsers.add", "set.allowedUsers.remove", "set.allowedUsers.permissions.write", "console.read", "console.write", "status", "stop", "start", "kill", "delete", "oldlogs.read", "serverfiles.read", "serverfiles.write", "serverfiles.delete", "serverfiles.upload", "serverfiles.download", "serverfiles.new", "serverfiles.rename", "plugins.download", "full"] as const;
export const _ServerPermissions = ["set.autostart", "set.autorestart", "set.port", "set.jvmArgs", "set.usejvmargs", "set.software", "set.version", "set.mem", "set.name", "set.allowedUsers.add", "set.allowedUsers.remove", "set.allowedUsers.permissions.write", "console.read", "console.write", "status", "stop", "start", "kill", "delete", "oldlogs.read", "serverfiles.read", "serverfiles.write", "serverfiles.delete", "serverfiles.upload", "serverfiles.download", "serverfiles.new", "serverfiles.rename", "plugins.download", "integrator.install", "full"] as const;
export const _ServersPermissions = ["create", "import", "all.view"] as const;
export const _UserPermissions = ["create", "view", "token.read", "token.reset", "delete", "permissions.read", "permissions.write", "password.change", "username.change.self", "username.change.all"] as const;
export const _SettingPermissions = [`set`, "read", "logging.set"] as const;
Expand Down
9 changes: 8 additions & 1 deletion Share/Requests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Config, ConfigValue } from "./Config";
import { GeneralInformation, ServerPerformance } from "./SystemInformation";
import { IntegratorServerInformation } from "./Integrator";
import { Server, ServerStatus, ServerStatuses } from "./Server";
import { ModrinthPluginResult, Plugin, Version } from "./Plugin";
import { User } from "./User";
Expand Down Expand Up @@ -122,7 +123,13 @@ export type RequestResponses = {
} | {
type: "downloadSuccess"
},
listSessions: {username?: string, _id?: string}[]
listSessions: {username?: string, _id?: string}[],
integrator: {
type: "install-success"
} | {
type: "status",
status: IntegratorServerInformation
}
}


Expand Down

0 comments on commit 04f33e4

Please sign in to comment.