#!/usr/bin/env bash # Enzonic Tunnel relay installer. # # curl -fsSL https://launcher.enzonic.com/tunnel/install.sh | sudo bash # # Re-running upgrades the binary in place and keeps config and data. # # Environment overrides: # ENZ_TUNNEL_BINARY_URL download this prebuilt linux binary instead of building # ENZ_TUNNEL_SRC_DIR build from this local directory (the tunnel/ module or the repo root) # ENZ_TUNNEL_SRC_URL build from this source tarball instead of downloading binaries # ENZ_TUNNEL_DIST_URL base URL of prebuilt binaries + SHA256SUMS # (default: https://launcher.enzonic.com/tunnel/bin) # ENZ_TUNNEL_DOMAIN DNS name of this relay (default on a fresh install: selfhosted.enzonic.dev). # Becomes the public host, gets an automatic Let's Encrypt # certificate for the control port, and turns on hostname # routing: players join . on TCP 25565. # Needs DNS records and *. -> this host. # Set it to an empty value (ENZ_TUNNEL_DOMAIN=) for IP-only mode. # On a re-run, unset keeps the existing config. # ENZ_TUNNEL_PUBLIC_HOST public host name / IP advertised to clients (default: the domain, # or the detected IP in IP-only mode). A DNS name also turns on ACME. # ENZ_TUNNEL_ROUTE_DOMAIN hostname routing domain (default: ENZ_TUNNEL_DOMAIN) # ENZ_TUNNEL_MC_LISTEN hostname-routed player listener (default :25565) # ENZ_TUNNEL_PUBLIC_IP IP printed in the DNS instructions (default: auto-detect) # ENZ_TUNNEL_ACME_EMAIL ACME contact email (default admin@enzonic.com) # ENZ_TUNNEL_ACME_HTTP HTTP-01 challenge listen address (default :80) # ENZ_TUNNEL_ACME_CACHE ACME cache directory (default /var/lib/enzonic-tunnel/acme) # ENZ_TUNNEL_TLS_CERT/ENZ_TUNNEL_TLS_KEY static PEM certificate/key instead of ACME # ENZ_TUNNEL_LISTEN control listen address (default :7000) # ENZ_TUNNEL_PORT_MIN first public port (default 25600) # ENZ_TUNNEL_PORT_MAX last public port (default 25999) # ENZ_TUNNEL_MAX_IPS player IPs per tunnel at once (default 3) # ENZ_TUNNEL_BUILD_VERSION version string baked into a source build # ENZ_NO_SYSTEMD=1 install everything but do not touch systemd (containers, CI) # ENZ_NO_FIREWALL=1 do not change ufw / firewalld rules set -euo pipefail DIST_URL_DEFAULT="https://launcher.enzonic.com/tunnel/bin" SVC_USER="enzonic-tunnel" BIN_DIR="/usr/local/bin" CONF_DIR="/etc/enzonic-tunnel" CONF_FILE="$CONF_DIR/config.env" DATA_DIR="/var/lib/enzonic-tunnel" CACHE_DIR="/var/cache/enzonic-tunnel" UNIT_FILE="/etc/systemd/system/enzonic-tunnel.service" GO_ROOT="/usr/local/go" if [ -t 1 ]; then C_RESET=$'\033[0m'; C_BOLD=$'\033[1m'; C_GREEN=$'\033[32m'; C_YELLOW=$'\033[33m'; C_RED=$'\033[31m'; C_DIM=$'\033[2m' else C_RESET=""; C_BOLD=""; C_GREEN=""; C_YELLOW=""; C_RED=""; C_DIM="" fi step() { printf '%s==>%s %s%s%s\n' "$C_GREEN" "$C_RESET" "$C_BOLD" "$*" "$C_RESET"; } info() { printf ' %s%s%s\n' "$C_DIM" "$*" "$C_RESET"; } warn() { printf '%s!! %s%s\n' "$C_YELLOW" "$*" "$C_RESET" >&2; } die() { printf '%sxx %s%s\n' "$C_RED" "$*" "$C_RESET" >&2; exit 1; } WORK_DIR="" cleanup() { if [ -n "$WORK_DIR" ]; then rm -rf "$WORK_DIR"; fi; } trap cleanup EXIT trap 'die "installer failed at line $LINENO"' ERR [ "$(id -u)" -eq 0 ] || die "run as root (pipe into: sudo bash)" [ "$(uname -s)" = "Linux" ] || die "the relay installer supports Linux only" # ---------------------------------------------------------------- platform case "$(uname -m)" in x86_64 | amd64) ARCH=amd64 ;; aarch64 | arm64) ARCH=arm64 ;; *) die "unsupported CPU architecture $(uname -m) (need x86_64 or aarch64)" ;; esac DISTRO_ID=unknown DISTRO_LIKE="" DISTRO_NAME="Linux" if [ -r /etc/os-release ]; then # shellcheck disable=SC1091 . /etc/os-release DISTRO_ID="${ID:-unknown}" DISTRO_LIKE="${ID_LIKE:-}" DISTRO_NAME="${PRETTY_NAME:-Linux}" fi step "Installing Enzonic Tunnel relay on $DISTRO_NAME ($ARCH)" USE_SYSTEMD=1 if [ "${ENZ_NO_SYSTEMD:-0}" = "1" ]; then USE_SYSTEMD=0 info "ENZ_NO_SYSTEMD=1: systemd will not be touched" elif ! command -v systemctl >/dev/null 2>&1 || [ ! -d /run/systemd/system ]; then USE_SYSTEMD=0 warn "systemd is not running on this host; the service will be installed but not started" fi # ---------------------------------------------------------------- packages pkg_install() { if command -v apt-get >/dev/null 2>&1; then DEBIAN_FRONTEND=noninteractive apt-get update -qq DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends "$@" >/dev/null elif command -v dnf >/dev/null 2>&1; then dnf install -y -q "$@" elif command -v yum >/dev/null 2>&1; then yum install -y -q "$@" elif command -v zypper >/dev/null 2>&1; then zypper --non-interactive --quiet install "$@" elif command -v pacman >/dev/null 2>&1; then pacman -Sy --noconfirm --needed "$@" >/dev/null elif command -v apk >/dev/null 2>&1; then apk add --no-cache "$@" >/dev/null else die "no supported package manager found; install manually: $*" fi } need=() command -v curl >/dev/null 2>&1 || need+=(curl) command -v tar >/dev/null 2>&1 || need+=(tar) command -v gzip >/dev/null 2>&1 || need+=(gzip) [ -e /etc/ssl/certs/ca-certificates.crt ] || [ -e /etc/pki/tls/certs/ca-bundle.crt ] || need+=(ca-certificates) if ! command -v useradd >/dev/null 2>&1 && ! command -v adduser >/dev/null 2>&1; then case "$DISTRO_ID $DISTRO_LIKE" in *alpine*) need+=(shadow) ;; *) need+=(passwd) ;; esac fi if [ "${#need[@]}" -gt 0 ]; then step "Installing packages: ${need[*]}" pkg_install "${need[@]}" fi fetch() { curl -fsSL --retry 3 --retry-delay 2 --connect-timeout 15 "$@"; } # ---------------------------------------------------------------- binary WORK_DIR="$(mktemp -d /tmp/enzonic-tunnel-install.XXXXXX)" NEW_BIN="$WORK_DIR/enzonic-tunnel-server" NEW_CLI="" go_version_of() { "$1" env GOVERSION 2>/dev/null | sed 's/^go//'; } version_ge() { [ "$(printf '%s\n%s\n' "$2" "$1" | sort -V | head -n1)" = "$2" ]; } ensure_go() { local required="$1" have="" latest file sum if [ -x "$GO_ROOT/bin/go" ]; then have="$(go_version_of "$GO_ROOT/bin/go")"; fi if [ -n "$have" ] && version_ge "$have" "$required"; then info "using Go $have from $GO_ROOT" GO_BIN="$GO_ROOT/bin/go" return fi if command -v go >/dev/null 2>&1; then have="$(go_version_of "$(command -v go)")" if [ -n "$have" ] && version_ge "$have" "$required"; then info "using Go $have from $(command -v go)" GO_BIN="$(command -v go)" return fi fi latest="$(fetch 'https://go.dev/VERSION?m=text' | head -n1)" case "$latest" in go1.*) ;; *) die "could not determine the latest Go release (got '$latest')" ;; esac version_ge "${latest#go}" "$required" || die "latest Go ${latest#go} is older than required $required" file="$latest.linux-$ARCH.tar.gz" step "Installing Go ${latest#go} to $GO_ROOT" sum="$(fetch 'https://go.dev/dl/?mode=json' | grep -A6 "\"filename\": \"$file\"" | sed -n 's/.*"sha256": "\([0-9a-f]\{64\}\)".*/\1/p' | head -n1)" [ -n "$sum" ] || die "no published sha256 for $file" fetch -o "$WORK_DIR/$file" "https://go.dev/dl/$file" echo "$sum $WORK_DIR/$file" | sha256sum -c --quiet - || die "checksum mismatch for $file" rm -rf "$GO_ROOT" tar -C /usr/local -xzf "$WORK_DIR/$file" GO_BIN="$GO_ROOT/bin/go" info "installed $("$GO_BIN" version)" } find_module() { # Prints the directory holding the tunnel module's go.mod under $1. local root="$1" mod for mod in "$root/go.mod" "$root/tunnel/go.mod"; do if [ -f "$mod" ] && grep -q '^module github.com/enzonic/launcher/tunnel$' "$mod"; then dirname "$mod" return fi done mod="$(find "$root" -maxdepth 3 -path '*/tunnel/go.mod' -print -quit)" [ -n "$mod" ] || die "no tunnel/go.mod found in $root" dirname "$mod" } build_from_source() { local src="$1" required version required="$(sed -n 's/^go \([0-9.]*\).*/\1/p' "$src/go.mod" | head -n1)" [ -n "$required" ] || die "cannot read Go version from $src/go.mod" ensure_go "$required" version="${ENZ_TUNNEL_BUILD_VERSION:-src-$(date -u +%Y%m%d%H%M)}" step "Building enzonic-tunnel $version" mkdir -p "$CACHE_DIR" ( cd "$src" export CGO_ENABLED=0 GOFLAGS=-mod=readonly GOTOOLCHAIN=local \ GOPATH="$CACHE_DIR/gopath" GOCACHE="$CACHE_DIR/gocache" GOMODCACHE="$CACHE_DIR/gomod" "$GO_BIN" build -trimpath -ldflags "-s -w -X main.version=$version" -o "$NEW_BIN" ./cmd/enzonic-tunnel-server "$GO_BIN" build -trimpath -ldflags "-s -w -X main.version=$version" -o "$WORK_DIR/enzonic-tunnel" ./cmd/enzonic-tunnel ) NEW_CLI="$WORK_DIR/enzonic-tunnel" } if [ -n "${ENZ_TUNNEL_BINARY_URL:-}" ]; then step "Downloading relay binary" info "$ENZ_TUNNEL_BINARY_URL" fetch -o "$NEW_BIN" "$ENZ_TUNNEL_BINARY_URL" chmod 0755 "$NEW_BIN" elif [ -n "${ENZ_TUNNEL_SRC_DIR:-}" ]; then [ -d "$ENZ_TUNNEL_SRC_DIR" ] || die "ENZ_TUNNEL_SRC_DIR=$ENZ_TUNNEL_SRC_DIR is not a directory" src="$(find_module "$ENZ_TUNNEL_SRC_DIR")" info "source: $src" # Build from a copy so a read-only mount works and the source tree is never modified. mkdir -p "$WORK_DIR/src" cp -R "$src/." "$WORK_DIR/src/" rm -rf "$WORK_DIR/src/dist" build_from_source "$WORK_DIR/src" elif [ -n "${ENZ_TUNNEL_SRC_URL:-}" ]; then step "Downloading source" info "$ENZ_TUNNEL_SRC_URL" mkdir -p "$WORK_DIR/src" fetch "$ENZ_TUNNEL_SRC_URL" | tar -xz -C "$WORK_DIR/src" build_from_source "$(find_module "$WORK_DIR/src")" else dist="${ENZ_TUNNEL_DIST_URL:-$DIST_URL_DEFAULT}" step "Downloading prebuilt binaries" info "$dist" fetch -o "$WORK_DIR/SHA256SUMS" "$dist/SHA256SUMS" for name in "enzonic-tunnel-server-linux-$ARCH" "enzonic-tunnel-linux-$ARCH"; do fetch -o "$WORK_DIR/$name" "$dist/$name" sum="$(awk -v f="$name" '{n=$2; sub(/^\*/, "", n)} n == f {print $1}' "$WORK_DIR/SHA256SUMS")" [ -n "$sum" ] || die "$name is not listed in $dist/SHA256SUMS" echo "$sum $WORK_DIR/$name" | sha256sum -c --quiet - || die "checksum mismatch for $name" done mv "$WORK_DIR/enzonic-tunnel-server-linux-$ARCH" "$NEW_BIN" mv "$WORK_DIR/enzonic-tunnel-linux-$ARCH" "$WORK_DIR/enzonic-tunnel" chmod 0755 "$NEW_BIN" "$WORK_DIR/enzonic-tunnel" NEW_CLI="$WORK_DIR/enzonic-tunnel" fi "$NEW_BIN" -version >/dev/null 2>&1 || die "the new binary does not run on this host ($(file -b "$NEW_BIN" 2>/dev/null || echo unknown format))" # ---------------------------------------------------------------- user & dirs if ! id "$SVC_USER" >/dev/null 2>&1; then step "Creating system user $SVC_USER" nologin="$(command -v nologin || echo /bin/false)" if command -v useradd >/dev/null 2>&1; then useradd --system --user-group --no-create-home --home-dir "$DATA_DIR" --shell "$nologin" "$SVC_USER" else addgroup -S "$SVC_USER" && adduser -S -D -H -h "$DATA_DIR" -s "$nologin" -G "$SVC_USER" "$SVC_USER" fi fi install -d -m 0750 -o "$SVC_USER" -g "$SVC_USER" "$DATA_DIR" install -d -m 0750 -o root -g "$SVC_USER" "$CONF_DIR" # ---------------------------------------------------------------- config detect_public_host() { local ip="" u for u in https://api.ipify.org https://ifconfig.me/ip https://icanhazip.com; do ip="$(curl -fsS --max-time 5 "$u" 2>/dev/null | tr -d '[:space:]' || true)" if printf '%s' "$ip" | grep -Eq '^([0-9]{1,3}\.){3}[0-9]{1,3}$|^[0-9a-fA-F:]+:[0-9a-fA-F:]*$'; then printf '%s' "$ip" return fi done ip="$(hostname -I 2>/dev/null | awk '{print $1}' || true)" [ -n "$ip" ] && printf '%s' "$ip" } is_ip() { printf '%s' "$1" | grep -Eq '^([0-9]{1,3}\.){3}[0-9]{1,3}$|^\[?[0-9a-fA-F:]+:[0-9a-fA-F:]*\]?$'; } is_dns_name() { ! is_ip "$1" && printf '%s' "$1" | grep -Eq '^([A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z][A-Za-z0-9-]{0,61}[A-Za-z0-9]$'; } DEFAULT_DOMAIN="selfhosted.enzonic.dev" # A fresh install defaults to the Enzonic domain. "${VAR-default}" (no colon) keeps an # explicitly empty ENZ_TUNNEL_DOMAIN= as "IP-only mode". An explicit public host or a # static certificate also means "not the default domain". Re-runs keep the existing config. if [ ! -f "$CONF_FILE" ] && [ -z "${ENZ_TUNNEL_PUBLIC_HOST:-}" ] && [ -z "${ENZ_TUNNEL_TLS_CERT:-}${ENZ_TUNNEL_TLS_KEY:-}" ]; then ENZ_TUNNEL_DOMAIN="${ENZ_TUNNEL_DOMAIN-$DEFAULT_DOMAIN}" fi if [ -n "${ENZ_TUNNEL_DOMAIN:-}" ]; then is_dns_name "$ENZ_TUNNEL_DOMAIN" || die "ENZ_TUNNEL_DOMAIN=$ENZ_TUNNEL_DOMAIN is not a DNS name (expected e.g. $DEFAULT_DOMAIN)" ENZ_TUNNEL_PUBLIC_HOST="$ENZ_TUNNEL_DOMAIN" ENZ_TUNNEL_ROUTE_DOMAIN="${ENZ_TUNNEL_ROUTE_DOMAIN:-$ENZ_TUNNEL_DOMAIN}" fi if [ -n "${ENZ_TUNNEL_ROUTE_DOMAIN:-}" ]; then is_dns_name "$ENZ_TUNNEL_ROUTE_DOMAIN" || die "ENZ_TUNNEL_ROUTE_DOMAIN=$ENZ_TUNNEL_ROUTE_DOMAIN is not a DNS name" fi if [ -n "${ENZ_TUNNEL_TLS_CERT:-}${ENZ_TUNNEL_TLS_KEY:-}" ]; then [ -n "${ENZ_TUNNEL_TLS_CERT:-}" ] && [ -n "${ENZ_TUNNEL_TLS_KEY:-}" ] || die "set both ENZ_TUNNEL_TLS_CERT and ENZ_TUNNEL_TLS_KEY" [ -z "${ENZ_TUNNEL_DOMAIN:-}" ] || die "ENZ_TUNNEL_DOMAIN (automatic TLS) and ENZ_TUNNEL_TLS_CERT/KEY (static TLS) are mutually exclusive; set only one" fi set_conf() { # key value: replace or append in config.env local key="$1" val="$2" if grep -q "^$key=" "$CONF_FILE"; then sed -i "s|^$key=.*|$key=$val|" "$CONF_FILE" else printf '%s=%s\n' "$key" "$val" >>"$CONF_FILE" fi } if [ ! -f "$CONF_FILE" ]; then step "Writing $CONF_FILE" public_host="${ENZ_TUNNEL_PUBLIC_HOST:-$(detect_public_host || true)}" [ -n "$public_host" ] || die "could not detect the public IP; re-run with ENZ_TUNNEL_PUBLIC_HOST=your.domain" cat >"$CONF_FILE" < --public-host). # Restart after editing: systemctl restart enzonic-tunnel ENZ_TUNNEL_PUBLIC_HOST=$public_host ENZ_TUNNEL_LISTEN=${ENZ_TUNNEL_LISTEN:-:7000} ENZ_TUNNEL_ADMIN=127.0.0.1:7001 ENZ_TUNNEL_PORT_MIN=${ENZ_TUNNEL_PORT_MIN:-25600} ENZ_TUNNEL_PORT_MAX=${ENZ_TUNNEL_PORT_MAX:-25999} ENZ_TUNNEL_DB=$DATA_DIR/tunnel.db ENZ_TUNNEL_MAX_IPS=${ENZ_TUNNEL_MAX_IPS:-3} ENZ_TUNNEL_MAX_CONNS=64 ENZ_TUNNEL_MAX_TUNNELS_PER_IP=3 ENZ_TUNNEL_CREATE_RATE=5 ENZ_TUNNEL_TUNNEL_TTL=720h # Hostname routing: players join . on the Minecraft port. # Needs DNS A records for the domain and *.domain. Empty = off (port mode only). #ENZ_TUNNEL_ROUTE_DOMAIN=$DEFAULT_DOMAIN #ENZ_TUNNEL_MC_LISTEN=:25565 # Log raw client/player IPs instead of short keyed hashes: #ENZ_TUNNEL_LOG_IPS=false # TLS for the control port. Launchers connect with TLS by default. # Automatic Let's Encrypt certificate (needs a DNS A/AAAA record and TCP 80 open); # the installer sets these when the public host is a DNS name: #ENZ_TUNNEL_ACME_DOMAIN=tunnel.example.com #ENZ_TUNNEL_ACME_EMAIL=admin@enzonic.com #ENZ_TUNNEL_ACME_CACHE=$DATA_DIR/acme #ENZ_TUNNEL_ACME_HTTP=:80 # Or static PEM files (readable by the $SVC_USER user; exclusive with ACME): #ENZ_TUNNEL_TLS_CERT=$CONF_DIR/fullchain.pem #ENZ_TUNNEL_TLS_KEY=$CONF_DIR/privkey.pem EOF else step "Keeping existing $CONF_FILE" for key in ENZ_TUNNEL_PUBLIC_HOST ENZ_TUNNEL_LISTEN ENZ_TUNNEL_PORT_MIN ENZ_TUNNEL_PORT_MAX ENZ_TUNNEL_MAX_IPS; do if [ -n "${!key:-}" ]; then set_conf "$key" "${!key}" info "updated $key=${!key}" fi done fi conf_get() { sed -n "s/^$1=//p" "$CONF_FILE" | tail -n1; } conf_or() { local v; v="$(conf_get "$1")"; printf '%s' "${v:-$2}"; } # ---------------------------------------------------------------- TLS if [ -n "${ENZ_TUNNEL_TLS_CERT:-}" ]; then set_conf ENZ_TUNNEL_TLS_CERT "$ENZ_TUNNEL_TLS_CERT" set_conf ENZ_TUNNEL_TLS_KEY "$ENZ_TUNNEL_TLS_KEY" sed -i '/^ENZ_TUNNEL_ACME_DOMAIN=/d' "$CONF_FILE" info "static TLS: $ENZ_TUNNEL_TLS_CERT" fi PUBLIC_HOST="$(conf_get ENZ_TUNNEL_PUBLIC_HOST)" if [ -z "$(conf_get ENZ_TUNNEL_TLS_CERT)" ] && is_dns_name "$PUBLIC_HOST"; then # A DNS name as public host: get a certificate for it automatically. if [ "$(conf_get ENZ_TUNNEL_ACME_DOMAIN)" != "$PUBLIC_HOST" ]; then step "Enabling automatic TLS (Let's Encrypt) for $PUBLIC_HOST" fi set_conf ENZ_TUNNEL_ACME_DOMAIN "$PUBLIC_HOST" set_conf ENZ_TUNNEL_ACME_EMAIL "${ENZ_TUNNEL_ACME_EMAIL:-$(conf_or ENZ_TUNNEL_ACME_EMAIL admin@enzonic.com)}" set_conf ENZ_TUNNEL_ACME_CACHE "${ENZ_TUNNEL_ACME_CACHE:-$(conf_or ENZ_TUNNEL_ACME_CACHE "$DATA_DIR/acme")}" set_conf ENZ_TUNNEL_ACME_HTTP "${ENZ_TUNNEL_ACME_HTTP:-$(conf_or ENZ_TUNNEL_ACME_HTTP :80)}" fi # ---------------------------------------------------------------- hostname routing if [ -n "${ENZ_TUNNEL_ROUTE_DOMAIN:-}" ]; then if [ "$(conf_get ENZ_TUNNEL_ROUTE_DOMAIN)" != "$ENZ_TUNNEL_ROUTE_DOMAIN" ]; then step "Enabling hostname routing for *.$ENZ_TUNNEL_ROUTE_DOMAIN" fi set_conf ENZ_TUNNEL_ROUTE_DOMAIN "$ENZ_TUNNEL_ROUTE_DOMAIN" set_conf ENZ_TUNNEL_MC_LISTEN "${ENZ_TUNNEL_MC_LISTEN:-$(conf_or ENZ_TUNNEL_MC_LISTEN :25565)}" elif [ -n "${ENZ_TUNNEL_MC_LISTEN:-}" ]; then set_conf ENZ_TUNNEL_MC_LISTEN "$ENZ_TUNNEL_MC_LISTEN" fi ROUTE_DOMAIN="$(conf_get ENZ_TUNNEL_ROUTE_DOMAIN)" MC_PORT="" if [ -n "$ROUTE_DOMAIN" ]; then mc_listen="$(conf_or ENZ_TUNNEL_MC_LISTEN :25565)" MC_PORT="${mc_listen##*:}" case "$MC_PORT" in '' | *[!0-9]*) die "ENZ_TUNNEL_MC_LISTEN=$mc_listen has no numeric port" ;; esac # 25565 is above 1024: no extra capability is needed. A port below 1024 is refused # rather than silently granting CAP_NET_BIND_SERVICE for it. [ "$MC_PORT" -ge 1024 ] || die "ENZ_TUNNEL_MC_LISTEN port $MC_PORT is below 1024; use 25565" fi chown root:"$SVC_USER" "$CONF_FILE" chmod 0640 "$CONF_FILE" ACME_DOMAIN="$(conf_get ENZ_TUNNEL_ACME_DOMAIN)" TLS_CERT="$(conf_get ENZ_TUNNEL_TLS_CERT)" ACME_HTTP_PORT="" READ_WRITE_PATHS="$DATA_DIR" CAPS="" if [ -n "$ACME_DOMAIN" ] && [ -n "$TLS_CERT" ]; then die "$CONF_FILE sets both ENZ_TUNNEL_ACME_DOMAIN and ENZ_TUNNEL_TLS_CERT; remove one" fi if [ -n "$ACME_DOMAIN" ]; then acme_http="$(conf_or ENZ_TUNNEL_ACME_HTTP :80)" ACME_HTTP_PORT="${acme_http##*:}" case "$ACME_HTTP_PORT" in '' | *[!0-9]*) die "ENZ_TUNNEL_ACME_HTTP=$acme_http has no numeric port" ;; esac acme_cache="$(conf_or ENZ_TUNNEL_ACME_CACHE "$DATA_DIR/acme")" install -d -m 0700 -o "$SVC_USER" -g "$SVC_USER" "$acme_cache" case "$acme_cache/" in "$DATA_DIR"/*) ;; *) READ_WRITE_PATHS="$DATA_DIR $acme_cache" ;; esac # Binding below 1024 needs this one capability; nothing else is granted. if [ "$ACME_HTTP_PORT" -lt 1024 ]; then CAPS="CAP_NET_BIND_SERVICE"; fi TLS_STATUS="active (Let's Encrypt certificate for $ACME_DOMAIN, HTTP-01 on TCP $ACME_HTTP_PORT)" elif [ -n "$TLS_CERT" ]; then TLS_STATUS="active (static certificate $TLS_CERT)" else TLS_STATUS="OFF (plain TCP; launchers default to TLS and will not connect)" fi LISTEN="$(conf_get ENZ_TUNNEL_LISTEN)" CONTROL_PORT="${LISTEN##*:}" PORT_MIN="$(conf_get ENZ_TUNNEL_PORT_MIN)" PORT_MAX="$(conf_get ENZ_TUNNEL_PORT_MAX)" ADMIN="$(conf_get ENZ_TUNNEL_ADMIN)" # ---------------------------------------------------------------- install binaries step "Installing binaries to $BIN_DIR" install -m 0755 "$NEW_BIN" "$BIN_DIR/enzonic-tunnel-server.new" mv -f "$BIN_DIR/enzonic-tunnel-server.new" "$BIN_DIR/enzonic-tunnel-server" info "$BIN_DIR/enzonic-tunnel-server ($("$BIN_DIR/enzonic-tunnel-server" -version))" if [ -n "$NEW_CLI" ]; then install -m 0755 "$NEW_CLI" "$BIN_DIR/enzonic-tunnel.new" mv -f "$BIN_DIR/enzonic-tunnel.new" "$BIN_DIR/enzonic-tunnel" info "$BIN_DIR/enzonic-tunnel (client)" fi # ---------------------------------------------------------------- systemd step "Writing $UNIT_FILE" mkdir -p "$(dirname "$UNIT_FILE")" cat >"$UNIT_FILE" </dev/null 2>&1 && ufw status 2>/dev/null | grep -q "Status: active"; then step "Opening ufw ports $CONTROL_PORT and $PORT_MIN-$PORT_MAX/tcp" ufw allow "$CONTROL_PORT/tcp" comment "Enzonic Tunnel control" >/dev/null ufw allow "$PORT_MIN:$PORT_MAX/tcp" comment "Enzonic Tunnel public ports" >/dev/null if [ -n "$MC_PORT" ]; then info "opening $MC_PORT/tcp for hostname-routed players" ufw allow "$MC_PORT/tcp" comment "Enzonic Tunnel Minecraft (hostname routing)" >/dev/null fi if [ -n "$ACME_HTTP_PORT" ]; then info "opening $ACME_HTTP_PORT/tcp for Let's Encrypt HTTP-01" ufw allow "$ACME_HTTP_PORT/tcp" comment "Enzonic Tunnel ACME HTTP-01" >/dev/null fi elif command -v firewall-cmd >/dev/null 2>&1 && firewall-cmd --state >/dev/null 2>&1; then step "Opening firewalld ports $CONTROL_PORT and $PORT_MIN-$PORT_MAX/tcp" firewall-cmd --quiet --permanent --add-port="$CONTROL_PORT/tcp" firewall-cmd --quiet --permanent --add-port="$PORT_MIN-$PORT_MAX/tcp" if [ -n "$MC_PORT" ]; then info "opening $MC_PORT/tcp for hostname-routed players" firewall-cmd --quiet --permanent --add-port="$MC_PORT/tcp" fi if [ -n "$ACME_HTTP_PORT" ]; then info "opening $ACME_HTTP_PORT/tcp for Let's Encrypt HTTP-01" firewall-cmd --quiet --permanent --add-port="$ACME_HTTP_PORT/tcp" fi firewall-cmd --quiet --reload else info "no active ufw/firewalld found; make sure TCP $CONTROL_PORT${MC_PORT:+, $MC_PORT}${ACME_HTTP_PORT:+, $ACME_HTTP_PORT} and $PORT_MIN-$PORT_MAX are reachable" fi # ---------------------------------------------------------------- start HOSTNAME_MODE="off (set ENZ_TUNNEL_DOMAIN to turn it on)" [ -z "$ROUTE_DOMAIN" ] || HOSTNAME_MODE="players join .$ROUTE_DOMAIN on TCP $MC_PORT" STATUS="installed (not started)" if [ "$USE_SYSTEMD" = "1" ]; then step "Starting enzonic-tunnel.service" systemctl daemon-reload systemctl enable --quiet enzonic-tunnel.service systemctl restart enzonic-tunnel.service ok=0 for _ in $(seq 1 30); do if curl -fsS --max-time 2 "http://$ADMIN/healthz" >/dev/null 2>&1; then ok=1; break; fi sleep 0.5 done if [ "$ok" = "1" ]; then STATUS="running" else systemctl --no-pager --lines=30 status enzonic-tunnel.service >&2 || true die "the relay did not become healthy; see: journalctl -u enzonic-tunnel" fi fi cat < $PUBLIC_IP EOF if [ -n "$ROUTE_DOMAIN" ]; then echo " $RR *.$ROUTE_DOMAIN -> $PUBLIC_IP" fi if [ -n "$ACME_DOMAIN" ] && [ -n "$ROUTE_DOMAIN" ] && [ "$ACME_DOMAIN" != "$ROUTE_DOMAIN" ]; then echo " $RR $ACME_DOMAIN -> $PUBLIC_IP" fi cat < $PUBLIC_HOST" warn " 2. re-run: curl -fsSL https://launcher.enzonic.com/tunnel/install.sh | sudo ENZ_TUNNEL_DOMAIN=relay.example.com bash" warn " or use your own certificate: sudo ENZ_TUNNEL_TLS_CERT=/path/fullchain.pem ENZ_TUNNEL_TLS_KEY=/path/privkey.pem bash install.sh" fi