mirror of
https://github.com/wnlen/clash-for-linux.git
synced 2026-03-21 22:06:45 +08:00
65 lines
1.5 KiB
Bash
65 lines
1.5 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
RUNTIME_DIR="$PROJECT_DIR/runtime"
|
|
LOG_DIR="$PROJECT_DIR/logs"
|
|
CONFIG_FILE="$RUNTIME_DIR/config.yaml"
|
|
PID_FILE="$RUNTIME_DIR/clash.pid"
|
|
|
|
mkdir -p "$RUNTIME_DIR" "$LOG_DIR"
|
|
|
|
FOREGROUND=false
|
|
DAEMON=false
|
|
|
|
# 解析参数
|
|
for arg in "$@"; do
|
|
case "$arg" in
|
|
--foreground) FOREGROUND=true ;;
|
|
--daemon) DAEMON=true ;;
|
|
*)
|
|
echo "[ERROR] Unknown arg: $arg" >&2
|
|
exit 2
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [ "$FOREGROUND" = true ] && [ "$DAEMON" = true ]; then
|
|
echo "[ERROR] Cannot use both --foreground and --daemon" >&2
|
|
exit 2
|
|
fi
|
|
|
|
if [ ! -s "$CONFIG_FILE" ]; then
|
|
echo "[ERROR] runtime config not found: $CONFIG_FILE" >&2
|
|
exit 2
|
|
fi
|
|
|
|
if grep -q '\${' "$CONFIG_FILE"; then
|
|
echo "[ERROR] unresolved placeholder found in $CONFIG_FILE" >&2
|
|
exit 2
|
|
fi
|
|
|
|
# shellcheck disable=SC1091
|
|
source "$PROJECT_DIR/scripts/get_cpu_arch.sh"
|
|
source "$PROJECT_DIR/scripts/resolve_clash.sh"
|
|
|
|
CLASH_BIN="$(resolve_clash_bin "$PROJECT_DIR" "${CpuArch:-}")"
|
|
|
|
# systemd 模式
|
|
if [ "$FOREGROUND" = true ]; then
|
|
write_run_state "running" "systemd"
|
|
exec "$CLASH_BIN" -f "$CONFIG_FILE" -d "$RUNTIME_DIR"
|
|
fi
|
|
|
|
# script / daemon 模式
|
|
if [ "$DAEMON" = true ]; then
|
|
nohup "$CLASH_BIN" -f "$CONFIG_FILE" -d "$RUNTIME_DIR" >>"$LOG_DIR/clash.log" 2>&1 &
|
|
pid=$!
|
|
echo "$pid" > "$PID_FILE"
|
|
write_run_state "running" "script" "$pid"
|
|
echo "[OK] Clash started in script mode, pid=$pid"
|
|
exit 0
|
|
fi
|
|
|
|
echo "[ERROR] Must specify --foreground or --daemon" >&2
|
|
exit 2 |