#!/usr/bin/env bash # https://github.com/GeorgianaBlake/Hysteria2 # Hysteria2一键管理脚本:安装/更新/查看/更改端口/更改密码/删除 # 适配 Debian/Ubuntu (apt) 与 CentOS/RHEL/Alma/Rocky (yum/dnf) set -euo pipefail SERVICE="hysteria-server.service" # Hysteria2服务 CONF_DIR="/etc/hysteria" # 配置目录 CONF_FILE="${CONF_DIR}/config.yaml" # 主配置文件 CLIENT_FILE="${CONF_DIR}/hyclient.txt" # 文本清单 TZ_DEFAULT="Asia/Shanghai" # 默认时区 SHELL_VERSION="0.1.0" # 版本 H2_SNI="bing.com" # 伪装域名 H2_ALIASES="Hysteria2" # 别名 # 字体颜色配置 Font="\033[0m" Black="\033[30m" # 黑色 Red="\033[31m" # 红色 Green="\033[32m" # 绿色 Yellow="\033[33m" # 黄色 Blue="\033[34m" # 蓝色 Magenta="\033[35m" # 紫/洋红 Cyan="\033[36m" # 青 White="\033[37m" # 白色 BBlack="\033[90m" BRed="\033[91m" BGreen="\033[92m" BYellow="\033[93m" BBlue="\033[94m" BMagenta="\033[95m" BCyan="\033[96m" BWhite="\033[97m" BlackBG="\033[40m" RedBG="\033[41m" GreenBG="\033[42m" YellowBG="\033[43m" BlueBG="\033[44m" MagentaBG="\033[45m" CyanBG="\033[46m" WhiteBG="\033[47m" Bold="\033[1m" Dim="\033[2m" Italic="\033[3m" Underline="\033[4m" Blink="\033[5m" Reverse="\033[7m" Hidden="\033[8m" Strike="\033[9m" OK="${Green}[OK]${Font}" ERROR="${Red}[ERROR]${Font}" WARN="${Yellow}[WARN]${Font}" INFO="${Cyan}[INFO]${Font}" trap 'echo -e "\n${WARN} 已中断"; exit 1' INT ensure_root() { if [[ $EUID -ne 0 ]]; then clear echo "Error: 必须使用 root 运行本脚本!" 1>&2 exit 1 fi } center() { local s="$1" local cols cols=$(tput cols 2>/dev/null || echo 80) local noansi noansi=$(printf '%b' "$s" | sed -r 's/\x1B\[[0-9;]*[A-Za-z]//g') local width width=$(LC_ALL=C.UTF-8 awk -v str="$noansi" ' BEGIN{ n = split(str, a, "") w = 0 for(i=1;i<=n;i++){ c = a[i] if (c ~ /[ -~]/) w += 1 else w += 2 } print w }') local pad=$(( (cols - width) / 2 )) (( pad < 0 )) && pad=0 printf "%*s%b\n" "$pad" "" "$s" } has_cmd() { command -v "$1" >/dev/null 2>&1; } os_install() { if command -v apt-get >/dev/null 2>&1; then apt-get update -y apt-get install -y curl gawk openssl elif command -v dnf >/dev/null 2>&1; then dnf install -y curl gawk openssl elif command -v yum >/dev/null 2>&1; then yum install -y epel-release || true yum install -y curl gawk openssl else echo "未识别的包管理器,请手动安装 curl、gawk、openssl 后重试" exit 1 fi } # -------- 安装状态检测 -------- service_installed() { systemctl status "${SERVICE}" >/dev/null 2>&1; } service_active() { systemctl is-active --quiet "${SERVICE}"; } is_installed() { # 满足:存在二进制 或 服务可用,并且存在配置文件,认为已安装 if { has_cmd hysteria || service_installed; } && [[ -f "${CONF_FILE}" ]]; then return 0 fi return 1 } status_text() { if is_installed; then if service_active; then echo -e "${BGreen}已安装(运行中)${Font}" else echo -e "${BYellow}已安装(已停止)${Font}" fi else echo -e "${BRed}未安装${Font}" fi } get_ip() { local ip4 ip6 ip4=$(curl -s -4 http://www.cloudflare.com/cdn-cgi/trace | awk -F= '/^ip=/{print $2}') if [[ -n "${ip4}" ]]; then echo "${ip4}" return fi ip6=$(curl -s -6 http://www.cloudflare.com/cdn-cgi/trace | awk -F= '/^ip=/{print $2}') if [[ -n "${ip6}" ]]; then echo "${ip6}" return fi curl -s https://api.ipify.org || true } random_port() { shuf -i 2000-65000 -n 1; } # 确保端口是数字并且在合法范围内 valid_port() { local p="${1:-}" [[ "$p" =~ ^[0-9]+$ ]] && (( p >= 1 && p <= 65535 )) } # 检查端口是否被占用 is_port_free() { local port="$1" ss -tuln | grep ":$port " >/dev/null 2>&1 } read_port_interactive() { local input while true; do read -t 15 -p "回车或等待15秒为随机端口,或者自定义端口请输入(1-65535):" input || true if [[ -z "${input:-}" ]]; then input=$(random_port) fi # 验证端口是否合法 if ! valid_port "$input"; then echo "端口不合法:$input,请输入一个有效的端口(1-65535)。" continue fi # 检查端口是否被占用 if is_port_free "$input"; then echo "端口 $input 已被占用,请选择另一个端口。" continue fi # 如果端口合法且未被占用,退出循环 echo "$input" break done } gen_password() { cat /proc/sys/kernel/random/uuid; } ensure_cert() { mkdir -p "${CONF_DIR}" if [[ ! -f "${CONF_DIR}/server.key" || ! -f "${CONF_DIR}/server.crt" ]]; then openssl req -x509 -nodes -newkey ec:<(openssl ecparam -name prime256v1) \ -keyout "${CONF_DIR}/server.key" \ -out "${CONF_DIR}/server.crt" -subj "/CN=${H2_SNI}" -days 36500 chown hysteria:hysteria "${CONF_DIR}/server.key" "${CONF_DIR}/server.crt" || true fi } write_config() { # 参数:端口 密码 local port="$1" pass="$2" mkdir -p "${CONF_DIR}" cat > "${CONF_FILE}" < "${CLIENT_FILE}" <