[TS] Renamed file extensions to typescript
This commit is contained in:
parent
4d5b347767
commit
4a6f20fa7b
31 changed files with 12 additions and 0 deletions
71
lib/controllers/sub-controllers/console-controller.ts
Normal file
71
lib/controllers/sub-controllers/console-controller.ts
Normal file
|
@ -0,0 +1,71 @@
|
|||
// Imports
|
||||
import k8s from "@kubernetes/client-node";
|
||||
import { Rcon as RconClient } from "rcon-client";
|
||||
import stream from "stream";
|
||||
import { ERR, WARN } from "../../util/logging.js";
|
||||
import { getServerEntry } from "../../database/queries/server-queries.js";
|
||||
import kc from "../../k8s/k8s-config.js";
|
||||
// Kubernetes Configuration
|
||||
|
||||
const k8sCore = kc.makeApiClient(k8s.CoreV1Api);
|
||||
const namespace = process.env.MCL_SERVER_NAMESPACE;
|
||||
|
||||
// Retrieves logs from the minecraft server container
|
||||
export async function webConsoleLogs(socket) {
|
||||
const { serverId } = socket.mcs;
|
||||
const server = await getServerEntry(serverId);
|
||||
const podName = `mcl-${server.mclName}`;
|
||||
const containerName = `${podName}-server`;
|
||||
const podResponse = await k8sCore.listNamespacedPod(namespace);
|
||||
const pods = podResponse.body.items.map((vp1) => vp1.metadata.name);
|
||||
const mcsPods = pods.filter((p) => p.startsWith(podName));
|
||||
if (mcsPods.length === 0)
|
||||
throw Error(`Could not find a pod that starts with ${podName}`);
|
||||
if (mcsPods.length > 1)
|
||||
throw Error(`Multiple pods match the name ${podName}`);
|
||||
|
||||
const log = new k8s.Log(kc);
|
||||
const logStream = new stream.PassThrough();
|
||||
var logstreamBuffer = "";
|
||||
logStream.on("data", (chunk) => {
|
||||
const bufferString = Buffer.from(chunk).toString();
|
||||
if (!bufferString.includes("\n")) return (logstreamBuffer += bufferString);
|
||||
const clientChunks = `${logstreamBuffer}${bufferString}`.split("\n");
|
||||
for (var c of clientChunks) socket.emit("push", c);
|
||||
});
|
||||
log
|
||||
.log(namespace, mcsPods[0], containerName, logStream, {
|
||||
follow: true,
|
||||
pretty: false,
|
||||
timestamps: false,
|
||||
})
|
||||
.catch((e) => ERR("CONSOLE CONTROLLER", "Error streaming logs", e));
|
||||
}
|
||||
|
||||
// Creates an RCON connection to the minecraft container
|
||||
export async function webConsoleRcon(socket) {
|
||||
if (socket.rconClient)
|
||||
return VERB("RCON", "Socket already connected to RCON");
|
||||
const { serverId } = socket.mcs;
|
||||
const server = await getServerEntry(serverId);
|
||||
const rconSecret = `mcl-${server.mclName}-rcon-secret`;
|
||||
const rconRes = await k8sCore.readNamespacedSecret(rconSecret, namespace);
|
||||
const rconPassword = Buffer.from(
|
||||
rconRes.body.data["rcon-password"],
|
||||
"base64",
|
||||
).toString("utf8");
|
||||
const rconHost = `mcl-${server.mclName}-rcon.${namespace}.svc.cluster.local`;
|
||||
const rcon = new RconClient({
|
||||
host: rconHost,
|
||||
port: 25575,
|
||||
password: rconPassword,
|
||||
});
|
||||
rcon.on("error", (error) => socket.emit("push", error));
|
||||
try {
|
||||
await rcon.connect();
|
||||
} catch (error) {
|
||||
socket.emit("rcon-error", "Could not connect RCON Input to server!");
|
||||
WARN("RCON", `Could not connect to '${rconHost}'`);
|
||||
}
|
||||
socket.rconClient = rcon;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue