Replace IW4MAdmin with direct RCON for player counts
IW4M kept hitting a network wall (container -> host LAN IP -> IW4M loopback bind) that we couldn't cleanly punch through, and the two-minute !rt token added enough operational friction that the pause-all-deployments gate never got a fair test. RCON gives us the same fact — how many players are on a server right now — direct from each game server over UDP, with no session, no token, no proxy. - Add integrations/rcon.ts: dgram UDP client. rconGetPlayerCount() and rconGetStatus() parse both `status` (print + scoreboard) and `statusResponse` (info line + rows) dialects. Timeout via RCON_TIMEOUT_MS. - Add komodoResolveGamePort() + extractUdpPort(). Reads Komodo's read API for a server, walks both docker-compose-style port objects and docker-run-style "host:container/udp" strings, caches into servers.game_port on first success. - Replace pipelineReady() guard in sweep with a simple integrations-configured check. Same held/failed/manual outcomes, but keyed on RCON+Komodo instead of IW4M+auth-state. - Worker's status refresh fans out per-server over RCON at concurrency=4 so a 15-server fleet doesn't spike UDP. - Schema: drop iw4m_match/iw4m_value, add game_port + rcon_host. - Delete every auth artifact: TokenModal, auth store, auth-state module, /api/auth/token, /api/events/auth, iw4m.test.ts. Layout no longer mounts the modal, pipeline store no longer tracks a frozen state. - README + .env.example + docker-compose.yml + komodo/stack.toml swap IW4M_* env for RCON_PASSWORD / RCON_HOST / RCON_TIMEOUT_MS. Same host.docker.internal:host-gateway extra_hosts entry, repurposed for RCON reach. - Tests: add rcon.test.ts and komodo.test.ts, update sweep.test.ts to drop the auth-freeze case. 23/23 passing.
This commit is contained in:
@@ -1,72 +0,0 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { newFixture, type Fixture } from '../helpers/fixture.js';
|
||||
|
||||
let fixture: Fixture;
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.resetModules();
|
||||
fixture = await newFixture();
|
||||
process.env.IW4M_ADDRESS = 'http://iw4m.test';
|
||||
process.env.IW4M_CLIENT_ID = '1';
|
||||
vi.stubGlobal('fetch', vi.fn());
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await fixture.cleanup();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe('loginWithToken', () => {
|
||||
it('returns cookie + expiry on 2xx', async () => {
|
||||
(globalThis.fetch as ReturnType<typeof vi.fn>).mockResolvedValueOnce(
|
||||
new Response('', {
|
||||
status: 200,
|
||||
headers: { 'Set-Cookie': '.AspNetCore.Cookies=abc; path=/; httponly' }
|
||||
})
|
||||
);
|
||||
const { loginWithToken } = await import('../../src/lib/server/integrations/iw4m.js');
|
||||
const res = await loginWithToken('abcd');
|
||||
expect(res.ok).toBe(true);
|
||||
if (res.ok) {
|
||||
expect(res.cookie).toContain('.AspNetCore.Cookies=abc');
|
||||
expect(res.expiresAt).toBeGreaterThan(Date.now());
|
||||
}
|
||||
});
|
||||
|
||||
it('returns rejected on 401', async () => {
|
||||
(globalThis.fetch as ReturnType<typeof vi.fn>).mockResolvedValueOnce(new Response('', { status: 401 }));
|
||||
const { loginWithToken } = await import('../../src/lib/server/integrations/iw4m.js');
|
||||
const res = await loginWithToken('abcd');
|
||||
expect(res.ok).toBe(false);
|
||||
if (!res.ok) expect(res.reason).toBe('rejected');
|
||||
});
|
||||
|
||||
it('returns unreachable on network error', async () => {
|
||||
(globalThis.fetch as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error('ECONNREFUSED'));
|
||||
const { loginWithToken } = await import('../../src/lib/server/integrations/iw4m.js');
|
||||
const res = await loginWithToken('abcd');
|
||||
expect(res.ok).toBe(false);
|
||||
if (!res.ok) expect(res.reason).toBe('unreachable');
|
||||
});
|
||||
});
|
||||
|
||||
describe('fetchStatus', () => {
|
||||
it('normalizes camelCase / PascalCase entries', async () => {
|
||||
(globalThis.fetch as ReturnType<typeof vi.fn>).mockResolvedValueOnce(
|
||||
new Response(
|
||||
JSON.stringify([
|
||||
{ id: 1, port: 3072, name: 'kino der toten', currentPlayers: 3 },
|
||||
{ Id: 2, Port: 3073, Name: 'ascension', CurrentPlayers: 0 }
|
||||
]),
|
||||
{ status: 200, headers: { 'Content-Type': 'application/json' } }
|
||||
)
|
||||
);
|
||||
const { fetchStatus } = await import('../../src/lib/server/integrations/iw4m.js');
|
||||
const res = await fetchStatus();
|
||||
expect(res.ok).toBe(true);
|
||||
if (res.ok) {
|
||||
expect(res.entries[0].currentPlayers).toBe(3);
|
||||
expect(res.entries[1].name).toBe('ascension');
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { extractUdpPort } from '../../src/lib/server/integrations/komodo.js';
|
||||
|
||||
describe('extractUdpPort', () => {
|
||||
it('returns the published port from docker-compose-style object entries', () => {
|
||||
const payload = {
|
||||
state: {
|
||||
container: {
|
||||
ports: [
|
||||
{ protocol: 'tcp', published: 3000, target: 3000 },
|
||||
{ protocol: 'udp', published: 4976, target: 28960 }
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
||||
expect(extractUdpPort(payload)).toBe(4976);
|
||||
});
|
||||
|
||||
it('parses docker-run-style "host:container/proto" strings', () => {
|
||||
const payload = { ports: ['3074:3074/tcp', '4977:28960/udp'] };
|
||||
expect(extractUdpPort(payload)).toBe(4977);
|
||||
});
|
||||
|
||||
it('picks the UDP entry even when TCP appears first', () => {
|
||||
const payload = {
|
||||
deeply: {
|
||||
nested: {
|
||||
ports: [
|
||||
{ Protocol: 'tcp', PublishedPort: 80 },
|
||||
{ Protocol: 'udp', PublishedPort: 5555 }
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
||||
expect(extractUdpPort(payload)).toBe(5555);
|
||||
});
|
||||
|
||||
it('returns null when no UDP mapping exists', () => {
|
||||
const payload = { ports: [{ protocol: 'tcp', published: 3000 }] };
|
||||
expect(extractUdpPort(payload)).toBeNull();
|
||||
});
|
||||
|
||||
it('survives cyclic references', () => {
|
||||
const cyc: Record<string, unknown> = {};
|
||||
cyc.self = cyc;
|
||||
expect(extractUdpPort(cyc)).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { parseInfoLine, parsePlayerCount } from '../../src/lib/server/integrations/rcon.js';
|
||||
|
||||
describe('parsePlayerCount', () => {
|
||||
it('counts rows after --- --- separator (status dialect)', () => {
|
||||
const body = [
|
||||
'print',
|
||||
'map: mp_kino',
|
||||
'num score ping guid name',
|
||||
'--- ----- ---- -------- ----',
|
||||
' 0 12 45 abc123def456 joe',
|
||||
' 1 0 72 xyz789abc123 jill',
|
||||
' 2 999 30 111222333444 kade'
|
||||
].join('\n');
|
||||
expect(parsePlayerCount(body)).toBe(3);
|
||||
});
|
||||
|
||||
it('returns 0 when the scoreboard has no rows', () => {
|
||||
const body = [
|
||||
'print',
|
||||
'map: mp_ascension',
|
||||
'num score ping guid name',
|
||||
'--- ----- ---- -------- ----',
|
||||
''
|
||||
].join('\n');
|
||||
expect(parsePlayerCount(body)).toBe(0);
|
||||
});
|
||||
|
||||
it('counts non-blank lines after the info line (statusResponse dialect)', () => {
|
||||
const body = [
|
||||
'statusResponse',
|
||||
'\\mapname\\mp_kino\\g_gametype\\zom\\sv_maxclients\\4',
|
||||
'10 45 "joe"',
|
||||
'0 72 "jill"'
|
||||
].join('\n');
|
||||
expect(parsePlayerCount(body)).toBe(2);
|
||||
});
|
||||
|
||||
it('returns null on malformed payloads with no separator or info line', () => {
|
||||
expect(parsePlayerCount('nonsense body\nno structure here')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseInfoLine', () => {
|
||||
it('splits \\key\\value pairs from an info string', () => {
|
||||
const body = [
|
||||
'statusResponse',
|
||||
'\\mapname\\mp_kino\\g_gametype\\zom\\sv_maxclients\\4'
|
||||
].join('\n');
|
||||
const info = parseInfoLine(body);
|
||||
expect(info.mapname).toBe('mp_kino');
|
||||
expect(info.g_gametype).toBe('zom');
|
||||
expect(info.sv_maxclients).toBe('4');
|
||||
});
|
||||
|
||||
it('returns empty object when no info line is present', () => {
|
||||
expect(parseInfoLine('nothing here')).toEqual({});
|
||||
});
|
||||
});
|
||||
@@ -8,7 +8,7 @@ beforeEach(async () => {
|
||||
fixture = await newFixture({ 'hud.gsc': 'v1' }, ['t5_kino_1', 't5_ascension_1']);
|
||||
// No integrations → sweep marks rows manual, exercising that branch.
|
||||
delete process.env.KOMODO_ADDRESS;
|
||||
delete process.env.IW4M_ADDRESS;
|
||||
delete process.env.RCON_PASSWORD;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
@@ -20,19 +20,13 @@ describe('sweep', () => {
|
||||
const { doPush } = await import('../../src/lib/server/fleet/push.js');
|
||||
const { sweep } = await import('../../src/lib/server/pipeline/sweep.js');
|
||||
const { seedServerRegistry } = await import('../../src/lib/server/fleet/servers.js');
|
||||
const { setAuthState } = await import('../../src/lib/server/integrations/auth-state.js');
|
||||
const { db } = await import('../../src/lib/server/db/index.js');
|
||||
await seedServerRegistry();
|
||||
|
||||
// Force ready so pipelineReady() lets sweep run (even though
|
||||
// integrations are off — that's the branch we want to test).
|
||||
setAuthState({ kind: 'ready', token: 'abcd', cookie: 'x=y', expiresAt: Date.now() + 100_000 });
|
||||
|
||||
const push = await doPush({ patterns: ['*'] });
|
||||
expect(push.deploymentId).not.toBeNull();
|
||||
|
||||
const result = await sweep();
|
||||
expect(result.frozen).toBe(false);
|
||||
await sweep();
|
||||
|
||||
const rows = db()
|
||||
.prepare("SELECT restart_state FROM deployment_servers WHERE deployment_id = ?")
|
||||
@@ -40,14 +34,13 @@ describe('sweep', () => {
|
||||
for (const r of rows) expect(r.restart_state).toBe('manual');
|
||||
});
|
||||
|
||||
it('freezes when auth is not ready', async () => {
|
||||
const { doPush } = await import('../../src/lib/server/fleet/push.js');
|
||||
it('is a no-op when nothing is pending', async () => {
|
||||
const { sweep } = await import('../../src/lib/server/pipeline/sweep.js');
|
||||
const { seedServerRegistry } = await import('../../src/lib/server/fleet/servers.js');
|
||||
await seedServerRegistry();
|
||||
|
||||
await doPush({ patterns: ['*'] });
|
||||
// No deployments → no pending rows. Should return zero counts.
|
||||
const result = await sweep();
|
||||
expect(result.frozen).toBe(true);
|
||||
expect(result).toEqual({ checked: 0, restarted: 0, held: 0, failed: 0 });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user