Compare commits

...
Author SHA1 Message Date
mregosar b616bbafc1 Docker: alte Server ohne passenden Host-Namen über die Adresse finden 2026-10-29 13:54:46 +01:00
mregosar 3040124b33 Merge feature/admin-server-einstellungen in main 2026-10-29 13:32:48 +01:00
mregosar 929134f2bc Admin: Gameserver-Einstellungen, Docker-Hosts und Image-Update im Admin-Bereich 2026-10-29 13:32:05 +01:00
mregosar 472bda31a9 Merge feature/admin-server-adresse in main 2026-10-29 12:59:09 +01:00
mregosar ed65b92be1 Admin: Adresszeile kompakt gestalten, bei gestopptem Server ausblenden 2026-10-29 12:58:25 +01:00
mregosar beaf635c23 Speichermessung: Bind-Ordner eines Volumes auch ohne laufenden Container finden 2026-10-29 12:57:11 +01:00
mregosar f6735824cc Admin: freien Speicher dort messen, wo das Gameserver-Volume liegt 2026-10-29 12:55:23 +01:00
mregosar 2873b5ba3a Admin: IP und Ports des Servers in der Matchkarte anzeigen 2026-10-29 12:53:27 +01:00
mregosar 9bb878c641 Merge feature/host-disk in main 2026-10-29 10:30:02 +01:00
mregosar 1243e9c852 Admin: freier Speicher pro Docker-Host anzeigen
Die Karte Docker-Hosts zeigt den freien Speicher unter /var/lib/docker, per SSH
mit df gemessen und eine Minute zwischengespeichert. Unter 20 GB wird die
Zeile rot.
2026-10-29 10:30:01 +01:00
mregosar 844da4713d Merge feature/host-usage in main 2026-10-29 10:12:17 +01:00
mregosar 91f979945e Admin: Auslastung der Docker-Hosts mit den laufenden Matches
Die Admin-Übersicht zeigt pro Docker-Host belegte und freie Plätze und die
Matches, die dort laufen. Ohne konfigurierte Hosts bleibt die Karte aus.
2026-10-29 10:12:17 +01:00
mregosar c1c9250126 Merge feature/multi-docker-host in main 2026-10-29 10:00:20 +01:00
12 changed files with 883 additions and 16 deletions

No files matched your search

