Skip to content

Commit

Permalink
more integrator work, seems to work
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueBurger committed Nov 28, 2023
1 parent 04f33e4 commit f1462ab
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 11 additions & 3 deletions Server/src/packets/integrator.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
}
Expand Down
22 changes: 19 additions & 3 deletions Server/src/serverIntegrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -107,7 +113,8 @@ export default new class ServerIntegrator {
}
prepareServer(server: Server) {
this.servers[server._id] = {
server
server,
cachedResponses: {}
};
this.requestCallbacks[server._id] = {};
}
Expand All @@ -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<any> {
request(server: Server, packet: string, data: any = {}, useCache: boolean = false): 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");
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,
Expand All @@ -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);
}
});
Expand Down
3 changes: 2 additions & 1 deletion Share/Integrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export type IntegratorPlayerInformation = {
};
export type IntegratorServerInformation = {
tps: number,
players: IntegratorPlayerInformation[]
players: IntegratorPlayerInformation[],
playerCount: number
};
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", "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;
Expand Down
3 changes: 3 additions & 0 deletions Share/Requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ export type RequestResponses = {
} | {
type: "status",
status: IntegratorServerInformation
} | {
type: "isInstalled",
installed: boolean
}
}

Expand Down
12 changes: 11 additions & 1 deletion Web/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,24 @@ 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(.*)*',
component: () => import("./pages/404.vue"),
meta: {
title: "404 Not Found"
}
},
}
],
})
let app = createApp(App).use(router).use(pinia);
Expand Down
6 changes: 6 additions & 0 deletions Web/src/pages/Management/Edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ async function changeUseJVMArgs() {
}" v-if="hasServerPermission(user.user, server, 'plugins.download')">
<button>Download Plugins</button>
</RouterLink>
<RouterLink :to="{
name: 'integrator',
params: {
server: props.server
}
}"><button>Integrator</button></RouterLink>
<button @click="servers.togglePin(server)">{{ servers.isPinned(server) ? "Unpin" : "Pin" }}</button>
<br/><hr/>
<div class="main-server-settings">
Expand Down
33 changes: 33 additions & 0 deletions Web/src/pages/Management/Integrator.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script setup lang="ts">
import { useServers } from '@stores/servers';
import { useWS } from '@stores/ws';
import { ref } from 'vue';
const ws = useWS();
const props = defineProps<{
server: string
}>()
const servers = useServers();
const server = ref(await servers.getServerByID(props.server));
const isInstalledResp = await ws.sendRequest("integrator", {
id: props.server,
action: "isInstalled"
});
if(isInstalledResp.type != "isInstalled") throw new Error("bad type");
const status = ref();
async function getStatus() {
const statusResp = await ws.sendRequest("integrator", {
id: props.server,
action: "status"
});
if(statusResp.type != "status") return;
status.value = statusResp.status;
}
</script>

<template>
Test<br/>
{{ server.name }}
{{ isInstalledResp.installed }}
<button @click="getStatus">get status</button>
<pre>{{ JSON.stringify(status, null, 2) }}</pre>
</template>

0 comments on commit f1462ab

Please sign in to comment.