Stage 3-5 server (military, expansion, social), web client for stages 1-2, ops/deploy

Built: units/research/training, movements (scan, raid, attack, takeover, reinforce, settle, transport), deterministic
combat resolved in the event queue, reports, power/offline units, founding + conquest + incumbent sites; alliances,
roles, diplomacy (24h pact cancel via events), messages, rankings; vanilla-JS phone-first client (auth, POP, map,
alerts, settings, en/nb); systemd units, backup timer, DEPLOY.md, tools/admin.js.
Tested: npm test 96/96 (fake clock, in-memory db, a few real-HTTP tests); client checked with headless Chromium
screenshots only. NOT done: marketplace/transport trade, client screens for military/social, WebSocket push,
bots/balance, e2e suite. All unit stats and costs are unbalanced first guesses.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
This commit is contained in:
jvanvik 2026-10-11 18:32:36 +02:00
parent b56b13880e
commit 249818a7aa
111 changed files with 17472 additions and 13 deletions

21
deploy/upstream-admin Executable file
View file

@ -0,0 +1,21 @@
#!/bin/sh
# Run tools/admin.js as the service user, against the service's database. Install:
# install -m 0755 /opt/upstream/deploy/upstream-admin /usr/local/bin/upstream-admin
# upstream-admin help
# Running admin.js as root would create the database's -wal/-shm files owned by root and the game could no longer open
# the database, hence the runuser. DB_PATH comes from the environment, else /etc/upstream/upstream.env, else the default.
set -eu
APP_DIR=${UPSTREAM_DIR:-/opt/upstream}
ENV_FILE=${UPSTREAM_ENV_FILE:-/etc/upstream/upstream.env}
if [ -z "${DB_PATH:-}" ] && [ -r "$ENV_FILE" ]; then
DB_PATH=$(sed -n 's/^[[:space:]]*DB_PATH=//p' "$ENV_FILE" | tail -n 1 | sed -e 's/^"\(.*\)"$/\1/' -e "s/^'\(.*\)'\$/\1/")
fi
DB_PATH=${DB_PATH:-/var/lib/upstream/game.db}
if [ "$(id -u)" = 0 ]; then
exec runuser -u upstream -- env DB_PATH="$DB_PATH" node "$APP_DIR/tools/admin.js" "$@"
elif [ "$(id -un)" = upstream ]; then
exec env DB_PATH="$DB_PATH" node "$APP_DIR/tools/admin.js" "$@"
else
echo "upstream-admin: run as root (it switches to the upstream user) or as the upstream user" >&2
exit 2
fi

View file

@ -0,0 +1,40 @@
# One database snapshot (VACUUM INTO, verified, dated file name, old ones pruned). Started by upstream-backup.timer,
# or by hand: systemctl start upstream-backup. Safe while the game server is running (SQLite WAL: readers do not block
# the writer). See docs/DEPLOY.md, "Backups".
#
# install -m 0644 deploy/upstream-backup.service deploy/upstream-backup.timer /etc/systemd/system/
# systemctl daemon-reload && systemctl enable --now upstream-backup.timer
[Unit]
Description=Upstream - database snapshot
Documentation=file:///opt/upstream/docs/DEPLOY.md
# Not started from the main unit's dependency graph: a backup must also work while the game is stopped.
[Service]
Type=oneshot
User=upstream
Group=upstream
WorkingDirectory=/opt/upstream
# Same state directory as upstream.service; snapshots go to /var/lib/upstream/backups (mode 0700).
StateDirectory=upstream
StateDirectoryMode=0700
# Same override file, so DB_PATH follows the main service if you changed it there.
EnvironmentFile=-/etc/upstream/upstream.env
Environment=DB_PATH=/var/lib/upstream/game.db
# Keep the newest 14 snapshots (daily timer = two weeks). Edit here, or `systemctl edit upstream-backup`.
# --keep-days D works too; with both, a snapshot survives if either rule keeps it.
ExecStart=/usr/bin/env node /opt/upstream/tools/admin.js backup /var/lib/upstream/backups --keep 14
# A failed snapshot (verification failed, disk full) must be visible: the unit fails and journalctl shows why.
TimeoutStartSec=15min
Nice=10
IOSchedulingClass=idle
NoNewPrivileges=yes
CapabilityBoundingSet=
AmbientCapabilities=
RestrictAddressFamilies=AF_UNIX
RestrictRealtime=yes
RestrictSUIDSGID=yes
LockPersonality=yes
SystemCallArchitectures=native
UMask=0077

View file

@ -0,0 +1,14 @@
# Daily snapshot at 04:15 (+ up to 10 min jitter). Persistent: a run missed while the container was off happens at
# the next boot. Every 6 hours instead: OnCalendar=*-*-* 00/6:15:00 and raise --keep in upstream-backup.service.
[Unit]
Description=Upstream - daily database snapshot
[Timer]
OnCalendar=*-*-* 04:15:00
RandomizedDelaySec=10min
Persistent=true
Unit=upstream-backup.service
[Install]
WantedBy=timers.target

