This commit is contained in:
Arvin
2026-03-21 16:23:46 +08:00
parent d3bb7a6b12
commit a792d29eae
54 changed files with 180 additions and 61 deletions

View File

@ -31,7 +31,7 @@ chmod +x "$Install_Dir"/scripts/* 2>/dev/null || true
chmod +x "$Install_Dir"/bin/* 2>/dev/null || true
# =========================
# 目录初始化(新结构)
# 目录初始化
# =========================
mkdir -p \
"$Install_Dir/runtime" \
@ -50,6 +50,120 @@ source "$Install_Dir/scripts/get_cpu_arch.sh"
# shellcheck disable=SC1090
source "$Install_Dir/scripts/resolve_clash.sh"
write_env_value() {
local key="$1"
local value="$2"
local env_file="$Install_Dir/.env"
local escaped="${value//\\/\\\\}"
escaped="${escaped//&/\\&}"
escaped="${escaped//|/\\|}"
escaped="${escaped//\'/\'\\\'\'}"
if grep -qE "^[[:space:]]*(export[[:space:]]+)?${key}=" "$env_file"; then
sed -i -E "s|^[[:space:]]*(export[[:space:]]+)?${key}=.*$|export ${key}='${escaped}'|g" "$env_file"
else
printf "export %s='%s'\n" "$key" "$value" >> "$env_file"
fi
}
read_env_value() {
local key="$1"
sed -nE "s/^[[:space:]]*(export[[:space:]]+)?${key}=['\"]?([^'\"]*)['\"]?$/\2/p" "$Install_Dir/.env" | head -n 1
}
get_public_ip() {
curl -fsS --max-time 5 ifconfig.me 2>/dev/null \
|| curl -fsS --max-time 5 ip.sb 2>/dev/null \
|| curl -fsS --max-time 5 api.ipify.org 2>/dev/null \
|| true
}
show_dashboard_info() {
local secret="$1"
local public_ip="$2"
local dashboard_port="9090"
local ui_url=""
if [ -n "${public_ip:-}" ]; then
ui_url="http://${public_ip}:${dashboard_port}/ui/#/setup?hostname=${public_ip}&port=${dashboard_port}&secret=${secret}"
else
ui_url="http://127.0.0.1:${dashboard_port}/ui/#/setup?hostname=127.0.0.1&port=${dashboard_port}&secret=${secret}"
fi
echo
echo "╔═══════════════════════════════════════════════╗"
echo "║ 😼 Web 控制台 ║"
echo "║═══════════════════════════════════════════════║"
echo "║ ║"
echo "║ 🔓 注意放行端口9090 ║"
if [ -n "${public_ip:-}" ]; then
printf "║ 🌏 公网http://%-27s║\n" "${public_ip}:9090/ui"
else
printf "║ 🏠 本地http://%-27s║\n" "127.0.0.1:9090/ui"
fi
echo "║ ║"
echo "╚═══════════════════════════════════════════════╝"
echo
echo "😼 当前密钥:${secret}"
echo "🎯 面板地址:${ui_url}"
}
wait_dashboard_ready() {
local host="$1"
local port="${2:-9090}"
local max_retry="${3:-20}"
local i
for ((i=1; i<=max_retry; i++)); do
if curl -fsS --max-time 2 "http://${host}:${port}/ui/" >/dev/null 2>&1; then
return 0
fi
sleep 1
done
return 1
}
prompt_and_apply_subscription() {
local sub_url=""
local secret=""
local public_ip=""
while true; do
echo
read -r -p "✈️ 请输入要添加的订阅链接:" sub_url
if [ -z "${sub_url:-}" ]; then
echo "❌ 订阅链接不能为空"
continue
fi
write_env_value "CLASH_URL" "$sub_url"
echo "⏳ 正在下载订阅..."
echo "🍃 验证订阅配置..."
if ! "$Install_Dir/scripts/generate_config.sh" >/dev/null 2>&1; then
echo "❌ 订阅不可用或转换失败,请检查链接后重试"
continue
fi
echo "🎉 订阅已添加:[1] $sub_url"
echo "🔥 订阅已生效"
if command -v systemctl >/dev/null 2>&1; then
systemctl restart "${Service_Name}.service"
else
"$Install_Dir/scripts/run_clash.sh" --daemon
fi
secret="$(read_env_value "CLASH_SECRET")"
public_ip="$(get_public_ip)"
show_dashboard_info "$secret" "$public_ip"
return 0
done
}
# =========================
# 内核检查
# =========================
@ -107,9 +221,6 @@ chmod 644 /etc/profile.d/clash-for-linux.sh
# =========================
# 安装 systemd
# =========================
Service_Enabled="unknown"
Service_Started="unknown"
if command -v systemctl >/dev/null 2>&1; then
CLASH_SERVICE_USER="$Service_User" CLASH_SERVICE_GROUP="$Service_Group" \
"$Install_Dir/scripts/install_systemd.sh" "$Install_Dir"
@ -117,43 +228,25 @@ if command -v systemctl >/dev/null 2>&1; then
if [ "${CLASH_ENABLE_SERVICE:-true}" = "true" ]; then
systemctl enable "${Service_Name}.service" || true
fi
if [ "${CLASH_START_SERVICE:-true}" = "true" ]; then
systemctl start "${Service_Name}.service" || true
fi
if systemctl is-enabled --quiet "${Service_Name}.service" 2>/dev/null; then
Service_Enabled="enabled"
else
Service_Enabled="disabled"
fi
if systemctl is-active --quiet "${Service_Name}.service" 2>/dev/null; then
Service_Started="active"
else
Service_Started="inactive"
fi
else
echo "[WARN] systemd not found, will use script mode"
fi
# =========================
# 输出(全部收敛到 clashctl
# 输出 + 订阅录入
# =========================
echo
echo "=== Install Complete ==="
echo "Install Dir : $Install_Dir"
echo "clashctl : /usr/local/bin/clashctl"
echo
echo "Next:"
echo " clashctl generate"
echo " clashctl start"
echo " clashctl doctor"
prompt_and_apply_subscription
echo
echo "Commands:"
echo " clashctl status"
echo " clashctl logs"
echo " clashctl restart"
echo " clashctl stop"
echo " clashctl stop"
echo " clashctl ui"
echo " clashctl secret"