fix:singbox

This commit is contained in:
dichgrem
2025-07-19 10:03:38 +08:00
parent 7f037c150a
commit d6f174867b

View File

@ -1,46 +1,65 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# install_singbox.sh # install_singbox.sh
# 一键安装 Sing-box并配置 VLESS Reality支持菜单操作安装、状态、显示链接、卸载、重装
set -euo pipefail set -euo pipefail
# 颜色定义
RED=$'\033[31m'
GREEN=$'\033[32m'
YELLOW=$'\033[33m'
BLUE=$'\033[34m'
CYAN=$'\033[36m'
BOLD=$'\033[1m'
NC=$'\033[0m'
# 权限检查
if [[ $EUID -ne 0 ]]; then
printf "${RED}错误:请以 root 用户或使用 sudo 运行此脚本${NC}\n" >&2
exit 1
fi
CONFIG_DIR=/etc/singbox CONFIG_DIR=/etc/singbox
SERVICE_FILE=/etc/systemd/system/sing-box.service
STATE_FILE="$CONFIG_DIR/state.env" STATE_FILE="$CONFIG_DIR/state.env"
BIN_NAME=sing-box BIN_NAME=sing-box
# 函数:安装 Sing-box 并生成配置 # 安装 Sing-box 并生成配置
install_singbox() { install_singbox() {
# 0. 输入名称 & SNI printf "${CYAN}===== 安装 Sing-box 并生成配置 =====${NC}\n"
read -rp "请输入用户名称 (name 字段,例如 AK-JP-100G) " NAME printf "${YELLOW}请输入用户名称 (name 字段,例如 AK-JP-100G)${NC}"
read -r NAME
[[ -z "$NAME" ]] && { [[ -z "$NAME" ]] && {
echo "名称不能为空,退出。" >&2 printf "${RED}名称不能为空,退出。${NC}\n" >&2
exit 1 exit 1
} }
read -rp "请输入 SNI 域名 (默认: s0.awsstatic.com) " SNI printf "${YELLOW}请输入 SNI 域名 (默认: s0.awsstatic.com)${NC}"
read -r SNI
SNI=${SNI:-s0.awsstatic.com} SNI=${SNI:-s0.awsstatic.com}
# 1. 安装 Sing-box # 安装
if command -v apt-get &>/dev/null; then if command -v apt-get &>/dev/null; then
echo "检测到 Debian/Ubuntu使用官方 deb 安装脚本..." printf "${BLUE}检测到 Debian/Ubuntu使用官方 deb 安装脚本...${NC}\n"
bash <(curl -fsSL https://sing-box.app/deb-install.sh) bash <(curl -fsSL https://sing-box.app/deb-install.sh)
elif command -v dnf &>/dev/null || command -v yum &>/dev/null; then elif command -v dnf &>/dev/null || command -v yum &>/dev/null; then
echo "检测到 RHEL/CentOS使用官方 rpm 安装脚本..." printf "${BLUE}检测到 RHEL/CentOS使用官方 rpm 安装脚本...${NC}\n"
bash <(curl -fsSL https://sing-box.app/rpm-install.sh) bash <(curl -fsSL https://sing-box.app/rpm-install.sh)
elif command -v pacman &>/dev/null; then elif command -v pacman &>/dev/null; then
echo "检测到 Arch Linux使用官方 arch 安装脚本..." printf "${BLUE}检测到 Arch Linux使用官方 arch 安装脚本...${NC}\n"
bash <(curl -fsSL https://sing-box.app/arch-install.sh) bash <(curl -fsSL https://sing-box.app/arch-install.sh)
else else
echo "无法识别发行版,请手动安装 Sing-box 内核" >&2 printf "${RED}无法识别发行版,请手动安装 Sing-box 内核${NC}\n" >&2
exit 1 exit 1
fi fi
BIN_PATH=$(command -v $BIN_NAME)
# 确认安装路径
hash -r
BIN_PATH=$(command -v $BIN_NAME || true)
[[ -z "$BIN_PATH" ]] && { [[ -z "$BIN_PATH" ]] && {
echo "未找到 $BIN_NAME,可执行文件路径异常,请检查安装" >&2 printf "${RED}未找到 $BIN_NAME,可执行文件路径异常,请检查安装${NC}\n" >&2
exit 1 exit 1
} }
echo "已安装 $BIN_NAME 版本:$($BIN_PATH version | head -n1)" VERSION=$($BIN_PATH version | head -n1 | awk '{print $NF}')
printf "${GREEN}已安装 $BIN_NAME 版本:%s${NC}\n" "$VERSION"
# 2. 生成 UUID / Reality 密钥 / ShortID / uTLS # 生成参数
UUID=$($BIN_PATH generate uuid) UUID=$($BIN_PATH generate uuid)
KEY_OUTPUT=$($BIN_PATH generate reality-keypair) KEY_OUTPUT=$($BIN_PATH generate reality-keypair)
PRIVATE_KEY=$(echo "$KEY_OUTPUT" | awk -F': ' '/PrivateKey/ {print $2}') PRIVATE_KEY=$(echo "$KEY_OUTPUT" | awk -F': ' '/PrivateKey/ {print $2}')
@ -74,6 +93,7 @@ install_singbox() {
"route":{"rules":[{"protocol":"dns","outbound":"dns-out"}]} "route":{"rules":[{"protocol":"dns","outbound":"dns-out"}]}
} }
EOF EOF
cat >"$STATE_FILE" <<EOF cat >"$STATE_FILE" <<EOF
NAME="$NAME" NAME="$NAME"
SNI="$SNI" SNI="$SNI"
@ -86,87 +106,73 @@ PORT="$PORT"
SPX="$SPX" SPX="$SPX"
EOF EOF
# 4. 创建并启动 Systemd 服务 # 启用并启动官方 systemd 单元
cat >"$SERVICE_FILE" <<EOF
[Unit]
Description=sing-box service
After=network.target
[Service]
Type=simple
ExecStart=$BIN_PATH run -c $CONFIG_DIR/config.json
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload systemctl daemon-reload
systemctl enable sing-box.service systemctl enable sing-box.service
systemctl restart sing-box.service systemctl restart sing-box.service
echo "安装并启动完成。"
printf "${GREEN}安装并启动完成。${NC}\n"
} }
# 函数:显示服务状态 # 查看服务状态
status_singbox() { status_singbox() {
if systemctl list-units --full -all | grep -q sing-box.service; then printf "${CYAN}===== Sing-box 服务状态 =====${NC}\n"
if systemctl status sing-box.service &>/dev/null; then
systemctl status sing-box.service --no-pager systemctl status sing-box.service --no-pager
else else
echo "服务未安装。" printf "${YELLOW}服务未安装。${NC}\n"
fi fi
} }
# 函数:显示 VLESS Reality 链接 # 显示 VLESS Reality 链接
show_link() { show_link() {
printf "${CYAN}===== 您的 VLESS Reality 链接 =====${NC}\n"
[[ -f "$STATE_FILE" ]] || { [[ -f "$STATE_FILE" ]] || {
echo "未找到状态文件,请先安装。" printf "${RED}未找到状态文件,请先安装。${NC}\n"
return return
} }
source "$STATE_FILE" source "$STATE_FILE"
LINK="vless://${UUID}@${SERVER_IP}:${PORT}?security=reality&sni=${SNI}&fp=${FP}&pbk=${PUB_KEY}&sid=${SHORT_ID}&spx=${SPX}&type=tcp&flow=xtls-rprx-vision&encryption=none#${NAME}" LINK="vless://${UUID}@${SERVER_IP}:${PORT}?security=reality&sni=${SNI}&fp=${FP}&pbk=${PUB_KEY}&sid=${SHORT_ID}&spx=${SPX}&type=tcp&flow=xtls-rprx-vision&encryption=none#${NAME}"
echo -e "\n====== 您的 VLESS Reality 链接 ======\n$LINK\n" printf "${GREEN}%s${NC}\n\n" "$LINK"
} }
# 函数:卸载 Sing-box # 卸载 Sing-box
uninstall_singbox() { uninstall_singbox() {
if systemctl list-units --full -all | grep -q sing-box.service; then printf "${CYAN}===== 卸载 Sing-box =====${NC}\n"
if systemctl status sing-box.service &>/dev/null; then
systemctl stop sing-box.service systemctl stop sing-box.service
systemctl disable sing-box.service systemctl disable sing-box.service
rm -f "$SERVICE_FILE"
rm -rf "$CONFIG_DIR" rm -rf "$CONFIG_DIR"
echo "卸载完成:已移除配置和服务。" printf "${GREEN}卸载完成:已移除配置。${NC}\n"
else else
echo "服务未安装,无需卸载。" printf "${YELLOW}服务未安装,无需卸载。${NC}\n"
fi fi
} }
# 函数:重新安装 # 重新安装
reinstall_singbox() { reinstall_singbox() {
printf "${CYAN}===== 重新安装 Sing-box =====${NC}\n"
uninstall_singbox uninstall_singbox
install_singbox install_singbox
} }
# 菜单主循环 # 菜单主循环
while true; do while true; do
cat <<EOF printf "${BOLD}${BLUE}请选择操作:${NC}\n"
请选择操作: printf " ${YELLOW}1)${NC} 安装 Sing-box 并生成配置\n"
1) 安装 Sing-box 并生成配置 printf " ${YELLOW}2)${NC} 查看服务状态\n"
2) 查看服务状态 printf " ${YELLOW}3)${NC} 显示 VLESS Reality 链接\n"
3) 显示 VLESS Reality 链接 printf " ${YELLOW}4)${NC} 卸载 Sing-box\n"
4) 卸载 Sing-box printf " ${YELLOW}5)${NC} 重新安装 Sing-box\n"
5) 重新安装 Sing-box printf " ${YELLOW}6)${NC} 退出\n"
6) 退出 printf "${BOLD}输入数字 [1-6]: ${NC}"
EOF read -r choice
read -rp "输入数字 [1-6]: " choice
case "$choice" in case "$choice" in
1) install_singbox ;; 1) install_singbox ;; 2) status_singbox ;; 3) show_link ;; 4) uninstall_singbox ;; 5) reinstall_singbox ;; 6)
2) status_singbox ;; printf "${GREEN}退出。${NC}\n"
3) show_link ;;
4) uninstall_singbox ;;
5) reinstall_singbox ;;
6)
echo "退出。"
exit 0 exit 0
;; ;;
*) echo "无效选项,请重试。" ;; *) printf "${RED}无效选项,请重试。${NC}\n" ;;
esac esac
echo
done done