From 4c5a194d4c4495247e0ab659a959003cf51e9ad6 Mon Sep 17 00:00:00 2001 From: Dich Date: Mon, 5 May 2025 22:53:07 +0800 Subject: [PATCH] add:sh --- singbox.sh | 64 +++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/singbox.sh b/singbox.sh index 56baa49..343695c 100644 --- a/singbox.sh +++ b/singbox.sh @@ -1,19 +1,18 @@ #!/usr/bin/env bash # install_singbox.sh -# 一键安装 Sing-box 并配置为 systemd 服务,自动生成 VLESS Reality 所需字段,用户自定义 name 字段,并输出完整链接 +# 一键安装 Sing-box,并配置 VLESS Reality,自动生成字段、写入配置、创建 Systemd 服务并输出链接 set -euo pipefail -# —— 0. 用户输入 —— # -read -rp "请输入用户名称 (name 字段,例如 AK-JP-100G):" NAME +### 0. 输入名称 ### +read -rp "请输入用户名称 (name 字段,例如 AK-JP-100G): " NAME if [[ -z "$NAME" ]]; then echo "名称不能为空,退出。" >&2 exit 1 fi +echo "使用名称:$NAME" -echo "使用的名称:${NAME}" - -# —— 1. 安装 Sing-box 内核 —— # +### 1. 安装 Sing-box 内核 ### if command -v apt-get &>/dev/null; then echo "检测到 Debian/Ubuntu,使用官方 deb 安装脚本..." bash <(curl -fsSL https://sing-box.app/deb-install.sh) @@ -28,28 +27,35 @@ else exit 1 fi -# 确认路径 +# 确定 sing-box 可执行文件 BIN_PATH=$(command -v sing-box || true) if [[ -z "$BIN_PATH" ]]; then - echo "未找到 sing-box 可执行文件,请检查安装" >&2 + echo "未找到 sing-box,可执行文件路径异常,请检查安装" >&2 exit 1 fi -echo "Sing-box 安装完成:$($BIN_PATH version | head -n1)" +echo "Sing-box 内核版本:$("$BIN_PATH" version | head -n1)" -# —— 2. 生成字段 —— # -UUID=$($BIN_PATH generate uuid) -KEY_OUTPUT=$($BIN_PATH generate reality-keypair) -PRIVATE_KEY=$(echo "$KEY_OUTPUT" | awk -F": " '/PrivateKey/ {print $2}') -PUB_KEY=$(echo "$KEY_OUTPUT" | awk -F": " '/PublicKey/ {print $2}') -# Short ID 随机生成 8 字节 +### 2. 生成 UUID / Reality 密钥 / ShortID / uTLS ### +UUID=$("$BIN_PATH" generate uuid) + +# reality-keypair 输出示例: +# PrivateKey: XXXXXX +# PublicKey: YYYYYY +KEY_OUTPUT=$("$BIN_PATH" generate reality-keypair) +PRIVATE_KEY=$(echo "$KEY_OUTPUT" | awk -F': ' '/PrivateKey/ {print $2}') +PUB_KEY =$(echo "$KEY_OUTPUT" | awk -F': ' '/PublicKey/ {print $2}') + +# Short ID(8 字节随机 hex) SHORT_ID=$(openssl rand -hex 8) -# uTLS 浏览器指纹,可按需修改 + +# uTLS 浏览器指纹(可根据需要调整) FP="chrome" -# —— 3. 写入配置文件 —— # +### 3. 写入配置文件 ### CONFIG_DIR=/etc/singbox mkdir -p "$CONFIG_DIR" -cat > "$CONFIG_DIR/config.json" << EOF + +cat > "$CONFIG_DIR/config.json" < "$CONFIG_DIR/config.json" << EOF "listen": "::", "listen_port": 443, "users": [ - { "name": "${NAME}", "uuid": "${UUID}", "flow": "xtls-rprx-vision" } + { "name": "$NAME", "uuid": "$UUID", "flow": "xtls-rprx-vision" } ], "tls": { "enabled": true, @@ -68,8 +74,8 @@ cat > "$CONFIG_DIR/config.json" << EOF "reality": { "enabled": true, "handshake": { "server": "s0.awsstatic.com", "server_port": 443 }, - "private_key": "${PRIVATE_KEY}", - "short_id": ["${SHORT_ID}"] + "private_key": "$PRIVATE_KEY", + "short_id": ["$SHORT_ID"] } } } @@ -79,38 +85,38 @@ cat > "$CONFIG_DIR/config.json" << EOF } EOF -echo "配置已写入 ${CONFIG_DIR}/config.json" +echo "配置已写入:$CONFIG_DIR/config.json" -# —— 4. 配置 systemd 服务 —— # +### 4. 创建 Systemd 服务 ### SERVICE_FILE=/etc/systemd/system/sing-box.service -cat > "$SERVICE_FILE" << 'EOL' +cat > "$SERVICE_FILE" <