#!/usr/bin/env node 'use strict'; /* * UPSTREAM admin CLI. Node core + the game's own modules only (server/db.js, server/state.js, server/world.js, * public/js/rules.js). Works on a stopped OR a running server's database: SQLite WAL allows a second process to read * and write, every write here is one short BEGIN IMMEDIATE transaction (db.tx) and the connection is always closed. * * node tools/admin.js help * DB_PATH=/var/lib/upstream/game.db node tools/admin.js worlds * * Run it as the service user (deploy/upstream-admin does that), otherwise the -wal/-shm files can end up owned by * root and the server can no longer open the database. Never prints tokens or password hashes: those columns are not * selected anywhere in this file. * * Exit codes: 0 ok, 1 failed, 2 usage / missing confirmation flag, 3 not found. */ const fs = require('fs'); const os = require('os'); const path = require('path'); const ROOT = path.join(__dirname, '..'); const R = require(path.join(ROOT, 'public', 'js', 'rules.js')); const { openDb, migrate } = require(path.join(ROOT, 'server', 'db.js')); const { createWorld } = require(path.join(ROOT, 'server', 'world.js')); const { loadWorld, townFromRow } = require(path.join(ROOT, 'server', 'state.js')); const { FakeClock } = require(path.join(ROOT, 'server', 'clock.js')); const HOUR = 3600 * 1000; const DAY = 24 * HOUR; class AdminError extends Error { constructor(message, code) { super(message); this.exitCode = code || 1; } } const usage = (m) => new AdminError(m, 2); const notFound = (m) => new AdminError(m, 3); // ---- argument parsing and small helpers (exported for tests) ----------------------------------------------------- const BOOLEAN_FLAGS = new Set(['yes', 'yes-destroy', 'json', 'pending', 'simulate', 'banned', 'help', 'new-seed']); // argv (without node and script) -> { _: [positionals], flags: { name: string | true } } function parseArgs(argv) { const out = { _: [], flags: {} }; for (let i = 0; i < argv.length; i++) { const a = argv[i]; if (a === '--') { out._.push(...argv.slice(i + 1)); break; } if (a.startsWith('--') && a.length > 2) { let name = a.slice(2), val; const eq = name.indexOf('='); if (eq >= 0) { val = name.slice(eq + 1); name = name.slice(0, eq); } if (BOOLEAN_FLAGS.has(name)) out.flags[name] = val === undefined ? true : !/^(0|false|no)$/i.test(val); else { if (val === undefined) { if (i + 1 >= argv.length) throw usage('--' + name + ' needs a value'); val = argv[++i]; } out.flags[name] = val; } } else if (a === '-h') out.flags.help = true; else out._.push(a); } return out; } function parseDuration(s) { const m = /^(\d+(?:\.\d+)?)\s*(s|m|h|d|w)$/i.exec(String(s).trim()); if (!m) throw usage('bad duration ' + JSON.stringify(s) + ' (examples: 90m, 36h, 14d)'); return Math.round(Number(m[1]) * { s: 1000, m: 60000, h: HOUR, d: DAY, w: 7 * DAY }[m[2].toLowerCase()]); } // "now", +2h, -30m, an ISO date/time or integer ms since the epoch -> ms function parseTime(s, now) { s = String(s).trim(); if (s === 'now') return now; if (/^\d{9,}$/.test(s)) return Number(s); if (/^[+-]/.test(s)) return now + (s[0] === '-' ? -1 : 1) * parseDuration(s.slice(1)); const t = Date.parse(s); if (Number.isNaN(t)) throw usage('bad time ' + JSON.stringify(s) + ' (use now, +2h, an ISO date or ms since the epoch)'); return t; } function intFlag(flags, name, { min = -Infinity, max = Infinity, def } = {}) { const v = flags[name]; if (v === undefined) return def; const n = Number(v); if (!Number.isInteger(n) || n < min || n > max) throw usage('--' + name + ' must be an integer between ' + min + ' and ' + max); return n; } function numFlag(flags, name, { min = -Infinity, max = Infinity, def } = {}) { const v = flags[name]; if (v === undefined) return def; const n = Number(v); if (!Number.isFinite(n) || n < min || n > max) throw usage('--' + name + ' must be a number between ' + min + ' and ' + max); return n; } const iso = (ms) => (ms === null || ms === undefined ? '-' : new Date(ms).toISOString().replace(/\.\d+Z$/, 'Z')); function rel(ms, now) { if (ms === null || ms === undefined) return '-'; const d = ms - now, a = Math.abs(d); const s = a < 90e3 ? Math.round(a / 1000) + 's' : a < 90 * 60e3 ? Math.round(a / 60e3) + 'm' : a < 48 * HOUR ? (a / HOUR).toFixed(1) + 'h' : (a / DAY).toFixed(1) + 'd'; return d >= 0 ? 'in ' + s : s + ' ago'; } const num = (n) => (Number.isInteger(n) ? String(n) : (Math.round(n * 10) / 10).toFixed(1)); // rows (array of objects) + column keys -> aligned text lines function table(rows, cols) { if (!rows.length) return ['(none)']; const cells = rows.map((r) => cols.map((c) => { const v = r[c]; return v === null || v === undefined ? '-' : String(v); })); const w = cols.map((c, i) => Math.max(c.length, ...cells.map((r) => r[i].length))); const line = (arr) => arr.map((v, i) => v.padEnd(w[i])).join(' ').trimEnd(); return [line(cols), line(w.map((n) => '-'.repeat(n))), ...cells.map(line)]; } // ---- database access ---------------------------------------------------------------------------------------------- function tableExists(db, name) { return !!db.get("SELECT 1 AS x FROM sqlite_master WHERE type = 'table' AND name = ?", name); } function openDatabase(ctx, { allowMissing } = {}) { const file = ctx.dbPath; if (file !== ':memory:' && !fs.existsSync(file)) { if (!allowMissing) throw new AdminError('database not found: ' + file + ' (set DB_PATH or --db; the server creates it on first start)'); } const db = openDb(file); ctx.opened.push(db); if (!allowMissing && !tableExists(db, 'worlds')) { throw new AdminError('database has no schema yet: ' + file + ' (start the server once, or run `admin.js migrate`)'); } return db; } function worldOrThrow(db, id) { const w = db.get('SELECT * FROM worlds WHERE id = ?', id); if (!w) throw notFound('no world with id ' + id); return w; } function idArg(args, what) { const v = args._[0]; if (v === undefined) throw usage('missing <' + what + '>'); if (!/^\d+$/.test(v)) throw usage('<' + what + '> must be a number, got ' + JSON.stringify(v)); return Number(v); } function userOrThrow(db, username) { if (!username) throw usage('missing '); const u = db.get('SELECT id, username, banned FROM users WHERE username = ?', username); if (!u) throw notFound('no user named ' + JSON.stringify(username)); return u; } function requireFlag(args, name, why) { if (!args.flags[name]) throw usage('refusing without --' + name + ': ' + why); } // ---- backups ------------------------------------------------------------------------------------------------------- const BACKUP_RE = /^upstream-(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z(?:-(\d+))?\.db$/; function backupStamp(ms) { return new Date(ms).toISOString().replace(/[-:]/g, '').replace(/\.\d+Z$/, 'Z'); } // Snapshot files of ours in `dir`, newest first: [{ name, at, seq }] function listBackups(dir) { if (!fs.existsSync(dir)) return []; const out = []; for (const name of fs.readdirSync(dir)) { const m = BACKUP_RE.exec(name); if (!m) continue; out.push({ name, at: Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6]), seq: m[7] ? Number(m[7]) : 0 }); } return out.sort((a, b) => b.at - a.at || b.seq - a.seq); } // Which of the (newest-first) backups to delete. `keep` = newest N, `keepDays` = younger than D days. With both, a // snapshot survives if EITHER keeps it. With neither, nothing is deleted. The newest one is never deleted. function pruneSet(backups, now, { keep, keepDays }) { if (keep === undefined && keepDays === undefined) return []; return backups.filter((b, i) => { if (i === 0) return false; const byCount = keep !== undefined && i < keep; const byAge = keepDays !== undefined && b.at >= now - keepDays * DAY; return !(byCount || byAge); }); } // Open a snapshot read-only and check it. Throws if it is not a healthy copy of an UPSTREAM database. function verifySnapshot(file) { const { DatabaseSync } = require('node:sqlite'); let raw; try { raw = new DatabaseSync(file, { readOnly: true }); } catch (e) { throw new AdminError('cannot open ' + file + ': ' + e.message); } try { const ic = raw.prepare('PRAGMA integrity_check').all().map((r) => r.integrity_check); if (ic.length !== 1 || ic[0] !== 'ok') throw new AdminError('integrity_check failed for ' + file + ': ' + ic.slice(0, 3).join('; ')); const has = (t) => !!raw.prepare("SELECT 1 AS x FROM sqlite_master WHERE type = 'table' AND name = ?").get(t); if (!has('worlds') || !has('schema_migrations')) throw new AdminError(file + ' is not an UPSTREAM database (no worlds / schema_migrations table)'); const count = (t) => Number(raw.prepare('SELECT COUNT(*) AS n FROM ' + t).get().n); const last = raw.prepare('SELECT name FROM schema_migrations ORDER BY name DESC LIMIT 1').get(); return { file, bytes: fs.statSync(file).size, integrity: 'ok', worlds: count('worlds'), users: has('users') ? count('users') : 0, towns: has('towns') ? count('towns') : 0, lastMigration: last ? last.name : null, }; } finally { raw.close(); } } // VACUUM INTO a partial file, verify it, then publish it under its final dated name without ever replacing a file. function doBackup(db, destDir, now, { keep, keepDays } = {}) { fs.mkdirSync(destDir, { recursive: true, mode: 0o700 }); const stamp = backupStamp(now); const partial = path.join(destDir, '.partial-' + process.pid + '-' + stamp + '.db'); try { db.backup(partial); verifySnapshot(partial); fs.chmodSync(partial, 0o600); let final = null; for (let seq = 0; seq < 1000 && !final; seq++) { const cand = path.join(destDir, 'upstream-' + stamp + (seq ? '-' + seq : '') + '.db'); try { fs.linkSync(partial, cand); final = cand; } catch (e) { if (e.code === 'EEXIST') continue; if (fs.existsSync(cand)) continue; fs.renameSync(partial, cand); final = cand; // file system without hard links } } if (!final) throw new AdminError('could not find a free backup file name in ' + destDir); return { file: final, bytes: fs.statSync(final).size, pruned: pruneBackups(destDir, now, { keep, keepDays }) }; } finally { try { fs.unlinkSync(partial); } catch (e) { /* already moved or never created */ } } } function pruneBackups(dir, now, opts) { const del = pruneSet(listBackups(dir), now, opts); for (const b of del) fs.unlinkSync(path.join(dir, b.name)); for (const name of fs.readdirSync(dir)) { // partial files left behind by a crashed run if (!name.startsWith('.partial-')) continue; const p = path.join(dir, name); try { if (now - fs.statSync(p).mtimeMs > DAY) fs.unlinkSync(p); } catch (e) { /* ignore */ } } return del.map((b) => b.name); } // ---- reset-world: schema-driven so tables added by later stages are handled without editing this file ------------ const NEVER_RESET = new Set(['worlds', 'users', 'tokens', 'schema_migrations', 'idempotency']); const IMPLICIT_FK = { town_id: 'towns', operator_id: 'operators' }; // -> [{ table, parents: [{ col, table, refCol }], scope }] children first. A table belongs to the world if it has a // world_id column or points (declared FK, or a town_id / operator_id column) at a table that does. function resetPlan(db, worldId) { const tables = db.all("SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'").map((r) => r.name); const info = {}; for (const t of tables) { const cols = db.all('PRAGMA table_info(' + JSON.stringify(t) + ')').map((c) => c.name); const parents = db.all('PRAGMA foreign_key_list(' + JSON.stringify(t) + ')').map((f) => ({ col: f.from, table: f.table, refCol: f.to || 'id' })); for (const c of cols) { if (IMPLICIT_FK[c] && tables.includes(IMPLICIT_FK[c]) && !parents.some((p) => p.col === c)) parents.push({ col: c, table: IMPLICIT_FK[c], refCol: 'id' }); } info[t] = { cols, parents }; } const scopeOf = (t, seen) => { const i = info[t]; if (i.cols.includes('world_id')) return { sql: 'world_id = ?', params: [worldId] }; for (const p of i.parents) { if (NEVER_RESET.has(p.table) || !info[p.table] || seen.has(p.table)) continue; const s = scopeOf(p.table, new Set([...seen, t])); if (s) return { sql: p.col + ' IN (SELECT ' + p.refCol + ' FROM ' + p.table + ' WHERE ' + s.sql + ')', params: s.params }; } return null; }; const owned = [], skipped = []; for (const t of tables) { if (NEVER_RESET.has(t)) continue; const scope = scopeOf(t, new Set()); if (scope) owned.push({ table: t, parents: info[t].parents, scope }); else skipped.push(t); } // children before parents const ordered = [], left = new Map(owned.map((o) => [o.table, o])); while (left.size) { let pick = null; for (const [t] of left) { let referenced = false; for (const [u, o] of left) if (u !== t && o.parents.some((p) => p.table === t)) { referenced = true; break; } if (!referenced) { pick = t; break; } } if (!pick) pick = left.keys().next().value; // a cycle: foreign keys are off during the delete ordered.push(left.get(pick)); left.delete(pick); } return { steps: ordered, skipped }; } // ---- commands ------------------------------------------------------------------------------------------------------ // Each returns { data, lines }: `data` is printed by --json, `lines` is the human view. const commands = {}; commands.help = () => ({ data: { commands: Object.keys(HELP_CMDS) }, lines: helpText() }); commands.status = (args, ctx) => { const db = openDatabase(ctx); const now = ctx.now(); const migDir = path.join(ROOT, 'server', 'migrations'); const files = fs.existsSync(migDir) ? fs.readdirSync(migDir).filter((f) => /^\d+.*\.sql$/.test(f)).sort() : []; const applied = tableExists(db, 'schema_migrations') ? db.all('SELECT name FROM schema_migrations ORDER BY name').map((r) => r.name) : []; const pending = files.filter((f) => !applied.includes(f)); const size = (f) => (fs.existsSync(f) ? fs.statSync(f).size : 0); const ev = tableExists(db, 'events') ? db.get("SELECT COUNT(*) AS pending, SUM(due_at <= ?) AS overdue FROM events WHERE status = 'pending'", now) : { pending: 0, overdue: 0 }; const data = { db: path.resolve(ctx.dbPath), bytes: size(ctx.dbPath), walBytes: size(ctx.dbPath + '-wal'), journalMode: db.get('PRAGMA journal_mode').journal_mode, migrationsApplied: applied.length, migrationsPending: pending, worlds: db.get('SELECT COUNT(*) AS n FROM worlds').n, users: db.get('SELECT COUNT(*) AS n FROM users').n, banned: db.get('SELECT COUNT(*) AS n FROM users WHERE banned = 1').n, eventsPending: ev.pending, eventsOverdue: ev.overdue || 0, }; const lines = [ 'database ' + data.db + ' (' + data.bytes + ' bytes, WAL ' + data.walBytes + ', mode ' + data.journalMode + ')', 'migrations ' + data.migrationsApplied + ' applied' + (pending.length ? ', PENDING: ' + pending.join(', ') + ' (start the server or run `migrate`)' : ', none pending'), 'worlds / users ' + data.worlds + ' / ' + data.users + (data.banned ? ' (' + data.banned + ' banned)' : ''), 'events pending ' + data.eventsPending + (data.eventsOverdue ? ' (' + data.eventsOverdue + ' overdue: the server is stopped or stuck)' : ''), ]; return { data, lines }; }; commands.migrate = (args, ctx) => { const db = openDatabase(ctx, { allowMissing: true }); const applied = migrate(db); return { data: { applied }, lines: [applied.length ? 'applied: ' + applied.join(', ') : 'nothing to apply'] }; }; commands.worlds = (args, ctx) => { const db = openDatabase(ctx); const rows = db.all(`SELECT w.id, w.name, w.speed, w.map_radius AS radius, w.seed, w.status, w.start_at AS startAt, w.end_at AS endAt, (SELECT COUNT(*) FROM operators o WHERE o.world_id = w.id) AS operators, (SELECT COUNT(*) FROM towns t WHERE t.world_id = w.id) AS towns, (SELECT COUNT(*) FROM events e WHERE e.world_id = w.id AND e.status = 'pending') AS pendingEvents FROM worlds w ORDER BY w.id`); const view = rows.map((r) => Object.assign({}, r, { start: iso(r.startAt), end: iso(r.endAt) })); return { data: { worlds: rows }, lines: table(view, ['id', 'name', 'speed', 'radius', 'seed', 'status', 'start', 'end', 'operators', 'towns', 'pendingEvents']) }; }; commands['create-world'] = (args, ctx) => { const f = args.flags; const speed = numFlag(f, 'speed', { min: 0.1, max: 100, def: undefined }); const radius = intFlag(f, 'radius', { min: 5, max: 500, def: undefined }); const seed = intFlag(f, 'seed', { min: 1, max: 2 ** 31 - 1, def: undefined }); const now = ctx.now(); const endAt = f['end-in'] !== undefined ? now + parseDuration(f['end-in']) : undefined; const db = openDatabase(ctx); const id = db.tx(() => createWorld(db, now, { name: f.name, speed, mapRadius: radius, seed, endAt })); const w = worldOrThrow(db, id); return { data: { world: w }, lines: ['created world ' + id + ' "' + w.name + '" speed ' + w.speed + ', radius ' + w.map_radius + ', seed ' + w.seed + (endAt ? ', planned end ' + iso(endAt) : '')] }; }; commands['end-world'] = (args, ctx) => { const id = idArg(args, 'world-id'); const db = openDatabase(ctx); const w = worldOrThrow(db, id); if (w.status === 'ended') return { data: { id, status: 'ended', changed: false }, lines: ['world ' + id + ' is already ended'] }; requireFlag(args, 'yes', 'ending world ' + id + ' ("' + w.name + '") stops signups and new joins'); db.tx(() => db.run("UPDATE worlds SET status = 'ended', end_at = ? WHERE id = ?", ctx.now(), id)); return { data: { id, status: 'ended', changed: true }, lines: ['world ' + id + ' ended'] }; }; commands.users = (args, ctx) => { const db = openDatabase(ctx); const rows = db.all(`SELECT u.id, u.username, u.banned, u.created_at AS createdAt, (SELECT COUNT(*) FROM tokens t WHERE t.user_id = u.id AND t.revoked_at IS NULL) AS tokens, (SELECT MAX(t.last_used_at) FROM tokens t WHERE t.user_id = u.id) AS lastSeen, (SELECT COUNT(*) FROM operators o WHERE o.user_id = u.id) AS operators FROM users u ${args.flags.banned ? 'WHERE u.banned = 1' : ''} ORDER BY u.id`); const view = rows.map((r) => Object.assign({}, r, { banned: r.banned ? 'BANNED' : '', created: iso(r.createdAt), lastSeen: iso(r.lastSeen) })); return { data: { users: rows }, lines: table(view, ['id', 'username', 'banned', 'created', 'lastSeen', 'tokens', 'operators']) }; }; commands.operators = (args, ctx) => { const db = openDatabase(ctx); const wid = intFlag(args.flags, 'world', { min: 1 }); const rows = db.all(`SELECT o.id, o.world_id AS world, o.name, o.as_number AS asNumber, u.username AS user, u.banned, o.alliance_id AS alliance, o.protection_until AS protectionUntil, (SELECT COUNT(*) FROM towns t WHERE t.owner_id = o.id) AS towns FROM operators o JOIN users u ON u.id = o.user_id ${wid ? 'WHERE o.world_id = ?' : ''} ORDER BY o.world_id, o.id`, ...(wid ? [wid] : [])); const now = ctx.now(); const view = rows.map((r) => Object.assign({}, r, { banned: r.banned ? 'BANNED' : '', protection: r.protectionUntil > now ? rel(r.protectionUntil, now) : '' })); return { data: { operators: rows }, lines: table(view, ['id', 'world', 'name', 'asNumber', 'user', 'banned', 'towns', 'alliance', 'protection']) }; }; commands.ban = (args, ctx) => { const db = openDatabase(ctx); const u = userOrThrow(db, args._[0]); const revoked = db.tx(() => { db.run('UPDATE users SET banned = 1 WHERE id = ?', u.id); return db.run('UPDATE tokens SET revoked_at = ? WHERE user_id = ? AND revoked_at IS NULL', ctx.now(), u.id).changes; }); return { data: { username: u.username, banned: true, tokensRevoked: Number(revoked) }, lines: [u.username + ' banned, ' + revoked + ' token(s) revoked (login now answers 403 forbidden)'] }; }; commands.unban = (args, ctx) => { const db = openDatabase(ctx); const u = userOrThrow(db, args._[0]); db.tx(() => db.run('UPDATE users SET banned = 0 WHERE id = ?', u.id)); return { data: { username: u.username, banned: false }, lines: [u.username + ' unbanned (they must log in again, their old tokens stay revoked)'] }; }; commands['revoke-tokens'] = (args, ctx) => { const db = openDatabase(ctx); const u = userOrThrow(db, args._[0]); const n = Number(db.tx(() => db.run('UPDATE tokens SET revoked_at = ? WHERE user_id = ? AND revoked_at IS NULL', ctx.now(), u.id).changes)); return { data: { username: u.username, revoked: n }, lines: [u.username + ': ' + n + ' token(s) revoked'] }; }; // One town as { town row, current resources } for inspection. `overdue` counts pending events that should already have run. function describeTown(db, row, now) { const world = loadWorld(db, row.world_id); const town = townFromRow(db, row, world); const cur = R.resourcesAt(town, now); const overdue = db.get("SELECT COUNT(*) AS n FROM events WHERE town_id = ? AND status = 'pending' AND due_at <= ?", row.id, now).n; const r1 = (o) => { const x = {}; for (const k of Object.keys(o)) x[k] = Math.round(o[k] * 10) / 10; return x; }; return { id: row.id, name: row.name, world: row.world_id, ownerId: row.owner_id, x: row.x, y: row.y, capital: !!row.is_capital, loyalty: Math.round(R.loyaltyAt(town.loyalty, town.loyaltyAt, now, town.speed) * 10) / 10, resourcesAt: now, resources: r1(cur.amounts), caps: r1(cur.caps), ratesPerHour: r1(cur.rates), storedAt: row.res_at, overdueEvents: overdue, }; } commands['inspect-town'] = (args, ctx) => { const id = idArg(args, 'town-id'); const db = openDatabase(ctx); const row = db.get('SELECT * FROM towns WHERE id = ?', id); if (!row) throw notFound('no town with id ' + id); const now = ctx.now(); const t = describeTown(db, row, now); const owner = db.get('SELECT o.id, o.name, o.as_number AS asNumber, u.username FROM operators o JOIN users u ON u.id = o.user_id WHERE o.id = ?', row.owner_id); const buildings = db.all('SELECT key, type, level FROM buildings WHERE town_id = ?', id) .sort((a, b) => (a.key[0] === 'f' && b.key[0] === 'f' ? Number(a.key.slice(1)) - Number(b.key.slice(1)) : a.key.localeCompare(b.key))); const queue = db.all("SELECT id, type, due_at AS dueAt, payload FROM events WHERE town_id = ? AND status = 'pending' ORDER BY due_at, id", id) .map((e) => ({ id: e.id, type: e.type, dueAt: e.dueAt, payload: e.payload })); const units = tableExists(db, 'town_units') ? db.all('SELECT unit, count FROM town_units WHERE town_id = ? ORDER BY unit', id) : null; const data = { town: t, owner, buildings, queue, units }; const res = ['fiber', 'hardware', 'capital', 'battery'].map((k) => k + ' ' + num(t.resources[k]) + (t.caps[k] ? '/' + num(t.caps[k]) : '')).join(' '); const lines = [ 'town ' + t.id + ' "' + t.name + '" at (' + t.x + ',' + t.y + ') world ' + t.world + (t.capital ? ' [capital]' : '') + ' loyalty ' + t.loyalty, 'owner ' + (owner ? '#' + owner.id + ' ' + owner.name + ' (AS' + owner.asNumber + ', user ' + owner.username + ')' : '#' + t.ownerId + ' (missing)'), 'now (' + iso(now) + '): ' + res, 'rates/h: fiber ' + num(t.ratesPerHour.fiber) + ' hardware ' + num(t.ratesPerHour.hardware) + ' capital ' + num(t.ratesPerHour.capital) + ' power out ' + num(t.ratesPerHour.powerOut) + ' draw ' + num(t.ratesPerHour.powerDraw) + ' net ' + num(t.ratesPerHour.powerNet), ]; if (t.overdueEvents) lines.push('NOTE: ' + t.overdueEvents + ' overdue pending event(s) for this town (server stopped?): values above ignore them'); lines.push('', 'buildings:', ...table(buildings, ['key', 'type', 'level'])); lines.push('', 'queue:', ...table(queue.map((e) => ({ id: e.id, type: e.type, due: iso(e.dueAt), in: rel(e.dueAt, now), payload: e.payload })), ['id', 'type', 'due', 'in', 'payload'])); if (units) lines.push('', 'units:', ...table(units, ['unit', 'count'])); return { data, lines }; }; commands['inspect-operator'] = (args, ctx) => { const key = args._[0]; if (key === undefined) throw usage('missing (or --id )'); const db = openDatabase(ctx); const wid = intFlag(args.flags, 'world', { min: 1 }); const wcond = wid ? ' AND o.world_id = ?' : ''; const wpar = wid ? [wid] : []; const cols = 'o.id, o.world_id AS world, o.name, o.as_number AS asNumber, o.alliance_id AS alliance, o.protection_until AS protectionUntil, o.created_at AS createdAt, u.id AS userId, u.username, u.banned'; let rows; if (args.flags.id !== undefined) rows = db.all('SELECT ' + cols + ' FROM operators o JOIN users u ON u.id = o.user_id WHERE o.id = ?' + wcond, intFlag(args.flags, 'id', { min: 1 }), ...wpar); else rows = db.all('SELECT ' + cols + ' FROM operators o JOIN users u ON u.id = o.user_id WHERE (o.name = ? COLLATE NOCASE OR u.username = ?)' + wcond + ' ORDER BY o.world_id, o.id', key, key, ...wpar); if (!rows.length) throw notFound('no operator matching ' + JSON.stringify(key)); if (rows.length > 1 && !args.flags.world) { throw usage('several operators match ' + JSON.stringify(key) + ': ' + rows.map((r) => '#' + r.id + ' (world ' + r.world + ')').join(', ') + '; use --world or --id '); } const now = ctx.now(); const out = rows.map((o) => { const towns = db.all('SELECT * FROM towns WHERE owner_id = ? ORDER BY id', o.id).map((r) => describeTown(db, r, now)); const tokens = db.get('SELECT COUNT(*) AS n FROM tokens WHERE user_id = ? AND revoked_at IS NULL', o.userId).n; return Object.assign({}, o, { banned: !!o.banned, activeTokens: tokens, towns }); }); const lines = []; for (const o of out) { lines.push('operator #' + o.id + ' "' + o.name + '" AS' + o.asNumber + ' world ' + o.world + ' user ' + o.username + (o.banned ? ' [BANNED]' : '') + ' tokens ' + o.activeTokens + (o.alliance ? ' alliance ' + o.alliance : '') + (o.protectionUntil > now ? ' protected ' + rel(o.protectionUntil, now) : '')); lines.push(...table(o.towns.map((t) => ({ id: t.id, name: t.name, pos: t.x + ',' + t.y, fiber: num(t.resources.fiber), hardware: num(t.resources.hardware), capital: num(t.resources.capital), battery: num(t.resources.battery), })), ['id', 'name', 'pos', 'fiber', 'hardware', 'capital', 'battery']), ''); } return { data: { operators: out }, lines }; }; commands.events = (args, ctx) => { const db = openDatabase(ctx); const f = args.flags; const where = [], par = []; const status = f.pending ? 'pending' : f.status; if (status) { where.push('status = ?'); par.push(status); } if (f.type) { where.push('type = ?'); par.push(f.type); } if (f.town !== undefined) { where.push('town_id = ?'); par.push(intFlag(f, 'town', { min: 1 })); } if (f.world !== undefined) { where.push('world_id = ?'); par.push(intFlag(f, 'world', { min: 1 })); } const limit = intFlag(f, 'limit', { min: 1, max: 10000, def: 50 }); const pendingOrder = status === 'pending'; const rows = db.all('SELECT id, world_id AS world, due_at AS dueAt, type, town_id AS town, status, error, payload FROM events' + (where.length ? ' WHERE ' + where.join(' AND ') : '') + (pendingOrder ? ' ORDER BY due_at, id' : ' ORDER BY id DESC') + ' LIMIT ' + limit, ...par); const now = ctx.now(); const view = rows.map((r) => Object.assign({}, r, { due: iso(r.dueAt), when: rel(r.dueAt, now), payload: r.payload.length > 60 ? r.payload.slice(0, 57) + '...' : r.payload, error: r.error ? r.error.slice(0, 40) : '' })); const lines = table(view, ['id', 'world', 'due', 'when', 'type', 'town', 'status', 'payload', 'error']); lines.push('(' + rows.length + ' shown, limit ' + limit + (pendingOrder ? ', in processing order' : ', newest first') + ')'); return { data: { events: rows }, lines }; }; // Dry run: what would the scheduler process up to `--until` (default now)? --simulate additionally runs the real // handlers on a scratch COPY of the database (VACUUM INTO a temp file, deleted afterwards) and reports the outcome. commands['replay-events'] = (args, ctx) => { const db = openDatabase(ctx); const f = args.flags; const now = ctx.now(); const until = f.until !== undefined ? parseTime(f.until, now) : now; const limit = intFlag(f, 'limit', { min: 1, max: 100000, def: 200 }); const wid = intFlag(f, 'world', { min: 1 }); const wc = wid ? ' AND world_id = ?' : ''; const wp = wid ? [wid] : []; const due = db.all("SELECT id, world_id AS world, due_at AS dueAt, type, town_id AS town, payload FROM events WHERE status = 'pending' AND due_at <= ?" + wc + ' ORDER BY due_at, id LIMIT ' + limit, until, ...wp); const total = db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'pending' AND due_at <= ?" + wc, until, ...wp).n; const next = db.get("SELECT id, due_at AS dueAt, type FROM events WHERE status = 'pending' AND due_at > ?" + wc + ' ORDER BY due_at, id LIMIT 1', until, ...wp); const data = { until, dueCount: total, due, next: next || null, simulated: null }; const lines = ['up to ' + iso(until) + ' the scheduler would process ' + total + ' event(s), in this order (nothing was changed):']; const view = due.map((e) => Object.assign({}, e, { due: iso(e.dueAt), payload: e.payload.length > 60 ? e.payload.slice(0, 57) + '...' : e.payload })); lines.push(...table(view, ['id', 'world', 'due', 'type', 'town', 'payload'])); if (total > due.length) lines.push('... ' + (total - due.length) + ' more (raise --limit)'); lines.push(next ? 'next after that: #' + next.id + ' ' + next.type + ' at ' + iso(next.dueAt) + ' (' + rel(next.dueAt, now) + ')' : 'no pending event after that'); if (f.simulate) { const sim = simulate(db, until, due.map((e) => e.id)); data.simulated = sim; lines.push('', 'simulation on a scratch copy (the real database was not touched):'); lines.push(...table(sim.outcome.map((o) => ({ id: o.id, type: o.type, status: o.status, error: o.error || '' })), ['id', 'type', 'status', 'error'])); lines.push('follow-up events enqueued: ' + sim.followUps.length + ', notifications written: ' + sim.notifications); if (sim.followUps.length) lines.push(...table(sim.followUps.map((e) => ({ id: e.id, type: e.type, due: iso(e.dueAt), status: e.status })), ['id', 'type', 'due', 'status'])); if (sim.logs.length) lines.push('handler log:', ...sim.logs.map((l) => ' ' + l)); } return { data, lines }; }; function simulate(db, until, ids) { const { Scheduler } = require(path.join(ROOT, 'server', 'scheduler.js')); const handlers = require(path.join(ROOT, 'server', 'events')); const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'upstream-replay-')); const file = path.join(dir, 'copy.db'); let copy; try { db.backup(file); copy = openDb(file); const maxBefore = copy.get('SELECT COALESCE(MAX(id), 0) AS m FROM events').m; const notesBefore = tableExists(copy, 'notifications') ? copy.get('SELECT COUNT(*) AS n FROM notifications').n : 0; const logs = []; const sched = new Scheduler({ db: copy, clock: new FakeClock(until), handlers, log: (m) => logs.push(String(m).split('\n')[0]), autoArm: false }); sched.runDue(until); const outcome = ids.length ? copy.all('SELECT id, type, status, error FROM events WHERE id IN (' + ids.map(() => '?').join(',') + ') ORDER BY due_at, id', ...ids) : []; const followUps = copy.all('SELECT id, type, due_at AS dueAt, status FROM events WHERE id > ? ORDER BY due_at, id', maxBefore); const notes = tableExists(copy, 'notifications') ? copy.get('SELECT COUNT(*) AS n FROM notifications').n - notesBefore : 0; return { outcome, followUps, notifications: notes, logs }; } finally { if (copy) copy.close(); fs.rmSync(dir, { recursive: true, force: true }); } } commands.backup = (args, ctx) => { const dest = args._[0]; if (!dest) throw usage('missing '); const keep = intFlag(args.flags, 'keep', { min: 1 }); const keepDays = numFlag(args.flags, 'keep-days', { min: 0.1 }); const db = openDatabase(ctx); const r = doBackup(db, path.resolve(ctx.cwd, dest), ctx.now(), { keep, keepDays }); const lines = ['backup written: ' + r.file + ' (' + r.bytes + ' bytes, verified)']; if (r.pruned.length) lines.push('pruned ' + r.pruned.length + ' old snapshot(s): ' + r.pruned.join(', ')); return { data: r, lines }; }; commands['verify-backup'] = (args) => { if (!args._[0]) throw usage('missing '); const file = path.resolve(args._[0]); if (!fs.existsSync(file)) throw notFound('no such file: ' + file); const v = verifySnapshot(file); return { data: v, lines: ['ok: ' + v.file + ' (' + v.bytes + ' bytes, integrity ok, ' + v.worlds + ' world(s), ' + v.users + ' user(s), ' + v.towns + ' town(s), last migration ' + v.lastMigration + ')'] }; }; commands['reset-world'] = (args, ctx) => { const id = idArg(args, 'world-id'); const db = openDatabase(ctx); const w = worldOrThrow(db, id); const plan = resetPlan(db, id); const counts = plan.steps.map((s) => ({ table: s.table, rows: db.get('SELECT COUNT(*) AS n FROM ' + s.table + ' WHERE ' + s.scope.sql, ...s.scope.params).n })); const summary = table(counts, ['table', 'rows']); if (!args.flags['yes-destroy']) { throw usage('refusing without --yes-destroy. reset-world ' + id + ' ("' + w.name + '") would delete these game rows (accounts and tokens are kept):\n ' + summary.join('\n ') + '\n untouched tables: ' + (plan.skipped.join(', ') || 'none') + '\nStop the server first and take a backup (`backup ` or --backup ), then repeat with --yes-destroy.'); } const now = ctx.now(); let backup = null; if (args.flags.backup) backup = doBackup(db, path.resolve(ctx.cwd, args.flags.backup), now, {}); const newSeed = args.flags.seed !== undefined ? intFlag(args.flags, 'seed', { min: 1, max: 2 ** 31 - 1 }) : args.flags['new-seed'] ? require('crypto').randomInt(1, 2 ** 31 - 1) : null; db.exec('PRAGMA foreign_keys = OFF'); // children are deleted first, but later stages may add odd tables try { db.tx(() => { if (tableExists(db, 'idempotency')) db.run('DELETE FROM idempotency WHERE user_id IN (SELECT user_id FROM operators WHERE world_id = ?)', id); for (const s of plan.steps) db.run('DELETE FROM ' + s.table + ' WHERE ' + s.scope.sql, ...s.scope.params); db.run("UPDATE worlds SET start_at = ?, end_at = NULL, status = 'running'" + (newSeed ? ', seed = ?' : '') + ' WHERE id = ?', now, ...(newSeed ? [newSeed] : []), id); const bad = db.all('PRAGMA foreign_key_check'); if (bad.length) throw new AdminError('reset would leave dangling references (' + bad.length + ', first in table ' + bad[0].table + '): rolled back'); }); } finally { db.exec('PRAGMA foreign_keys = ON'); } const lines = ['world ' + id + ' reset (running again, start ' + iso(now) + (newSeed ? ', new seed ' + newSeed : ', same seed') + '). Deleted:', ...summary, 'Accounts were kept; players must join the world again.']; if (backup) lines.push('backup before reset: ' + backup.file); return { data: { id, deleted: counts, seed: newSeed, backup: backup && backup.file }, lines }; }; commands.grant = (args, ctx) => { const id = idArg(args, 'town-id'); const f = args.flags; const keys = ['fiber', 'hardware', 'capital', 'battery']; const add = {}; const all = numFlag(f, 'all', { min: 0, max: 1e9 }); for (const k of keys) { const v = numFlag(f, k, { min: 0, max: 1e9 }); if (v !== undefined) add[k] = v; else if (all !== undefined && k !== 'battery') add[k] = all; } if (!Object.keys(add).length) throw usage('nothing to grant: use --fiber N --hardware N --capital N --battery N (or --all N for the three stockpiles)'); const db = openDatabase(ctx); const row = db.get('SELECT * FROM towns WHERE id = ?', id); if (!row) throw notFound('no town with id ' + id); requireFlag(args, 'yes', 'grant changes a town\'s resources outside the rules (test tool)'); const now = ctx.now(); const before = describeTown(db, row, now).resources; // Added to the stored amounts (valid at res_at): the lazy model then carries them forward, no settle needed. db.tx(() => db.run('UPDATE towns SET fiber = fiber + ?, hardware = hardware + ?, capital = capital + ?, battery = battery + ? WHERE id = ?', add.fiber || 0, add.hardware || 0, add.capital || 0, add.battery || 0, id)); const after = describeTown(db, db.get('SELECT * FROM towns WHERE id = ?', id), now).resources; return { data: { id, granted: add, before, after }, lines: ['granted to town ' + id + ': ' + Object.keys(add).map((k) => k + ' +' + num(add[k])).join(', '), 'now: ' + keys.map((k) => k + ' ' + num(before[k]) + ' -> ' + num(after[k])).join(' ')] }; }; // ---- help --------------------------------------------------------------------------------------------------------- const HELP_CMDS = { help: 'this text', status: 'database file, migrations, counts, overdue events', migrate: 'apply pending migrations (the server does this at boot too; creates the file if missing)', worlds: 'list worlds', 'create-world': '[--name N] [--speed X] [--radius R] [--seed S] [--end-in 90d] new world (a second one runs in parallel)', 'end-world': ' --yes mark a world ended (no new signups/joins)', users: '[--banned] list accounts (no hashes, no tokens)', operators: '[--world ID] list operators (a user in a world)', ban: ' ban and revoke all tokens; login answers 403', unban: '', 'revoke-tokens': ' log the user out everywhere', 'inspect-operator': ' [--world ID] | --id N operator with POPs and current resources', 'inspect-town': ' current resources (rules.resourcesAt), buildings, queue, units', events: '[--pending] [--status S] [--type T] [--town ID] [--world ID] [--limit N] event queue', 'replay-events': '[--until now|+2h|ISO] [--world ID] [--limit N] [--simulate] dry run: what the scheduler would process; --simulate runs the handlers on a scratch copy', backup: ' [--keep N] [--keep-days D] consistent VACUUM INTO snapshot, verified, never overwrites; prunes old ones', 'verify-backup': ' integrity check of a snapshot (read-only)', 'reset-world': ' --yes-destroy [--backup DIR] [--seed S | --new-seed] delete that world\'s game rows, keep accounts', grant: ' --yes [--fiber N] [--hardware N] [--capital N] [--battery N] [--all N] add resources (testing)', }; function helpText() { const w = Math.max(...Object.keys(HELP_CMDS).map((k) => k.length)); return [ 'UPSTREAM admin: node tools/admin.js [args] [--db FILE] [--json]', '', 'Database: --db FILE, else $DB_PATH, else ./data/game.db. Safe on a running server (WAL); run it as the service user', '(deploy/upstream-admin does). Destructive commands need an explicit flag: --yes or --yes-destroy.', 'Exit codes: 0 ok, 1 failed, 2 usage or missing confirmation flag, 3 not found.', '', ...Object.keys(HELP_CMDS).map((k) => ' ' + k.padEnd(w + 1) + HELP_CMDS[k]), ]; } // ---- entry point --------------------------------------------------------------------------------------------------- // main(argv, io) -> exit code. io = { out, err, env, cwd, now } (all optional, for tests). function main(argv, io) { io = io || {}; const out = io.out || ((s) => process.stdout.write(s + '\n')); const err = io.err || ((s) => process.stderr.write(s + '\n')); const env = io.env || process.env; const ctx = { opened: [], cwd: io.cwd || process.cwd(), now: io.now || (() => Date.now()), dbPath: null }; try { const args = parseArgs(argv); const name = args._.shift() || 'help'; if (args.flags.help && name !== 'help') { for (const l of helpText()) out(l); return 0; } const cmd = commands[name]; if (!cmd) throw usage('unknown command ' + JSON.stringify(name) + ' (try `help`)'); ctx.dbPath = args.flags.db || env.DB_PATH || './data/game.db'; if (ctx.dbPath !== ':memory:') ctx.dbPath = path.resolve(ctx.cwd, ctx.dbPath); const res = cmd(args, ctx); if (args.flags.json) out(JSON.stringify(res.data, null, 2)); else for (const l of res.lines) out(l); return 0; } catch (e) { if (e instanceof AdminError) { err('error: ' + e.message); return e.exitCode; } err('error: ' + (e && e.message || e)); if (env.DEBUG && e && e.stack) err(e.stack); return 1; } finally { for (const db of ctx.opened) { try { db.close(); } catch (e) { /* already closed */ } } } } module.exports = { main, commands, parseArgs, parseTime, parseDuration, listBackups, pruneSet, backupStamp, verifySnapshot, doBackup, resetPlan, table, AdminError }; if (require.main === module) process.exitCode = main(process.argv.slice(2));