This commit is contained in:
Arvin
2026-03-20 22:22:59 +08:00
parent 9c31ef0e3c
commit d1cc7d9b3b
7 changed files with 238 additions and 96 deletions

View File

@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -e
PROJECT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
RUNTIME_DIR="$PROJECT_DIR/runtime"
echo "=== Clash Doctor ==="
if [ -f "$RUNTIME_DIR/config.yaml" ]; then
echo "[OK] config exists"
else
echo "[ERROR] config missing"
fi
if command -v systemctl >/dev/null 2>&1; then
if systemctl is-active --quiet clash-for-linux.service; then
echo "[OK] service running"
else
echo "[WARN] service not running"
fi
fi

View File

@ -3,19 +3,22 @@ set -euo pipefail
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
RUNTIME_DIR="$PROJECT_DIR/runtime" RUNTIME_DIR="$PROJECT_DIR/runtime"
CONF_DIR="$PROJECT_DIR/conf" CONFIG_DIR="$PROJECT_DIR/config"
TEMP_DIR="$PROJECT_DIR/temp"
LOG_DIR="$PROJECT_DIR/logs" LOG_DIR="$PROJECT_DIR/logs"
RUNTIME_CONFIG="$RUNTIME_DIR/config.yaml" RUNTIME_CONFIG="$RUNTIME_DIR/config.yaml"
STATE_FILE="$RUNTIME_DIR/state.env" STATE_FILE="$RUNTIME_DIR/state.env"
TEMP_DOWNLOAD="$TEMP_DIR/clash.yaml"
TEMP_CONVERTED="$TEMP_DIR/clash_config.yaml"
mkdir -p "$RUNTIME_DIR" "$CONF_DIR" "$TEMP_DIR" "$LOG_DIR" TMP_DOWNLOAD="$RUNTIME_DIR/subscription.raw.yaml"
TMP_NORMALIZED="$RUNTIME_DIR/subscription.normalized.yaml"
TMP_PROXY_FRAGMENT="$RUNTIME_DIR/proxy.fragment.yaml"
# shellcheck disable=SC1091 mkdir -p "$RUNTIME_DIR" "$CONFIG_DIR" "$LOG_DIR"
source "$PROJECT_DIR/.env"
if [ -f "$PROJECT_DIR/.env" ]; then
# shellcheck disable=SC1091
source "$PROJECT_DIR/.env"
fi
# shellcheck disable=SC1091 # shellcheck disable=SC1091
source "$PROJECT_DIR/scripts/get_cpu_arch.sh" source "$PROJECT_DIR/scripts/get_cpu_arch.sh"
@ -58,8 +61,9 @@ EOF
generate_secret() { generate_secret() {
if [ -n "${CLASH_SECRET:-}" ]; then if [ -n "${CLASH_SECRET:-}" ]; then
echo "$CLASH_SECRET" echo "$CLASH_SECRET"
return return 0
fi fi
if command -v openssl >/dev/null 2>&1; then if command -v openssl >/dev/null 2>&1; then
openssl rand -hex 16 openssl rand -hex 16
else else
@ -69,8 +73,11 @@ generate_secret() {
SECRET="$(generate_secret)" SECRET="$(generate_secret)"
upsert_yaml_kv() { upsert_yaml_kv_local() {
local file="$1" key="$2" value="$3" local file="$1"
local key="$2"
local value="$3"
[ -f "$file" ] || touch "$file" [ -f "$file" ] || touch "$file"
if grep -qE "^[[:space:]]*${key}:" "$file"; then if grep -qE "^[[:space:]]*${key}:" "$file"; then
@ -80,110 +87,102 @@ upsert_yaml_kv() {
fi fi
} }
force_write_secret() { apply_secret_to_config() {
local file="$1" local file="$1"
upsert_yaml_kv "$file" "secret" "$SECRET" upsert_yaml_kv_local "$file" "secret" "$SECRET"
} }
force_write_controller_and_ui() { apply_controller_to_config() {
local file="$1" local file="$1"
if [ "$EXTERNAL_CONTROLLER_ENABLED" = "true" ]; then if [ "$EXTERNAL_CONTROLLER_ENABLED" = "true" ]; then
upsert_yaml_kv "$file" "external-controller" "$EXTERNAL_CONTROLLER" upsert_yaml_kv_local "$file" "external-controller" "$EXTERNAL_CONTROLLER"
mkdir -p "$RUNTIME_DIR" mkdir -p "$RUNTIME_DIR"
ln -sfn "$PROJECT_DIR/dashboard/public" "$RUNTIME_DIR/ui" ln -sfn "$PROJECT_DIR/dashboard/public" "$RUNTIME_DIR/ui"
upsert_yaml_kv "$file" "external-ui" "$RUNTIME_DIR/ui" upsert_yaml_kv_local "$file" "external-ui" "$RUNTIME_DIR/ui"
fi fi
} }
download_subscription() { download_subscription() {
[ -n "$CLASH_URL" ] || return 1 [ -n "$CLASH_URL" ] || return 1
local curl_cmd=(curl -fL -S --retry 2 --connect-timeout 10 -m 30 -o "$TEMP_DOWNLOAD") local curl_cmd=(curl -fL -S --retry 2 --connect-timeout 10 -m 30 -o "$TMP_DOWNLOAD")
[ "$ALLOW_INSECURE_TLS" = "true" ] && curl_cmd+=(-k) [ "$ALLOW_INSECURE_TLS" = "true" ] && curl_cmd+=(-k)
curl_cmd+=("$CLASH_URL") curl_cmd+=("$CLASH_URL")
"${curl_cmd[@]}" "${curl_cmd[@]}"
} }
use_fallback() { is_complete_clash_config() {
[ -s "$CONF_DIR/fallback_config.yaml" ] || return 1
cp -f "$CONF_DIR/fallback_config.yaml" "$RUNTIME_CONFIG"
force_write_controller_and_ui "$RUNTIME_CONFIG"
force_write_secret "$RUNTIME_CONFIG"
}
is_full_config() {
local file="$1" local file="$1"
grep -qE '^(proxies:|proxy-providers:|mixed-port:|port:)' "$file" grep -qE '^(proxies:|proxy-providers:|mixed-port:|port:)' "$file"
} }
cleanup_tmp_files() {
rm -f "$TMP_NORMALIZED" "$TMP_PROXY_FRAGMENT"
}
main() { main() {
if [ "$CLASH_AUTO_UPDATE" != "true" ]; then local template_file="$CONFIG_DIR/template.yaml"
if [ -s "$RUNTIME_CONFIG" ]; then
write_state "success" "auto_update_disabled_keep_runtime" "runtime_existing" if [ "$CLASH_AUTO_UPDATE" != "true" ]; then
exit 0 if [ -s "$RUNTIME_CONFIG" ]; then
fi write_state "success" "auto_update_disabled_keep_runtime" "runtime_existing"
use_fallback exit 0
write_state "success" "auto_update_disabled_use_fallback" "fallback"
exit 0
fi fi
if ! download_subscription; then echo "[ERROR] auto update disabled and runtime config missing: $RUNTIME_CONFIG" >&2
if [ -s "$RUNTIME_CONFIG" ]; then write_state "failed" "runtime_missing" "none"
write_state "success" "download_failed_keep_last_good" "runtime_existing"
exit 0
fi
use_fallback
write_state "success" "download_failed_use_fallback" "fallback"
exit 0
fi
cp -f "$TEMP_DOWNLOAD" "$TEMP_CONVERTED"
if is_full_config "$TEMP_CONVERTED"; then
cp -f "$TEMP_CONVERTED" "$RUNTIME_CONFIG"
force_write_controller_and_ui "$RUNTIME_CONFIG"
force_write_secret "$RUNTIME_CONFIG"
write_state "success" "subscription_full" "subscription_full"
exit 0
fi
# 片段订阅:这里先保留模板拼接逻辑
TEMPLATE_FILE=""
if [ -s "$CONF_DIR/template.yaml" ]; then
TEMPLATE_FILE="$CONF_DIR/template.yaml"
elif [ -s "$TEMP_DIR/template.yaml" ]; then
TEMPLATE_FILE="$TEMP_DIR/template.yaml"
elif [ -s "$CONF_DIR/template.yaml" ]; then
TEMPLATE_FILE="$CONF_DIR/template.yaml"
elif [ -s "$PROJECT_DIR/temp/template.yaml" ]; then
TEMPLATE_FILE="$PROJECT_DIR/temp/template.yaml"
fi
if [ -z "$TEMPLATE_FILE" ]; then
echo "[ERROR] missing template config file (template.yaml / template.yaml)" >&2
write_state "failed" "missing_template" "none"
exit 1 exit 1
fi
if ! download_subscription; then
if [ -s "$RUNTIME_CONFIG" ]; then
write_state "success" "download_failed_keep_runtime" "runtime_existing"
exit 0
fi fi
sed -n '/^proxies:/,$p' "$TEMP_CONVERTED" > "$TEMP_DIR/proxy.txt" echo "[ERROR] failed to download subscription and runtime config missing" >&2
cat "$TEMPLATE_FILE" > "$RUNTIME_CONFIG" write_state "failed" "download_failed" "none"
cat "$TEMP_DIR/proxy.txt" >> "$RUNTIME_CONFIG" exit 1
fi
sed -i "s/CLASH_HTTP_PORT_PLACEHOLDER/${CLASH_HTTP_PORT}/g" "$RUNTIME_CONFIG" cp -f "$TMP_DOWNLOAD" "$TMP_NORMALIZED"
sed -i "s/CLASH_SOCKS_PORT_PLACEHOLDER/${CLASH_SOCKS_PORT}/g" "$RUNTIME_CONFIG"
sed -i "s/CLASH_REDIR_PORT_PLACEHOLDER/${CLASH_REDIR_PORT}/g" "$RUNTIME_CONFIG"
sed -i "s/CLASH_LISTEN_IP_PLACEHOLDER/${CLASH_LISTEN_IP}/g" "$RUNTIME_CONFIG"
sed -i "s/CLASH_ALLOW_LAN_PLACEHOLDER/${CLASH_ALLOW_LAN}/g" "$RUNTIME_CONFIG"
force_write_controller_and_ui "$RUNTIME_CONFIG" if is_complete_clash_config "$TMP_NORMALIZED"; then
force_write_secret "$RUNTIME_CONFIG" cp -f "$TMP_NORMALIZED" "$RUNTIME_CONFIG"
apply_controller_to_config "$RUNTIME_CONFIG"
apply_secret_to_config "$RUNTIME_CONFIG"
write_state "success" "subscription_full" "subscription_full"
cleanup_tmp_files
exit 0
fi
write_state "success" "subscription_fragment_merged" "subscription_fragment" if [ ! -s "$template_file" ]; then
echo "[ERROR] missing template config file: $template_file" >&2
write_state "failed" "missing_template" "none"
cleanup_tmp_files
exit 1
fi
sed -n '/^proxies:/,$p' "$TMP_NORMALIZED" > "$TMP_PROXY_FRAGMENT"
cat "$template_file" > "$RUNTIME_CONFIG"
cat "$TMP_PROXY_FRAGMENT" >> "$RUNTIME_CONFIG"
sed -i "s/CLASH_HTTP_PORT_PLACEHOLDER/${CLASH_HTTP_PORT}/g" "$RUNTIME_CONFIG"
sed -i "s/CLASH_SOCKS_PORT_PLACEHOLDER/${CLASH_SOCKS_PORT}/g" "$RUNTIME_CONFIG"
sed -i "s/CLASH_REDIR_PORT_PLACEHOLDER/${CLASH_REDIR_PORT}/g" "$RUNTIME_CONFIG"
sed -i "s/CLASH_LISTEN_IP_PLACEHOLDER/${CLASH_LISTEN_IP}/g" "$RUNTIME_CONFIG"
sed -i "s/CLASH_ALLOW_LAN_PLACEHOLDER/${CLASH_ALLOW_LAN}/g" "$RUNTIME_CONFIG"
apply_controller_to_config "$RUNTIME_CONFIG"
apply_secret_to_config "$RUNTIME_CONFIG"
write_state "success" "subscription_fragment_merged" "subscription_fragment"
cleanup_tmp_files
} }
main "$@" main "$@"

View File

@ -0,0 +1,20 @@
#!/usr/bin/env bash
set -e
PROJECT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
LOG_FILE="$PROJECT_DIR/logs/clash.log"
SERVICE_NAME="clash-for-linux.service"
if [ "${1:-}" = "-f" ]; then
if command -v journalctl >/dev/null 2>&1; then
journalctl -u "$SERVICE_NAME" -f
else
tail -f "$LOG_FILE"
fi
else
if command -v journalctl >/dev/null 2>&1; then
journalctl -u "$SERVICE_NAME" -n 50 --no-pager
else
tail -n 50 "$LOG_FILE"
fi
fi

View File

@ -41,10 +41,29 @@ fi
# shellcheck disable=SC1091 # shellcheck disable=SC1091
source "$PROJECT_DIR/scripts/get_cpu_arch.sh" source "$PROJECT_DIR/scripts/get_cpu_arch.sh"
# shellcheck disable=SC1091
source "$PROJECT_DIR/scripts/resolve_clash.sh" source "$PROJECT_DIR/scripts/resolve_clash.sh"
# shellcheck disable=SC1091
source "$PROJECT_DIR/scripts/service_lib.sh"
CLASH_BIN="$(resolve_clash_bin "$PROJECT_DIR" "${CpuArch:-}")" CLASH_BIN="$(resolve_clash_bin "$PROJECT_DIR" "${CpuArch:-}")"
if [ ! -x "$CLASH_BIN" ]; then
echo "[ERROR] clash binary not found or not executable: $CLASH_BIN" >&2
exit 2
fi
test_config() {
local bin="$1"
local config="$2"
"$bin" -t -f "$config" >/dev/null 2>&1
}
if ! test_config "$CLASH_BIN" "$CONFIG_FILE"; then
echo "[ERROR] config test failed: $CONFIG_FILE" >&2
exit 2
fi
# systemd 模式 # systemd 模式
if [ "$FOREGROUND" = true ]; then if [ "$FOREGROUND" = true ]; then
write_run_state "running" "systemd" write_run_state "running" "systemd"

View File

@ -4,6 +4,7 @@ set -euo pipefail
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
RUNTIME_DIR="$PROJECT_DIR/runtime" RUNTIME_DIR="$PROJECT_DIR/runtime"
PID_FILE="$RUNTIME_DIR/clash.pid" PID_FILE="$RUNTIME_DIR/clash.pid"
STATE_FILE="$RUNTIME_DIR/state.env"
SERVICE_NAME="clash-for-linux.service" SERVICE_NAME="clash-for-linux.service"
mkdir -p "$RUNTIME_DIR" mkdir -p "$RUNTIME_DIR"
@ -17,6 +18,17 @@ service_unit_exists() {
systemctl show "$SERVICE_NAME" -p LoadState --value 2>/dev/null | grep -q '^loaded$' systemctl show "$SERVICE_NAME" -p LoadState --value 2>/dev/null | grep -q '^loaded$'
} }
read_pid() {
[ -f "$PID_FILE" ] || return 1
cat "$PID_FILE"
}
is_script_running() {
local pid
pid="$(read_pid 2>/dev/null || true)"
[ -n "${pid:-}" ] && kill -0 "$pid" 2>/dev/null
}
detect_mode() { detect_mode() {
if service_unit_exists && systemctl is-active --quiet "$SERVICE_NAME"; then if service_unit_exists && systemctl is-active --quiet "$SERVICE_NAME"; then
echo "systemd" echo "systemd"
@ -29,15 +41,38 @@ detect_mode() {
fi fi
} }
read_pid() { write_run_state() {
[ -f "$PID_FILE" ] || return 1 local status="$1"
cat "$PID_FILE" local mode="${2:-unknown}"
} local pid="${3:-}"
is_script_running() { touch "$STATE_FILE"
local pid
pid="$(read_pid 2>/dev/null || true)" if grep -q '^LAST_RUN_STATUS=' "$STATE_FILE" 2>/dev/null; then
[ -n "${pid:-}" ] && kill -0 "$pid" 2>/dev/null sed -i -E "s/^LAST_RUN_STATUS=.*/LAST_RUN_STATUS=${status}/" "$STATE_FILE"
else
echo "LAST_RUN_STATUS=${status}" >> "$STATE_FILE"
fi
if grep -q '^LAST_RUN_MODE=' "$STATE_FILE" 2>/dev/null; then
sed -i -E "s/^LAST_RUN_MODE=.*/LAST_RUN_MODE=${mode}/" "$STATE_FILE"
else
echo "LAST_RUN_MODE=${mode}" >> "$STATE_FILE"
fi
if grep -q '^LAST_RUN_AT=' "$STATE_FILE" 2>/dev/null; then
sed -i -E "s/^LAST_RUN_AT=.*/LAST_RUN_AT=$(date -Iseconds)/" "$STATE_FILE"
else
echo "LAST_RUN_AT=$(date -Iseconds)" >> "$STATE_FILE"
fi
if [ -n "$pid" ]; then
if grep -q '^LAST_RUN_PID=' "$STATE_FILE" 2>/dev/null; then
sed -i -E "s/^LAST_RUN_PID=.*/LAST_RUN_PID=${pid}/" "$STATE_FILE"
else
echo "LAST_RUN_PID=${pid}" >> "$STATE_FILE"
fi
fi
} }
start_via_systemd() { start_via_systemd() {
@ -46,6 +81,8 @@ start_via_systemd() {
stop_via_systemd() { stop_via_systemd() {
systemctl stop "$SERVICE_NAME" systemctl stop "$SERVICE_NAME"
write_run_state "stopped" "systemd"
rm -f "$PID_FILE"
} }
restart_via_systemd() { restart_via_systemd() {
@ -63,6 +100,7 @@ start_via_script() {
stop_via_script() { stop_via_script() {
local pid local pid
pid="$(read_pid 2>/dev/null || true)" pid="$(read_pid 2>/dev/null || true)"
if [ -n "${pid:-}" ] && kill -0 "$pid" 2>/dev/null; then if [ -n "${pid:-}" ] && kill -0 "$pid" 2>/dev/null; then
echo "[INFO] stopping clash pid=$pid" echo "[INFO] stopping clash pid=$pid"
kill "$pid" kill "$pid"
@ -71,16 +109,10 @@ stop_via_script() {
kill -9 "$pid" 2>/dev/null || true kill -9 "$pid" 2>/dev/null || true
fi fi
fi fi
rm -f "$PID_FILE"
if [ -f "$PROJECT_DIR/runtime/state.env" ]; then
if grep -q '^LAST_RUN_STATUS=' "$PROJECT_DIR/runtime/state.env" 2>/dev/null; then
sed -i -E "s/^LAST_RUN_STATUS=.*/LAST_RUN_STATUS=stopped/" "$PROJECT_DIR/runtime/state.env"
else
echo "LAST_RUN_STATUS=stopped" >> "$PROJECT_DIR/runtime/state.env"
fi
fi
}
rm -f "$PID_FILE"
write_run_state "stopped" "script"
}
restart_via_script() { restart_via_script() {
stop_via_script || true stop_via_script || true

View File

@ -0,0 +1,28 @@
#!/usr/bin/env bash
set -e
PROJECT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
# shellcheck disable=SC1091
source "$PROJECT_DIR/scripts/service_lib.sh"
mode="$(detect_mode)"
echo "=== Clash Status ==="
echo "Project : $PROJECT_DIR"
echo "Mode : $mode"
case "$mode" in
systemd)
echo "Running : yes (systemd)"
;;
script)
echo "Running : yes (script)"
;;
systemd-installed)
echo "Running : no (installed but not started)"
;;
*)
echo "Running : no"
;;
esac

View File

@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -e
PROJECT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
# shellcheck disable=SC1091
source "$PROJECT_DIR/scripts/service_lib.sh"
mode="$(detect_mode)"
case "$mode" in
systemd)
stop_via_systemd
echo "[OK] stopped via systemd"
;;
script)
stop_via_script
echo "[OK] stopped via script"
;;
*)
echo "[WARN] nothing is running"
;;
esac