From f1462abe9885e71ec3493a560912696effb806b3 Mon Sep 17 00:00:00 2001 From: e <58081413+TheBlueBurger@users.noreply.github.com> Date: Tue, 28 Nov 2023 08:51:16 +0100 Subject: [PATCH] more integrator work, seems to work --- .../packets/StatusPacket.java | 1 + Server/src/packets/integrator.ts | 14 ++++++-- Server/src/serverIntegrator.ts | 22 +++++++++++-- Share/Integrator.ts | 3 +- Share/Permission.ts | 2 +- Share/Requests.ts | 3 ++ Web/src/main.ts | 12 ++++++- Web/src/pages/Management/Edit.vue | 6 ++++ Web/src/pages/Management/Integrator.vue | 33 +++++++++++++++++++ 9 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 Web/src/pages/Management/Integrator.vue diff --git a/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/packets/StatusPacket.java b/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/packets/StatusPacket.java index a3a22c3..1491f81 100644 --- a/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/packets/StatusPacket.java +++ b/Integrator/src/main/java/io/github/theblueburger/burgerpanelintegrator/packets/StatusPacket.java @@ -35,6 +35,7 @@ protected void execute(JSONObject data, String id) throws IOException { playerArray.add(playerObject); } responseObj.put("players", playerArray); + responseObj.put("playerCount", players.size()); BurgerPanelIntegrator.respond(id, responseObj); } } diff --git a/Server/src/packets/integrator.ts b/Server/src/packets/integrator.ts index 55b32aa..7bcd884 100644 --- a/Server/src/packets/integrator.ts +++ b/Server/src/packets/integrator.ts @@ -1,8 +1,9 @@ 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 { userHasAccessToServer } from "../serverManager.js"; +import { hasServerPermission } from "../../../Share/Permission.js"; import { Request } from "../../../Share/Requests.js"; +import fs from "node:fs/promises"; import IntegratorInstall from "./integrator/IntegratorInstall.js" import serverIntegrator from "../serverIntegrator.js"; @@ -13,16 +14,23 @@ export default class IntegratorPacket extends Packet { 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!"; + if(!hasServerPermission(client.data.auth.user, server.toJSON(), "integrator.basic")) return "You can't use the integrator of this server!"; 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"); + if(!hasServerPermission(client.data.auth.user, server.toJSON(), "integrator.serverstatus")) return "You got no permission for getting the status!"; + const status = await serverIntegrator.request(server.toJSON(), "status", {}, true); return { type: "status", status } + case "isInstalled": + return { + type: "isInstalled", + installed: (await fs.readdir(server.path + "/plugins/")).some(a => a.endsWith(".jar") && a.startsWith("BurgerPanelIntegrator-")) + } } return; } diff --git a/Server/src/serverIntegrator.ts b/Server/src/serverIntegrator.ts index dbbf742..9189ae6 100644 --- a/Server/src/serverIntegrator.ts +++ b/Server/src/serverIntegrator.ts @@ -29,7 +29,13 @@ export default new class ServerIntegrator { servers: { [id: string]: { server: Server, - client?: OurIntegratorClient + client?: OurIntegratorClient, + cachedResponses: { + [name: string]: { + data: any, + expiresAt: number + } + } } } = {}; constructor() { @@ -107,7 +113,8 @@ export default new class ServerIntegrator { } prepareServer(server: Server) { this.servers[server._id] = { - server + server, + cachedResponses: {} }; this.requestCallbacks[server._id] = {}; } @@ -119,10 +126,14 @@ export default new class ServerIntegrator { } throw new Error("somehow didnt find a id in 100 tries"); } - request(server: Server, packet: string, data: any = {}): Promise { + request(server: Server, packet: string, data: any = {}, useCache: boolean = false): Promise { 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"); + if(useCache) { + const cachedResponses = this.servers[server._id]?.cachedResponses; + if(cachedResponses && cachedResponses[packet] && Date.now() < cachedResponses[packet].expiresAt) return Promise.resolve(cachedResponses[packet].data); + } let id = this.findUnusedID(server); let toSend = JSON.stringify({ id, @@ -137,7 +148,12 @@ export default new class ServerIntegrator { rej("Request timed out"); }, 10_000); this.requestCallbacks[server._id][id] = (d) => { + delete this.requestCallbacks[server._id][id]; clearTimeout(timeout); + if(useCache) this.servers[server._id].cachedResponses[packet] = { + expiresAt: Date.now() + 5000, + data: d + } res(d); } }); diff --git a/Share/Integrator.ts b/Share/Integrator.ts index 2b9aaab..021f6ec 100644 --- a/Share/Integrator.ts +++ b/Share/Integrator.ts @@ -10,5 +10,6 @@ export type IntegratorPlayerInformation = { }; export type IntegratorServerInformation = { tps: number, - players: IntegratorPlayerInformation[] + players: IntegratorPlayerInformation[], + playerCount: number }; \ No newline at end of file diff --git a/Share/Permission.ts b/Share/Permission.ts index 207c601..9d52bc5 100644 --- a/Share/Permission.ts +++ b/Share/Permission.ts @@ -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", "integrator.install", "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.basic", "integrator.serverstatus", "integrator.serverstatus.players", "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; diff --git a/Share/Requests.ts b/Share/Requests.ts index f78d604..10ab66b 100644 --- a/Share/Requests.ts +++ b/Share/Requests.ts @@ -129,6 +129,9 @@ export type RequestResponses = { } | { type: "status", status: IntegratorServerInformation + } | { + type: "isInstalled", + installed: boolean } } diff --git a/Web/src/main.ts b/Web/src/main.ts index 1d31e20..a83d1ca 100644 --- a/Web/src/main.ts +++ b/Web/src/main.ts @@ -148,6 +148,16 @@ const router = createRouter({ title: "Debug" } }, + { + name: "integrator", + path: "/manage/server/:server/edit/integrator", + props: true, + component: () => import("./pages/Management/Integrator.vue"), + meta: { + setTitle: false, + title: "Integrator Options" + } + }, { name: "404", path: '/:pathMatch(.*)*', @@ -155,7 +165,7 @@ const router = createRouter({ meta: { title: "404 Not Found" } - }, + } ], }) let app = createApp(App).use(router).use(pinia); diff --git a/Web/src/pages/Management/Edit.vue b/Web/src/pages/Management/Edit.vue index d0ed0fb..3118a22 100644 --- a/Web/src/pages/Management/Edit.vue +++ b/Web/src/pages/Management/Edit.vue @@ -199,6 +199,12 @@ async function changeUseJVMArgs() { }" v-if="hasServerPermission(user.user, server, 'plugins.download')"> +

diff --git a/Web/src/pages/Management/Integrator.vue b/Web/src/pages/Management/Integrator.vue new file mode 100644 index 0000000..d5c1a84 --- /dev/null +++ b/Web/src/pages/Management/Integrator.vue @@ -0,0 +1,33 @@ + + + \ No newline at end of file