67 lines
1.6 KiB
Bash
67 lines
1.6 KiB
Bash
#!/usr/bin/env bash
|
|
set -e
|
|
|
|
CONFIG_FILE="/app/data/client.json"
|
|
SINGBOX_PID=""
|
|
|
|
# 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 :3456 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
|
|
echo "$(date): Starting Web UI on port 3456..."
|
|
python3 /app/web/server.py &
|
|
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
|