mirror of
https://github.com/Dichgrem/singbox-example.git
synced 2025-07-31 17:19:31 -04:00
update:singbox.sh
This commit is contained in:
235
singbox.sh
235
singbox.sh
@ -1,78 +1,93 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# install_singbox.sh
|
# install_singbox.sh
|
||||||
# 一键安装 Sing-box,并配置 VLESS Reality,自动生成字段、写入配置、创建 Systemd 服务并输出链接
|
# 一键安装 Sing-box,并配置 VLESS Reality,支持菜单操作:安装、状态、显示链接、卸载、重装
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
### 0. 输入名称 ###
|
|
||||||
read -rp "请输入用户名称 (name 字段,例如 AK-JP-100G): " NAME
|
|
||||||
if [[ -z "$NAME" ]]; then
|
|
||||||
echo "名称不能为空,退出。" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
echo "使用名称:$NAME"
|
|
||||||
### 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)
|
|
||||||
elif command -v dnf &>/dev/null || command -v yum &>/dev/null; then
|
|
||||||
echo "检测到 RHEL/CentOS,使用官方 rpm 安装脚本..."
|
|
||||||
bash <(curl -fsSL https://sing-box.app/rpm-install.sh)
|
|
||||||
elif command -v pacman &>/dev/null; then
|
|
||||||
echo "检测到 Arch Linux,使用官方 arch 安装脚本..."
|
|
||||||
bash <(curl -fsSL https://sing-box.app/arch-install.sh)
|
|
||||||
else
|
|
||||||
echo "无法识别发行版,请手动安装 Sing-box 内核" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
# 确定 sing-box 可执行文件
|
|
||||||
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. 生成 UUID / Reality 密钥 / ShortID / uTLS ###
|
|
||||||
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=$(openssl rand -hex 8)
|
|
||||||
FP="chrome"
|
|
||||||
### 3. 写入配置文件 ###
|
|
||||||
CONFIG_DIR=/etc/singbox
|
CONFIG_DIR=/etc/singbox
|
||||||
mkdir -p "$CONFIG_DIR"
|
SERVICE_FILE=/etc/systemd/system/sing-box.service
|
||||||
cat > "$CONFIG_DIR/config.json" <<EOF
|
STATE_FILE="$CONFIG_DIR/state.env"
|
||||||
|
BIN_NAME=sing-box
|
||||||
|
|
||||||
|
# 函数:安装 Sing-box 并生成配置
|
||||||
|
install_singbox() {
|
||||||
|
# 0. 输入名称 & SNI
|
||||||
|
read -rp "请输入用户名称 (name 字段,例如 AK-JP-100G): " NAME
|
||||||
|
[[ -z "$NAME" ]] && {
|
||||||
|
echo "名称不能为空,退出。" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
read -rp "请输入 SNI 域名 (默认: s0.awsstatic.com): " SNI
|
||||||
|
SNI=${SNI:-s0.awsstatic.com}
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
elif command -v dnf &>/dev/null || command -v yum &>/dev/null; then
|
||||||
|
echo "检测到 RHEL/CentOS,使用官方 rpm 安装脚本..."
|
||||||
|
bash <(curl -fsSL https://sing-box.app/rpm-install.sh)
|
||||||
|
elif command -v pacman &>/dev/null; then
|
||||||
|
echo "检测到 Arch Linux,使用官方 arch 安装脚本..."
|
||||||
|
bash <(curl -fsSL https://sing-box.app/arch-install.sh)
|
||||||
|
else
|
||||||
|
echo "无法识别发行版,请手动安装 Sing-box 内核" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
BIN_PATH=$(command -v $BIN_NAME)
|
||||||
|
[[ -z "$BIN_PATH" ]] && {
|
||||||
|
echo "未找到 $BIN_NAME,可执行文件路径异常,请检查安装" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
echo "已安装 $BIN_NAME 版本:$($BIN_PATH version | head -n1)"
|
||||||
|
|
||||||
|
# 2. 生成 UUID / Reality 密钥 / ShortID / uTLS
|
||||||
|
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=$(openssl rand -hex 8)
|
||||||
|
FP="chrome"
|
||||||
|
SERVER_IP=$(curl -s https://ifconfig.me)
|
||||||
|
PORT=443
|
||||||
|
SPX="/"
|
||||||
|
|
||||||
|
# 写入配置和状态
|
||||||
|
mkdir -p "$CONFIG_DIR"
|
||||||
|
cat >"$CONFIG_DIR/config.json" <<EOF
|
||||||
{
|
{
|
||||||
"log": { "level": "info" },
|
"log": {"level": "info"},
|
||||||
"dns": { "servers": [{ "address": "tls://8.8.8.8" }] },
|
"dns": {"servers": [{"address": "tls://8.8.8.8"}]},
|
||||||
"inbounds": [
|
"inbounds": [{
|
||||||
{
|
"type": "vless",
|
||||||
"type": "vless",
|
"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":"$SNI","reality":{
|
||||||
{ "name": "$NAME", "uuid": "$UUID", "flow": "xtls-rprx-vision" }
|
"enabled":true,
|
||||||
],
|
"handshake":{"server":"$SNI","server_port":443},
|
||||||
"tls": {
|
"private_key":"$PRIVATE_KEY",
|
||||||
"enabled": true,
|
"short_id":["$SHORT_ID"]
|
||||||
"server_name": "s0.awsstatic.com",
|
}}
|
||||||
"reality": {
|
}],
|
||||||
"enabled": true,
|
"outbounds":[{"type":"direct"},{"type":"dns","tag":"dns-out"}],
|
||||||
"handshake": { "server": "s0.awsstatic.com", "server_port": 443 },
|
"route":{"rules":[{"protocol":"dns","outbound":"dns-out"}]}
|
||||||
"private_key": "$PRIVATE_KEY",
|
|
||||||
"short_id": ["$SHORT_ID"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"outbounds": [ { "type": "direct" }, { "type": "dns", "tag": "dns-out" } ],
|
|
||||||
"route": { "rules": [ { "protocol": "dns", "outbound": "dns-out" } ] }
|
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
echo "配置已写入:$CONFIG_DIR/config.json"
|
cat >"$STATE_FILE" <<EOF
|
||||||
### 4. 创建 Systemd 服务 ###
|
NAME="$NAME"
|
||||||
SERVICE_FILE=/etc/systemd/system/sing-box.service
|
SNI="$SNI"
|
||||||
cat > "$SERVICE_FILE" <<EOF
|
UUID="$UUID"
|
||||||
|
PUB_KEY="$PUB_KEY"
|
||||||
|
SHORT_ID="$SHORT_ID"
|
||||||
|
FP="$FP"
|
||||||
|
SERVER_IP="$SERVER_IP"
|
||||||
|
PORT="$PORT"
|
||||||
|
SPX="$SPX"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# 4. 创建并启动 Systemd 服务
|
||||||
|
cat >"$SERVICE_FILE" <<EOF
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=sing-box service
|
Description=sing-box service
|
||||||
After=network.target
|
After=network.target
|
||||||
@ -84,16 +99,74 @@ LimitNOFILE=65536
|
|||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
EOF
|
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 "安装并启动完成。"
|
||||||
echo "服务状态:"
|
}
|
||||||
systemctl status sing-box.service --no-pager
|
|
||||||
### 5. 输出最终 VLESS Reality 链接 ###
|
# 函数:显示服务状态
|
||||||
SERVER_IP=$(curl -s https://ifconfig.me)
|
status_singbox() {
|
||||||
PORT=443
|
if systemctl list-units --full -all | grep -q sing-box.service; then
|
||||||
SNI="s0.awsstatic.com"
|
systemctl status sing-box.service --no-pager
|
||||||
SPX="/"
|
else
|
||||||
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\n"
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# 函数:显示 VLESS Reality 链接
|
||||||
|
show_link() {
|
||||||
|
[[ -f "$STATE_FILE" ]] || {
|
||||||
|
echo "未找到状态文件,请先安装。"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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}"
|
||||||
|
echo -e "\n====== 您的 VLESS Reality 链接 ======\n$LINK\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 函数:卸载 Sing-box
|
||||||
|
uninstall_singbox() {
|
||||||
|
if systemctl list-units --full -all | grep -q sing-box.service; then
|
||||||
|
systemctl stop sing-box.service
|
||||||
|
systemctl disable sing-box.service
|
||||||
|
rm -f "$SERVICE_FILE"
|
||||||
|
rm -rf "$CONFIG_DIR"
|
||||||
|
echo "卸载完成:已移除配置和服务。"
|
||||||
|
else
|
||||||
|
echo "服务未安装,无需卸载。"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# 函数:重新安装
|
||||||
|
reinstall_singbox() {
|
||||||
|
uninstall_singbox
|
||||||
|
install_singbox
|
||||||
|
}
|
||||||
|
|
||||||
|
# 菜单主循环
|
||||||
|
while true; do
|
||||||
|
cat <<EOF
|
||||||
|
请选择操作:
|
||||||
|
1) 安装 Sing-box 并生成配置
|
||||||
|
2) 查看服务状态
|
||||||
|
3) 显示 VLESS Reality 链接
|
||||||
|
4) 卸载 Sing-box
|
||||||
|
5) 重新安装 Sing-box
|
||||||
|
6) 退出
|
||||||
|
EOF
|
||||||
|
read -rp "输入数字 [1-6]: " choice
|
||||||
|
case "$choice" in
|
||||||
|
1) install_singbox ;;
|
||||||
|
2) status_singbox ;;
|
||||||
|
3) show_link ;;
|
||||||
|
4) uninstall_singbox ;;
|
||||||
|
5) reinstall_singbox ;;
|
||||||
|
6)
|
||||||
|
echo "退出。"
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
*) echo "无效选项,请重试。" ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user