refactor: реорганизация структуры проекта на логические папки
- Созданы директории: docker/, scripts/, config/ - Перемещены файлы Docker (Dockerfile, entrypoint.sh) в docker/ - Перемещены утилитарные скрипты в scripts/ - Шаблон конфигурации перенесен в config/ - Веб-сервер перемещен в web/ и переименован в server.py - Обновлены пути в docker-compose.yml, Dockerfile и entrypoint.sh
This commit is contained in:
19
docker/Dockerfile.singbox
Normal file
19
docker/Dockerfile.singbox
Normal file
@@ -0,0 +1,19 @@
|
||||
FROM alpine:3.20
|
||||
ARG SINGBOX_VER=1.8.10
|
||||
ARG VLESS_URL
|
||||
RUN apk add --no-cache curl ca-certificates tar jq bash coreutils netcat-openbsd python3 && update-ca-certificates \
|
||||
&& curl -L -o /tmp/sb.tar.gz https://github.com/SagerNet/sing-box/releases/download/v${SINGBOX_VER}/sing-box-${SINGBOX_VER}-linux-amd64.tar.gz \
|
||||
&& tar -xf /tmp/sb.tar.gz -C /tmp \
|
||||
&& mv /tmp/sing-box-${SINGBOX_VER}-linux-amd64/sing-box /usr/local/bin/sing-box \
|
||||
&& chmod +x /usr/local/bin/sing-box \
|
||||
&& adduser -D -u 1000 suser
|
||||
COPY --chown=suser:suser config/client.template.json /app/
|
||||
COPY --chown=suser:suser scripts/gen-client-from-url.sh scripts/menu.sh /app/
|
||||
COPY --chown=suser:suser docker/entrypoint.sh /app/
|
||||
COPY --chown=suser:suser web/ /app/web/
|
||||
RUN chmod +x /app/gen-client-from-url.sh /app/entrypoint.sh /app/menu.sh
|
||||
|
||||
ENV VLESS_URL=$VLESS_URL
|
||||
|
||||
EXPOSE 8082 9090 3456
|
||||
ENTRYPOINT ["/app/entrypoint.sh"]
|
||||
106
docker/entrypoint.sh
Normal file
106
docker/entrypoint.sh
Normal file
@@ -0,0 +1,106 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
# Default update interval: 60 minutes
|
||||
UPDATE_INTERVAL=${UPDATE_INTERVAL:-60}
|
||||
CONFIG_FILE="/app/data/client.json"
|
||||
SINGBOX_PID=""
|
||||
|
||||
# Ensure data directory exists
|
||||
mkdir -p /app/data
|
||||
|
||||
# Function to generate config
|
||||
generate_config() {
|
||||
echo "$(date): Generating config..."
|
||||
if ./gen-client-from-url.sh "$VLESS_URL" "$CONFIG_FILE"; then
|
||||
echo "$(date): Config generated successfully."
|
||||
return 0
|
||||
else
|
||||
echo "$(date): Error generating config."
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
# Initial generation (if URL provided)
|
||||
if [[ -n "$VLESS_URL" ]]; then
|
||||
generate_config
|
||||
fi
|
||||
|
||||
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.
|
||||
# Endpoints:
|
||||
# /update -> Regenerate from ENV (VLESS_URL) & Restart
|
||||
# /reload -> Just Restart (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 /update"; then
|
||||
echo "$(date): Action: UPDATE (Regen from ENV + Restart)"
|
||||
if generate_config; then
|
||||
restart_singbox
|
||||
fi
|
||||
elif echo "$REQ" | grep -q "GET /reload"; then
|
||||
echo "$(date): Action: RELOAD (Restart only)"
|
||||
restart_singbox
|
||||
else
|
||||
echo "$(date): Unknown request or ping."
|
||||
fi
|
||||
done
|
||||
) &
|
||||
CONTROL_PID=$!
|
||||
|
||||
# Periodic Update Loop (only if VLESS_URL is set)
|
||||
if [[ -n "$VLESS_URL" ]]; then
|
||||
(
|
||||
while true; do
|
||||
sleep "$((UPDATE_INTERVAL * 60))"
|
||||
echo "$(date): Checking for periodic update..."
|
||||
if generate_config; then
|
||||
restart_singbox
|
||||
fi
|
||||
done
|
||||
) &
|
||||
UPDATE_PID=$!
|
||||
fi
|
||||
|
||||
# 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
|
||||
Reference in New Issue
Block a user