#!/usr/bin/env bash # server-init:在一台新机器上一键部署 VLESS + Reality(sing-box),输出二维码与订阅链接。 # 安装: bash <(curl -fsSL https://server-init.pflm.net) # 管理: server-init show | rotate | sub | uninstall # 可选环境变量:SI_PORT=端口 SI_SNI=伪装域名 SI_NO_SUB=1(不生成订阅) SB_VERSION=1.x.y set -euo pipefail API="${SERVER_INIT_API:-https://server-init.pflm.net}" BASE=/etc/server-init STATE=$BASE/state.env BIN_DIR=/opt/server-init SB=$BIN_DIR/sing-box SVC=server-init-reality SVC_USER=server-init CLI=/usr/local/bin/server-init SNI_CANDIDATES="www.microsoft.com www.apple.com gateway.icloud.com itunes.apple.com swdist.apple.com www.amazon.com www.nvidia.com www.amd.com www.samsung.com www.sony.com www.oracle.com www.mozilla.org addons.mozilla.org dl.google.com www.tesla.com s0.awsstatic.com d1.awsstatic.com www.lovelive-anime.jp www.yahoo.co.jp" G='\033[32m'; Y='\033[33m'; R='\033[31m'; N='\033[0m' say() { printf "${G}==>${N} %s\n" "$*"; } warn() { printf "${Y}[!]${N} %s\n" "$*" >&2; } die() { printf "${R}[x]${N} %s\n" "$*" >&2; exit 1; } need_root() { [ "$(id -u)" = 0 ] || die "请用 root 运行(先 sudo -i)"; } check_os() { [ -r /etc/os-release ] || die "无法识别系统" . /etc/os-release case " ${ID:-} ${ID_LIKE:-} " in *" debian "*|*" ubuntu "*) ;; *) die "只支持 Ubuntu 22.04+ / Debian 12+,当前:${PRETTY_NAME:-unknown}" ;; esac command -v systemctl >/dev/null || die "需要 systemd" case "$(uname -m)" in x86_64|amd64) ARCH=amd64 ;; aarch64|arm64) ARCH=arm64 ;; *) die "不支持的架构:$(uname -m)" ;; esac } install_deps() { local pk=() p for p in curl:curl qrencode:qrencode tar:tar ss:iproute2; do command -v "${p%%:*}" >/dev/null || pk+=("${p#*:}") done [ -e /etc/ssl/certs/ca-certificates.crt ] || pk+=(ca-certificates) if [ ${#pk[@]} -gt 0 ]; then say "安装依赖:${pk[*]}" export DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a NEEDRESTART_SUSPEND=1 local log; log=$(mktemp) { apt-get update -qq && apt-get install -y -qq "${pk[@]}"; } >"$log" 2>&1 || { tail -20 "$log" >&2; die "安装依赖失败"; } rm -f "$log" fi } install_singbox() { local ver="${SB_VERSION:-}" tmp if [ -z "$ver" ]; then ver=$(curl -fsSLI -o /dev/null -w '%{url_effective}' https://github.com/SagerNet/sing-box/releases/latest | sed -n 's#.*/tag/v##p') [ -n "$ver" ] || die "获取 sing-box 版本失败(GitHub 不可达?)" fi if [ -x "$SB" ] && "$SB" version 2>/dev/null | grep -q "version $ver\$"; then return; fi say "安装 sing-box $ver ($ARCH)" tmp=$(mktemp -d) curl -fsSL --retry 3 -o "$tmp/sb.tgz" \ "https://github.com/SagerNet/sing-box/releases/download/v$ver/sing-box-$ver-linux-$ARCH.tar.gz" || die "下载 sing-box 失败" tar -xzf "$tmp/sb.tgz" -C "$tmp" install -d -m 755 "$BIN_DIR" install -m 755 "$tmp/sing-box-$ver-linux-$ARCH/sing-box" "$SB" rm -rf "$tmp" } detect_ip() { local r r=$(curl -4 -fsS --max-time 8 "$API/ip" 2>/dev/null || true) [ -n "$r" ] || r=$(curl -6 -fsS --max-time 8 "$API/ip" 2>/dev/null || true) [ -n "$r" ] || die "获取公网 IP 失败" IP=${r%% *}; GEO=${r#"$IP"} local tail=${IP##*[.:]} NAME=$(printf '%s-%s' "$GEO" "$tail" | tr -s ' ' '-' | tr -cd 'A-Za-z0-9-' | sed 's/^-*//') [ -n "$NAME" ] || NAME="node-$tail" } port_free() { ! ss -Hltn "sport = :$1" | grep -q .; } pick_port() { if [ -n "${SI_PORT:-}" ]; then port_free "$SI_PORT" || die "端口 $SI_PORT 已被占用"; PORT=$SI_PORT; return; fi if port_free 443; then PORT=443; return; fi local i p for i in $(seq 50); do p=$(( (RANDOM * 32768 + RANDOM) % 40000 + 20000 )) if port_free "$p"; then PORT=$p; return; fi done die "找不到可用端口" } pick_sni() { if [ -n "${SI_SNI:-}" ]; then SNI=$SI_SNI; return; fi say "测试伪装域名(要求 TLS1.3 + H2,选握手最快的)" local tmp d f out best="" bt=99 tmp=$(mktemp -d) for d in $SNI_CANDIDATES; do ( curl -so /dev/null --tlsv1.3 --http2 --max-time 5 -w '%{http_version} %{time_appconnect}' "https://$d/" >"$tmp/$d" 2>/dev/null || rm -f "$tmp/$d" ) & done wait for f in "$tmp"/*; do [ -s "$f" ] || continue read -r v t <"$f" || true [ "$v" = 2 ] || continue if awk -v a="$t" -v b="$bt" 'BEGIN{exit !(a>0 && a/dev/null 2>&1 || useradd --system --no-create-home --shell /usr/sbin/nologin "$SVC_USER" install -d -m 750 -o root -g "$SVC_USER" "$BASE" umask 027 cat >"$BASE/config.json" </etc/systemd/system/$SVC.service <&2; die "服务启动失败"; } } open_firewall() { if command -v ufw >/dev/null && ufw status 2>/dev/null | grep -q "Status: active"; then ufw allow "$PORT"/tcp comment server-init >/dev/null; say "ufw 已放行 $PORT/tcp" elif command -v firewall-cmd >/dev/null && firewall-cmd --state >/dev/null 2>&1; then firewall-cmd -q --permanent --add-port="$PORT"/tcp && firewall-cmd -q --reload; say "firewalld 已放行 $PORT/tcp" fi } close_firewall() { if command -v ufw >/dev/null && ufw status 2>/dev/null | grep -q "Status: active"; then ufw delete allow "$PORT"/tcp >/dev/null 2>&1 || true elif command -v firewall-cmd >/dev/null && firewall-cmd --state >/dev/null 2>&1; then firewall-cmd -q --permanent --remove-port="$PORT"/tcp 2>/dev/null && firewall-cmd -q --reload || true fi } enable_bbr() { modprobe tcp_bbr 2>/dev/null || true if sysctl -n net.ipv4.tcp_available_congestion_control 2>/dev/null | grep -qw bbr; then printf 'net.core.default_qdisc = fq\nnet.ipv4.tcp_congestion_control = bbr\n' >/etc/sysctl.d/99-server-init-bbr.conf sysctl -q -p /etc/sysctl.d/99-server-init-bbr.conf >/dev/null 2>&1 || true fi } try_connect() { # $1 = 连接目标地址 local lp cfg pid code lp=$(( RANDOM % 10000 + 40000 )) cfg=$(mktemp) cat >"$cfg" </dev/null 2>&1 & pid=$! sleep 2 code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 -x "socks5h://127.0.0.1:$lp" https://www.gstatic.com/generate_204 || true) kill "$pid" 2>/dev/null || true; wait "$pid" 2>/dev/null || true rm -f "$cfg" [ "$code" = 204 ] } self_test() { say "自检:模拟客户端连接" if try_connect "$IP"; then say "自检通过(经公网 IP)" elif try_connect 127.0.0.1; then warn "本机自检通过,但经公网 IP 回连失败。部分机房不支持回连,一般不影响使用;若手机连不上,请在云厂商安全组放行 $PORT/tcp" else journalctl -u "$SVC" -n 20 --no-pager >&2; die "自检失败" fi } make_link() { local h=$IP [[ $IP == *:* ]] && h="[$IP]" LINK="vless://$UUID@$h:$PORT?encryption=none&flow=xtls-rprx-vision&security=reality&sni=$SNI&fp=chrome&pbk=$PUB&sid=$SID&type=tcp#$NAME" } json_field() { sed -n "s/.*\"$1\":\"\([^\"]*\)\".*/\1/p"; } sub_push() { [ "${SI_NO_SUB:-0}" = 1 ] && return 0 local body r body=$(printf '{"name":"%s","links":["%s"]}' "$NAME" "$LINK") if [ -n "${SUB_ID:-}" ]; then curl -fsS --max-time 10 -X PUT -H "X-Edit-Key: $EDIT_KEY" -H 'Content-Type: application/json' \ --data "$body" "$API/api/sub/$SUB_ID" >/dev/null || warn "订阅更新失败,稍后运行 server-init sub 重试" else r=$(curl -fsS --max-time 10 -X POST -H 'Content-Type: application/json' --data "$body" "$API/api/sub") \ || { warn "订阅生成失败,稍后运行 server-init sub 重试"; return 0; } SUB_ID=$(json_field id <<<"$r"); EDIT_KEY=$(json_field key <<<"$r") fi } sub_delete() { [ -n "${SUB_ID:-}" ] || return 0 curl -fsS --max-time 10 -X DELETE -H "X-Edit-Key: $EDIT_KEY" "$API/api/sub/$SUB_ID" >/dev/null 2>&1 || warn "删除订阅失败" } save_state() { ( umask 077; cat >"$STATE" </dev/null || true rm -f /etc/systemd/system/$SVC.service; systemctl daemon-reload close_firewall sub_delete rm -rf "$BASE" "$BIN_DIR" userdel "$SVC_USER" 2>/dev/null || true rm -f "$CLI" say "已卸载(BBR 设置保留在 /etc/sysctl.d/99-server-init-bbr.conf)" } case "${1:-install}" in install) do_install ;; show) need_root; show ;; rotate) do_rotate ;; sub) do_sub ;; uninstall) do_uninstall ;; *) echo "用法:server-init [install|show|rotate|sub|uninstall]"; exit 1 ;; esac