mirror of
https://github.com/Dichgrem/singbox-example.git
synced 2025-07-31 00:59:32 -04:00
Compare commits
7 Commits
3dc0030a87
...
0866fc0647
Author | SHA1 | Date | |
---|---|---|---|
0866fc0647 | |||
9e6053e987 | |||
14ca45f223 | |||
d2125fc06c | |||
d6f174867b | |||
7f037c150a | |||
5d49d6ccb0 |
@ -6,7 +6,7 @@ Sing-box 是一个现代、高性能、功能强大的跨平台代理核心,
|
||||
|
||||
- 手动安装:见 [Hysteria2](https://github.com/Dichgrem/singbox-example/blob/main/hysteria2.md) / [Reality](https://github.com/Dichgrem/singbox-example/blob/main/singbox.md)
|
||||
- 全自动安装Reality:
|
||||
``wget https://raw.githubusercontent.com/Dichgrem/singbox-example/refs/heads/main/singbox.sh``,随后``bash ./singbox.sh``
|
||||
``wget https://raw.githubusercontent.com/Dichgrem/singbox-example/refs/heads/main/script/singbox.sh``,随后``bash ./singbox.sh``
|
||||
- 全自动安装Hysteria2:
|
||||
``wget https://raw.githubusercontent.com/Dichgrem/singbox-example/refs/heads/main/hysteria2.sh``,随后``bash ./hysteria2.sh``
|
||||
- VPS一键更换密钥登录:``wget https://raw.githubusercontent.com/Dichgrem/singbox-example/refs/heads/main/ssh-key.sh``,随后``bash ./ssh-key.sh``
|
||||
``wget https://raw.githubusercontent.com/Dichgrem/singbox-example/refs/heads/main/script/hysteria2.sh``,随后``bash ./hysteria2.sh``
|
||||
- VPS一键更换密钥登录:``wget https://raw.githubusercontent.com/Dichgrem/singbox-example/refs/heads/main/script/ssh-key.sh``,随后``bash ./ssh-key.sh``
|
||||
|
236
script/singbox.sh
Normal file
236
script/singbox.sh
Normal file
@ -0,0 +1,236 @@
|
||||
#!/usr/bin/env bash
|
||||
# install_singbox.sh
|
||||
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
|
||||
STATE_FILE="$CONFIG_DIR/state.env"
|
||||
BIN_NAME=sing-box
|
||||
|
||||
# 检查本地与远程版本,并提示
|
||||
check_update() {
|
||||
if command -v curl &>/dev/null && command -v grep &>/dev/null; then
|
||||
LOCAL_VER=$($BIN_NAME version 2>/dev/null | head -n1 | awk '{print $NF}') || LOCAL_VER="未安装"
|
||||
LATEST_VER=$(curl -s https://api.github.com/repos/SagerNet/sing-box/releases/latest |
|
||||
grep '"tag_name"' | head -n1 | cut -d '"' -f4 | sed 's/^v//') || LATEST_VER="未知"
|
||||
if [[ "$LOCAL_VER" != "$LATEST_VER" ]]; then
|
||||
printf "${YELLOW}检测到新版本:${LATEST_VER},当前版本:${LOCAL_VER}。请选择 6) 升级 Sing-box 二进制。${NC}\n"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# 升级/安装 Sing-box 二进制
|
||||
update_singbox() {
|
||||
printf "${CYAN}===== 升级/安装 Sing-box 二进制 =====${NC}\n"
|
||||
if command -v apt-get &>/dev/null; then
|
||||
bash <(curl -fsSL https://sing-box.app/deb-install.sh)
|
||||
elif command -v dnf &>/dev/null || command -v yum &>/dev/null; then
|
||||
bash <(curl -fsSL https://sing-box.app/rpm-install.sh)
|
||||
elif command -v pacman &>/dev/null; then
|
||||
bash <(curl -fsSL https://sing-box.app/arch-install.sh)
|
||||
else
|
||||
printf "${RED}无法识别发行版,请手动升级 Sing-box 二进制${NC}\n" >&2
|
||||
return 1
|
||||
fi
|
||||
hash -r
|
||||
NEW_VER=$($BIN_NAME version | head -n1 | awk '{print $NF}')
|
||||
printf "${GREEN}Sing-box 已升级到版本:%s${NC}\n" "$NEW_VER"
|
||||
printf "${CYAN}重启服务...${NC}\n"
|
||||
if systemctl restart sing-box.service; then
|
||||
printf "${GREEN}服务已重启。${NC}\n"
|
||||
else
|
||||
printf "${YELLOW}服务重启失败,请手动检查。${NC}\n"
|
||||
fi
|
||||
}
|
||||
|
||||
# 安装 Sing-box 并生成配置
|
||||
install_singbox() {
|
||||
printf "${CYAN}===== 安装 Sing-box 并生成配置 =====${NC}\n"
|
||||
printf "${YELLOW}请输入用户名称 (name 字段,例如 AK-JP-100G):${NC}"
|
||||
read -r NAME
|
||||
[[ -z "$NAME" ]] && {
|
||||
printf "${RED}名称不能为空,退出。${NC}\n" >&2
|
||||
exit 1
|
||||
}
|
||||
printf "${YELLOW}请输入 SNI 域名 (默认: s0.awsstatic.com):${NC}"
|
||||
read -r SNI
|
||||
SNI=${SNI:-s0.awsstatic.com}
|
||||
|
||||
update_singbox
|
||||
hash -r
|
||||
BIN_PATH=$(command -v $BIN_NAME || true)
|
||||
[[ -z "$BIN_PATH" ]] && {
|
||||
printf "${RED}未找到 $BIN_NAME,可执行文件路径异常,请检查安装${NC}\n" >&2
|
||||
exit 1
|
||||
}
|
||||
VERSION=$($BIN_PATH version | head -n1 | awk '{print $NF}')
|
||||
printf "${GREEN}已安装/更新 sing-box 版本:%s${NC}\n" "$VERSION"
|
||||
|
||||
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"},
|
||||
"dns": {"servers": [{"address": "tls://8.8.8.8"}]},
|
||||
"inbounds": [{
|
||||
"type": "vless",
|
||||
"tag": "VLESSReality",
|
||||
"listen": "::",
|
||||
"listen_port": 443,
|
||||
"users": [{"name":"$NAME","uuid":"$UUID","flow":"xtls-rprx-vision"}],
|
||||
"tls": {"enabled":true,"server_name":"$SNI","reality":{
|
||||
"enabled":true,
|
||||
"handshake": {"server":"$SNI","server_port":443},
|
||||
"private_key":"$PRIVATE_KEY",
|
||||
"short_id":["$SHORT_ID"]
|
||||
}}
|
||||
}],
|
||||
"outbounds": [{"type":"direct"},{"type":"dns","tag":"dns-out"}],
|
||||
"route": {"rules": [{"protocol":"dns","outbound":"dns-out"}]}
|
||||
}
|
||||
EOF
|
||||
|
||||
cat >"$STATE_FILE" <<EOF
|
||||
NAME="$NAME"
|
||||
SNI="$SNI"
|
||||
UUID="$UUID"
|
||||
PUB_KEY="$PUB_KEY"
|
||||
SHORT_ID="$SHORT_ID"
|
||||
FP="$FP"
|
||||
SERVER_IP="$SERVER_IP"
|
||||
PORT="$PORT"
|
||||
SPX="$SPX"
|
||||
EOF
|
||||
|
||||
systemctl enable sing-box.service
|
||||
systemctl restart sing-box.service
|
||||
printf "${GREEN}安装并启动完成。${NC}\n"
|
||||
}
|
||||
|
||||
# 更换 SNI 域名
|
||||
change_sni() {
|
||||
printf "${CYAN}===== 更换 SNI 域名 =====${NC}\n"
|
||||
[[ -f "$CONFIG_DIR/config.json" ]] || {
|
||||
printf "${RED}配置文件不存在,请先安装。${NC}\n"
|
||||
return
|
||||
}
|
||||
|
||||
printf "${YELLOW}请输入新的 SNI 域名 (当前: $(
|
||||
source "$STATE_FILE"
|
||||
echo "$SNI"
|
||||
)):${NC}"
|
||||
read -r NEW_SNI
|
||||
[[ -z "$NEW_SNI" ]] && {
|
||||
printf "${RED}SNI 域名不能为空,取消更换。${NC}\n"
|
||||
return
|
||||
}
|
||||
|
||||
# 替换 config.json 中的 SNI 字段
|
||||
sed -i "s/\"server_name\":\s*\"[^\"]*\"/\"server_name\": \"$NEW_SNI\"/" "$CONFIG_DIR/config.json"
|
||||
sed -i "s/\"server\":\s*\"[^\"]*\"/\"server\": \"$NEW_SNI\"/" "$CONFIG_DIR/config.json"
|
||||
|
||||
# 替换 state.env 中的 SNI
|
||||
sed -i "s/^SNI=.*/SNI=\"$NEW_SNI\"/" "$STATE_FILE"
|
||||
|
||||
systemctl restart sing-box.service &&
|
||||
printf "${GREEN}SNI 已更换为 $NEW_SNI,服务已重启。${NC}\n" ||
|
||||
printf "${RED}服务重启失败,请手动检查。${NC}\n"
|
||||
}
|
||||
|
||||
# 查看服务状态
|
||||
status_singbox() {
|
||||
printf "${CYAN}===== Sing-box 服务状态 =====${NC}\n"
|
||||
if systemctl status sing-box.service &>/dev/null; then
|
||||
systemctl status sing-box.service --no-pager
|
||||
else
|
||||
printf "${YELLOW}服务未安装。${NC}\n"
|
||||
fi
|
||||
}
|
||||
|
||||
# 显示 VLESS Reality 链接
|
||||
show_link() {
|
||||
printf "${CYAN}===== 您的 VLESS Reality 链接 =====${NC}\n"
|
||||
[[ -f "$STATE_FILE" ]] || {
|
||||
printf "${RED}未找到状态文件,请先安装。${NC}\n"
|
||||
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}"
|
||||
printf "${GREEN}%s${NC}\n\n" "$LINK"
|
||||
}
|
||||
|
||||
# 卸载 Sing-box
|
||||
uninstall_singbox() {
|
||||
printf "${CYAN}===== 卸载 Sing-box =====${NC}\n"
|
||||
systemctl stop sing-box.service 2>/dev/null || true
|
||||
systemctl disable sing-box.service 2>/dev/null || true
|
||||
rm -rf "$CONFIG_DIR"
|
||||
if command -v apt-get &>/dev/null; then
|
||||
apt-get remove -y sing-box
|
||||
elif command -v yum &>/dev/null; then
|
||||
yum remove -y sing-box
|
||||
elif command -v pacman &>/dev/null; then
|
||||
pacman -Rss --noconfirm sing-box
|
||||
fi
|
||||
printf "${GREEN}卸载完成。${NC}\n"
|
||||
}
|
||||
|
||||
# 重新安装
|
||||
reinstall_singbox() {
|
||||
uninstall_singbox
|
||||
install_singbox
|
||||
}
|
||||
|
||||
# 菜单主循环
|
||||
check_update
|
||||
while true; do
|
||||
printf "${BOLD}${BLUE}请选择操作:${NC}\n"
|
||||
printf " ${YELLOW}1)${NC} 安装 Sing-box 并生成配置\n"
|
||||
printf " ${YELLOW}2)${NC} 查看服务状态\n"
|
||||
printf " ${YELLOW}3)${NC} 显示 VLESS Reality 链接\n"
|
||||
printf " ${YELLOW}4)${NC} 卸载 Sing-box\n"
|
||||
printf " ${YELLOW}5)${NC} 重新安装 Sing-box\n"
|
||||
printf " ${YELLOW}6)${NC} 升级 Sing-box 二进制\n"
|
||||
printf " ${YELLOW}7)${NC} 更换 SNI 域名\n"
|
||||
printf " ${YELLOW}8)${NC} 退出\n"
|
||||
printf "${BOLD}输入数字 [1-8]: ${NC}"
|
||||
read -r choice
|
||||
case "$choice" in
|
||||
1) install_singbox ;;
|
||||
2) status_singbox ;;
|
||||
3) show_link ;;
|
||||
4) uninstall_singbox ;;
|
||||
5) reinstall_singbox ;;
|
||||
6) update_singbox ;;
|
||||
7) change_sni ;;
|
||||
8)
|
||||
printf "${GREEN}退出。${NC}\n"
|
||||
exit 0
|
||||
;;
|
||||
*) printf "${RED}无效选项,请重试。${NC}\n" ;;
|
||||
esac
|
||||
echo
|
||||
done
|
172
singbox.sh
172
singbox.sh
@ -1,172 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# install_singbox.sh
|
||||
# 一键安装 Sing-box,并配置 VLESS Reality,支持菜单操作:安装、状态、显示链接、卸载、重装
|
||||
set -euo pipefail
|
||||
|
||||
CONFIG_DIR=/etc/singbox
|
||||
SERVICE_FILE=/etc/systemd/system/sing-box.service
|
||||
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"},
|
||||
"dns": {"servers": [{"address": "tls://8.8.8.8"}]},
|
||||
"inbounds": [{
|
||||
"type": "vless",
|
||||
"tag": "VLESSReality",
|
||||
"listen": "::",
|
||||
"listen_port": 443,
|
||||
"users": [{"name":"$NAME","uuid":"$UUID","flow":"xtls-rprx-vision"}],
|
||||
"tls": {"enabled":true,"server_name":"$SNI","reality":{
|
||||
"enabled":true,
|
||||
"handshake":{"server":"$SNI","server_port":443},
|
||||
"private_key":"$PRIVATE_KEY",
|
||||
"short_id":["$SHORT_ID"]
|
||||
}}
|
||||
}],
|
||||
"outbounds":[{"type":"direct"},{"type":"dns","tag":"dns-out"}],
|
||||
"route":{"rules":[{"protocol":"dns","outbound":"dns-out"}]}
|
||||
}
|
||||
EOF
|
||||
cat >"$STATE_FILE" <<EOF
|
||||
NAME="$NAME"
|
||||
SNI="$SNI"
|
||||
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]
|
||||
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 enable sing-box.service
|
||||
systemctl restart sing-box.service
|
||||
echo "安装并启动完成。"
|
||||
}
|
||||
|
||||
# 函数:显示服务状态
|
||||
status_singbox() {
|
||||
if systemctl list-units --full -all | grep -q sing-box.service; then
|
||||
systemctl status sing-box.service --no-pager
|
||||
else
|
||||
echo "服务未安装。"
|
||||
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