clash-linux commited on
Commit
149033c
·
verified ·
1 Parent(s): 12f42f6

Upload 18 files

Browse files
Files changed (2) hide show
  1. Dockerfile +5 -8
  2. entrypoint.sh +72 -94
Dockerfile CHANGED
@@ -114,17 +114,14 @@ COPY app/ ./app/
114
 
115
  # 复制启动脚本并赋予执行权限
116
  COPY entrypoint.sh ./
117
-
118
- # 确保脚本使用 Unix 换行符 (LF) 并设置执行权限
119
- RUN sed -i 's/\r$//' ./entrypoint.sh && \
120
- chmod +x ./entrypoint.sh
121
 
122
  # 给脚本和二进制文件执行权限 (重复的chmod可能不需要,但在构建阶段设置更安全)
123
- # RUN chmod +x ./clash_core/clash.meta-linux-amd64 || true
124
- # RUN chmod +x ./subconverter/subconverter || true
125
 
126
  # 暴露端口
127
  EXPOSE $FLASK_PORT $CLASH_PROXY_PORT $CLASH_API_PORT
128
 
129
- # 使用entrypoint脚本启动应用 (明确指定解释器)
130
- ENTRYPOINT ["/bin/bash", "/app/entrypoint.sh"]
 
114
 
115
  # 复制启动脚本并赋予执行权限
116
  COPY entrypoint.sh ./
117
+ RUN chmod +x ./entrypoint.sh
 
 
 
118
 
119
  # 给脚本和二进制文件执行权限 (重复的chmod可能不需要,但在构建阶段设置更安全)
120
+ RUN chmod +x ./clash_core/clash.meta-linux-amd64 || true
121
+ RUN chmod +x ./subconverter/subconverter || true
122
 
123
  # 暴露端口
124
  EXPOSE $FLASK_PORT $CLASH_PROXY_PORT $CLASH_API_PORT
125
 
126
+ # 使用entrypoint脚本启动应用
127
+ ENTRYPOINT ["/app/entrypoint.sh"]
entrypoint.sh CHANGED
@@ -1,11 +1,6 @@
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,111 +8,94 @@ GREEN='\033[0;32m'
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!"
 
1
+ #!/bin/sh
2
+ # 不再使用set -e,避免因chmod失败而中止脚本
3
+ # set -e
 
 
 
 
 
4
 
5
  # 设置颜色输出
6
  RED='\033[0;31m'
 
8
  YELLOW='\033[1;33m'
9
  NC='\033[0m' # No Color
10
 
11
+ echo "${GREEN}Starting Simple Clash Relay...${NC}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ # 添加诊断信息
14
+ echo "${YELLOW}System information:${NC}"
15
  uname -a
16
+ echo "${YELLOW}Running as user:${NC}"
17
  id
18
 
19
+ echo "${YELLOW}Checking executables:${NC}"
20
+ file /app/clash_core/clash.meta-linux-amd64
21
+ ls -la /app/clash_core/clash.meta-linux-amd64
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ # 可选清理旧配置(如果指定CLEAN_CONFIG=true)
24
  if [ "${CLEAN_CONFIG}" = "true" ]; then
25
+ echo "${YELLOW}Cleaning old config files...${NC}"
26
+ rm -f /app/data/config.yaml /app/data/config.yaml.raw
27
+ echo "${GREEN}Old config files cleaned${NC}"
28
  fi
29
 
30
+ # 确保数据目录存在
31
+ mkdir -p /app/data || echo "${YELLOW}Warning: Failed to create /app/data directory${NC}"
 
 
 
 
 
 
 
 
 
32
 
33
+ # 尝试设置权限,但允许失败
34
+ chmod -R 777 /app/data 2>/dev/null || echo "${YELLOW}Warning: Failed to set permissions for /app/data, continuing anyway${NC}"
 
 
35
 
36
+ # 尝试设置subconverter执行权限,但允许失败
37
+ if [ -f "/app/subconverter/subconverter" ]; then
38
+ chmod +x /app/subconverter/subconverter 2>/dev/null || echo "${YELLOW}Warning: Failed to set executable permission for subconverter${NC}"
 
 
 
 
39
  fi
40
 
41
+ # 尝试设置Clash Core执行权限,但允许失败
42
+ if [ -f "/app/clash_core/clash.meta-linux-amd64" ]; then
43
+ chmod +x /app/clash_core/clash.meta-linux-amd64 2>/dev/null || echo "${YELLOW}Warning: Failed to set executable permission for Clash Meta${NC}"
44
+ echo "${GREEN}Clash Meta found${NC}"
45
  else
46
+ echo "${RED}Error: Clash Core executable not found!${NC}"
47
+ exit 1
48
  fi
49
 
50
+ # 加载环境变量
51
+ if [ -f ".env" ]; then
52
+ echo "${GREEN}Loading environment variables from .env file...${NC}"
53
+ export $(grep -v '^#' .env | xargs -d '\n')
54
+ fi
55
 
56
+ # 检查必要的环境变量
57
+ if [ -z "$SUB_URL" ]; then
58
+ echo "${RED}Error: SUB_URL environment variable is not set!${NC}"
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
+ # 启动Flask应用
87
+ echo "${GREEN}Starting Flask application...${NC}"
 
88
 
89
+ # 使用gunicorn启动Flask应用(生产环境推荐)
90
+ # 如果WORKER_COUNT未设置,使用CPU核心数+1的worker数量
91
+ WORKER_COUNT=${WORKER_COUNT:-$(( $(nproc) + 1 ))}
92
+ echo "Using $WORKER_COUNT workers"
93
 
94
+ # 确保所有参数在一行或使用清晰的续行符
95
  exec gunicorn \
96
+ --workers $WORKER_COUNT \
97
+ --bind 0.0.0.0:${FLASK_PORT:-7860} \
98
  --log-level info \
99
+ --access-logfile - \
100
+ --error-logfile - \
101
+ app.main:app