From 7de4960ca8f8031136c704bd8eeef513e4c2dd08 Mon Sep 17 00:00:00 2001 From: Cap Date: Thu, 26 Feb 2026 12:42:58 +0100 Subject: [PATCH] server.js aktualisiert --- server.js | 112 +++++++++++++----------------------------------------- 1 file changed, 27 insertions(+), 85 deletions(-) diff --git a/server.js b/server.js index 40a087c..5e47346 100644 --- a/server.js +++ b/server.js @@ -1,19 +1,16 @@ -'use strict'; +import pkg from 'gamedig'; -const http = require('http'); -const fs = require('fs'); -const path = require('path'); +// pkg.query ist direkt verfügbar in gamedig v4 +const GameDig = pkg; -// ── GameDig v4+ benötigt destructured import ────────────────────────────────── -const { GameDig } = require('gamedig'); +import http from 'http'; +import fs from 'fs'; -// ── Konfiguration ───────────────────────────────────────────────────────────── -const CONFIG_PATH = path.join('/home/container', 'config.json'); +const CONFIG_PATH = '/home/container/config.json'; const PORT = parseInt(process.env.SERVER_PORT || process.env.PORT || 21337, 10); const QUERY_INTERVAL = Math.max(10, parseInt(process.env.QUERY_INTERVAL || '60', 10)) * 1000; const API_KEY = process.env.API_KEY || ''; -// ── Config laden ────────────────────────────────────────────────────────────── let servers = []; try { const raw = fs.readFileSync(CONFIG_PATH, 'utf8'); @@ -25,118 +22,65 @@ try { process.exit(1); } -// ── Status-Cache ────────────────────────────────────────────────────────────── let cache = { updated: null, interval_seconds: QUERY_INTERVAL / 1000, servers: servers.map(s => ({ - label: s.label, - type: s.type, - address: `${s.host}:${s.port}`, - image: s.image || '', - status: 'pending', - players: 0, - maxPlayers: 0, - map: '', - ping: 0 + label: s.label, type: s.type, + address: `${s.host}:${s.port}`, image: s.image || '', + status: 'pending', players: 0, maxPlayers: 0, map: '', ping: 0 })) }; -// ── GameDig Abfrage ─────────────────────────────────────────────────────────── async function queryServer(srv) { try { - const opts = { - type: srv.type, - host: srv.host, - port: srv.port, - }; - // DayZ: query port ist game port + 24714 - if (srv.type === 'dayz' && !srv.queryPort) { - opts.port = srv.queryPort || (srv.port + 24714); - } - if (srv.queryPort) { - opts.port = srv.queryPort; - } - + const opts = { type: srv.type, host: srv.host, port: srv.port }; + if (srv.type === 'dayz') opts.port = srv.queryPort || (srv.port + 24714); + if (srv.queryPort) opts.port = srv.queryPort; const state = await GameDig.query(opts); return { - status: 'online', - players: state.players ? state.players.length : 0, + status: 'online', + players: Array.isArray(state.players) ? state.players.length : 0, maxPlayers: state.maxplayers || 0, - map: state.map || '', - ping: state.ping || 0 + map: state.map || '', + ping: Math.round(state.ping || 0) }; } catch (err) { - return { - status: 'offline', - error: err.message, - players: 0, - maxPlayers: 0, - map: '', - ping: 0 - }; + return { status: 'offline', error: String(err.message || err).substring(0, 200), players: 0, maxPlayers: 0, map: '', ping: 0 }; } } async function runQueries() { console.log(`[QUERY] Starte Abfragen (${servers.length} Server)...`); const start = Date.now(); - const results = await Promise.all(servers.map(srv => queryServer(srv))); - results.forEach((res, i) => { const srv = servers[i]; - cache.servers[i] = { - label: srv.label, - type: srv.type, - address: `${srv.host}:${srv.port}`, - image: srv.image || '', - ...res - }; + cache.servers[i] = { label: srv.label, type: srv.type, address: `${srv.host}:${srv.port}`, image: srv.image || '', ...res }; const icon = res.status === 'online' ? '✓' : '✗'; - const info = res.status === 'online' - ? `${res.players}/${res.maxPlayers} Spieler, ${res.ping}ms` - : res.error || 'offline'; + const info = res.status === 'online' ? `${res.players}/${res.maxPlayers} Spieler, Map: ${res.map}, ${res.ping}ms` : `FEHLER: ${res.error}`; console.log(` ${icon} [${srv.label}] ${res.status} – ${info}`); }); - cache.updated = new Date().toISOString(); console.log(`[QUERY] Abgeschlossen in ${Date.now() - start}ms.`); } -// ── HTTP Server ─────────────────────────────────────────────────────────────── function checkAuth(req) { if (!API_KEY) return true; const headerKey = req.headers['x-api-key']; - const urlKey = new URL(req.url, `http://localhost`).searchParams.get('key'); - return headerKey === API_KEY || urlKey === API_KEY; + const url = new URL(req.url, `http://localhost`); + return headerKey === API_KEY || url.searchParams.get('key') === API_KEY; } const server = http.createServer((req, res) => { const url = req.url.split('?')[0]; - - if (url === '/health') { - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ status: 'ok', uptime: process.uptime() })); - return; - } - + if (url === '/health') { res.writeHead(200, {'Content-Type': 'application/json'}); res.end(JSON.stringify({status:'ok',uptime:process.uptime()})); return; } if (url === '/api/servers') { - if (!checkAuth(req)) { - res.writeHead(401, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ error: 'Unauthorized' })); - return; - } - res.writeHead(200, { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*' - }); - res.end(JSON.stringify(cache, null, 2)); - return; + if (!checkAuth(req)) { res.writeHead(401, {'Content-Type':'application/json'}); res.end(JSON.stringify({error:'Unauthorized'})); return; } + res.writeHead(200, {'Content-Type':'application/json','Access-Control-Allow-Origin':'*'}); + res.end(JSON.stringify(cache, null, 2)); return; } - - res.writeHead(404, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ error: 'Not found' })); + res.writeHead(404, {'Content-Type':'application/json'}); res.end(JSON.stringify({error:'Not found'})); }); server.listen(PORT, () => { @@ -149,8 +93,6 @@ server.listen(PORT, () => { console.log('║ GET /api/servers ║'); console.log('║ GET /health ║'); console.log('╚══════════════════════════════════════════╝'); - - // Erste Abfrage sofort, dann im Intervall runQueries(); setInterval(runQueries, QUERY_INTERVAL); -}); \ No newline at end of file +});