#!/usr/bin/env bash # Hangar: the game server panel. One line installs everything: # # curl -fsSL https://api.mintmotive.com.au/hangar/install.sh | sudo bash # # It works on Ubuntu, Debian, Raspberry Pi OS, Linux Mint, Pop!_OS, Fedora, Rocky/Alma, Arch and openSUSE, # 64-bit Intel/AMD or ARM (a Raspberry Pi 4 or 5 is fine). It installs what Hangar needs (Docker, Node.js, # unpackers), downloads Hangar and checks it against its published fingerprint, sets it up as a service that # starts with the machine, finds the game servers already here, and updates itself each night (keeping the # previous version, and going back to it if the new one doesn't start). # # Running it again updates Hangar. Nothing you've set up is touched. # From the mint-cloud folder it installs that copy instead: sudo ./apps/hangar/install.sh # # Options (put them before bash: curl … | sudo HANGAR_PORT=9000 bash): # HANGAR_PORT=8800 the panel's port # HANGAR_HOST=0.0.0.0 who can reach it: 0.0.0.0 (your network) or 127.0.0.1 (this machine, plus Tailscale if it's here) # HANGAR_DATA, HANGAR_SERVERS where Hangar keeps its own files and the game servers # HANGAR_USER=you the account Hangar runs as (default: whoever ran sudo, or a new "hangar" account) # HANGAR_AUTO_UPDATE=0 don't update by itself each night # HANGAR_VERSION=1.4.0 install that version instead of the newest # HANGAR_ADOPT="acserver" systemd services Hangar may always start and stop (game servers it finds are added too) # HANGAR_LICENCE_KEY=HANGAR-… a licence key for Hangar to use on its first start # HANGAR_SETUP_CODE=XXXX-… the code for making the owner account (otherwise one is made) # HANGAR_HOSTED=1 a server we host: Hangar sits behind HTTPS on https://
.sslip.io set -euo pipefail GIVEN_SITE=${HANGAR_SITE:-} SITE=${GIVEN_SITE:-https://api.mintmotive.com.au/hangar} OPT=${HANGAR_OPT:-/opt/hangar} QUIET=${HANGAR_QUIET:-0} if [ -t 1 ]; then B=$'\e[1m'; G=$'\e[32m'; Y=$'\e[33m'; R=$'\e[31m'; D=$'\e[2m'; N=$'\e[0m'; else B='' G='' Y='' R='' D='' N=''; fi say() { [ "$QUIET" = 1 ] || printf '%s▸%s %s\n' "$G" "$N" "$*"; } note() { [ "$QUIET" = 1 ] || printf ' %s%s%s\n' "$D" "$*" "$N"; } warn() { printf '%s!%s %s\n' "$Y" "$N" "$*"; } die() { printf '%s✕ %s%s\n' "$R" "$*" "$N" >&2; exit 1; } trap 'die "The install stopped at line $LINENO. Anything that was already running hasn’t been changed. Run it again, or send this output to support."' ERR [ "$(id -u)" = 0 ] || die "Run it with sudo: curl -fsSL $SITE/install.sh | sudo bash" [ -d /run/systemd/system ] || die "Hangar needs a Linux machine that uses systemd (most do: Ubuntu, Debian, Raspberry Pi OS, Fedora…)." [ "$QUIET" = 1 ] || printf '\n%sHANGAR%s %sgame server panel%s\n\n' "$B" "$N" "$D" "$N" # ---------------------------------------------------------------- this machine . /etc/os-release 2>/dev/null || true if command -v apt-get >/dev/null; then PM=apt; elif command -v dnf >/dev/null; then PM=dnf; elif command -v yum >/dev/null; then PM=yum; elif command -v pacman >/dev/null; then PM=pacman; elif command -v zypper >/dev/null; then PM=zypper; else die "Hangar doesn't know how to install software on ${PRETTY_NAME:-this system} yet."; fi case "$(uname -m)" in x86_64|amd64) ARCH=x64 ;; aarch64|arm64) ARCH=arm64 ;; armv7l) ARCH=armv7l; warn "32-bit ARM works, but a 64-bit system runs game servers much better." ;; *) die "Hangar runs on 64-bit Intel/AMD or ARM machines; this is $(uname -m)." ;; esac note "${PRETTY_NAME:-Linux} on $(uname -m)" pkg() { case "$PM" in apt) DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "$@" >/dev/null ;; dnf|yum) "$PM" install -y -q "$@" >/dev/null ;; pacman) pacman -Sy --noconfirm --needed "$@" >/dev/null ;; zypper) zypper -q -n install "$@" >/dev/null ;; esac } say "Getting the basics (downloads, unpackers)" case "$PM" in apt) apt-get update -qq >/dev/null || true; pkg ca-certificates curl tar xz-utils unzip libarchive-tools sudo git ;; dnf|yum) pkg ca-certificates curl tar xz unzip bsdtar sudo git ;; pacman) pkg ca-certificates curl tar xz unzip libarchive sudo git ;; zypper) pkg ca-certificates curl tar xz unzip bsdtar sudo git ;; esac # ---------------------------------------------------------------- the account Hangar runs as, and where things live # An update keeps every setting: what's in /etc/hangar.env wins over the defaults (options given now win over both). for k in HANGAR_DATA HANGAR_SERVERS HANGAR_PORT HANGAR_HOST HANGAR_AUTO_UPDATE; do eval "GIVEN_$k=\${$k:-}"; done GIVEN_USER=${HANGAR_USER:-} [ -f /etc/hangar.env ] && . /etc/hangar.env SITE=${GIVEN_SITE:-${HANGAR_SITE:-$SITE}} for k in HANGAR_DATA HANGAR_SERVERS HANGAR_PORT HANGAR_HOST HANGAR_AUTO_UPDATE; do eval "v=\$GIVEN_$k"; [ -n "$v" ] && eval "$k=\$v"; done RUN_AS=${GIVEN_USER:-${HANGAR_RUN_AS:-${SUDO_USER:-}}} if [ -z "$RUN_AS" ] || [ "$RUN_AS" = root ]; then RUN_AS=hangar if ! id hangar >/dev/null 2>&1; then say "Making an account for Hangar to run as (hangar)"; useradd --system --create-home --home-dir /var/lib/hangar --shell /usr/sbin/nologin hangar; fi fi id "$RUN_AS" >/dev/null 2>&1 || die "There's no account called $RUN_AS." HOME_DIR=$(getent passwd "$RUN_AS" | cut -d: -f6) if [ -d /srv/mint-data ]; then DEF_DATA=/srv/mint-data/hangar; DEF_SERVERS=/srv/mint-data/hangar-servers; else DEF_DATA=$HOME_DIR/hangar-data; DEF_SERVERS=$HOME_DIR/hangar-servers; fi DATA=${HANGAR_DATA:-$DEF_DATA} SERVERS=${HANGAR_SERVERS:-$DEF_SERVERS} PORT=${HANGAR_PORT:-8800} ADOPT=${HANGAR_ADOPT:-"acserver acserver@*"} AUTO=${HANGAR_AUTO_UPDATE:-1} HOSTED=${HANGAR_HOSTED:-0} if [ "$HOSTED" = 1 ]; then HANGAR_HOST=127.0.0.1; fi if [ -z "${HANGAR_HOST:-}" ]; then # With Tailscale here, the panel stays on this machine and Tailscale carries it (with HTTPS). Otherwise it's # open on your network, and protected by the setup code until you've made your account. if command -v tailscale >/dev/null && tailscale status >/dev/null 2>&1; then HOST=127.0.0.1; else HOST=0.0.0.0; fi else HOST=$HANGAR_HOST; fi # ---------------------------------------------------------------- Docker: every game runs in its own container if ! command -v docker >/dev/null; then say "Installing Docker" case "$PM" in apt) pkg docker.io || curl -fsSL https://get.docker.com | sh >/dev/null ;; dnf|yum) pkg moby-engine 2>/dev/null || pkg docker 2>/dev/null || curl -fsSL https://get.docker.com | sh >/dev/null ;; pacman|zypper) pkg docker ;; esac fi systemctl enable --now docker >/dev/null 2>&1 || true docker info >/dev/null 2>&1 || die "Docker is installed but isn't running. Try: sudo systemctl start docker, then run this again." note "$(docker --version)" # ---------------------------------------------------------------- Node.js 22 (Hangar's own copy, if the system's is older) NODE="" if command -v node >/dev/null && [ "$(node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo 0)" -ge 22 ]; then NODE=$(command -v node) elif [ -x "$OPT/node/bin/node" ] && [ "$("$OPT/node/bin/node" -p 'process.versions.node.split(".")[0]')" -ge 22 ]; then NODE=$OPT/node/bin/node else say "Getting Node.js 22 for Hangar (its own copy, in $OPT/node)" TMPN=$(mktemp -d) curl -fsSL https://nodejs.org/dist/latest-v22.x/SHASUMS256.txt -o "$TMPN/SUMS" FILE=$(grep -oE "node-v22\.[0-9]+\.[0-9]+-linux-$ARCH\.tar\.xz" "$TMPN/SUMS" | head -1) [ -n "$FILE" ] || die "Couldn't find Node.js 22 for $ARCH." curl -fsSL "https://nodejs.org/dist/latest-v22.x/$FILE" -o "$TMPN/$FILE" (cd "$TMPN" && grep " $FILE\$" SUMS | sha256sum -c --quiet -) || die "Node.js didn't match its published fingerprint, so it wasn't installed." rm -rf "$OPT/node" && mkdir -p "$OPT/node" && tar -xJf "$TMPN/$FILE" -C "$OPT/node" --strip-components=1 --no-same-owner rm -rf "$TMPN"; NODE=$OPT/node/bin/node fi note "Node.js $("$NODE" -v)" # ---------------------------------------------------------------- Hangar itself SRC=""; SELF="${BASH_SOURCE[0]:-}" if [ -n "$SELF" ] && [ -f "$SELF" ] && [ -f "$(dirname "$SELF")/lib/app.js" ] && [ "${HANGAR_FROM_SITE:-0}" != 1 ]; then SRC=$(cd "$(dirname "$SELF")" && pwd); fi mkdir -p "$OPT/releases" if [ -n "$SRC" ]; then VERSION=$("$NODE" -p "require('$SRC/package.json').version") say "Installing Hangar $VERSION from $SRC" DEST="$OPT/releases/$VERSION"; rm -rf "$DEST.new"; mkdir -p "$DEST.new/bin" cp -r "$SRC/lib" "$SRC/public" "$SRC/server.js" "$SRC/package.json" "$SRC/install.sh" "$DEST.new/" cp "$SRC/bin/hangar" "$DEST.new/bin/hangar" CHANNEL=local else TMPH=$(mktemp -d) curl -fsSL "$SITE/latest.json" -o "$TMPH/latest.json" || die "Couldn't reach $SITE. Is this machine online?" read -r VERSION TARBALL SHA <<<"$("$NODE" -e ' const j = require(process.argv[1]); const want = process.argv[2]; const r = !want || j.version === want ? j : (j.releases || []).find((x) => x.version === want); if (!r) { process.exit(1); } console.log(r.version, r.tarball, r.sha256);' "$TMPH/latest.json" "${HANGAR_VERSION:-}")" || die "Hangar ${HANGAR_VERSION:-} isn't on $SITE." say "Downloading Hangar $VERSION" curl -fsSL "$SITE/$TARBALL" -o "$TMPH/$TARBALL" echo "$SHA $TMPH/$TARBALL" | sha256sum -c --quiet - || die "The download didn't match Hangar's published fingerprint, so nothing was changed." note "Fingerprint checked: ${SHA:0:16}…" DEST="$OPT/releases/$VERSION"; rm -rf "$DEST.new"; mkdir -p "$DEST.new" tar -xzf "$TMPH/$TARBALL" -C "$DEST.new" --strip-components=1 --no-same-owner rm -rf "$TMPH" CHANNEL=site fi [ -f "$DEST.new/server.js" ] && [ -f "$DEST.new/bin/hangar" ] || die "That copy of Hangar is incomplete." chown -R root:root "$DEST.new"; chmod -R go-w "$DEST.new" PREV=$(readlink -f "$OPT/current" 2>/dev/null || true) [ "$PREV" = "$DEST" ] && PREV="" rm -rf "$DEST" && mv "$DEST.new" "$DEST" ln -sfn "$DEST" "$OPT/current" # the layout from before releases/ (lib, public… straight in /opt/hangar) goes rm -rf "$OPT/lib" "$OPT/public" "$OPT/server.js" "$OPT/package.json" # keep this version and the two before it ls -1t "$OPT/releases" | grep -v '\.new$' | tail -n +4 | while read -r old; do [ "$OPT/releases/$old" = "$PREV" ] || rm -rf "${OPT:?}/releases/$old"; done # ---------------------------------------------------------------- folders, settings usermod -aG docker "$RUN_AS" if getent group systemd-journal >/dev/null; then usermod -aG systemd-journal "$RUN_AS"; fi mkdir -p "$DATA" "$SERVERS" && chown "$RUN_AS:$RUN_AS" "$DATA" "$SERVERS" cat > /etc/hangar.env < /usr/local/sbin/hangar-svc.new <&2; exit 2 ;; esac echo "\$2" | grep -Eq '^[A-Za-z0-9@._-]{1,80}\$' || { echo "hangar-svc: bad service name" >&2; exit 2; } ok=0 case "\$2" in $CASES *) grep -qxF "\$2" /etc/hangar/allow 2>/dev/null && ok=1 ;; esac [ "\$ok" = 1 ] || { echo "hangar-svc: not allowed yet. Run: sudo hangar-allow \$2" >&2; exit 2; } [ "\$1" = check ] && exit 0 exec $SYSTEMCTL "\$1" "\$2" HELPER cat > /usr/local/sbin/hangar-allow.new <<'ALLOW' #!/bin/sh # Let Hangar start, stop and restart one more systemd service: sudo hangar-allow [ "$(id -u)" = 0 ] || { echo "Run it with sudo: sudo hangar-allow $1"; exit 1; } name=${1%.service} echo "$name" | grep -Eq '^[A-Za-z0-9@._-]{1,80}$' || { echo "That isn't a service name."; exit 2; } case "$name" in ssh*|systemd-*|dbus*|tailscale*|docker*|containerd*|hangar*|sudo*|cron*|networking*|NetworkManager*|getty*|user@*|polkit*|udisks*|snapd*|ufw*|wg-*|mint-cloud*) echo "Hangar won't control $name."; exit 2 ;; esac systemctl cat "$name" >/dev/null 2>&1 || { echo "There's no service called $name."; exit 2; } grep -qxF "$name" /etc/hangar/allow 2>/dev/null || echo "$name" >> /etc/hangar/allow echo "Hangar may now start, stop and restart $name." ALLOW cat > /usr/local/sbin/hangar-update.new <<'UPDATE' #!/bin/sh # Update Hangar from its own site: fetch the installer, check it against the fingerprint the site publishes, # and run it (it checks Hangar's own download the same way, and goes back if the new version won't start). # hangar-update update if there's a newer version # hangar-update --check say what's installed and what's newest set -eu [ "$(id -u)" = 0 ] || { echo "Run it with sudo."; exit 1; } . /etc/hangar.env CUR=$(basename "$(readlink /opt/hangar/current)") [ "${HANGAR_CHANNEL:-site}" = site ] || { echo "installed $CUR (from a local copy; update it there)"; exit 0; } T=$(mktemp -d); trap 'rm -rf "$T"' EXIT curl -fsSL "$HANGAR_SITE/latest.json" -o "$T/latest.json" NEW=$("$HANGAR_NODE" -p "require('$T/latest.json').version") if [ "${1:-}" = --check ]; then echo "installed $CUR, newest $NEW"; exit 0; fi [ "$NEW" != "$CUR" ] || { echo "Hangar $CUR is the newest."; exit 0; } SUM=$("$HANGAR_NODE" -p "require('$T/latest.json').installSha256") curl -fsSL "$HANGAR_SITE/install.sh" -o "$T/install.sh" echo "$SUM $T/install.sh" | sha256sum -c --quiet - || { echo "The installer didn't match its fingerprint; not updating."; exit 4; } [ ! -x /usr/local/sbin/dyno-keep ] || /usr/local/sbin/dyno-keep before-update hangar "$NEW" || true HANGAR_SITE="$HANGAR_SITE" HANGAR_QUIET=1 HANGAR_FROM_SITE=1 bash "$T/install.sh" echo "Updated Hangar $CUR → $NEW." UPDATE # each helper is swapped in whole: hangar-update may be the one running this, and a shell reads its script as it goes for h in svc allow update; do chown root:root /usr/local/sbin/hangar-$h.new; chmod 755 /usr/local/sbin/hangar-$h.new; mv -f /usr/local/sbin/hangar-$h.new /usr/local/sbin/hangar-$h; done install -m 755 -o root -g root "$OPT/current/bin/hangar" /usr/local/bin/hangar printf '%s ALL=(root) NOPASSWD: /usr/local/sbin/hangar-svc\n%s ALL=(root) NOPASSWD: /usr/local/sbin/hangar-update\n' "$RUN_AS" "$RUN_AS" > /etc/sudoers.d/hangar chmod 440 /etc/sudoers.d/hangar visudo -cf /etc/sudoers.d/hangar >/dev/null || { rm -f /etc/sudoers.d/hangar; die "Couldn't add the sudo rule."; } # Game servers already set up as services: found now, so bringing them in needs nothing more. FOUND=$(HOME="$HOME_DIR" "$NODE" "$OPT/current/server.js" --find-services 2>/dev/null || true) for svc in $FOUND; do /usr/local/sbin/hangar-allow "$svc" >/dev/null 2>&1 || true; done if [ -n "$FOUND" ]; then say "Found game servers running as services: $FOUND"; fi # ---------------------------------------------------------------- the service, and nightly updates cat > /etc/systemd/system/hangar.service < /etc/systemd/system/hangar-update.service <<'UNIT' [Unit] Description=Update Hangar After=network-online.target Wants=network-online.target [Service] Type=oneshot ExecStart=/usr/local/sbin/hangar-update UNIT cat > /etc/systemd/system/hangar-update.timer <<'UNIT' [Unit] Description=Update Hangar each night [Timer] OnCalendar=*-*-* 04:40 RandomizedDelaySec=45min Persistent=true [Install] WantedBy=timers.target UNIT systemctl daemon-reload systemctl enable hangar >/dev/null 2>&1 if [ "$AUTO" = 1 ] && [ "$CHANNEL" = site ]; then systemctl enable --now hangar-update.timer >/dev/null 2>&1; else systemctl disable --now hangar-update.timer >/dev/null 2>&1 || true; fi # A licence key given now is used on Hangar's first start, then forgotten. if [ -n "${HANGAR_LICENCE_KEY:-}" ]; then printf '%s\n' "$HANGAR_LICENCE_KEY" > "$DATA/licence-key"; chown "$RUN_AS:$RUN_AS" "$DATA/licence-key"; chmod 600 "$DATA/licence-key"; fi # The first visit needs this code to make the owner account, so nobody else on your network can claim the panel first. CODE="" if [ ! -s "$DATA/users.json" ] || [ "$(tr -d ' \n' < "$DATA/users.json")" = "[]" ]; then if [ -n "${HANGAR_SETUP_CODE:-}" ] && [ ! -s "$DATA/setup-code" ]; then printf '%s\n' "$HANGAR_SETUP_CODE" > "$DATA/setup-code"; fi CODE=$(cat "$DATA/setup-code" 2>/dev/null || true) if [ -z "$CODE" ]; then CODE=$(od -An -N6 -tx1 /dev/urandom | tr -d ' \n' | tr 'a-f' 'A-F' | sed 's/\(....\)\(....\)\(....\)/\1-\2-\3/'); echo "$CODE" > "$DATA/setup-code"; fi chown "$RUN_AS:$RUN_AS" "$DATA/setup-code"; chmod 600 "$DATA/setup-code" fi say "Starting Hangar" systemctl restart hangar ok=0; for _ in $(seq 1 40); do if curl -fsS -m 2 "http://127.0.0.1:$PORT/api/me" -H 'x-hangar: 1' >/dev/null 2>&1; then ok=1; break; fi; sleep 0.5; done if [ "$ok" != 1 ]; then journalctl -u hangar -n 20 --no-pager >&2 || true if [ -n "$PREV" ] && [ -d "$PREV" ]; then ln -sfn "$PREV" "$OPT/current"; systemctl restart hangar; rm -rf "$DEST" die "Hangar $VERSION didn't start (the lines above say why), so the previous version, $(basename "$PREV"), is back." fi die "Hangar didn't start. The lines above say why." fi # ---------------------------------------------------------------- a server we host: HTTPS in front # Caddy gets a certificate for a name made from the public address (1.2.3.4 → 1-2-3-4.sslip.io) and passes # everything to Hangar, which only listens on this machine. if [ "$HOSTED" = 1 ]; then say "Putting Hangar behind HTTPS" command -v caddy >/dev/null || pkg caddy || { curl -1sLf https://dl.cloudsmith.io/public/caddy/stable/gpg.key | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg && curl -1sLf https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt > /etc/apt/sources.list.d/caddy-stable.list && apt-get update -qq && pkg caddy; } PUBLIC_IP=$(curl -fsS -m 5 https://api.ipify.org 2>/dev/null || hostname -I | awk '{print $1}') NAME="$(printf '%s' "$PUBLIC_IP" | tr . -).sslip.io" printf '%s {\n encode gzip\n reverse_proxy 127.0.0.1:%s\n}\n' "$NAME" "$PORT" > /etc/caddy/Caddyfile systemctl enable caddy >/dev/null 2>&1; systemctl restart caddy if command -v ufw >/dev/null && ufw status 2>/dev/null | grep -q "Status: active"; then ufw allow 80/tcp >/dev/null; ufw allow 443/tcp >/dev/null; fi fi # ---------------------------------------------------------------- how to open it OPEN=() [ "$HOSTED" = 1 ] && OPEN+=("https://$NAME") if command -v tailscale >/dev/null && tailscale status >/dev/null 2>&1; then tailscale serve --bg --https=10001 "http://127.0.0.1:$PORT" >/dev/null 2>&1 || true TS=$(tailscale status --json 2>/dev/null | grep -m1 '"DNSName"' | sed -E 's/.*"DNSName": *"([^"]+)\.".*/\1/' || true) if [ -n "$TS" ]; then OPEN+=("https://$TS:10001"); fi fi if [ "$HOST" = 0.0.0.0 ]; then if command -v ufw >/dev/null && ufw status 2>/dev/null | grep -q "Status: active"; then ufw allow "$PORT/tcp" comment Hangar >/dev/null || true; fi if command -v firewall-cmd >/dev/null && firewall-cmd --state >/dev/null 2>&1; then firewall-cmd -q --permanent --add-port="$PORT/tcp" && firewall-cmd -q --reload || true; fi for ip in $(hostname -I 2>/dev/null); do case "$ip" in *:*|172.17.*|172.18.*) ;; *) OPEN+=("http://$ip:$PORT") ;; esac; done fi OPEN+=("http://127.0.0.1:$PORT") [ "$QUIET" = 1 ] && exit 0 printf '\n%sHangar %s is ready.%s\n\n' "$B" "$VERSION" "$N" if [ -n "$CODE" ]; then echo "Open this to make your account (the setup code is in the link):" printf ' %s%s/?code=%s%s\n' "$B" "${OPEN[0]}" "$CODE" "$N" printf ' Setup code: %s%s%s\n' "$B" "$CODE" "$N" echo "Then Hangar finds the game servers already on this machine." [ "${#OPEN[@]}" -gt 1 ] && { echo; echo "It's also at:"; for u in "${OPEN[@]:1}"; do echo " $u"; done; } else echo "Open it:"; for u in "${OPEN[@]}"; do echo " $u"; done fi echo echo "From here on: hangar status · hangar logs · hangar update · hangar help" if [ "$AUTO" = 1 ] && [ "$CHANNEL" = site ]; then echo "Hangar updates itself each night (to turn that off: hangar auto-update off)."; fi echo "Game servers live in: $SERVERS"