#!/usr/bin/env bash
# Astelyx dedicated server: install or update it from the release archive.
#
# curl -fsSL https://astelyx.com/install-server.sh | bash
# curl -fsSL https://astelyx.com/install-server.sh | bash -s -- --dir ~/my-server
#
# macOS, Linux and Windows (Git Bash). On Windows the PowerShell twin is
# https://astelyx.com/install-server.ps1.
#
# WHAT THIS DOES, AND NOTHING ELSE:
# 1. asks the Astelyx registry for the newest stable dedicated server build
# for this operating system and CPU;
# 2. downloads it over HTTPS from the short-lived link the registry hands
# out, and checks its size and SHA-256 against the registry's answer;
# 3. unpacks it into
/astelyx/ (default ~/astelyx-server), replacing
# only an earlier copy of the server program there. Worlds, server.toml
# and anything else in are left alone;
# 4. prints how to start it.
# It never starts the server, never asks for sudo or a password, never changes
# a firewall, security or system setting, and writes nothing outside .
#
# The game client is not here: it is installed by the Astelyx launcher.
set -euo pipefail
# The registry this copy talks to. astelyx.com fills it in when it serves the
# script; ASTELYX_REGISTRY_URL overrides it (https, or http on this machine).
REGISTRY_URL_SERVED='https://api.astelyx.net'
DOWNLOAD_PAGE='https://astelyx.com/download'
say() { printf 'astelyx: %s\n' "$*"; }
die() { printf 'astelyx: error: %s\n' "$*" >&2; exit 1; }
usage() {
cat <<'EOF'
Install or update the Astelyx dedicated server.
curl -fsSL https://astelyx.com/install-server.sh | bash -s -- [--dir PATH]
--dir PATH where the server lives (default: ~/astelyx-server). The program
goes in PATH/astelyx/; nothing else in PATH is touched.
--help this text.
EOF
}
dir=""
while [ $# -gt 0 ]; do
case "$1" in
--dir)
[ $# -ge 2 ] && [ -n "$2" ] || die "--dir needs a path"
dir=$2
shift 2
;;
--dir=*)
dir=${1#--dir=}
[ -n "$dir" ] || die "--dir needs a path"
shift
;;
-h | --help)
usage
exit 0
;;
*) die "unknown option '$1' (try --help)" ;;
esac
done
# ---- where to ask --------------------------------------------------------
# https://, or http:// on this machine only (a local test registry).
url_ok() {
local https_re='^https://[A-Za-z0-9.-]+(:[0-9]+)?(/[^[:space:]"\\]*)?$'
local local_re='^http://(127\.0\.0\.1|localhost|\[::1\])(:[0-9]+)?(/[^[:space:]"\\]*)?$'
[[ $1 =~ $https_re ]] || [[ $1 =~ $local_re ]]
}
registry=${ASTELYX_REGISTRY_URL:-$REGISTRY_URL_SERVED}
case "$registry" in
__*) die "this copy of the script does not name a registry; use the command on $DOWNLOAD_PAGE" ;;
esac
registry=${registry%/}
url_ok "$registry" || die "the registry address '$registry' is not an https:// URL"
for tool in curl uname mkdir mv rm find chmod; do
command -v "$tool" >/dev/null 2>&1 || die "'$tool' is needed and was not found"
done
# ---- this machine --------------------------------------------------------
case "$(uname -s)" in
Darwin) os=macos ;;
Linux) os=linux ;;
MINGW* | MSYS* | CYGWIN*) os=windows ;;
*) die "no Astelyx server is built for $(uname -s)" ;;
esac
arch=""
case "$(uname -m)" in
x86_64 | amd64) arch=x86_64 ;;
arm64 | aarch64) arch=arm64 ;;
esac
if [ "$os" = macos ] && [ "$(sysctl -n hw.optional.arm64 2>/dev/null || true)" = 1 ]; then
# A shell running under Rosetta reports x86_64 on an Apple-silicon Mac.
arch=arm64
fi
if [ "$os" = windows ]; then
case "${PROCESSOR_ARCHITEW6432:-${PROCESSOR_ARCHITECTURE:-}}" in
ARM64) arch=arm64 ;;
AMD64) arch=x86_64 ;;
esac
fi
[ -n "$arch" ] || die "no Astelyx server is built for the $(uname -m) processor"
if [ "$os" = windows ]; then program=voxel-server.exe; else program=voxel-server; fi
if [ -z "$dir" ]; then
[ -n "${HOME:-}" ] || die "HOME is not set; pass --dir"
dir="$HOME/astelyx-server"
fi
# ---- small helpers -------------------------------------------------------
# GET into ; prints the HTTP status. Follows redirects only to the
# same scheme.
http_get() {
local proto='=https'
case "$1" in http://*) proto='=http' ;; esac
curl --silent --show-error --location --proto "$proto" --proto-redir "$proto" \
--connect-timeout 20 --retry 2 --output "$2" --write-out '%{http_code}' "$1"
}
# The first "key":"string" in a JSON document (the registry's answers are
# flat enough for this; anything with a quote or backslash in it is refused).
json_str() {
local re="\"$2\":\"([^\"\\\\]*)\""
[[ $1 =~ $re ]] && printf '%s' "${BASH_REMATCH[1]}"
}
json_num() {
local re="\"$2\":([0-9]+)"
[[ $1 =~ $re ]] && printf '%s' "${BASH_REMATCH[1]}"
}
sha256_of() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | cut -d' ' -f1
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1" | cut -d' ' -f1
else
die "no sha256sum or shasum to check the download with"
fi
}
registry_error() { #
local body detail
body=$(cat "$2" 2>/dev/null || true)
detail=$(json_str "$body" detail || true)
[ -n "$detail" ] || detail=$(json_str "$body" error || true)
printf '%s' "${detail:-HTTP $1}"
}
# ---- what is published ---------------------------------------------------
mkdir -p "$dir" || die "cannot create $dir"
dir=$(cd "$dir" && pwd -P)
[ -w "$dir" ] || die "$dir is not writable by you; pass --dir with a folder you own"
# Everything temporary lives inside , so the final step is a rename on
# one file system, and it all goes when the script ends.
work=$(mktemp -d "$dir/.astelyx-install.XXXXXX") || die "cannot create a temporary folder in $dir"
cleanup() { rm -rf "$work"; }
trap cleanup EXIT
say "asking $registry for the newest dedicated server for $os $arch"
status=$(http_get "$registry/v1/builds/latest?category=server&os=$os&arch=$arch" "$work/latest.json") \
|| die "could not reach $registry"
case "$status" in
200) ;;
404) die "no dedicated server is published for $os on $arch yet (see $DOWNLOAD_PAGE)" ;;
*) die "the registry refused the lookup: $(registry_error "$status" "$work/latest.json")" ;;
esac
latest=$(cat "$work/latest.json")
id=$(json_str "$latest" id || true)
version=$(json_str "$latest" version || true)
sha=$(json_str "$latest" sha256 || true)
size=$(json_num "$latest" size_bytes || true)
got_category=$(json_str "$latest" category || true)
got_os=$(json_str "$latest" os || true)
got_arch=$(json_str "$latest" arch || true)
[[ $id =~ ^[0-9a-f-]{36}$ ]] || die "the registry's answer has no build id"
[[ $version =~ ^[A-Za-z0-9.+_-]{1,64}$ ]] || die "the registry's answer has no usable version"
[[ $sha =~ ^[0-9a-f]{64}$ ]] || die "the registry's answer has no sha256"
[[ $size =~ ^[0-9]+$ ]] || die "the registry's answer has no size"
[ "$got_category" = server ] && [ "$got_os" = "$os" ] \
|| die "the registry answered with a $got_category build for $got_os, not a server for $os"
case "$got_arch" in
"$arch") ;;
universal) [ "$os" = macos ] || die "the registry answered with a universal build for $os" ;;
*) die "the registry answered with a build for $got_arch, not $arch" ;;
esac
say "newest: $version ($got_arch, $size bytes, sha256 $sha)"
status=$(http_get "$registry/v1/builds/$id/download" "$work/descriptor.json") \
|| die "could not reach $registry"
case "$status" in
200) ;;
403) die "server downloads are not open right now: $(registry_error "$status" "$work/descriptor.json"). Install it from the Astelyx launcher instead." ;;
*) die "the registry refused the download: $(registry_error "$status" "$work/descriptor.json")" ;;
esac
descriptor=$(cat "$work/descriptor.json")
url=$(json_str "$descriptor" url || true)
want=$(json_str "$descriptor" sha256 || true)
url_ok "$url" || die "the registry's download link is not an https:// URL"
[ "$want" = "$sha" ] || die "the download the registry offers is not the build it listed; run the command again"
# ---- download and verify --------------------------------------------------
say "downloading"
status=$(http_get "$url" "$work/server.zip") || die "the download failed"
[ "$status" = 200 ] || die "the download failed (HTTP $status)"
got_size=$(wc -c <"$work/server.zip" | tr -d '[:space:]')
[ "$got_size" = "$size" ] || die "the download is $got_size bytes, the registry says $size; nothing was installed"
got=$(sha256_of "$work/server.zip")
[ "$got" = "$sha" ] || die "the download's sha256 is $got, the registry says $sha; nothing was installed"
say "sha256 verified: $got"
# ---- unpack ---------------------------------------------------------------
mkdir "$work/unpacked"
bsdtar_bin=""
if command -v bsdtar >/dev/null 2>&1; then
bsdtar_bin=bsdtar
elif tar --version 2>/dev/null | grep -q bsdtar; then
bsdtar_bin=tar
elif [ "$os" = windows ] && [ -x "${SYSTEMROOT:-/c/Windows}/System32/tar.exe" ]; then
bsdtar_bin="${SYSTEMROOT:-/c/Windows}/System32/tar.exe"
fi
# Every entry must be a plain relative name inside the archive.
list_entries() {
if command -v unzip >/dev/null 2>&1; then
unzip -Z1 "$1"
elif [ -n "$bsdtar_bin" ]; then
"$bsdtar_bin" -tf "$1"
elif command -v python3 >/dev/null 2>&1; then
python3 -I -c 'import sys, zipfile; print("\n".join(zipfile.ZipFile(sys.argv[1]).namelist()))' "$1"
else
die "no unzip, bsdtar or python3 to unpack the server with; install unzip and run this again"
fi
}
entries=$(list_entries "$work/server.zip") || die "the download is not a zip archive"
while IFS= read -r entry; do
[ -n "$entry" ] || continue
case "/$entry/" in
*'\'* | *'/../'* | *'/./'* | '//'*) die "the archive holds an unsafe entry '$entry'; nothing was installed" ;;
esac
case "$entry" in
[A-Za-z]:*) die "the archive holds an unsafe entry '$entry'; nothing was installed" ;;
esac
done </dev/null 2>&1; then
unzip -q "$work/server.zip" -d "$work/unpacked"
elif [ -n "$bsdtar_bin" ]; then
"$bsdtar_bin" -xf "$work/server.zip" -C "$work/unpacked"
else
python3 -I -c 'import sys, zipfile; zipfile.ZipFile(sys.argv[1]).extractall(sys.argv[2])' \
"$work/server.zip" "$work/unpacked"
fi
[ -z "$(find "$work/unpacked" -type l)" ] || die "the archive holds a symbolic link; nothing was installed"
new_program="$work/unpacked/astelyx/$program"
[ -f "$new_program" ] && [ -s "$new_program" ] \
|| die "the archive has no astelyx/$program; nothing was installed"
[ "$os" = windows ] || chmod 0755 "$new_program"
if [ "$os" = macos ]; then
# Apple silicon runs only signed code; an ad hoc signature is enough for a
# program that did not come through a browser. Checked, never changed.
codesign --verify --strict "$new_program" 2>/dev/null \
|| die "astelyx/$program carries no valid code signature, so macOS would not run it; nothing was installed"
signature=$(codesign -dv "$new_program" 2>&1 | sed -n 's/^Authority=//p' | head -n 1)
[ -n "$signature" ] || signature="ad hoc (no Developer ID)"
fi
# ---- install --------------------------------------------------------------
target="$dir/astelyx"
if [ -e "$target" ]; then
[ -d "$target" ] && { [ -f "$target/voxel-server" ] || [ -f "$target/voxel-server.exe" ]; } \
|| die "$target exists and is not an Astelyx server program; move it away or pass another --dir"
if [ "$os" != windows ] && command -v pgrep >/dev/null 2>&1 \
&& pgrep -f "$target/$program" >/dev/null 2>&1; then
die "the server in $target is running; stop it, then run this again"
fi
mv "$target" "$work/previous" \
|| die "could not move the old program aside (is the server still running?); nothing was changed"
replaced=yes
else
replaced=no
fi
if ! mv "$work/unpacked/astelyx" "$target"; then
[ "$replaced" = no ] || mv "$work/previous" "$target" || true
die "could not put the new program in place; the old one was kept"
fi
# ---- what was done --------------------------------------------------------
echo
say "installed the Astelyx dedicated server $version for $os $arch"
say " program $target/$program"
say " sha256 $sha (matches the registry)"
[ "$os" != macos ] || say " signed $signature"
if [ "$replaced" = yes ]; then
say " replaced the earlier program in $target; nothing else in $dir was touched"
else
say " nothing else was written"
fi
echo
cat <