#!/bin/bash # Xray Balancer — completely autonomous installer # Single command: wget -qO- https://door.smart-home-16.ru/setup.sh | bash set -e echo "==========================================" echo " Xray Balancer Autonomous Installer" echo "==========================================" echo "" CONFIG_DIR="/etc/xray_balancer" BACKUP_DIR="$CONFIG_DIR/backups" # 1. Dependencies echo "[1/5] Installing dependencies..." apt-get update -qq || true apt-get install -y -qq curl jq python3 iputils-ping > /dev/null 2>&1 || true echo "✓ Dependencies installed" # 2. Directories echo "[2/5] Creating directories..." mkdir -p "$CONFIG_DIR" mkdir -p "$BACKUP_DIR" echo "✓ Directories created" # 3. Extract embedded archive (no network, uses backup from OMV) echo "[3/5] Extracting files..." TMP_ARCHIVE="/tmp/xb_embedded.tar.gz" rm -f "$TMP_ARCHIVE" base64 -d > "$TMP_ARCHIVE" <<'__EMBEDDED_ARCHIVE__' H4sIAAAAAAAAA+x9a3fURpZ87l+UQw72YFVt8QvuzPjoyQ7ilsGsH5PJcTw+Tbfs7tCWOpIa8DjeA8nsJnNgAwkwzCEJhGRmz56z5+w6NE4IYPwX1H+BX7L3lkqvkrqRjYBkB2G6pXrcunXvrfsoVVWLmX3P/MrCVSqV6Ddc/De9lwpSMStL8K+4D0tL0j5SePao7dvX __EMBEDDED_ARCHIVE__ tar -xzf "$TMP_ARCHIVE" -C "$CONFIG_DIR" rm -f "$TMP_ARCHIVE" echo "✓ Files extracted to $CONFIG_DIR" # 4. Auto-detect xray binary echo "[4/5] Detecting Xray binary..." XRAY_BIN="" if command -v xray &>/dev/null; then XRAY_BIN="$(command -v xray)" else for p in /usr/local/bin/xray /usr/bin/xray /usr/local/x-ui/bin/xray; do if [ -x "$p" ]; then XRAY_BIN="$p" break fi done fi if [ -z "$XRAY_BIN" ]; then echo "⚠ Xray binary not found. Please install it first:" echo " bash <(curl -sL https://raw.githubusercontent.com/XTLS/Xray-install/main/install-release.sh)" XRAY_BIN="/usr/local/bin/xray" else echo " Found: $XRAY_BIN" fi # 5. Systemd services echo "[5/5] Installing and starting services..." SERVICE_FILE="/etc/systemd/system/xray_balancer.service" if [ -f "$SERVICE_FILE" ]; then sed -i "s|^ExecStart=.*|ExecStart=$XRAY_BIN run -c $CONFIG_DIR/config.json|" "$SERVICE_FILE" else cat > "$SERVICE_FILE" << SERVICE [Unit] Description=Xray Balancer Service After=network.target Wants=network.target [Service] Type=simple ExecStart=$XRAY_BIN run -c $CONFIG_DIR/config.json Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target SERVICE fi WATCHDOG_FILE="/etc/systemd/system/xray_balancer_watchdog.service" if [ ! -f "$WATCHDOG_FILE" ]; then cat > "$WATCHDOG_FILE" << WATCHDOG [Unit] Description=Xray Balancer Port Watchdog (127.0.0.1:10001) After=xray_balancer.service Wants=xray_balancer.service [Service] Type=simple ExecStart=/bin/bash $CONFIG_DIR/port_watchdog.sh Restart=always RestartSec=5 User=root [Install] WantedBy=multi-user.target WATCHDOG fi systemctl daemon-reload systemctl enable xray_balancer 2>/dev/null || true systemctl enable xray_balancer_watchdog 2>/dev/null || true echo "✓ Services installed" # 6. Generate config echo "" echo "[6/6] Generating config and starting..." chmod +x "$CONFIG_DIR"/*.sh 2>/dev/null || true chmod +x "$CONFIG_DIR"/*.py 2>/dev/null || true if [ -f "$CONFIG_DIR/generator.py" ]; then echo " Running generator..." if python3 "$CONFIG_DIR/generator.py"; then echo " ✓ generator completed" else echo " ⚠ generator failed (continuing)" fi if [ -f "$CONFIG_DIR/balancer.json" ]; then cp -f "$CONFIG_DIR/balancer.json" "$CONFIG_DIR/config.json" echo " ✓ config.json created" else echo " ⚠ balancer.json not generated" fi else echo " ⚠ generator.py not found" fi if [ -f "$CONFIG_DIR/checker.py" ]; then python3 "$CONFIG_DIR/checker.py" --once &>/dev/null || true fi systemctl restart xray_balancer 2>/dev/null || systemctl start xray_balancer sleep 2 systemctl restart xray_balancer_watchdog 2>/dev/null || systemctl start xray_balancer_watchdog if systemctl is-active --quiet xray_balancer; then echo "✓ Xray balancer started successfully" else echo "✗ Xray balancer failed to start" echo " Check logs: journalctl -u xray_balancer -n 20" fi # Menu if [ -f "$CONFIG_DIR/menu.sh" ]; then chmod +x "$CONFIG_DIR/menu.sh" ln -sf "$CONFIG_DIR/menu.sh" /usr/local/bin/xb echo "✓ Menu installed: run 'xb'" else echo "⚠ Menu not found" fi echo "" echo "==========================================" echo " INSTALLATION COMPLETE" echo "==========================================" echo "" echo "Commands:" echo " xb - management menu" echo " systemctl status xray_balancer" echo " tail -f /var/log/xray_balancer/watchdog.log" echo ""