Add systemd installer and non-root support

This commit is contained in:
wnlen
2026-01-13 23:46:40 +08:00
parent a1d50ae9be
commit a8982d6d3d
8 changed files with 222 additions and 17 deletions

View File

@ -21,7 +21,11 @@ else
echo "$decoded_content" > ${Server_Dir}/temp/clash_config.yaml
else
echo "解码后的内容不符合clash标准尝试将其转换为标准格式"
${Server_Dir}/tools/subconverter/subconverter -g &>> ${Server_Dir}/logs/subconverter.log
if [ -z "$SUBCONVERTER_BIN" ]; then
echo "subconverter 未配置,无法执行转换"
exit 1
fi
"${SUBCONVERTER_BIN}" -g &>> ${Server_Dir}/logs/subconverter.log
converted_file=${Server_Dir}/temp/clash_config.yaml
# 判断转换后的内容是否符合clash配置文件标准
if awk '/^proxies:/{p=1} /^proxy-groups:/{g=1} /^rules:/{r=1} p&&g&&r{exit} END{if(p&&g&&r) exit 0; else exit 1}' $converted_file; then

52
scripts/install_systemd.sh Executable file
View File

@ -0,0 +1,52 @@
#!/bin/bash
set -euo pipefail
Server_Dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
Service_Name="clash-for-linux"
Service_User="${CLASH_SERVICE_USER:-clash}"
Service_Group="${CLASH_SERVICE_GROUP:-$Service_User}"
Unit_Path="/etc/systemd/system/${Service_Name}.service"
if [ "$(id -u)" -ne 0 ]; then
echo -e "\033[31m[ERROR] 需要 root 权限来安装 systemd 单元\033[0m"
exit 1
fi
if ! getent group "$Service_Group" >/dev/null 2>&1; then
groupadd --system "$Service_Group"
fi
if ! id "$Service_User" >/dev/null 2>&1; then
useradd --system --no-create-home --shell /usr/sbin/nologin --gid "$Service_Group" "$Service_User"
fi
install -d -m 0755 "$Server_Dir/conf" "$Server_Dir/logs" "$Server_Dir/temp"
chown -R "$Service_User:$Service_Group" "$Server_Dir/conf" "$Server_Dir/logs" "$Server_Dir/temp"
cat >"$Unit_Path"<<EOF
[Unit]
Description=Clash for Linux
After=network.target
[Service]
Type=simple
WorkingDirectory=$Server_Dir
ExecStart=/bin/bash $Server_Dir/start.sh
ExecStop=/bin/bash $Server_Dir/shutdown.sh
Restart=on-failure
RestartSec=5
User=$Service_User
Group=$Service_Group
PIDFile=$Server_Dir/temp/clash.pid
Environment=CLASH_ENV_FILE=$Server_Dir/temp/clash-for-linux.sh
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
echo -e "\033[32m[OK] 已生成 systemd 单元: ${Unit_Path}\033[0m"
echo -e "可执行以下命令启动服务:"
echo -e " sudo systemctl enable --now ${Service_Name}.service"

82
scripts/resolve_subconverter.sh Executable file
View File

@ -0,0 +1,82 @@
#!/bin/bash
Subconverter_Bin=""
Subconverter_Ready=false
Subconverter_Dir="${Server_Dir}/tools/subconverter"
Default_Bin="${Subconverter_Dir}/subconverter"
resolve_subconverter_arch() {
local raw_arch="$1"
case "$raw_arch" in
x86_64|amd64)
echo "linux-amd64"
;;
aarch64|arm64)
echo "linux-arm64"
;;
armv7*|armv7l)
echo "linux-armv7"
;;
*)
echo ""
;;
esac
}
try_subconverter_bin() {
local candidate="$1"
if [ -n "$candidate" ] && [ -x "$candidate" ]; then
Subconverter_Bin="$candidate"
Subconverter_Ready=true
return 0
fi
return 1
}
if [ -n "$SUBCONVERTER_PATH" ]; then
try_subconverter_bin "$SUBCONVERTER_PATH" && return 0
else
try_subconverter_bin "$Default_Bin" && return 0
fi
Detected_Arch="${CpuArch:-$(uname -m 2>/dev/null)}"
Resolved_Arch=$(resolve_subconverter_arch "$Detected_Arch")
if [ -n "$Resolved_Arch" ]; then
try_subconverter_bin "${Subconverter_Dir}/subconverter-${Resolved_Arch}" && return 0
try_subconverter_bin "${Subconverter_Dir}/bin/subconverter-${Resolved_Arch}" && return 0
try_subconverter_bin "${Subconverter_Dir}/${Resolved_Arch}/subconverter" && return 0
fi
if [ "${SUBCONVERTER_AUTO_DOWNLOAD:-false}" = "true" ] && [ -n "$Resolved_Arch" ]; then
Download_Template="${SUBCONVERTER_DOWNLOAD_URL_TEMPLATE:-}"
if [ -z "$Download_Template" ]; then
echo -e "\033[33m[WARN] 未设置 SUBCONVERTER_DOWNLOAD_URL_TEMPLATE跳过 subconverter 自动下载\033[0m"
return 0
fi
Download_Url="${Download_Template//\{arch\}/${Resolved_Arch}}"
Download_Archive="${Server_Dir}/temp/subconverter-${Resolved_Arch}.tar.gz"
Extract_Dir="${Server_Dir}/temp/subconverter-${Resolved_Arch}"
mkdir -p "${Extract_Dir}"
if command -v curl >/dev/null 2>&1; then
curl -L -sS -o "${Download_Archive}" "${Download_Url}"
elif command -v wget >/dev/null 2>&1; then
wget -q -O "${Download_Archive}" "${Download_Url}"
else
echo -e "\033[33m[WARN] 未找到 curl 或 wget无法自动下载 subconverter\033[0m"
return 0
fi
if [ -f "${Download_Archive}" ]; then
tar -xzf "${Download_Archive}" -C "${Extract_Dir}" 2>/dev/null
Downloaded_Bin=$(find "${Extract_Dir}" -maxdepth 3 -type f -name "subconverter" -print -quit)
if [ -n "${Downloaded_Bin}" ]; then
mv "${Downloaded_Bin}" "${Subconverter_Dir}/subconverter-${Resolved_Arch}"
chmod +x "${Subconverter_Dir}/subconverter-${Resolved_Arch}"
try_subconverter_bin "${Subconverter_Dir}/subconverter-${Resolved_Arch}" && return 0
fi
fi
fi