94
deploy/upstream.service Normal file
View file

@ -0,0 +1,94 @@
# Upstream systemd unit (Debian 13 LXC; see docs/DEPLOY.md).
#
# install -m 0644 deploy/upstream.service /etc/systemd/system/upstream.service
# systemctl daemon-reload && systemctl enable --now upstream
# journalctl -u upstream -f
#
# Expects the app in /opt/upstream (server/, public/, node_modules/) and a Node >= 22.13 on the PATH (the Debian 13
# package is Node 20 and is too old for node:sqlite: see docs/DEPLOY.md section 1). The database lives in
# /var/lib/upstream (systemd creates it, owned by the service user, from StateDirectory=).
[Unit]
Description=Upstream - persistent network-operator strategy game (Node server, SQLite)
Documentation=file:///opt/upstream/docs/DEPLOY.md
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=upstream
Group=upstream
WorkingDirectory=/opt/upstream
# /usr/bin/env finds node on systemd's default PATH: /usr/local/bin (official tarball) before /usr/bin (NodeSource).
ExecStart=/usr/bin/env node /opt/upstream/server/index.js
# ---- state --------------------------------------------------------------------------------------------------
# Creates /var/lib/upstream owned by the service user (mode 0700) before start. This is the ONLY place the game
# writes. Do not put the database in /opt/upstream.
StateDirectory=upstream
StateDirectoryMode=0700
# Optional file for settings and secrets (SIGNUP_CODE). Values in it override the Environment= lines below.
# Keep it root:upstream 0640. The leading "-" means: fine if it does not exist.
EnvironmentFile=-/etc/upstream/upstream.env
# ---- configuration (see docs/DEPLOY.md, "Configuration") ----------------------------------------------------
Environment=NODE_ENV=production
Environment=PORT=8080
# "::" = one dual-stack socket for IPv4 + IPv6 (falls back to 0.0.0.0 if the kernel has no IPv6).
Environment=HOST=::
Environment=DB_PATH=/var/lib/upstream/game.db
# auto: believe X-Forwarded-For / X-Forwarded-Host only from loopback / private-range peers (your proxy).
Environment=TRUST_PROXY=auto
# Only when the proxy reaches this server from a NON-private address (global IPv6, Tailscale / CGNAT 100.64.0.0/10):
#Environment=TRUSTED_PROXIES=100.64.0.0/10,2001:db8::6
# Only when the proxy rewrites the Host header or you serve several public names (include the port if not 80/443):
#Environment=ALLOWED_ORIGINS=https://upstream.example.com
#Environment=MAX_CONN_PER_IP=40
# World settings: only read when the database has NO world yet (first boot). Change them later with
# `upstream-admin create-world` / `reset-world`, not here.
#Environment=WORLD_SPEED=1
#Environment=WORLD_RADIUS=50
#Environment=WORLD_NAME=World 1
# Invite code for signup. Empty/unset = open signup. Put the real value in /etc/upstream/upstream.env, not here.
#Environment=SIGNUP_CODE=
# ---- lifecycle ----------------------------------------------------------------------------------------------
Restart=on-failure
RestartSec=2
# SIGTERM closes the listener and the database and exits 0 within about a second; SIGKILL after 15 s as a backstop.
# (A SIGKILL is still safe for SQLite: the WAL is replayed on the next start and overdue events are caught up.)
TimeoutStopSec=15
# ---- hardening that works in any container (no mount namespaces needed) ------------------------------------
NoNewPrivileges=yes
CapabilityBoundingSet=
AmbientCapabilities=
# AF_NETLINK: Node's os.networkInterfaces() (start-up banner) and glibc's resolver use it.
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX AF_NETLINK
RestrictRealtime=yes
RestrictSUIDSGID=yes
LockPersonality=yes
SystemCallArchitectures=native
# New files (the database, its -wal/-shm, snapshots) are readable by the service user only: they hold password hashes.
UMask=0077
# Do NOT add MemoryDenyWriteExecute=yes: V8's JIT needs writable+executable memory and Node would crash.
# ---- optional extra hardening: needs mount namespaces ------------------------------------------------------
# In an unprivileged Proxmox LXC these fail with "status=226/NAMESPACE" unless the container has the
# "Nesting" feature enabled (Proxmox: container > Options > Features > Nesting; then restart the CT).
# Unlike Netwall this app writes to disk, so ProtectSystem=strict needs the ReadWritePaths line next to it.
# (StateDirectory= already makes /var/lib/upstream writable under strict; ReadWritePaths is the explicit belt.)
#ProtectSystem=strict
#ReadWritePaths=/var/lib/upstream
#ProtectHome=yes
#PrivateTmp=yes
#PrivateDevices=yes
#ProtectKernelTunables=yes
#ProtectKernelModules=yes
#ProtectKernelLogs=yes
#ProtectControlGroups=yes
#ProtectClock=yes
#ProtectHostname=yes
[Install]
WantedBy=multi-user.target