#!/usr/bin/env bash # ─── Script di configurazione automatica RustDesk per PoggioLab ───────────── # Uso: # curl -fsSL https://rustdesk.poggiolab.net/setup.sh | sudo bash # oppure: # sudo ./setup.sh # # Supporta: Ubuntu/Debian (.deb), Fedora/RHEL/CentOS (.rpm), AppImage set -euo pipefail # ─── Colori ────────────────────────────────────────────────────────────────── RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' CYAN='\033[0;36m' NC='\033[0m' # No Color info() { echo -e "${CYAN}[*]${NC} $*"; } ok() { echo -e "${GREEN}[✓]${NC} $*"; } warn() { echo -e "${YELLOW}[!]${NC} $*"; } err() { echo -e "${RED}[✗]${NC} $*"; } # ─── Configurazione server ─────────────────────────────────────────────────── ID_SERVER="rustdesk.poggiolab.net" RELAY_SERVER="rustdesk.poggiolab.net" KEY="cyjuq5sRFHTxCH8ZIHbmjFIDiV5am44IuTBe3dITDig=" WS_SERVER="wss://rustdesk.poggiolab.net:8443" GITHUB_RELEASE="https://github.com/rustdesk/rustdesk/releases/latest/download" VERSION="1.5.0" # ─── Verifica root ─────────────────────────────────────────────────────────── if [ "$(id -u)" -ne 0 ]; then err "Questo script deve essere eseguito come root (usa sudo)" exit 1 fi # ─── Rileva il gestore pacchetti ───────────────────────────────────────────── PKG_MANAGER="" PKG_FILE="" ARCH=$(uname -m) case "$ARCH" in x86_64) ARCH_SUFFIX="x86_64" ;; aarch64) ARCH_SUFFIX="aarch64" ;; *) err "Architettura non supportata: $ARCH"; exit 1 ;; esac # ─── Installa curl se mancante ─────────────────────────────────────────────── if ! command -v curl &>/dev/null; then info "curl non trovato — lo installo..." if command -v apt-get &>/dev/null; then apt-get update -qq && apt-get install -y -qq curl elif command -v dnf &>/dev/null; then dnf install -y curl elif command -v yum &>/dev/null; then yum install -y curl elif command -v zypper &>/dev/null; then zypper install -y curl elif command -v apk &>/dev/null; then apk add curl else err "Impossibile installare curl — installalo manualmente e riprova" exit 1 fi ok "curl installato" fi if command -v apt-get &>/dev/null; then PKG_MANAGER="apt" PKG_FILE="rustdesk-${VERSION}-${ARCH_SUFFIX}.deb" elif command -v dnf &>/dev/null; then PKG_MANAGER="dnf" PKG_FILE="rustdesk-${VERSION}-0.${ARCH_SUFFIX}.rpm" elif command -v yum &>/dev/null; then PKG_MANAGER="yum" PKG_FILE="rustdesk-${VERSION}-0.${ARCH_SUFFIX}.rpm" elif command -v zypper &>/dev/null; then PKG_MANAGER="zypper" PKG_FILE="rustdesk-${VERSION}-0.${ARCH_SUFFIX}-suse.rpm" else warn "Nessun gestore pacchetti supportato trovato (apt/dnf/yum/zypper)" warn "Provo con AppImage..." PKG_MANAGER="appimage" fi # ─── Header ────────────────────────────────────────────────────────────────── echo "" echo -e "${CYAN}╔══════════════════════════════════════╗${NC}" echo -e "${CYAN}║ RustDesk — Configurazione PoggioLab ║${NC}" echo -e "${CYAN}╚══════════════════════════════════════╝${NC}" echo "" # ─── 1. Installa RustDesk ──────────────────────────────────────────────────── info "Rilevato: $PKG_MANAGER ($ARCH)" if [ "$PKG_MANAGER" = "appimage" ]; then # ── AppImage fallback ── info "Scarico AppImage..." APPIMAGE_PATH="/opt/rustdesk/rustdesk.AppImage" mkdir -p /opt/rustdesk curl -fsSL "${GITHUB_RELEASE}/rustdesk-${VERSION}-${ARCH_SUFFIX}.AppImage" -o "$APPIMAGE_PATH" chmod +x "$APPIMAGE_PATH" ok "AppImage scaricato: $APPIMAGE_PATH" RUSTDESK_BIN="$APPIMAGE_PATH" elif [ "$PKG_MANAGER" = "apt" ] || [ "$PKG_MANAGER" = "dnf" ] || [ "$PKG_MANAGER" = "yum" ] || [ "$PKG_MANAGER" = "zypper" ]; then # ── Pacchetto .deb / .rpm ── TMP_DIR=$(mktemp -d) trap 'rm -rf "$TMP_DIR"' EXIT PKG_PATH="${TMP_DIR}/${PKG_FILE}" info "Scarico $PKG_FILE..." curl -fsSL "${GITHUB_RELEASE}/${PKG_FILE}" -o "$PKG_PATH" ok "Pacchetto scaricato" info "Installo RustDesk..." case "$PKG_MANAGER" in apt) apt-get install -y "$PKG_PATH" 2>/dev/null || dpkg -i "$PKG_PATH" && apt-get install -f -y ;; dnf) dnf install -y "$PKG_PATH" ;; yum) yum install -y "$PKG_PATH" ;; zypper) zypper install -y "$PKG_PATH" ;; esac ok "RustDesk installato" RUSTDESK_BIN="rustdesk" fi # ─── 2. Crea configurazione ───────────────────────────────────────────────── info "Creo configurazione server..." # Percorso config: system-wide in /etc/rustdesk/ CONFIG_DIR="/etc/rustdesk" CONFIG_FILE="${CONFIG_DIR}/config.toml" mkdir -p "$CONFIG_DIR" cat > "$CONFIG_FILE" << EOF # ─── Configurazione RustDesk per PoggioLab ────────────────────────────────── # Generato automaticamente il $(date '+%Y-%m-%d %H:%M') # Server ID (hbbs) — registrazione ID dispositivo id-server = "${ID_SERVER}" # Server Relay (hbbr) — relay per connessioni relay-server = "${RELAY_SERVER}" # Chiave pubblica del server (sicurezza) key = "${KEY}" # Porte server id-server-port = "21116" relay-server-port = "21117" # ─── WebSocket Secure (WSS) ───────────────────────────────────────────────── use-websocket = true websocket-server = "${WS_SERVER}" # ─── Opzioni ──────────────────────────────────────────────────────────────── direct-server = false approve-mode = "click" EOF ok "Configurazione salvata: $CONFIG_FILE" # ─── 3. Config per utente corrente (se non root, ma qui siamo root) ────────── # Copia anche nella home dell'utente che ha invocato sudo (se diversa da root) SUDO_USER_HOME="" if [ -n "${SUDO_USER:-}" ] && [ "${SUDO_USER}" != "root" ]; then SUDO_USER_HOME=$(getent passwd "$SUDO_USER" | cut -d: -f6) fi if [ -n "$SUDO_USER_HOME" ] && [ -d "$SUDO_USER_HOME" ]; then USER_CONFIG_DIR="${SUDO_USER_HOME}/.config/RustDesk/config" mkdir -p "$USER_CONFIG_DIR" cp "$CONFIG_FILE" "${USER_CONFIG_DIR}/config.toml" chown -R "${SUDO_USER}:${SUDO_USER}" "${SUDO_USER_HOME}/.config/RustDesk" ok "Configurazione copiata anche in: ${USER_CONFIG_DIR}/config.toml" fi # ─── 4. Abilita e avvia il servizio (se systemd è disponibile) ─────────────── if command -v systemctl &>/dev/null; then if systemctl list-unit-files 2>/dev/null | grep -q rustdesk; then info "Abilito servizio RustDesk..." systemctl enable rustdesk 2>/dev/null || true systemctl restart rustdesk 2>/dev/null || true ok "Servizio RustDesk abilitato e avviato" fi fi # ─── 5. Firewall: verifica porte aperte ───────────────────────────────────── info "Verifico porte firewall..." if command -v firewall-cmd &>/dev/null && systemctl is-active --quiet firewalld; then for port in 21115 21116 21117 21118 21119; do firewall-cmd --permanent --add-port=${port}/tcp 2>/dev/null || true done firewall-cmd --permanent --add-port=21116/udp 2>/dev/null || true firewall-cmd --permanent --add-port=8443/tcp 2>/dev/null || true firewall-cmd --reload 2>/dev/null || true ok "Porte firewall configurate" elif command -v ufw &>/dev/null && ufw status 2>/dev/null | grep -q active; then for port in 21115 21116 21117 21118 21119 8443; do ufw allow ${port}/tcp 2>/dev/null || true done ufw allow 21116/udp 2>/dev/null || true ok "Porte firewall configurate (ufw)" fi # ─── Riepilogo ─────────────────────────────────────────────────────────────── echo "" echo -e "${GREEN}╔══════════════════════════════════════╗${NC}" echo -e "${GREEN}║ Configurazione completata! ✓ ║${NC}" echo -e "${GREEN}╚══════════════════════════════════════╝${NC}" echo "" echo -e " ${CYAN}ID Server:${NC} ${ID_SERVER}" echo -e " ${CYAN}Relay:${NC} ${RELAY_SERVER}" echo -e " ${CYAN}WebSocket:${NC} ${WS_SERVER}" echo -e " ${CYAN}Key:${NC} ${KEY}" echo "" ok "RustDesk è pronto per connettersi ai PC di PoggioLab!" # ─── Avvia RustDesk se ambiente grafico disponibile ───────────────────────── if [ -n "${DISPLAY:-}" ] || [ -n "${WAYLAND_DISPLAY:-}" ]; then info "Avvio RustDesk..." if [ -n "${SUDO_USER:-}" ] && [ "${SUDO_USER}" != "root" ]; then su - "${SUDO_USER}" -c "nohup ${RUSTDESK_BIN} &>/dev/null &" 2>/dev/null || true else nohup "${RUSTDESK_BIN}" &>/dev/null & 2>/dev/null || true fi else info "Nessun display grafico rilevato — avvia RustDesk manualmente dal menu applicazioni." fi echo ""