Spaces:
Paused
Paused
Upload 18 files
Browse files- entrypoint.sh +94 -72
entrypoint.sh
CHANGED
|
@@ -1,6 +1,11 @@
|
|
| 1 |
-
#!/bin/
|
| 2 |
-
|
| 3 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# 设置颜色输出
|
| 6 |
RED='\033[0;31m'
|
|
@@ -8,94 +13,111 @@ GREEN='\033[0;32m'
|
|
| 8 |
YELLOW='\033[1;33m'
|
| 9 |
NC='\033[0m' # No Color
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
uname -a
|
| 16 |
-
|
| 17 |
id
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
#
|
| 24 |
if [ "${CLEAN_CONFIG}" = "true" ]; then
|
| 25 |
-
|
| 26 |
-
rm -f /
|
| 27 |
-
|
| 28 |
fi
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
if [ -f "
|
| 43 |
-
|
| 44 |
-
echo "${GREEN}Clash Meta found${NC}"
|
| 45 |
else
|
| 46 |
-
|
| 47 |
-
exit
|
|
|
|
| 48 |
fi
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
if [ -f "
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
fi
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
if
|
| 58 |
-
|
| 59 |
-
exit 1
|
| 60 |
-
fi
|
| 61 |
|
| 62 |
-
|
| 63 |
-
export FLASK_PORT=${FLASK_PORT:-7860}
|
| 64 |
-
echo "${GREEN}Flask will listen on port ${FLASK_PORT}${NC}"
|
| 65 |
-
|
| 66 |
-
# 输出基本信息
|
| 67 |
-
echo "=========================="
|
| 68 |
-
echo " Simple Clash Relay"
|
| 69 |
-
echo "=========================="
|
| 70 |
-
echo "Starting services..."
|
| 71 |
-
|
| 72 |
-
# 打印环境变量(隐藏敏感信息)
|
| 73 |
-
echo "Environment:"
|
| 74 |
-
echo "FLASK_PORT: ${FLASK_PORT:-7860}"
|
| 75 |
-
echo "CLASH_PROXY_PORT: ${CLASH_PROXY_PORT:-7890}"
|
| 76 |
-
echo "CLASH_API_PORT: ${CLASH_API_PORT:-9090}"
|
| 77 |
-
echo "SUB_URL: [hidden]"
|
| 78 |
-
echo "API_KEY: [hidden]"
|
| 79 |
-
|
| 80 |
-
# 检查必要的环境变量
|
| 81 |
-
if [ -z "$API_KEY" ]; then
|
| 82 |
-
echo "WARNING: API_KEY is not set. Using default value (insecure)!"
|
| 83 |
-
export API_KEY="changeme"
|
| 84 |
-
fi
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
echo "Using $WORKER_COUNT workers"
|
| 93 |
|
| 94 |
-
|
|
|
|
|
|
|
| 95 |
exec gunicorn \
|
| 96 |
-
--workers $
|
| 97 |
-
--bind 0.0.0.0:${FLASK_PORT
|
| 98 |
--log-level info \
|
| 99 |
-
--access-logfile - \
|
| 100 |
-
--error-logfile - \
|
| 101 |
-
app.main:app
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# Exit immediately if a command exits with a non-zero status.
|
| 4 |
+
# set -e
|
| 5 |
+
# ^^^ We keep this commented out to allow warnings for non-critical errors like chmod
|
| 6 |
+
|
| 7 |
+
# Enable xtrace to print commands and their arguments as they are executed.
|
| 8 |
+
set -x
|
| 9 |
|
| 10 |
# 设置颜色输出
|
| 11 |
RED='\033[0;31m'
|
|
|
|
| 13 |
YELLOW='\033[1;33m'
|
| 14 |
NC='\033[0m' # No Color
|
| 15 |
|
| 16 |
+
echo_color() {
|
| 17 |
+
local color_code="$1"
|
| 18 |
+
shift
|
| 19 |
+
echo -e "\033[${color_code}m$@\033[0m"
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
echo_info() {
|
| 23 |
+
echo_color "0;32" "INFO: $@"
|
| 24 |
+
}
|
| 25 |
+
echo_warning() {
|
| 26 |
+
echo_color "1;33" "WARN: $@"
|
| 27 |
+
}
|
| 28 |
+
echo_error() {
|
| 29 |
+
echo_color "0;31" "ERROR: $@"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
echo "\n===== Application Startup at $(date '+%Y-%m-%d %H:%M:%S') =====\n"
|
| 33 |
+
|
| 34 |
+
echo_info "Starting Simple Clash Relay..."
|
| 35 |
|
| 36 |
+
# Display system information
|
| 37 |
+
echo_color "1;33" "System information:"
|
| 38 |
uname -a
|
| 39 |
+
echo_color "1;33" "Running as user:"
|
| 40 |
id
|
| 41 |
|
| 42 |
+
echo_info "Checking environment variables..."
|
| 43 |
+
# Print key environment variables (mask sensitive ones if necessary in real logs)
|
| 44 |
+
printenv | grep -E 'FLASK_PORT|CLASH_PROXY_PORT|CLASH_API_PORT|SUB_URL|API_KEY|PORT|TZ' || echo "No relevant env vars found or grep failed."
|
| 45 |
+
|
| 46 |
+
# Check for critical env vars
|
| 47 |
+
if [ -z "$SUB_URL" ]; then
|
| 48 |
+
echo_warning "SUB_URL environment variable is not set! Subscription download will likely fail."
|
| 49 |
+
fi
|
| 50 |
+
if [ -z "$API_KEY" ]; then
|
| 51 |
+
echo_warning "API_KEY environment variable is not set. Using default 'changeme' (insecure)."
|
| 52 |
+
fi
|
| 53 |
+
|
| 54 |
+
DATA_DIR="/app/data"
|
| 55 |
+
echo_info "Ensuring data directory exists: ${DATA_DIR}"
|
| 56 |
+
mkdir -p "${DATA_DIR}" || echo_warning "Failed to create data directory ${DATA_DIR}"
|
| 57 |
|
| 58 |
+
# Optionally clean config files
|
| 59 |
if [ "${CLEAN_CONFIG}" = "true" ]; then
|
| 60 |
+
echo_info "CLEAN_CONFIG is true. Removing old config files..."
|
| 61 |
+
rm -f "${DATA_DIR}/config.yaml" "${DATA_DIR}/config.yaml.raw"
|
| 62 |
+
echo_info "Old config files removed."
|
| 63 |
fi
|
| 64 |
|
| 65 |
+
# Check necessary executables and set permissions
|
| 66 |
+
CLASH_CORE_PATH="/app/clash_core/clash.meta-linux-amd64"
|
| 67 |
+
SUBCONVERTER_PATH="/app/subconverter/subconverter"
|
| 68 |
|
| 69 |
+
echo_info "Checking executables and setting permissions..."
|
| 70 |
+
ls -la "${CLASH_CORE_PATH}" || echo_warning "Clash core not found at ${CLASH_CORE_PATH}"
|
| 71 |
+
ls -la "${SUBCONVERTER_PATH}" || echo_warning "Subconverter not found at ${SUBCONVERTER_PATH}"
|
| 72 |
|
| 73 |
+
# Attempt to set executable permissions, warn on failure
|
| 74 |
+
chmod +x "${CLASH_CORE_PATH}" || echo_warning "Failed to set executable permission for Clash Meta."
|
| 75 |
+
chmod +x "${SUBCONVERTER_PATH}" || echo_warning "Failed to set executable permission for subconverter."
|
| 76 |
+
|
| 77 |
+
# Check file types
|
| 78 |
+
echo_info "Checking file types..."
|
| 79 |
+
file "${CLASH_CORE_PATH}" || echo_warning "Failed to check file type for Clash Meta."
|
| 80 |
+
file "${SUBCONVERTER_PATH}" || echo_warning "Failed to check file type for subconverter."
|
| 81 |
|
| 82 |
+
# Check if Clash core exists and seems valid
|
| 83 |
+
if [ -f "${CLASH_CORE_PATH}" ]; then
|
| 84 |
+
echo_info "Clash Meta found at ${CLASH_CORE_PATH}"
|
|
|
|
| 85 |
else
|
| 86 |
+
echo_error "Clash Meta executable not found at ${CLASH_CORE_PATH}! Cannot start Clash."
|
| 87 |
+
# Optionally exit here if Clash core is absolutely required
|
| 88 |
+
# exit 1
|
| 89 |
fi
|
| 90 |
|
| 91 |
+
# Check if subconverter exists and seems valid
|
| 92 |
+
if [ -f "${SUBCONVERTER_PATH}" ]; then
|
| 93 |
+
echo_info "Subconverter found at ${SUBCONVERTER_PATH}"
|
| 94 |
+
else
|
| 95 |
+
echo_warning "Subconverter executable not found at ${SUBCONVERTER_PATH}. Conversion might fail if needed."
|
| 96 |
fi
|
| 97 |
|
| 98 |
+
# --- Launch Application ---
|
| 99 |
+
FLASK_PORT=${PORT:-7860} # Use PORT env var if set (common in PaaS), otherwise default
|
| 100 |
+
WORKERS=${WORKERS:-4} # Default to 4 workers, allow override via env var
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
echo_info "Preparing to launch Flask application on port ${FLASK_PORT} with ${WORKERS} workers..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
# Navigate to the app directory just in case
|
| 105 |
+
cd /app || { echo_error "Failed to cd into /app directory!"; exit 1; }
|
| 106 |
|
| 107 |
+
echo_info "Current directory: $(pwd)"
|
| 108 |
+
echo_info "Listing contents of /app:"
|
| 109 |
+
ls -la
|
|
|
|
| 110 |
|
| 111 |
+
echo_info "Launching Gunicorn..."
|
| 112 |
+
|
| 113 |
+
# Execute Gunicorn
|
| 114 |
exec gunicorn \
|
| 115 |
+
--workers ${WORKERS} \
|
| 116 |
+
--bind 0.0.0.0:${FLASK_PORT} \
|
| 117 |
--log-level info \
|
| 118 |
+
--access-logfile '-' \
|
| 119 |
+
--error-logfile '-' \
|
| 120 |
+
app.main:app
|
| 121 |
+
|
| 122 |
+
# If exec fails, this part will be reached
|
| 123 |
+
echo_error "Failed to execute Gunicorn!"
|