From c1ae9da13b0e0fbf1b1350d8d215dbb8560f912c Mon Sep 17 00:00:00 2001 From: Andrei Smaliak Date: Sun, 15 Mar 2026 07:37:07 +0400 Subject: [PATCH] Refactor reverse tunnels installer --- .../stage_reverse_tunnels_install.sh | 251 +++++++++++------- 1 file changed, 160 insertions(+), 91 deletions(-) mode change 100755 => 100644 stage_reverse_tunnels/stage_reverse_tunnels_install.sh diff --git a/stage_reverse_tunnels/stage_reverse_tunnels_install.sh b/stage_reverse_tunnels/stage_reverse_tunnels_install.sh old mode 100755 new mode 100644 index 5a3f0e1..b4c66fc --- a/stage_reverse_tunnels/stage_reverse_tunnels_install.sh +++ b/stage_reverse_tunnels/stage_reverse_tunnels_install.sh @@ -1,11 +1,11 @@ #!/usr/bin/env bash # ============================================================= -# IOMATEX Stage4 — Reverse SSH Tunnels (production) +# IOMATEX Stage4 Reverse SSH Tunnels (production) # - 3 independent tunnels: Cockpit / Zigbee2MQTT / WebUI -# - services ENABLED but NOT started (start after reboot) +# - services are ENABLED but NOT started # - autossh + systemd restart for resilience # - ServerAlive keepalive for network/IP changes -# - prints one-line VPS command (user + key + fail2ban allow) +# - prints one-line VPS bootstrap command # ============================================================= set -euo pipefail @@ -13,18 +13,6 @@ export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" export DEBIAN_FRONTEND=noninteractive LOG="/var/log/iomatex-stage4-reverse-tunnel-install.log" -mkdir -p /var/log >/dev/null 2>&1 || true -touch "$LOG" >/dev/null 2>&1 || true -exec > >(tee -a "$LOG") 2>&1 - -log(){ echo "[$(date -u +'%F %T UTC')] $*"; } - -need_root(){ - if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then - echo "Run as root: sudo bash $0" - exit 1 - fi -} EDGE_USER="${EDGE_USER:-iomatex}" @@ -38,31 +26,72 @@ WEBUI_LOCAL_PORT="${WEBUI_LOCAL_PORT:-8099}" PORT_BASE="${PORT_BASE:-20000}" PORT_SLOTS="${PORT_SLOTS:-4000}" -ensure_autossh(){ - if ! command -v autossh >/dev/null 2>&1; then - log "Installing autossh..." - apt-get update -y - apt-get install -y autossh +WRAPPER_DIR="/usr/local/sbin" +SYSTEMD_DIR="/etc/systemd/system" + +mkdir -p /var/log >/dev/null 2>&1 || true +touch "$LOG" >/dev/null 2>&1 || true +exec > >(tee -a "$LOG") 2>&1 + +log() { + echo "[$(date -u +'%F %T UTC')] $*" +} + +die() { + log "ERROR: $*" + exit 1 +} + +require_root() { + if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then + echo "Run as root: sudo bash $0" >&2 + exit 1 fi } -compute_ports(){ - local hn="$1" - local crc slot cockpit z2m webui - crc="$(cksum <<<"$hn" | awk '{print $1}')" - slot="$(( crc % PORT_SLOTS ))" - cockpit="$(( PORT_BASE + slot*3 + 0 ))" - z2m="$(( PORT_BASE + slot*3 + 1 ))" - webui="$(( PORT_BASE + slot*3 + 2 ))" - echo "$cockpit $z2m $webui" +require_cmd() { + local cmd="$1" + command -v "$cmd" >/dev/null 2>&1 || die "Required command not found: $cmd" } -ensure_key(){ +preflight_checks() { + require_cmd id + require_cmd cksum + require_cmd awk + require_cmd getent + require_cmd runuser + require_cmd ssh-keygen + require_cmd systemctl +} + +ensure_autossh_installed() { + if command -v autossh >/dev/null 2>&1; then + return 0 + fi + require_cmd apt-get + log "Installing autossh..." + apt-get update -y + apt-get install -y autossh +} + +compute_ports() { + local hostname_value="$1" + local crc slot cockpit_port z2m_port webui_port + + crc="$(cksum <<<"$hostname_value" | awk '{print $1}')" + slot="$(( crc % PORT_SLOTS ))" + cockpit_port="$(( PORT_BASE + slot * 3 + 0 ))" + z2m_port="$(( PORT_BASE + slot * 3 + 1 ))" + webui_port="$(( PORT_BASE + slot * 3 + 2 ))" + + echo "$cockpit_port $z2m_port $webui_port" +} + +ensure_key() { # IMPORTANT: # - stdout: ONLY "edge_home|key|keypub" - # - any info/errors: stderr - - local hn="$1" + # - informational messages: stderr only + local hostname_value="$1" local edge_home ssh_dir key keypub if ! id -u "$EDGE_USER" >/dev/null 2>&1; then @@ -77,14 +106,14 @@ ensure_key(){ fi ssh_dir="${edge_home}/.ssh" - key="${ssh_dir}/iomatex-${hn}" + key="${ssh_dir}/iomatex-${hostname_value}" keypub="${key}.pub" install -d -m 700 -o "$EDGE_USER" -g "$EDGE_USER" "$ssh_dir" if [[ ! -f "$key" || ! -f "$keypub" ]]; then echo "Generating SSH key for ${EDGE_USER}..." >&2 - runuser -u "$EDGE_USER" -- ssh-keygen -t ed25519 -N "" -f "$key" -C "$hn" >/dev/null + runuser -u "$EDGE_USER" -- ssh-keygen -t ed25519 -N "" -f "$key" -C "$hostname_value" >/dev/null else echo "SSH key already exists: $key" >&2 fi @@ -92,41 +121,42 @@ ensure_key(){ chmod 600 "$key" 2>/dev/null || true chmod 644 "$keypub" 2>/dev/null || true chown "$EDGE_USER:$EDGE_USER" "$key" "$keypub" 2>/dev/null || true - runuser -u "$EDGE_USER" -- bash -lc "touch '$ssh_dir/known_hosts' && chmod 600 '$ssh_dir/known_hosts'" >/dev/null 2>&1 || true echo "$edge_home|$key|$keypub" } -write_wrapper(){ +write_wrapper() { local name="$1" local local_port="$2" local remote_port="$3" local vps_user="$4" local key="$5" local edge_home="$6" - local wrapper="/usr/local/sbin/iomatex-rt-${name}.sh" + local wrapper_path="${WRAPPER_DIR}/iomatex-rt-${name}.sh" - cat > "$wrapper" <