+300
View File
@@ -26,6 +26,7 @@ const storeToken = (t) => {
export default function AdminPage() {
const [token, setToken] = useState(getToken);
const [matches, setMatches] = useState(null);
const [hosts, setHosts] = useState([]);
const [error, setError] = useState(null);
const [notice, setNotice] = useState(null);
const [busy, setBusy] = useState(null);
@@ -42,6 +43,7 @@ export default function AdminPage() {
try {
const res = await api('/admin/matches', { token });
setMatches(res.matches);
setHosts(res.hosts ?? []);
setError(null);
} catch (err) {
if (err.status === 401) {
@@ -98,9 +100,12 @@ export default function AdminPage() {
<button className={`admin-tab ${tab === 'settings' ? 'is-active' : ''}`} onClick={() => setTab('settings')}>Einstellungen</button>
</nav>
{tab === 'settings' && token && <ServerSettingsPanel token={token} setNotice={setNotice} />}
{tab === 'settings' && token && <DockerHostsPanel token={token} setNotice={setNotice} />}
{tab === 'settings' && token && <NotificationSettings token={token} setNotice={setNotice} />}
{notice && <div className={`toast toast-${notice.tone}`} role="status">{notice.text}</div>}
{error && <div className="alert alert-error">{error}</div>}
{tab === 'matches' && <HostUsage hosts={hosts} />}
{tab === 'matches' && matches === null && !error && <p className="muted">Lade Matches …</p>}
{tab === 'matches' && matches && matches.length === 0 && <p className="muted">Noch keine Matches.</p>}
@@ -293,6 +298,14 @@ function MatchAdminCard({ m, run, busy, token, reload, setNotice }) {
{current.live?.roundsPlayed != null && <span className="muted">Runde {current.live.roundsPlayed + 1}</span>}
</div>
)}
{m.server?.address && m.server.status !== 'stopped' && (
<div className="admin-address">
<span><span className="muted">Server</span> <code>{m.server.address.host}:{m.server.address.port}</code></span>
{m.server.address.tvPort && (
<span><span className="muted">CSTV</span> <code>{m.server.address.host}:{m.server.address.tvPort}</code></span>
)}
</div>
)}
{paused && <span className="admin-badge is-paused">Pausiert</span>}
</div>
<button className="link-btn" onClick={() => setOpen(!open)}>{open ? 'Weniger' : 'Details'}</button>
@@ -386,3 +399,290 @@ function MatchAdminCard({ m, run, busy, token, reload, setNotice }) {
</section>
);
}
/** Auslastung der Docker-Hosts: wie viele Gameserver läuft auf welchem Host, mit Liste der Matches. */
function HostUsage({ hosts }) {
if (hosts.length === 0) return null;
return (
<section className="host-usage">
<h2>Docker-Hosts</h2>
<div className="host-grid">
{hosts.map((h) => {
const percent = h.total ? Math.round((h.used / h.total) * 100) : 0;
return (
<div key={h.name} className={`host-card ${percent >= 90 ? 'is-full' : ''}`}>
<div className="host-head">
<strong>{h.name}</strong>
<span className="muted small">{h.publicHost}</span>
<span className="host-count">{h.used} / {h.total} belegt</span>
</div>
<div className="host-bar" role="progressbar" aria-valuenow={percent} aria-valuemin={0} aria-valuemax={100}>
<span style={{ width: `${percent}%` }} />
</div>
{h.disk && (
<div className={`host-disk ${h.disk.freeBytes < 20e9 ? 'is-low' : ''}`}>
Speicher: {gb(h.disk.freeBytes)} frei von {gb(h.disk.totalBytes)}
</div>
)}
{h.matches.length === 0 ? (
<p className="muted small">Keine Gameserver</p>
) : (
<ul className="host-matches">
{h.matches.map((m) => <li key={m.id}>{m.title} <span className="muted small">{m.status}</span></li>)}
</ul>
)}
</div>
);
})}
</div>
</section>
);
}
function gb(bytes) {
return `${Math.round(bytes / 1e9)} GB`;
}
const SETTING_GROUPS = [
{ key: 'match', title: 'Match-Regeln' },
{ key: 'tv', title: 'CSTV' },
{ key: 'times', title: 'Zeiten' },
];
const toInput = (field, value) => (field.type === 'commands' ? value.join('; ') : value);
const shownDefault = (field) => {
if (field.type === 'bool') return field.default ? 'an' : 'aus';
if (field.type === 'commands') return field.default.length ? field.default.join('; ') : 'keine';
return field.default;
};
/** Gameserver-Einstellungen: Vorgaben aus der .env, im Admin-Bereich änderbar. */
function ServerSettingsPanel({ token, setNotice }) {
const [fields, setFields] = useState(null);
const [values, setValues] = useState({});
const [busy, setBusy] = useState(false);
const apply = (list) => {
setFields(list);
setValues(Object.fromEntries(list.map((f) => [f.key, toInput(f, f.value)])));
};
useEffect(() => {
api('/admin/server-settings', { token })
.then((res) => apply(res.fields))
.catch((err) => setNotice({ tone: 'error', text: err.message }));
}, [token, setNotice]);
if (!fields) return null;
const changedKeys = fields.filter((f) => String(values[f.key]) !== String(toInput(f, f.value))).map((f) => f.key);
const save = async () => {
setBusy(true);
try {
const body = Object.fromEntries(changedKeys.map((key) => {
const field = fields.find((f) => f.key === key);
return [key, field.type === 'int' ? Number(values[key]) : values[key]];
}));
const res = await api('/admin/server-settings', { method: 'PUT', body: { values: body }, token });
apply(res.fields);
const parts = ['Einstellungen gespeichert'];
if (res.live.updated) parts.push(`${res.live.updated} laufende${res.live.updated === 1 ? 'r' : ''} Server aktualisiert`);
if (res.live.failed.length) parts.push(`${res.live.failed.length} Server nicht erreichbar`);
setNotice({ tone: res.live.failed.length ? 'error' : 'ok', text: parts.join(' · ') });
} catch (err) {
setNotice({ tone: 'error', text: err.message });
} finally {
setBusy(false);
}
};
const input = (f) => {
if (f.type === 'bool') {
return (
<label className="admin-check">
<input type="checkbox" checked={!!values[f.key]} onChange={(e) => setValues({ ...values, [f.key]: e.target.checked })} />
{f.label}
</label>
);
}
return (
<label className="setting-field">
<span>{f.label}</span>
{f.type === 'commands' ? (
<textarea rows={2} value={values[f.key]} placeholder="z. B. mp_freezetime 15; sv_alltalk 0"
onChange={(e) => setValues({ ...values, [f.key]: e.target.value })} />
) : (
<input type="number" min={f.min} max={f.max} value={values[f.key]}
onChange={(e) => setValues({ ...values, [f.key]: e.target.value })} />
)}
</label>
);
};
return (
<section className="admin-settings">
<h2>Gameserver</h2>
<p className="small muted">
Markierte Werte (live) gehen beim Speichern sofort an alle laufenden Server. Die übrigen gelten für neue Server und Vetos.
</p>
{SETTING_GROUPS.map((g) => (
<div key={g.key} className="setting-group">
<h3>{g.title}</h3>
<div className="setting-grid">
{fields.filter((f) => f.group === g.key).map((f) => (
<div key={f.key} className="setting-item">
{input(f)}
<div className="small muted">
{f.live && <span className="setting-live">live</span>}
{f.overridden ? `Vorgabe aus .env: ${shownDefault(f)}` : 'Vorgabe aus .env'}
</div>
</div>
))}
</div>
</div>
))}
<div className="admin-actions">
<button className="btn btn-sm btn-primary" onClick={save} disabled={busy || changedKeys.length === 0}>Speichern</button>
{changedKeys.length > 0 && (
<button className="btn btn-sm" disabled={busy} onClick={() => apply(fields)}>Verwerfen</button>
)}
</div>
</section>
);
}
const EMPTY_HOST = { name: '', dockerHost: 'ssh://', publicHost: '', portStart: 27100, portEnd: 27139, volume: 'cs2-mapveto-gamefiles' };
const HOST_COLUMNS = [
{ key: 'name', label: 'Name', placeholder: 'host1' },
{ key: 'dockerHost', label: 'Docker-Ziel', placeholder: 'ssh://server@192.168.178.45' },
{ key: 'publicHost', label: 'Adresse für Spieler', placeholder: '192.168.178.45' },
{ key: 'portStart', label: 'Port von', type: 'number' },
{ key: 'portEnd', label: 'Port bis', type: 'number' },
{ key: 'volume', label: 'Volume', placeholder: 'cs2-mapveto-gamefiles' },
];
/** Docker-Hosts verwalten und das CS2-Image auf allen Hosts gleichzeitig aktualisieren. */
function DockerHostsPanel({ token, setNotice }) {
const [data, setData] = useState(null);
const [rows, setRows] = useState([]);
const [busy, setBusy] = useState(false);
const apply = useCallback((res) => {
setData(res);
setRows(res.hosts.map((h) => ({ ...EMPTY_HOST, ...h, dockerHost: h.dockerHost ?? '' })));
}, []);
const load = useCallback(() => api('/admin/docker-hosts', { token }), [token]);
useEffect(() => {
load().then(apply).catch((err) => setNotice({ tone: 'error', text: err.message }));
}, [load, apply, setNotice]);
// Während das Image lädt, alle 3 Sekunden den Stand holen (ohne die bearbeiteten Zeilen zu überschreiben)
const pulling = !!data?.pull?.running;
useEffect(() => {
if (!pulling) return undefined;
const timer = setInterval(() => {
load().then((res) => setData((d) => ({ ...d, pull: res.pull }))).catch(() => {});
}, 3000);
return () => clearInterval(timer);
}, [pulling, load]);
if (!data) return null;
if (!data.editable) return null;
const original = JSON.stringify(data.hosts.map((h) => ({ ...EMPTY_HOST, ...h, dockerHost: h.dockerHost ?? '' })));
const dirty = JSON.stringify(rows) !== original;
const setCell = (i, key, value) => setRows(rows.map((r, j) => (j === i ? { ...r, [key]: value } : r)));
const run = async (fn, success) => {
setBusy(true);
try {
const res = await fn();
apply(res);
if (success) setNotice({ tone: 'ok', text: success });
} catch (err) {
setNotice({ tone: 'error', text: err.message });
} finally {
setBusy(false);
}
};
const save = () => run(() => api('/admin/docker-hosts', {
method: 'PUT',
token,
body: {
hosts: rows.map((r) => ({
name: r.name.trim(),
dockerHost: r.dockerHost.trim() || null,
publicHost: r.publicHost.trim(),
...(r.rconHost && r.rconHost !== r.publicHost ? { rconHost: r.rconHost } : {}),
portStart: Number(r.portStart),
portEnd: Number(r.portEnd),
volume: r.volume.trim() || undefined,
})),
},
}), 'Docker-Hosts gespeichert');
const reset = () => {
if (!window.confirm('Hosts aus der .env verwenden? Die hier gespeicherte Liste wird gelöscht.')) return;
run(() => api('/admin/docker-hosts', { method: 'DELETE', token }), 'Hosts aus der .env aktiv');
};
const pull = () => {
if (!window.confirm(`Image ${data.image} auf allen Hosts neu laden? Laufende Server laufen weiter.`)) return;
run(() => api('/admin/docker-hosts/pull', { method: 'POST', token }));
};
return (
<section className="admin-settings">
<h2>Docker-Hosts</h2>
<p className="small muted">
{data.overridden ? 'Diese Liste ist im Admin-Bereich gespeichert und ersetzt CS2_DOCKER_HOSTS aus der .env.' : 'Aktuell gilt die Liste aus der .env.'}
{' '}Hosts mit laufenden Servern lassen sich erst nach dem Stoppen entfernen.
</p>
<div className="host-edit">
{rows.map((r, i) => (
<div key={i} className="host-edit-row">
{HOST_COLUMNS.map((c) => (
<label key={c.key} className={`setting-field host-col-${c.key}`}>
<span>{c.label}</span>
<input type={c.type ?? 'text'} value={r[c.key] ?? ''} placeholder={c.placeholder}
onChange={(e) => setCell(i, c.key, e.target.value)} />
</label>
))}
<button className="link-btn danger" disabled={busy || rows.length === 1}
onClick={() => setRows(rows.filter((_, j) => j !== i))}>Entfernen</button>
</div>
))}
</div>
<div className="admin-actions">
<button className="btn btn-sm" disabled={busy} onClick={() => setRows([...rows, { ...EMPTY_HOST }])}>Host hinzufügen</button>
<button className="btn btn-sm btn-primary" disabled={busy || !dirty} onClick={save}>Hosts speichern</button>
{dirty && <button className="btn btn-sm" disabled={busy} onClick={() => apply(data)}>Verwerfen</button>}
{data.overridden && <button className="btn btn-sm btn-ghost" disabled={busy} onClick={reset}>Liste aus .env verwenden</button>}
</div>
<h3>Image aktualisieren</h3>
<p className="small muted">
Lädt <code>{data.image}</code> auf allen Hosts gleichzeitig neu. Laufende Server laufen weiter, neue Server starten mit dem neuen Image.
</p>
<div className="admin-actions">
<button className="btn btn-sm btn-primary" disabled={busy || pulling || dirty} onClick={pull}>
{pulling ? 'Wird aktualisiert …' : 'Auf allen Hosts aktualisieren'}
</button>
</div>
{data.pull?.results?.length > 0 && (
<ul className="pull-results">
{data.pull.results.map((r) => (
<li key={r.name} className={`pull-${r.state}`}>
<strong>{r.name}</strong>
<span>{r.state === 'running' ? 'lädt …' : r.state === 'ok' ? 'aktuell' : 'fehlgeschlagen'}</span>
{r.message && <code className="small">{r.message}</code>}
</li>
))}
{data.pull.finishedAt && (
<li className="small muted">Zuletzt fertig: {new Date(data.pull.finishedAt).toLocaleString('de-DE')}</li>
)}
</ul>
)}
</section>
);
}
+35
View File
@@ -554,3 +554,38 @@ input:focus, select:focus { border-color: var(--link); outline: none; }
.run-vs { font-size: 1rem; color: var(--muted); font-style: normal; }
.run-foot { display: flex; justify-content: space-between; gap: 8px; font-size: .875rem; color: var(--muted); }
.run-series { font-variant-numeric: tabular-nums; }
.host-usage { margin-bottom: 20px; }
.host-usage h2 { font-size: 1.1rem; margin: 0 0 10px; }
.host-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.host-card { border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius); padding: 12px 14px; }
.host-card.is-full { border-color: var(--pending); }
.host-head { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; margin-bottom: 8px; }
.host-count { margin-left: auto; font-variant-numeric: tabular-nums; font-size: .875rem; }
.host-bar { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; margin-bottom: 8px; }
.host-bar span { display: block; height: 100%; background: var(--primary); transition: width .3s ease; }
.host-card.is-full .host-bar span { background: var(--pending); }
.host-matches { list-style: none; margin: 0; padding: 0; font-size: .9rem; }
.host-matches li { padding: 3px 0; border-top: 1px solid var(--line); }
.host-disk { font-size: .85rem; color: var(--muted); margin: -2px 0 8px; font-variant-numeric: tabular-nums; }
.host-disk.is-low { color: var(--error); font-weight: 600; }
.admin-address { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 8px; font-size: .85rem; }
.admin-address code { font: 500 .85rem ui-monospace, SFMono-Regular, Consolas, monospace; padding: 1px 6px; border: 1px solid var(--line); border-radius: 4px; background: var(--surface); }
.admin-settings h3 { margin: 16px 0 8px; font-size: .95rem; }
.setting-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px 20px; }
.setting-item { display: flex; flex-direction: column; gap: 4px; }
.setting-field { display: flex; flex-direction: column; gap: 4px; font-size: .9rem; }
.setting-field input, .setting-field textarea { width: 100%; box-sizing: border-box; font: inherit; color: var(--text); background: var(--bg, transparent); border: 1px solid var(--line); border-radius: 4px; padding: 6px 8px; }
.setting-field textarea { resize: vertical; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: .85rem; }
.setting-live { display: inline-block; margin-right: 6px; padding: 0 6px; border-radius: 999px; font-size: .7rem; font-weight: 600; color: var(--primary); border: 1px solid var(--primary); }
.host-edit { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.host-edit-row { display: grid; grid-template-columns: 1fr 2fr 1.4fr .8fr .8fr 1.4fr auto; gap: 8px; align-items: end; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
@media (max-width: 900px) {
.host-edit-row { grid-template-columns: 1fr 1fr; }
.host-col-dockerHost { grid-column: 1 / -1; }
}
.pull-results { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; font-size: .9rem; }
.pull-results li { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; }
.pull-results code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.pull-running span { color: var(--pending); }
.pull-ok span { color: var(--ok, #22c55e); }
.pull-failed span { color: var(--error); }
+49 -3
View File
@@ -14,7 +14,7 @@ function bearer(req) {
* (nur im Speicher, verfällt nach 12 Stunden oder beim Neustart). Admin-Aktionen nutzen die bestehenden
* Organisator-Funktionen mit einem Admin-Zugang, die Berechtigungen bleiben also an einer Stelle.
*/
export function createAdminRouter({ service, manager, config }) {
export function createAdminRouter({ service, manager, config, settings }) {
const router = express.Router();
const sessions = new Map(); // Token -> Ablaufzeit (ms)
const loginLimit = createRateLimiter({ windowMs: 60_000, max: 5 });
@@ -77,6 +77,52 @@ export function createAdminRouter({ service, manager, config }) {
router.get('/session', requireAdmin, (req, res) => res.json({ ok: true }));
// Discord-Meldungen: welche Ereignisse gemeldet werden
// Gameserver-Einstellungen: Vorgaben aus der .env, vom Admin überschreibbar
router.get('/server-settings', requireAdmin, wrap(() => ({ fields: settings.view() })));
router.put('/server-settings', requireAdmin, wrap(async (req) => {
const changed = settings.update(req.body?.values ?? {});
if (changed.includes('vetoStepSeconds')) service.startVetoTimer();
const live = changed.length ? await manager.applyLiveSettings(changed) : { updated: 0, failed: [] };
return { fields: settings.view(), changed, live };
}));
// Docker-Hosts: Liste wie CS2_DOCKER_HOSTS, gespeichert in der Datenbank
const hostView = () => ({
hosts: (manager.adapter.hosts ?? []).map((h) => ({
name: h.name,
dockerHost: h.dockerHost,
publicHost: h.publicHost,
rconHost: h.rconHost,
portStart: h.portRangeStart,
portEnd: h.portRangeEnd,
volume: h.volume,
})),
overridden: settings.hostsOverridden(),
editable: !!manager.adapter.setHosts,
image: config.docker.image,
pull: manager.adapter.pullStatus?.() ?? null,
});
router.get('/docker-hosts', requireAdmin, wrap(() => hostView()));
router.put('/docker-hosts', requireAdmin, wrap((req) => {
const list = req.body?.hosts;
const hosts = settings.parseHosts(list);
manager.setDockerHosts(hosts);
settings.saveHosts(list, hosts);
return hostView();
}));
router.delete('/docker-hosts', requireAdmin, wrap(() => {
manager.setDockerHosts(settings.defaultHosts);
settings.resetHosts();
return hostView();
}));
// Image auf allen Hosts neu laden. Läuft im Hintergrund, Stand über GET /docker-hosts.
router.post('/docker-hosts/pull', requireAdmin, wrap(() => {
if (!manager.adapter.pullImage) throw new HttpError(409, 'Nur mit dem Docker-Adapter möglich', 'unsupported');
if (manager.adapter.pullStatus().running) throw new HttpError(409, 'Das Image wird bereits aktualisiert', 'busy');
manager.adapter.pullImage().catch(() => {});
return hostView();
}));
router.get('/notifications', requireAdmin, wrap(() => service.notificationSettings()));
router.put('/notifications', requireAdmin, wrap((req) => service.setNotificationEvents(req.body?.events)));
router.post('/notifications/test', requireAdmin, wrap(async () => {
@@ -89,9 +135,9 @@ export function createAdminRouter({ service, manager, config }) {
}));
// Alle Matches mit allen Details (Codes, Verbindungsdaten, Spieler, Veto, Serie, Ereignisse)
router.get('/matches', requireAdmin, wrap(() => {
router.get('/matches', requireAdmin, wrap(async () => {
const ids = service.db.prepare('SELECT id FROM matches ORDER BY created_at DESC LIMIT 100').all();
return { matches: ids.map(({ id }) => service.snapshot(id, adminViewer(id))) };
return { matches: ids.map(({ id }) => service.snapshot(id, adminViewer(id))), hosts: await manager.hostUsage() };
}));
const action = (path, fn) => router.post(`/matches/:id/${path}`, requireAdmin, wrap((req) => {
+5 -1
View File
@@ -11,10 +11,14 @@ import { MatchService } from './matchService.js';
import { attachRealtime } from './realtime.js';
import { createServerAdapter } from './servers/adapter.js';
import { ServerManager } from './servers/serverManager.js';
import { ServerSettings } from './serverSettings.js';
/** Baut die komplette Anwendung zusammen (auch für Tests nutzbar). */
export function createApp(config, { adapter: adapterOverride, logger = console } = {}) {
const db = openDatabase(config.databaseFile);
// Vom Admin geänderte Gameserver-Einstellungen auf die Konfiguration legen, bevor der Adapter entsteht
const settings = new ServerSettings({ db, config });
settings.load();
const pools = loadMapPools(config.mapPoolFile);
const notifier = createDiscordNotifier(config.discordWebhookUrl, { logger });
const service = new MatchService({ db, pools, config, notifier });
@@ -35,7 +39,7 @@ export function createApp(config, { adapter: adapterOverride, logger = console }
res.set('Referrer-Policy', 'no-referrer');
next();
});
app.use('/api', createApiRouter({ service, manager, config, pools, adapter }));
app.use('/api', createApiRouter({ service, manager, config, pools, adapter, settings }));
// Gebautes Frontend ausliefern (npm run build), SPA-Fallback auf index.html.
const dist = path.join(REPO_ROOT, 'client', 'dist');
+1 -1
View File
@@ -22,7 +22,7 @@ function bool(name, fallback) {
return ['1', 'true', 'yes', 'ja', 'on'].includes(raw.toLowerCase());
}
function parseDockerHosts(raw) {
export function parseDockerHosts(raw) {
if (!raw) return null;
let list;
try {
+2 -2
View File
@@ -14,7 +14,7 @@ function accessTokens(req) {
return [bearer(req), req.get('x-player-token')?.trim() || null];
}
export function createApiRouter({ service, manager, config, pools, adapter }) {
export function createApiRouter({ service, manager, config, pools, adapter, settings }) {
const router = express.Router();
// Live-Logs vom CS2-Server (logaddress_add_http). Vor dem JSON-Parser, weil CS2 Klartext sendet.
@@ -32,7 +32,7 @@ export function createApiRouter({ service, manager, config, pools, adapter }) {
});
router.use(express.json({ limit: '32kb' }));
router.use('/admin', createAdminRouter({ service, manager, config }));
router.use('/admin', createAdminRouter({ service, manager, config, settings }));
const limiters = {
create: createRateLimiter({ windowMs: 60_000, max: 10 }),
+3
View File
@@ -956,6 +956,9 @@ export class MatchService extends EventEmitter {
updatedAt: row.updated_at,
idleTimeoutMinutes: this.config.idleTimeoutMinutes,
};
if (viewer.isOrganizer && row.host) {
view.address = { host: row.host, port: row.port, tvPort: row.tv_port };
}
const configured = row.status === 'configured';
if (!configured || viewer.role === 'public') return view;
+187
View File
@@ -0,0 +1,187 @@
// Gameserver-Einstellungen, die der Admin zur Laufzeit ändern kann. Die Werte aus der .env sind die
// Vorgabe; Änderungen landen in der Tabelle settings und überschreiben die Vorgabe auch nach einem Neustart.
import { HttpError } from './matchService.js';
import { parseDockerHosts } from './config.js';
const SETTINGS_KEY = 'server_settings';
const HOSTS_KEY = 'docker_hosts';
/**
* Änderbare Felder. `get`/`set` greifen auf das gemeinsame config-Objekt zu, das Match-Service,
* Server-Manager und Docker-Adapter lesen. `live` = wird an laufende Server geschickt.
*/
export const SERVER_SETTING_FIELDS = [
{ key: 'maxRoundsCompetitive', group: 'match', label: 'Runden Competitive (mp_maxrounds)', type: 'int', min: 2, max: 60, live: true,
get: (c) => c.match.maxRoundsCompetitive, set: (c, v) => { c.match.maxRoundsCompetitive = v; } },
{ key: 'maxRoundsWingman', group: 'match', label: 'Runden Wingman (mp_maxrounds)', type: 'int', min: 2, max: 60, live: true,
get: (c) => c.match.maxRoundsWingman, set: (c, v) => { c.match.maxRoundsWingman = v; } },
{ key: 'overtime', group: 'match', label: 'Verlängerung (mp_overtime_enable)', type: 'bool', live: true,
get: (c) => c.match.overtime, set: (c, v) => { c.match.overtime = v; } },
{ key: 'extraCommands', group: 'match', label: 'Zusätzliche Konsolenbefehle (durch ; getrennt)', type: 'commands', live: true,
get: (c) => c.match.extraCommands, set: (c, v) => { c.match.extraCommands = v; } },
{ key: 'tvEnabled', group: 'tv', label: 'CSTV aktiv (wirkt ab dem nächsten Mapwechsel)', type: 'bool', live: false,
get: (c) => c.match.tvEnabled, set: (c, v) => { c.match.tvEnabled = v; } },
{ key: 'tvDelaySeconds', group: 'tv', label: 'CSTV-Verzögerung in Sekunden (tv_delay)', type: 'int', min: 0, max: 300, live: true,
get: (c) => c.match.tvDelaySeconds, set: (c, v) => { c.match.tvDelaySeconds = v; } },
{ key: 'vetoStepSeconds', group: 'times', label: 'Zeit pro Veto-Schritt in Sekunden (0 = ohne Frist)', type: 'int', min: 0, max: 600, live: false,
get: (c) => c.vetoStepSeconds, set: (c, v) => { c.vetoStepSeconds = v; } },
{ key: 'idleTimeoutMinutes', group: 'times', label: 'Leerlauf bis zum Abschalten in Minuten', type: 'int', min: 1, max: 1440, live: false,
get: (c) => c.idleTimeoutMinutes, set: (c, v) => { c.idleTimeoutMinutes = v; } },
{ key: 'maxLifetimeHours', group: 'times', label: 'Maximale Laufzeit eines Servers in Stunden', type: 'int', min: 1, max: 72, live: false,
get: (c) => c.maxLifetimeHours, set: (c, v) => { c.maxLifetimeHours = v; } },
{ key: 'startTimeoutMinutes', group: 'times', label: 'Startzeit eines Servers in Minuten (inkl. Download)', type: 'int', min: 5, max: 240, live: false,
get: (c) => c.docker.startTimeoutMinutes, set: (c, v) => { c.docker.startTimeoutMinutes = v; } },
];
const fieldFor = (key) => SERVER_SETTING_FIELDS.find((f) => f.key === key);
/** Konsolenbefehle aus Text: getrennt durch ; oder Zeilenumbruch. Befehle, die Zugänge ändern oder den Server beenden, sind gesperrt. */
export function parseCommands(value) {
const list = (Array.isArray(value) ? value : String(value ?? '').split(/[;\r\n]+/))
.map((c) => String(c).replace(/[\r\n;]/g, ' ').trim())
.filter(Boolean);
if (list.length > 30) throw new HttpError(400, 'Höchstens 30 zusätzliche Befehle', 'invalid');
for (const c of list) {
if (c.length > 120) throw new HttpError(400, `Befehl zu lang: ${c.slice(0, 30)} …`, 'invalid');
if (/^(rcon_password|sv_password|tv_password|rcon|quit|exit|_restart|logaddress_\w*)\b/i.test(c)) {
throw new HttpError(400, `Befehl nicht erlaubt: ${c.split(' ')[0]}`, 'invalid');
}
}
return list;
}
function parseValue(field, raw) {
if (field.type === 'bool') {
if (typeof raw !== 'boolean') throw new HttpError(400, `${field.label}: an oder aus erwartet`, 'invalid');
return raw;
}
if (field.type === 'int') {
const value = Number(raw);
if (raw === '' || raw === null || !Number.isInteger(value) || value < field.min || value > field.max) {
throw new HttpError(400, `${field.label}: ganze Zahl von ${field.min} bis ${field.max} erwartet`, 'invalid');
}
return value;
}
return parseCommands(raw);
}
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
export class ServerSettings {
constructor({ db, config }) {
this.db = db;
this.config = config;
// Vorgaben aus der .env merken, damit der Admin zurück auf die Vorgabe kann
this.defaults = Object.fromEntries(SERVER_SETTING_FIELDS.map((f) => [f.key, structuredClone(f.get(config))]));
this.defaultHosts = config.docker.hosts;
}
readJson(key) {
const row = this.db.prepare('SELECT value FROM settings WHERE key = ?').get(key);
if (!row) return null;
try {
return JSON.parse(row.value);
} catch {
return null;
}
}
writeJson(key, value) {
this.db.prepare('INSERT INTO settings (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value')
.run(key, JSON.stringify(value));
}
/** Beim Start: gespeicherte Werte auf das config-Objekt legen (vor dem Anlegen des Docker-Adapters). */
load() {
const saved = this.readJson(SETTINGS_KEY) ?? {};
for (const field of SERVER_SETTING_FIELDS) {
if (!(field.key in saved)) continue;
try {
field.set(this.config, parseValue(field, saved[field.key]));
} catch {
// Ungültiger gespeicherter Wert: Vorgabe aus der .env behalten
}
}
const hosts = this.readJson(HOSTS_KEY);
if (hosts) {
try {
this.config.docker.hosts = parseDockerHosts(JSON.stringify(hosts));
} catch {
// Kaputte Host-Liste ignorieren, die .env gilt
}
}
}
view() {
const saved = this.readJson(SETTINGS_KEY) ?? {};
return SERVER_SETTING_FIELDS.map(({ key, group, label, type, min, max, live, get }) => ({
key, group, label, type, min, max, live,
value: get(this.config),
default: this.defaults[key],
overridden: key in saved,
}));
}
/** Übernimmt geänderte Werte. Liefert die Schlüssel, die sich wirklich geändert haben. */
update(input = {}) {
const parsed = Object.entries(input).map(([key, raw]) => {
const field = fieldFor(key);
if (!field) throw new HttpError(400, `Unbekannte Einstellung: ${key}`, 'invalid');
return [field, parseValue(field, raw)];
});
const saved = this.readJson(SETTINGS_KEY) ?? {};
const changed = [];
for (const [field, value] of parsed) {
if (!same(field.get(this.config), value)) changed.push(field.key);
field.set(this.config, value);
if (same(value, this.defaults[field.key])) delete saved[field.key];
else saved[field.key] = value;
}
this.writeJson(SETTINGS_KEY, saved);
return changed;
}
/** Prüft und speichert eine neue Host-Liste (Format wie CS2_DOCKER_HOSTS). */
parseHosts(list) {
if (!Array.isArray(list)) throw new HttpError(400, 'Host-Liste erwartet', 'invalid');
let hosts;
try {
hosts = parseDockerHosts(JSON.stringify(list));
} catch (err) {
throw new HttpError(400, err.message, 'invalid');
}
for (const h of hosts) {
if (h.dockerHost && !/^(ssh:\/\/[\w.@-]+(:\d+)?|tcp:\/\/[\w.-]+:\d+)$/.test(h.dockerHost)) {
throw new HttpError(400, `Ungültiges Docker-Ziel für ${h.name}: ${h.dockerHost}`, 'invalid');
}
if (!/^[\w.-]+$/.test(h.name)) throw new HttpError(400, `Ungültiger Name: ${h.name}`, 'invalid');
if (!/^[\w.-]+$/.test(h.publicHost) || !/^[\w.-]+$/.test(h.rconHost)) {
throw new HttpError(400, `Ungültige Adresse für ${h.name}`, 'invalid');
}
if (h.volume && !/^[\w.-]+$/.test(h.volume)) throw new HttpError(400, `Ungültiger Volume-Name für ${h.name}`, 'invalid');
const start = h.portRangeStart ?? this.config.docker.portRangeStart;
const end = h.portRangeEnd ?? this.config.docker.portRangeEnd;
if (!Number.isInteger(start) || !Number.isInteger(end) || start < 1024 || end > 65535 || end - start < 1) {
throw new HttpError(400, `Ungültiger Portbereich für ${h.name}`, 'invalid');
}
}
return hosts;
}
saveHosts(list, hosts) {
this.writeJson(HOSTS_KEY, list);
this.config.docker.hosts = hosts;
}
/** Entfernt die gespeicherte Host-Liste; danach gilt wieder die .env. */
resetHosts() {
this.db.prepare('DELETE FROM settings WHERE key = ?').run(HOSTS_KEY);
this.config.docker.hosts = this.defaultHosts;
return this.defaultHosts;
}
hostsOverridden() {
return this.readJson(HOSTS_KEY) != null;
}
}
+120 -7
View File
@@ -4,6 +4,16 @@ import { configureViaRcon, listRoundBackups, loadRoundBackup, rconStatus } from
import { runRcon } from './rcon.js';
const exec = promisify(execFile);
const DISK_CACHE_MS = 60_000;
/** Liest Gesamt- und freien Speicher aus einer Zeile von `df -B1` (Spalten: Filesystem, 1B-blocks, Used, Available …). */
export function parseDf(output) {
const parts = String(output).trim().split(/\s+/);
const totalBytes = Number(parts[1]);
const freeBytes = Number(parts[3]);
if (!Number.isFinite(totalBytes) || !Number.isFinite(freeBytes)) return null;
return { totalBytes, freeBytes };
}
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// Diese Zeile schreibt das Image, sobald SteamCMD fertig ist und der Spielserver startet.
@@ -36,7 +46,7 @@ export class DockerServerAdapter {
}
async docker(args, timeout = 60_000) {
if (this.runDocker) return this.runDocker(args);
if (this.runDocker) return this.runDocker(args, this.options.name);
const env = this.options.dockerHost ? { ...process.env, DOCKER_HOST: this.options.dockerHost } : process.env;
const { stdout } = await exec(this.options.bin, args, { timeout, maxBuffer: 4 * 1024 * 1024, env });
return stdout.trim();
@@ -210,7 +220,18 @@ export class DockerHostPool {
this.name = 'docker';
this.simulated = false;
this.depsGetUsedPorts = getUsedPorts;
const hosts = options.hosts ?? [{
this.runDocker = runDocker;
this.options = options;
this.diskCache = new Map();
this.diskInflight = new Map();
this.pull = { running: false, startedAt: null, finishedAt: null, results: [] };
this.setHosts(options.hosts);
}
/** Baut die Host-Liste neu auf (beim Start und wenn der Admin die Hosts ändert). */
setHosts(list) {
const options = this.options;
const hosts = list ?? [{
name: 'default',
dockerHost: process.env.DOCKER_HOST || null,
publicHost: options.host,
@@ -230,12 +251,93 @@ export class DockerHostPool {
portRangeEnd: h.portRangeEnd ?? options.portRangeEnd,
volume: h.volume ?? options.volume,
};
return { ...options, ...host, host: host.publicHost };
const merged = { ...options, ...host, host: host.publicHost };
// Die Startzeit kann der Admin zur Laufzeit ändern, deshalb immer aus den gemeinsamen Einstellungen lesen
Object.defineProperty(merged, 'startTimeoutMinutes', { get: () => options.startTimeoutMinutes, enumerable: true });
return merged;
});
this.adapters = new Map(this.hosts.map((h) => [h.name, new DockerServerAdapter(h, {
getUsedPorts: () => (getUsedPorts ? getUsedPorts(h.publicHost) : new Set()),
runDocker,
getUsedPorts: () => (this.depsGetUsedPorts ? this.depsGetUsedPorts(h.publicHost) : new Set()),
runDocker: this.runDocker,
})]));
this.diskCache.clear();
}
/**
* Docker-Befehl direkt auf dem Host ausführen. Bei ssh:// per SSH, damit der Pull die Anmeldedaten des Hosts
* nutzt und nicht den Credential-Helper des Rechners, auf dem die App läuft.
*/
async hostDocker(name, args, timeout) {
const host = this.hosts.find((h) => h.name === name);
if (!this.runDocker && host?.dockerHost?.startsWith('ssh://')) {
const { stdout } = await exec('ssh', [
'-o', 'BatchMode=yes', '-o', 'ConnectTimeout=10', host.dockerHost.slice('ssh://'.length),
['docker', ...args].map((a) => `'${a}'`).join(' '),
], { timeout, maxBuffer: 4 * 1024 * 1024 });
return stdout.trim();
}
return this.adapters.get(name).docker(args, timeout);
}
/** Lädt das CS2-Image auf allen Hosts gleichzeitig neu. Laufende Server bleiben unberührt. */
async pullImage() {
if (this.pull.running) throw new Error('Das Image wird bereits aktualisiert');
if (!/^[\w./:@-]+$/.test(this.options.image)) throw new Error('Ungültiger Image-Name');
const hosts = this.hosts.map((h) => h.name);
this.pull = {
running: true,
startedAt: Date.now(),
finishedAt: null,
image: this.options.image,
results: hosts.map((name) => ({ name, state: 'running', message: null })),
};
await Promise.all(hosts.map(async (name, i) => {
const result = this.pull.results[i];
try {
await this.hostDocker(name, ['pull', '--quiet', this.options.image], 20 * 60_000);
const digest = await this.hostDocker(name, ['image', 'inspect', this.options.image, '--format', '{{index .RepoDigests 0}}'], 30_000)
.catch(() => '');
Object.assign(result, { state: 'ok', message: digest.split('@')[1]?.slice(0, 19) ?? 'aktualisiert' });
} catch (err) {
Object.assign(result, { state: 'failed', message: String(err.stderr || err.message).trim().split('\n').pop().slice(0, 200) });
}
}));
this.pull.running = false;
this.pull.finishedAt = Date.now();
return this.pull;
}
pullStatus() {
return this.pull;
}
/**
* Freier Speicher auf einem Host, per SSH: dort, wo das Gameserver-Volume liegt. Ergebnis wird eine Minute lang
* zwischengespeichert, weil die Admin-Seite alle paar Sekunden abfragt. Ohne ssh:// gibt es null.
*/
async diskUsage(name) {
const host = this.hosts.find((h) => h.name === name);
if (!host?.dockerHost?.startsWith('ssh://')) return null;
const cached = this.diskCache.get(name);
if (cached && Date.now() - cached.at < DISK_CACHE_MS) return cached.value;
if (!this.diskInflight.has(name)) {
this.diskInflight.set(name, (async () => {
let value = null;
try {
const { stdout } = await exec('ssh', [
'-o', 'BatchMode=yes', '-o', 'ConnectTimeout=5', host.dockerHost.slice('ssh://'.length),
`df -B1 "$(docker volume inspect ${host.volume} --format '{{with .Options}}{{.device}}{{end}}' 2>/dev/null | grep . || docker volume inspect ${host.volume} --format '{{.Mountpoint}}' 2>/dev/null || echo /var/lib/docker)" 2>/dev/null || df -B1 /var/lib/docker 2>/dev/null || df -B1 /`,
], { timeout: 10_000 });
value = parseDf(stdout.trim().split('\n').pop());
} catch {
value = null;
}
this.diskCache.set(name, { at: Date.now(), value });
this.diskInflight.delete(name);
return value;
})());
}
return this.diskInflight.get(name);
}
/** Freie Port-Paare auf einem Host. */
@@ -248,6 +350,14 @@ export class DockerHostPool {
return free;
}
/** Auslastung pro Host: Anzahl Port-Paare insgesamt und belegt. */
hostStats() {
return this.hosts.map((h) => {
const total = Math.floor((h.portRangeEnd - h.portRangeStart + 1) / 2);
return { name: h.name, publicHost: h.publicHost, total, used: total - this.freeSlots(h) };
});
}
getUsedPorts(publicHost) {
return this.depsGetUsedPorts?.(publicHost) ?? new Set();
}
@@ -268,8 +378,11 @@ export class DockerHostPool {
adapterFor(handle) {
const name = handle?.hostName ?? this.hosts[0].name;
const adapter = this.adapters.get(name);
if (!adapter) throw new Error(`Docker-Host "${name}" ist nicht (mehr) konfiguriert`);
return adapter;
if (adapter) return adapter;
// Server aus der Zeit vor der Host-Liste (Name "default") oder nach einer Umbenennung: über die Adresse finden
const byAddress = handle?.rconHost && this.hosts.find((h) => h.rconHost === handle.rconHost || h.publicHost === handle.rconHost);
if (byAddress) return this.adapters.get(byAddress.name);
throw new Error(`Docker-Host "${name}" ist nicht (mehr) konfiguriert`);
}
async status(handle) {
+91
View File
@@ -160,6 +160,97 @@ export class ServerManager {
this.service.changed(matchId);
}
/** Auslastung der Docker-Hosts mit den Matches, die dort laufen. Leer, wenn der Adapter keine Hosts kennt. */
async hostUsage() {
if (!this.adapter.hostStats) return [];
const rows = this.db.prepare(`
SELECT s.host, s.status, m.id AS match_id, m.team_a_name, m.team_b_name
FROM servers s JOIN matches m ON m.id = s.match_id
WHERE s.status != 'stopped' AND s.host IS NOT NULL
`).all();
const stats = this.adapter.hostStats();
const disks = await Promise.all(stats.map((h) => this.adapter.diskUsage?.(h.name) ?? null));
return stats.map((h, i) => ({
...h,
disk: disks[i],
matches: rows
.filter((r) => r.host === h.publicHost)
.map((r) => ({ id: r.match_id, title: `${r.team_a_name} vs. ${r.team_b_name}`, status: r.status })),
}));
}
/** Server, die gerade laufen oder starten, mit dem Docker-Host aus ihrem Handle. */
activeServers() {
return this.db.prepare(`
SELECT s.match_id, s.status, s.host, s.handle, m.mode
FROM servers s JOIN matches m ON m.id = s.match_id
WHERE s.status != 'stopped' AND s.handle IS NOT NULL
`).all().map((r) => {
let handle = null;
try {
handle = JSON.parse(r.handle);
} catch {
// kaputter Handle: Server zählt trotzdem als aktiv
}
return { ...r, handle, hostName: handle?.hostName ?? null };
});
}
/**
* Übernimmt eine neue Liste von Docker-Hosts. Hosts mit laufenden Servern dürfen nicht wegfallen
* und ihre Adresse nicht ändern, sonst verliert die App den Kontakt zu diesen Servern.
*/
setDockerHosts(hosts) {
if (!this.adapter.setHosts) throw new HttpError(409, 'Nur mit dem Docker-Adapter möglich', 'unsupported');
const before = new Map(this.adapter.hosts.map((h) => [h.name, h]));
// Ohne Liste gilt der eine Host aus DOCKER_HOST/CS2_PUBLIC_HOST
const nextList = hosts ?? [{ name: 'default', publicHost: this.config.docker.host, dockerHost: process.env.DOCKER_HOST || null }];
for (const server of this.activeServers()) {
const name = server.hostName ?? this.adapter.hosts[0]?.name;
const old = before.get(name);
const next = nextList.find((h) => h.name === name);
if (!next) {
throw new HttpError(409, `Auf ${name} läuft noch ein Server. Erst stoppen, dann den Host entfernen.`, 'host_busy');
}
if (old && (next.publicHost !== old.publicHost || (next.dockerHost ?? null) !== (old.dockerHost ?? null))) {
throw new HttpError(409, `Auf ${name} läuft noch ein Server. Adresse und Docker-Ziel erst danach ändern.`, 'host_busy');
}
}
this.adapter.setHosts(hosts);
}
/**
* Schickt geänderte Match-Einstellungen an alle bereiten Server. Liefert, wie viele Server
* aktualisiert wurden und welche nicht erreichbar waren.
*/
async applyLiveSettings(changed) {
const keys = new Set(changed);
const rows = this.activeServers().filter((r) => r.status === 'configured');
if (!this.adapter.sendCommands || rows.length === 0) return { updated: 0, failed: [] };
const m = this.config.match;
const result = { updated: 0, failed: [] };
for (const row of rows) {
const wingman = row.mode === 'wingman';
const commands = [];
if (keys.has(wingman ? 'maxRoundsWingman' : 'maxRoundsCompetitive')) {
commands.push(`mp_maxrounds ${wingman ? m.maxRoundsWingman : m.maxRoundsCompetitive}`);
}
if (keys.has('overtime')) commands.push(`mp_overtime_enable ${m.overtime ? 1 : 0}`);
if (keys.has('tvDelaySeconds')) commands.push(`tv_delay ${m.tvDelaySeconds}`);
if (keys.has('extraCommands')) commands.push(...m.extraCommands);
if (commands.length === 0) continue;
try {
await this.adapter.sendCommands(row.handle, commands);
result.updated += 1;
this.service.log(row.match_id, 'server', `Einstellungen übernommen (Admin): ${commands.join('; ')}`);
} catch (err) {
result.failed.push(row.match_id);
this.logger.warn?.(`Einstellungen für ${row.match_id} nicht übernommen: ${err.message}`);
}
}
return result;
}
/** Ports, die auf einem bestimmten Docker-Host belegt sind. */
usedPortsOn(host) {
const used = new Set();
+29 -2
View File
@@ -72,11 +72,38 @@ test('Status, Stopp und Befehle gehen an den Host des Matches', async () => {
await p.stop(r.handle);
assert.deepEqual(calls.at(-1)[0], 'rm');
assert.equal(calls.at(-1).at(-1), r.handle.container);
// Ein Handle von einem Host, der nicht mehr konfiguriert ist, wird abgelehnt
await assert.rejects(p.status({ ...r.handle, hostName: 'gelöscht' }), /nicht \(mehr\) konfiguriert/);
// Ein Handle von einem Host, der weder per Name noch per Adresse bekannt ist, wird abgelehnt
await assert.rejects(p.status({ ...r.handle, hostName: 'gelöscht', rconHost: '10.9.9.9' }), /nicht \(mehr\) konfiguriert/);
});
test('Alte Handles ohne Host-Namen laufen auf dem ersten Host', () => {
const { pool: p } = pool([hostA, hostB]);
assert.equal(p.adapterFor({ container: 'x' }), p.adapters.get('a'));
});
test('Auslastung pro Host: Port-Paare insgesamt und belegt', () => {
const { pool: p } = pool([hostA, hostB], { '10.0.0.1': [27100] });
assert.deepEqual(p.hostStats(), [
{ name: 'a', publicHost: '10.0.0.1', total: 2, used: 1 },
{ name: 'b', publicHost: '10.0.0.2', total: 20, used: 0 },
]);
});
test('df-Ausgabe wird in Gesamt- und freien Speicher zerlegt', async () => {
const { parseDf } = await import('../src/servers/dockerAdapter.js');
assert.deepEqual(parseDf('/dev/sda1 1000000000000 400000000000 600000000000 40% /var/lib/docker'), {
totalBytes: 1000000000000,
freeBytes: 600000000000,
});
assert.equal(parseDf('kaputt'), null);
});
test('Alter Handle ohne passenden Host-Namen wird über die Adresse dem richtigen Host zugeordnet', async () => {
const docker = fakeDocker();
const p = new DockerHostPool(options([
{ ...hostA, rconHost: '10.0.0.1' },
{ ...hostB, rconHost: '10.0.0.2' },
]), { runDocker: docker.run });
await p.stop({ container: 'cs2mv-alt', hostName: 'default', rconHost: '10.0.0.2', port: 27100 });
assert.deepEqual(docker.calls.at(-1), ['rm', '-f', 'cs2mv-alt']);
assert.equal(p.adapterFor({ hostName: 'default', rconHost: '10.0.0.2' }).options.name, 'b');
assert.throws(() => p.adapterFor({ hostName: 'weg', rconHost: '10.9.9.9' }), /nicht \(mehr\) konfiguriert/);
});
+61
View File
@@ -0,0 +1,61 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { loadConfig } from '../src/config.js';
import { openDatabase } from '../src/db.js';
import { ServerSettings } from '../src/serverSettings.js';
import { DockerHostPool } from '../src/servers/dockerAdapter.js';
function setup() {
const config = loadConfig({ match: { maxRoundsCompetitive: 24, extraCommands: [] }, vetoStepSeconds: 30 });
const db = openDatabase(':memory:');
return { config, db, settings: new ServerSettings({ db, config }) };
}
test('Geänderte Einstellungen landen in der Konfiguration und überstehen einen Neustart', () => {
const { config, db, settings } = setup();
const changed = settings.update({ maxRoundsCompetitive: 30, extraCommands: 'mp_freezetime 15; sv_cheats 0', vetoStepSeconds: 30 });
assert.deepEqual(changed, ['maxRoundsCompetitive', 'extraCommands']);
assert.equal(config.match.maxRoundsCompetitive, 30);
assert.deepEqual(config.match.extraCommands, ['mp_freezetime 15', 'sv_cheats 0']);
// Neustart: neue Konfiguration aus der .env, gespeicherte Werte werden wieder darübergelegt
const fresh = loadConfig({ match: { maxRoundsCompetitive: 24, extraCommands: [] } });
new ServerSettings({ db, config: fresh }).load();
assert.equal(fresh.match.maxRoundsCompetitive, 30);
assert.deepEqual(fresh.match.extraCommands, ['mp_freezetime 15', 'sv_cheats 0']);
});
test('Ungültige Werte und gesperrte Befehle werden abgelehnt', () => {
const { config, settings } = setup();
assert.throws(() => settings.update({ maxRoundsCompetitive: 500 }), /ganze Zahl von 2 bis 60/);
assert.throws(() => settings.update({ overtime: 'ja' }), /an oder aus/);
assert.throws(() => settings.update({ extraCommands: 'rcon_password neu' }), /nicht erlaubt/);
assert.throws(() => settings.update({ unbekannt: 1 }), /Unbekannte Einstellung/);
// Nichts davon wurde übernommen
assert.equal(config.match.maxRoundsCompetitive, 24);
});
test('Zurück auf die Vorgabe entfernt die gespeicherte Abweichung', () => {
const { settings } = setup();
settings.update({ maxRoundsCompetitive: 30 });
assert.equal(settings.view().find((f) => f.key === 'maxRoundsCompetitive').overridden, true);
settings.update({ maxRoundsCompetitive: 24 });
assert.equal(settings.view().find((f) => f.key === 'maxRoundsCompetitive').overridden, false);
});
test('Docker-Hosts: Prüfung der Liste und Übernahme in den Pool, Startzeit wirkt sofort', () => {
const { config, settings } = setup();
assert.throws(() => settings.parseHosts([{ name: 'a', publicHost: '1.2.3.4', dockerHost: 'ssh://x; rm -rf /' }]), /Ungültiges Docker-Ziel/);
assert.throws(() => settings.parseHosts([{ name: 'a', publicHost: '1.2.3.4', portStart: 80, portEnd: 90 }]), /Portbereich/);
const hosts = settings.parseHosts([
{ name: 'a', publicHost: '10.0.0.1', dockerHost: 'ssh://server@10.0.0.1', portStart: 27100, portEnd: 27139 },
{ name: 'b', publicHost: '10.0.0.2', dockerHost: 'ssh://b', portStart: 27100, portEnd: 27119 },
]);
const pool = new DockerHostPool({ ...config.docker, startTimeoutMinutes: 20, hosts: null });
pool.setHosts(hosts);
assert.deepEqual(pool.hostStats().map((h) => [h.name, h.total]), [['a', 20], ['b', 10]]);
pool.options.startTimeoutMinutes = 45;
assert.equal(pool.adapters.get('b').options.startTimeoutMinutes, 45);
});