mirror of
https://github.com/Dichgrem/singbox-example.git
synced 2025-08-01 17:49:30 -04:00
add:sh
This commit is contained in:
69
singbox.sh
69
singbox.sh
@ -4,17 +4,16 @@
|
|||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
### —— 0. 用户输入 —— ###
|
# —— 0. 用户输入 —— #
|
||||||
read -rp "请输入用户名称 (name 字段,例如 AK-JP-100G):" NAME
|
read -rp "请输入用户名称 (name 字段,例如 AK-JP-100G):" NAME
|
||||||
if [[ -z "$NAME" ]]; then
|
if [[ -z "$NAME" ]]; then
|
||||||
echo "名称不能为空,退出。" >&2
|
echo "名称不能为空,退出。" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "将使用的 name 字段:${NAME}"
|
echo "使用的名称:${NAME}"
|
||||||
|
|
||||||
### —— 1. 安装 Sing-box —— ###
|
# —— 1. 安装 Sing-box 内核 —— #
|
||||||
# 使用官方一键脚本安装内核
|
|
||||||
if command -v apt-get &>/dev/null; then
|
if command -v apt-get &>/dev/null; then
|
||||||
echo "检测到 Debian/Ubuntu,使用官方 deb 安装脚本..."
|
echo "检测到 Debian/Ubuntu,使用官方 deb 安装脚本..."
|
||||||
bash <(curl -fsSL https://sing-box.app/deb-install.sh)
|
bash <(curl -fsSL https://sing-box.app/deb-install.sh)
|
||||||
@ -25,30 +24,31 @@ elif command -v pacman &>/dev/null; then
|
|||||||
echo "检测到 Arch Linux,使用官方 arch 安装脚本..."
|
echo "检测到 Arch Linux,使用官方 arch 安装脚本..."
|
||||||
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
|
echo "无法识别发行版,请手动安装 Sing-box 内核" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Sing-box 安装完成:$(sing-box version)"
|
# 确认路径
|
||||||
|
BIN_PATH=$(command -v sing-box || true)
|
||||||
|
if [[ -z "$BIN_PATH" ]]; then
|
||||||
|
echo "未找到 sing-box 可执行文件,请检查安装" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "Sing-box 安装完成:$($BIN_PATH version | head -n1)"
|
||||||
|
|
||||||
### —— 2. 生成配置及字段 —— ###
|
# —— 2. 生成字段 —— #
|
||||||
# UUID
|
UUID=$($BIN_PATH generate uuid)
|
||||||
UUID=$(sing-box generate uuid)
|
KEY_OUTPUT=$($BIN_PATH generate reality-keypair)
|
||||||
# Reality 密钥对
|
|
||||||
KEY_OUTPUT=$(sing-box generate reality-keypair)
|
|
||||||
PRIVATE_KEY=$(echo "$KEY_OUTPUT" | awk -F": " '/PrivateKey/ {print $2}')
|
PRIVATE_KEY=$(echo "$KEY_OUTPUT" | awk -F": " '/PrivateKey/ {print $2}')
|
||||||
PUB_KEY=$(echo "$KEY_OUTPUT" | awk -F": " '/PublicKey/ {print $2}')
|
PUB_KEY=$(echo "$KEY_OUTPUT" | awk -F": " '/PublicKey/ {print $2}')
|
||||||
# Short ID
|
# Short ID 随机生成 8 字节
|
||||||
SHORT_ID=$(openssl rand -hex 8)
|
SHORT_ID=$(openssl rand -hex 8)
|
||||||
# uTLS 浏览器指纹,可自定义
|
# uTLS 浏览器指纹,可按需修改
|
||||||
FP="chrome"
|
FP="chrome"
|
||||||
|
|
||||||
# 配置目录
|
# —— 3. 写入配置文件 —— #
|
||||||
CONFIG_DIR=/etc/singbox
|
CONFIG_DIR=/etc/singbox
|
||||||
echo "创建配置目录:${CONFIG_DIR}"
|
|
||||||
mkdir -p "$CONFIG_DIR"
|
mkdir -p "$CONFIG_DIR"
|
||||||
|
|
||||||
# 写入配置文件
|
|
||||||
cat > "$CONFIG_DIR/config.json" << EOF
|
cat > "$CONFIG_DIR/config.json" << EOF
|
||||||
{
|
{
|
||||||
"log": { "level": "info" },
|
"log": { "level": "info" },
|
||||||
@ -59,8 +59,12 @@ cat > "$CONFIG_DIR/config.json" << EOF
|
|||||||
"tag": "VLESSReality",
|
"tag": "VLESSReality",
|
||||||
"listen": "::",
|
"listen": "::",
|
||||||
"listen_port": 443,
|
"listen_port": 443,
|
||||||
"users": [ { "name": "${NAME}", "uuid": "${UUID}", "flow": "xtls-rprx-vision" } ],
|
"users": [
|
||||||
"tls": { "enabled": true, "server_name": "s0.awsstatic.com",
|
{ "name": "${NAME}", "uuid": "${UUID}", "flow": "xtls-rprx-vision" }
|
||||||
|
],
|
||||||
|
"tls": {
|
||||||
|
"enabled": true,
|
||||||
|
"server_name": "s0.awsstatic.com",
|
||||||
"reality": {
|
"reality": {
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"handshake": { "server": "s0.awsstatic.com", "server_port": 443 },
|
"handshake": { "server": "s0.awsstatic.com", "server_port": 443 },
|
||||||
@ -75,41 +79,38 @@ cat > "$CONFIG_DIR/config.json" << EOF
|
|||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
echo "配置文件已写入:${CONFIG_DIR}/config.json"
|
echo "配置已写入 ${CONFIG_DIR}/config.json"
|
||||||
|
|
||||||
### —— 3. 创建并启动 systemd 服务 —— ###
|
# —— 4. 配置 systemd 服务 —— #
|
||||||
SERVICE_FILE=/etc/systemd/system/singbox.service
|
SERVICE_FILE=/etc/systemd/system/sing-box.service
|
||||||
cat > "$SERVICE_FILE" << 'EOF'
|
cat > "$SERVICE_FILE" << 'EOL'
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=sing-box service
|
Description=sing-box service
|
||||||
After=network.target
|
After=network.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=simple
|
Type=simple
|
||||||
ExecStart=/usr/local/bin/sing-box run -c /etc/singbox/config.json
|
ExecStart='$(command -v sing-box)' run -c /etc/singbox/config.json
|
||||||
Restart=on-failure
|
Restart=on-failure
|
||||||
LimitNOFILE=65536
|
LimitNOFILE=65536
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
EOF
|
EOL
|
||||||
|
|
||||||
|
# 重载并启动
|
||||||
systemctl daemon-reload
|
systemctl daemon-reload
|
||||||
if ! systemctl is-enabled singbox.service &>/dev/null; then
|
systemctl enable sing-box.service
|
||||||
systemctl enable singbox.service
|
systemctl restart sing-box.service
|
||||||
fi
|
|
||||||
systemctl restart singbox.service
|
|
||||||
|
|
||||||
echo "服务已启动:"
|
echo "服务已启动:"
|
||||||
systemctl status singbox.service --no-pager
|
systemctl status sing-box.service --no-pager
|
||||||
|
|
||||||
### —— 4. 输出 VLESS Reality 链接 —— ###
|
# —— 5. 输出 VLESS Reality 链接 —— #
|
||||||
SERVER_IP=$(curl -s https://ifconfig.me)
|
SERVER_IP=$(curl -s https://ifconfig.me)
|
||||||
PORT=443
|
PORT=443
|
||||||
SNI="s0.awsstatic.com"
|
SNI="s0.awsstatic.com"
|
||||||
SPX="/"
|
SPX="/"
|
||||||
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
|
echo -e "\n====== 您的 VLESS Reality 链接 ======\n$LINK"
|
||||||
echo "====== 您的 VLESS Reality 链接 ======"
|
|
||||||
echo "$LINK"
|
|
||||||
|
Reference in New Issue
Block a user