#!/usr/bin/env bash set -e CONFIG_FILE="/app/data/client.json" SINGBOX_PID="" # Порты из ENV (по умолчанию: 3456 для веба, 8080 для прокси) PORT="${PORT:-3456}" PROXY_PORT="${PROXY_PORT:-8080}" # Ensure data directory exists mkdir -p /app/data start_singbox() { if [[ -f "$CONFIG_FILE" ]]; then echo "$(date): Starting sing-box..." sing-box run -c "$CONFIG_FILE" & SINGBOX_PID=$! echo "$(date): sing-box started with PID $SINGBOX_PID" else echo "$(date): Config file not found. Use web UI at :$PORT to apply config." SINGBOX_PID="" fi } stop_singbox() { if [[ -n "$SINGBOX_PID" ]]; then echo "$(date): Stopping sing-box (PID $SINGBOX_PID)..." kill "$SINGBOX_PID" 2>/dev/null || true wait "$SINGBOX_PID" 2>/dev/null || true SINGBOX_PID="" fi } restart_singbox() { stop_singbox start_singbox } start_singbox # Start Web UI Server with configurable port echo "$(date): Starting Web UI on port $PORT..." PORT=$PORT PROXY_PORT=$PROXY_PORT node /app/web/api/dist/main.js & WEBUI_PID=$! # HTTP Control Server (Simple Netcat loop) # Listens on 9090. # Endpoint: /reload -> Restart sing-box (used by web_server.py after config change) ( while true; do # Read the request using nc. REQ=$(echo -e "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" | nc -l -p 9090 -q 1) echo "$(date): Received request on 9090" if echo "$REQ" | grep -q "GET /reload"; then echo "$(date): Action: RELOAD (Restart sing-box)" restart_singbox else echo "$(date): Unknown request or ping." fi done ) & CONTROL_PID=$! # Keep container alive - wait for any background process echo "$(date): Entrypoint ready. Waiting for processes..." # Wait indefinitely - if WebUI dies, restart container wait $WEBUI_PID