2026-04-13 00:46:58 -06:00
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
import { status } from "minecraft-server-util";
|
|
|
|
|
import { execSync } from "child_process";
|
|
|
|
|
import { MC_SERVER_IP, MC_SERVER_PORT } from "@/lib/constants";
|
|
|
|
|
import { sendCommand } from "@/lib/rcon";
|
2026-04-13 00:59:10 -06:00
|
|
|
import { memoAsync } from "@/lib/cache";
|
2026-04-13 00:46:58 -06:00
|
|
|
|
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
|
2026-04-13 00:59:10 -06:00
|
|
|
type StatusResult = {
|
|
|
|
|
online: boolean;
|
|
|
|
|
starting?: boolean;
|
|
|
|
|
players: { online: number; max: number };
|
|
|
|
|
version?: string;
|
|
|
|
|
motd?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function probeStatus(): Promise<StatusResult> {
|
|
|
|
|
// Race protocol ping + RCON in parallel — whichever wins first signals "online"
|
|
|
|
|
const ping = status(MC_SERVER_IP, MC_SERVER_PORT, { timeout: 3000 }).then(
|
|
|
|
|
(r): StatusResult => ({
|
2026-04-13 00:46:58 -06:00
|
|
|
online: true,
|
2026-04-13 00:59:10 -06:00
|
|
|
players: { online: r.players.online, max: r.players.max },
|
|
|
|
|
version: r.version.name,
|
|
|
|
|
motd: r.motd.clean,
|
|
|
|
|
})
|
|
|
|
|
);
|
2026-04-13 00:46:58 -06:00
|
|
|
|
2026-04-13 00:59:10 -06:00
|
|
|
const rcon = sendCommand("list").then((response): StatusResult => {
|
2026-04-13 00:46:58 -06:00
|
|
|
const match = response.match(/There are (\d+) of a max of (\d+) players/);
|
2026-04-13 00:59:10 -06:00
|
|
|
return {
|
2026-04-13 00:46:58 -06:00
|
|
|
online: true,
|
|
|
|
|
players: {
|
|
|
|
|
online: match ? parseInt(match[1], 10) : 0,
|
|
|
|
|
max: match ? parseInt(match[2], 10) : 20,
|
|
|
|
|
},
|
2026-04-13 00:59:10 -06:00
|
|
|
};
|
|
|
|
|
});
|
2026-04-13 00:46:58 -06:00
|
|
|
|
|
|
|
|
try {
|
2026-04-13 00:59:10 -06:00
|
|
|
return await Promise.any([ping, rcon]);
|
|
|
|
|
} catch {
|
|
|
|
|
// Both failed — check if process is up
|
|
|
|
|
let starting = false;
|
|
|
|
|
try {
|
|
|
|
|
const out = execSync("systemctl is-active minecraft.service", {
|
|
|
|
|
encoding: "utf8",
|
|
|
|
|
}).trim();
|
|
|
|
|
starting = out === "active" || out === "activating";
|
|
|
|
|
} catch {}
|
|
|
|
|
return { online: false, starting, players: { online: 0, max: 0 } };
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-13 00:46:58 -06:00
|
|
|
|
2026-04-13 00:59:10 -06:00
|
|
|
export async function GET() {
|
|
|
|
|
const result = await memoAsync("status", 3000, probeStatus);
|
|
|
|
|
return NextResponse.json(result, {
|
|
|
|
|
headers: { "Cache-Control": "public, max-age=3" },
|
2026-04-13 00:46:58 -06:00
|
|
|
});
|
|
|
|
|
}
|