make the minecraft script fully manage the minecraft servert process on-demand

This commit is contained in:
hippoz 2022-03-06 01:37:59 +02:00
parent d59f1be9e7
commit b24a2dbae9
No known key found for this signature in database
GPG key ID: 7C52899193467641

View file

@ -7,14 +7,21 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import { WebSocket } from "ws"; import { WebSocket } from "ws";
import { spawn } from "node:child_process";
import Rcon from "rcon"; import Rcon from "rcon";
const TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InN0YWdpbmctbWluZWNyYWZ0LWJyaWRnZSIsImF2YXRhclVSTCI6bnVsbCwiZGlzY29yZElEIjoiMCIsImd1aWxkQWNjZXNzIjpbIjczNjI5MjUwOTEzNDc0OTgwNyJdLCJpc1N1cGVyVG9rZW4iOnRydWUsImlhdCI6MTY0NDQ1MzM4MX0.-XIBl6VLnXVwve9iqhWs51ABZkm1i_v1tS6X01SPk3U"; // A supertoken is required to send messages from Minecraft. const TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InN0YWdpbmctbWluZWNyYWZ0LWJyaWRnZSIsImF2YXRhclVSTCI6bnVsbCwiZGlzY29yZElEIjoiMCIsImd1aWxkQWNjZXNzIjpbIjczNjI5MjUwOTEzNDc0OTgwNyJdLCJpc1N1cGVyVG9rZW4iOnRydWUsImlhdCI6MTY0NDQ1MzM4MX0.-XIBl6VLnXVwve9iqhWs51ABZkm1i_v1tS6X01SPk3U"; // A supertoken is required to send messages from Minecraft.
const TARGET_GUILD_ID = "_"; const TARGET_GUILD_ID = "_";
const TARGET_CHANNEL_ID = "_"; const TARGET_CHANNEL_ID = "_";
const ORIGIN = "http://localhost:4050"; const ORIGIN = "http://localhost:4050";
const GATEWAY_ORIGIN = "ws://localhost:4050/gateway"; const GATEWAY_ORIGIN = "ws://localhost:4050/gateway";
const FRONTEND_ORIGIN = "http://localhost:4050/";
const chatMessageRegex = /^\[(?:.*?)\]: \<(?<username>[a-zA-Z0-9]+)\> (?<message>.*)/;
const joinNotificationRegex = /^\[(?:.*?)\]: (?<username>[a-zA-Z0-9]+) joined the game/;
const leaveNotificationRegex = /^\[(?:.*?)\]: (?<username>[a-zA-Z0-9]+) left the game/;
const messageSchema = { t: "number", d: "object" }; const messageSchema = { t: "number", d: "object" };
const messageTypes = { const messageTypes = {
HELLO: 0, HELLO: 0,
@ -22,17 +29,29 @@ const messageTypes = {
READY: 2, READY: 2,
EVENT: 3 EVENT: 3
}; };
const EXPRIMENTAL_ACCOUNT_LINK = false;
const chatMessageRegex = /^\[(?:.*?)\]: \<(?<username>[a-zA-Z0-9]+)\> (?<message>.*)/; const createServerChildProcess = () => {
const joinNotificationRegex = /^\[(?:.*?)\]: (?<username>[a-zA-Z0-9]+) joined the game/; const serverJar = process.env.SERVER_JAR;
const leaveNotificationRegex = /^\[(?:.*?)\]: (?<username>[a-zA-Z0-9]+) left the game/; if (!serverJar) {
const rconConnection = new Rcon("localhost", "25575", process.env.RCON_PASSWORD); throw new Error("SERVER_JAR not found in env");
}
const pendingHandoffs = new Map(); const serverPwd = process.env.SERVER_PWD;
const usernameToToken = new Map(); if (!serverPwd) {
throw new Error("SERVER_PWD not found in env");
}
export default class GatewayClient { return spawn("java", [
"-Xms1G", "-Xmx5G", "-XX:+UseG1GC", "-XX:+ParallelRefProcEnabled", "-XX:MaxGCPauseMillis=200", "-XX:+UnlockExperimentalVMOptions", "-XX:+DisableExplicitGC", "-XX:+AlwaysPreTouch", "-XX:G1NewSizePercent=30",
"-XX:G1MaxNewSizePercent=40", "-XX:G1HeapRegionSize=8M", "-XX:G1ReservePercent=20", "-XX:G1HeapWastePercent=5", "-XX:G1MixedGCCountTarget=4", "-XX:InitiatingHeapOccupancyPercent=15", "-XX:G1MixedGCLiveThresholdPercent=90",
"-XX:G1RSetUpdatingPauseTimePercent=5", "-XX:SurvivorRatio=32", "-XX:+PerfDisableSharedMem", "-XX:MaxTenuringThreshold=1", "-Dusing.aikars.flags=https://mcflags.emc.gs",
"-Daikars.new.flags=true", "-jar", serverJar, "nogui"
], {
cwd: serverPwd
});
};
class GatewayClient {
constructor(gatewayPath) { constructor(gatewayPath) {
this.gatewayPath = gatewayPath; this.gatewayPath = gatewayPath;
this.ws = null; this.ws = null;
@ -137,8 +156,139 @@ export default class GatewayClient {
} }
} }
class Bridge {
constructor() {
this.gatewayConnection = new GatewayClient(GATEWAY_ORIGIN);
this.process = null;
this.rconConnection = null;
this.playerCount = 0;
this.serverStartedAt = null;
async function sendBridgeMessageAs(guildId, channelId, content, username=undefined, avatarURL=undefined) { this.gatewayConnection.onEvent = (e) => {
if (
e.eventType === "MESSAGE_CREATE" &&
e.message.channel_id === TARGET_CHANNEL_ID &&
e.message.guild_id === TARGET_GUILD_ID &&
!e.message.webhook_id
) {
if (e.message.content === "!:mc-start") {
this.userRequestedServerJob();
return;
} else if (e.message.content === "!:mc-status") {
this.sendBridgeMessageAs(TARGET_GUILD_ID,
TARGET_CHANNEL_ID,
`:scroll: **Server information**\n**Player Count**: ${this.playerCount}\n**Started**: <t:${this.serverStartedAt}:R>`,
null,
null
);
return;
}
this.sendMinecraftMessageAs(
e.message.author.username,
e.message.content,
e.message.attachments,
e.message.referenced_message
);
}
};
}
rconConnect() {
if (this.rconConnection) {
this.rconConnection.disconnect();
this.rconConnection = null;
}
this.rconConnection = new Rcon("localhost", "25575", process.env.RCON_PASSWORD);
this.rconConnection.connect();
this.rconConnection.on("error", (e) => {
if (e.code === "ECONNREFUSED" && e.syscall === "connect") {
console.warn("rcon: ECONNREFUSED, reconnecting in 5s...");
setTimeout(() => {
console.log("rcon: reconnecting...");
this.rconConnect();
}, 5000);
} else {
console.error("rcon: got error", e);
console.error("rcon: don't know what to do, disconnecting rcon due to previous error");
this.rconConnection.disconnect();
this.rconConnection = null;
}
});
this.rconConnection.on("connect", () => {
this.serverStartedAt = Date.now();
this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":zap: Server started!", null, null);
});
}
start() {
this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":bridge_at_night: The bridge server has started! Type `!:mc-start` in the chat to start the Minecraft server. Any further updates related to the server's status will be sent in this channel.", null, null);
this.gatewayConnection.connect(TOKEN);
}
spawnProcess() {
if (this.process) {
console.warn("server job: spawnProcess(): another instance already exists");
this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":thinking: It seems like the server is already running! If you think this is a mistake, contact the server manager.", null, null);
return;
}
console.log("server job: spawning");
this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":clock5: Starting server...", null, null);
this.process = createServerChildProcess();
this.process.stderr.on("data", async (rawDataBuffer) => {
const stringData = rawDataBuffer.toString().trim();
console.error(`[server process stderr]: ${stringData}`);
});
this.process.stdout.on("data", async (rawDataBuffer) => {
const stringData = rawDataBuffer.toString().trim();
console.log(`[server process stdout]: ${stringData}`);
const joinResult = joinNotificationRegex.exec(stringData);
if (joinResult) {
this.onPlayerCountUpdate(1);
await this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, `:door: **${joinResult.groups.username}** joined the game. Player count is ${this.playerCount}.`, null, null);
return;
}
const leaveResult = leaveNotificationRegex.exec(stringData);
if (leaveResult) {
this.onPlayerCountUpdate(-1);
await this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, `:door: **${leaveResult.groups.username}** left the game. Player count is ${this.playerCount}.`, null, null);
return;
}
const messageResult = chatMessageRegex.exec(stringData);
if (messageResult) {
await this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, messageResult.groups.message, messageResult.groups.username, null);
}
});
this.process.on("spawn", () => {
console.log("server process: spawn");
this.process.stdout.resume();
this.process.stderr.resume();
this.rconConnect();
});
this.process.on("exit", (code) => {
console.log(`server process: exited with code ${code}`);
this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":zap: Server is now closed.", null, null);
this.process = null;
});
this.process.on("error", (e) => {
console.error("server process: error", e);
this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":flushed: Server process error.", null, null);
this.process = null;
});
}
async sendBridgeMessageAs(guildId, channelId, content, username=undefined, avatarURL=undefined) {
return await fetch(`${ORIGIN}/api/v1/guilds/${guildId}/channels/${channelId}/messages/create`, { return await fetch(`${ORIGIN}/api/v1/guilds/${guildId}/channels/${channelId}/messages/create`, {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
@ -153,11 +303,7 @@ async function sendBridgeMessageAs(guildId, channelId, content, username=undefin
}); });
} }
async function sendMinecraftMessageAs(rcon, username, content, attachments, referencedMessage=null) { sendMinecraftMessageAs(username, content, attachments=[], referencedMessage=null) {
if (!attachments) {
attachments = [];
}
const tellrawPayload = [ const tellrawPayload = [
{ text: "[" }, { text: "[" },
{ text: `${username}`, color: "gray" }, { text: `${username}`, color: "gray" },
@ -166,12 +312,13 @@ async function sendMinecraftMessageAs(rcon, username, content, attachments, refe
]; ];
if (referencedMessage) { if (referencedMessage) {
let trimmedContent = referencedMessage.content.substring(0, 50); let trimmedContent = referencedMessage.content.substring(0, 70);
if (trimmedContent !== referencedMessage.content) { if (trimmedContent !== referencedMessage.content) {
trimmedContent += "..."; trimmedContent += "...";
} }
tellrawPayload.push({ tellrawPayload.push({
text: `<replying to ${referencedMessage.author.username}: ${trimmedContent}>` text: `<replying to ${referencedMessage.author.username}: ${trimmedContent}>`,
color: "gray"
}); });
tellrawPayload.push({ tellrawPayload.push({
text: " " text: " "
@ -194,99 +341,49 @@ async function sendMinecraftMessageAs(rcon, username, content, attachments, refe
tellrawPayload.push({ text: content }); tellrawPayload.push({ text: content });
rcon.send(`tellraw @a ${JSON.stringify(tellrawPayload)}`); if (this.rconConnection)
this.rconConnection.send(`tellraw @a ${JSON.stringify(tellrawPayload)}`);
} }
async function startHandoffForUser(username) { onPlayerCountUpdate(amount) {
const handoffRes = await fetch(`${ORIGIN}/api/v1/tokens/handoff/create`, { this.playerCount += amount;
method: "POST",
body: JSON.stringify({}),
headers: {
"content-type": "application/json",
"authorization": TOKEN
}
});
const { token: handoffToken, dispatchUUID } = (await handoffRes.json());
pendingHandoffs.set(dispatchUUID, username); if (this.playerCount === 0) {
// TODO: probably not a good idea this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":wave: Everyone has left the server. Closing...", null, null);
setTimeout(() => { console.log("server job: playerCount has reached 0, will close child server process");
pendingHandoffs.delete(dispatchUUID); if (this.process) {
}, 40000); console.log("server job: closing process...");
this.process.kill("SIGINT");
return handoffToken;
}
async function main() {
rconConnection.on("error", (e) => {
console.error("rcon: got error", e);
if (!rconConnection.hasAuthed) {
console.log("rcon: reconnecting in 5000ms due to error before hasAuthed (server might not be up yet?)");
setTimeout(() => {
rconConnection.connect();
}, 5000);
}
});
const gateway = new GatewayClient(GATEWAY_ORIGIN);
gateway.onEvent = (e) => {
if (e.eventType === "$BRIDGE_HANDOFF_FULFILLMENT") {
const username = pendingHandoffs.get(e.dispatchUUID);
if (!username)
return;
usernameToToken.set(username, e.token);
sendMinecraftMessageAs(rconConnection, "Minecraft Bridge System", `${username} has been successfully linked to a bridge token.`, null, null);
} else if (e.eventType === "MESSAGE_CREATE" && e.message.channel_id === TARGET_CHANNEL_ID && e.message.guild_id === TARGET_GUILD_ID && !e.message.webhook_id) {
sendMinecraftMessageAs(rconConnection, e.message.author.username, e.message.content, e.message.attachments, e.message.referenced_message);
}
};
rconConnection.connect();
gateway.connect(TOKEN);
process.stdin.resume();
process.stdin.on("data", async (rawDataBuffer) => {
const stringData = rawDataBuffer.toString().trim();
console.log(stringData);
const joinResult = joinNotificationRegex.exec(stringData);
if (joinResult) {
await sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, `**${joinResult.groups.username}** joined the game`, null, null);
return;
}
const leaveResult = leaveNotificationRegex.exec(stringData);
if (leaveResult) {
await sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, `**${leaveResult.groups.username}** left the game`, null, null);
return;
}
const messageResult = chatMessageRegex.exec(stringData);
if (messageResult) {
if (EXPRIMENTAL_ACCOUNT_LINK && messageResult.groups.message === "!:minecraft-link") {
try {
const handoffToken = await startHandoffForUser(messageResult.groups.username);
const handoffLink = `${FRONTEND_ORIGIN}#token_handoff,,${handoffToken}`;
sendMinecraftMessageAs(
rconConnection,
"Minecraft Bridge System",
`${messageResult.groups.username}: Click on the attached link to finish linking your account. You have 40 seconds.`,
[
{
filename: `link account to ${messageResult.groups.username}`,
proxy_url: handoffLink
}
]
);
} catch(e) {
sendMinecraftMessageAs(rconConnection, "Minecraft Bridge System", `Failed to create link for ${messageResult.groups.username}.`, null, null);
}
} else { } else {
await sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, messageResult.groups.message, messageResult.groups.username, null); console.warn("server job: no process to kill");
} }
} }
}
userRequestedServerJob() {
console.log("server job: user requested server job, spawning...");
this.spawnProcess();
// wait for 5 minutes - if no one has joined the server in that time, close it
setTimeout(() => {
if (this.playerCount === 0) {
console.log("server job: no players on server after 5 minutes, closing...");
this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":pensive: Server was started, yet no one joined after 5 minutes. Closing again...", null, null);
}
}, 300000);
}
}
function main() {
const bridge = new Bridge();
bridge.start();
process.on("beforeExit", () => {
if (bridge.process) {
console.log("server process: killing on parent exit");
bridge.process.kill("SIGINT");
}
}); });
} }
await main(); main();