我突然发现用ip访问宝塔面板的ssl不是自签证书,原来不知道啥时候开始let's encrypt支持ip证书了,虽然只有6天,但是可以无限续签。

那我直接把它用到hysteria2上不就可以了么。

我也忘记怎么改了,直接先一键清除:

sudo bash <<'EOF'
#!/bin/bash

set -e

echo "======================================"
echo "       Hysteria2 旧版本清理工具"
echo "======================================"

# 备份旧配置
if [ -d /etc/hysteria ]; then
    BACKUP="/root/hysteria-backup-$(date +%Y%m%d-%H%M%S)"
    cp -a /etc/hysteria "$BACKUP"
    echo "[备份] 旧配置已保存至:$BACKUP"
fi

echo "[1/5] 停止并禁用旧服务..."

for service in \
    hysteria-server.service \
    hysteria-cert-watch.service \
    hysteria-cert-watch.timer
do
    systemctl stop "$service" 2>/dev/null || true
    systemctl disable "$service" 2>/dev/null || true
done

echo "[2/5] 删除 systemd 服务文件..."

rm -f /etc/systemd/system/hysteria-server.service
rm -f /etc/systemd/system/hysteria-cert-watch.service
rm -f /etc/systemd/system/hysteria-cert-watch.timer

echo "[3/5] 删除 Hysteria2 程序及配置..."

rm -f /usr/local/bin/hysteria
rm -f /usr/bin/hysteria
rm -f /usr/local/bin/hysteria-cert-watch.sh
rm -rf /etc/hysteria

echo "[4/5] 清理 systemd 状态..."

systemctl daemon-reload
systemctl reset-failed

echo "[5/5] 检查清理结果..."

if systemctl list-unit-files --no-legend | grep -q hysteria; then
    echo "提示:仍存在名称包含 hysteria 的服务,请进一步检查。"
else
    echo "systemd 服务清理完成。"
fi

if command -v hysteria >/dev/null 2>&1; then
    echo "提示:仍检测到 Hysteria2 程序,请检查安装路径。"
else
    echo "Hysteria2 程序清理完成。"
fi

echo "======================================"
echo "       Hysteria2 清理执行完毕"
echo "======================================"
echo "宝塔 SSL 证书及 Nginx 未受影响。"
EOF

然后创建一键安装脚本

vim hysteria-manager.sh

粘贴下面的内容,并保存

#!/usr/bin/env bash
# Hysteria2 一键管理脚本
# Author: Yaodo
# SSL: BaoTa Let's Encrypt IP Certificate

set -Eeuo pipefail

CONFIG="/etc/hysteria/config.yaml"
SERVICE="/etc/systemd/system/hysteria-server.service"

WATCHER="/usr/local/bin/hysteria-cert-watch.sh"
WATCH_SERVICE="/etc/systemd/system/hysteria-cert-watch.service"
WATCH_TIMER="/etc/systemd/system/hysteria-cert-watch.timer"
FINGERPRINT="/etc/hysteria/cert-fingerprint"

IP=""
PORT=""
PASSWORD=""
CERT=""
KEY=""

check_root() {
    if [ "$(id -u)" -ne 0 ]; then
        echo "请使用 sudo 运行此脚本。"
        exit 1
    fi
}

# 获取服务器默认公网 IPv4
get_server_ip() {
    local IP

    IP=$(curl -4 -fsS --max-time 8 https://api.ipify.org 2>/dev/null || true)

    if [ -z "$IP" ]; then
        IP=$(curl -4 -fsS --max-time 8 https://ipv4.icanhazip.com 2>/dev/null || true)
    fi

    if [ -z "$IP" ]; then
        echo "无法自动获取服务器公网 IP。"
        read -r -p "请手动输入服务器 IP: " IP
    fi

    echo "$IP"
}

# 验证 IP
validate_ip() {
    python3 -c "import ipaddress; ipaddress.ip_address('$1')" 2>/dev/null
}

# 配置 IP、端口和密码
get_user_config() {
    local DEFAULT_IP DEFAULT_PORT

    echo "=============================="
    echo "       Hysteria2 参数配置"
    echo "=============================="

    DEFAULT_IP=$(get_server_ip)

    echo
    echo "服务器默认 IP:$DEFAULT_IP"
    read -r -p "请输入连接 IP(直接回车使用默认 IP): " IP
    IP=${IP:-$DEFAULT_IP}

    if ! validate_ip "$IP"; then
        echo "IP 地址格式错误。"
        exit 1
    fi

    # 根据自定义 IP 自动设置证书路径
    CERT="/www/server/panel/vhost/letsencrypt/$IP/fullchain.pem"
    KEY="/www/server/panel/vhost/letsencrypt/$IP/privkey.pem"

    DEFAULT_PORT=$(shuf -i 9000-19999 -n 1)

    echo
    read -r -p "请输入 UDP 端口(默认 $DEFAULT_PORT): " PORT
    PORT=${PORT:-$DEFAULT_PORT}

    if ! [[ "$PORT" =~ ^[0-9]+$ ]] || [ "$PORT" -lt 1 ] || [ "$PORT" -gt 65535 ]; then
        echo "端口格式错误。"
        exit 1
    fi

    echo
    read -r -p "请输入连接密码(留空自动生成): " PASSWORD
    PASSWORD=${PASSWORD:-$(openssl rand -hex 16)}

    if ! [[ "$PASSWORD" =~ ^[A-Za-z0-9._-]+$ ]]; then
        echo "密码只允许字母、数字、点、下划线和连字符。"
        exit 1
    fi

    echo
    echo "========== 配置信息 =========="
    echo "连接 IP:$IP"
    echo "UDP 端口:$PORT"
    echo "连接密码:$PASSWORD"
    echo "证书路径:$CERT"
    echo "=============================="
}

# 检查宝塔 SSL 证书
check_certificate() {
    echo
    echo "正在检查宝塔 SSL 证书..."

    if [ ! -f "$CERT" ] || [ ! -f "$KEY" ]; then
        echo "错误:未找到对应 IP 的宝塔 SSL 证书。"
        echo "证书路径:$CERT"
        echo "私钥路径:$KEY"
        exit 1
    fi

    if ! openssl x509 -in "$CERT" -noout -checkip "$IP" >/dev/null 2>&1; then
        echo "错误:证书不包含 IP $IP。"
        exit 1
    fi

    if ! openssl x509 -in "$CERT" -noout -checkend 3600 >/dev/null 2>&1; then
        echo "错误:证书已过期或将在一小时内过期。"
        exit 1
    fi

    local CERT_HASH KEY_HASH

    CERT_HASH=$(openssl x509 -in "$CERT" -pubkey -noout | sha256sum | awk '{print $1}')
    KEY_HASH=$(openssl pkey -in "$KEY" -pubout | sha256sum | awk '{print $1}')

    if [ "$CERT_HASH" != "$KEY_HASH" ]; then
        echo "错误:证书和私钥不匹配。"
        exit 1
    fi

    echo "证书检查通过!"
}

# 安装 Hysteria2
install_hysteria_binary() {
    if command -v hysteria >/dev/null 2>&1; then
        echo "检测到 Hysteria2 已安装。"
        return
    fi

    echo "正在安装依赖..."
    apt-get update -y
    apt-get install -y curl openssl ca-certificates python3

    echo "正在安装 Hysteria2..."
    bash <(curl -fsSL https://get.hy2.sh/)

    if ! command -v hysteria >/dev/null 2>&1; then
        echo "Hysteria2 安装失败。"
        exit 1
    fi
}

# 配置证书更新监控
configure_certificate_watcher() {
    echo "正在配置证书自动更新监控..."

    cat > "$WATCHER" <<EOF
#!/usr/bin/env bash
set -euo pipefail

CERT="$CERT"
KEY="$KEY"
IP="$IP"
STATE="$FINGERPRINT"

[ -f "\$CERT" ] || exit 0
[ -f "\$KEY" ] || exit 0

# 检查证书有效期
openssl x509 -in "\$CERT" -noout -checkip "\$IP" >/dev/null 2>&1 || exit 0
openssl x509 -in "\$CERT" -noout -checkend 3600 >/dev/null 2>&1 || exit 0

# 检查证书和私钥是否匹配
CERT_HASH=\$(openssl x509 -in "\$CERT" -pubkey -noout | sha256sum | awk '{print \$1}')
KEY_HASH=\$(openssl pkey -in "\$KEY" -pubout | sha256sum | awk '{print \$1}')

[ "\$CERT_HASH" = "\$KEY_HASH" ] || exit 0

# 获取证书指纹
NEW_FP=\$(openssl x509 -in "\$CERT" -fingerprint -sha256 -noout | cut -d= -f2-)

# 首次运行只记录指纹
if [ ! -f "\$STATE" ]; then
    echo "\$NEW_FP" > "\$STATE"
    exit 0
fi

OLD_FP=\$(cat "\$STATE")

# 证书变化时重启 Hysteria2
if [ "\$NEW_FP" != "\$OLD_FP" ]; then
    echo "检测到宝塔 SSL 证书更新,正在重启 Hysteria2..."

    if systemctl restart hysteria-server; then
        sleep 2

        if systemctl is-active --quiet hysteria-server; then
            echo "\$NEW_FP" > "\$STATE"
            echo "Hysteria2 已加载新证书。"
        fi
    fi
fi
EOF

    chmod 700 "$WATCHER"

    cat > "$WATCH_SERVICE" <<EOF
[Unit]
Description=Check BaoTa SSL Certificate for Hysteria2

[Service]
Type=oneshot
ExecStart=$WATCHER
EOF

    cat > "$WATCH_TIMER" <<'EOF'
[Unit]
Description=Hysteria2 Certificate Watch Timer

[Timer]
OnBootSec=2min
OnUnitActiveSec=5min
Persistent=true

[Install]
WantedBy=timers.target
EOF

    # 记录当前证书指纹
    openssl x509 -in "$CERT" -fingerprint -sha256 -noout |
        cut -d= -f2- > "$FINGERPRINT"

    systemctl daemon-reload
    systemctl enable --now hysteria-cert-watch.timer

    echo "证书自动更新监控已启用。"
}

# 安装配置
install_hysteria() {
    echo "=============================="
    echo "   Hysteria2 一键安装配置"
    echo "=============================="

    apt-get update -y
    apt-get install -y curl openssl ca-certificates python3

    get_user_config
    check_certificate
    install_hysteria_binary

    local HYSTERIA_BIN
    HYSTERIA_BIN=$(command -v hysteria)

    mkdir -p /etc/hysteria

    # 备份原配置
    if [ -f "$CONFIG" ]; then
        cp "$CONFIG" "$CONFIG.bak.$(date +%Y%m%d%H%M%S)"
    fi

    echo
    echo "正在写入 Hysteria2 配置..."

    cat > "$CONFIG" <<EOF
listen: :$PORT

tls:
  cert: $CERT
  key: $KEY

auth:
  type: password
  password: "$PASSWORD"

bandwidth:
  up: 1 gbps
  down: 1 gbps

masquerade:
  type: proxy
  proxy:
    url: https://www.bing.com
    rewriteHost: true
EOF

    chmod 600 "$CONFIG"

    # 创建 systemd 服务
    cat > "$SERVICE" <<EOF
[Unit]
Description=Hysteria2 Server
After=network.target

[Service]
Type=simple
ExecStart=$HYSTERIA_BIN server --config $CONFIG
Restart=always
RestartSec=3
User=root
LimitNOFILE=512000

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable hysteria-server
    systemctl restart hysteria-server

    sleep 2

    if ! systemctl is-active --quiet hysteria-server; then
        echo "Hysteria2 启动失败,错误日志:"
        journalctl -u hysteria-server -n 50 --no-pager
        exit 1
    fi

    # 配置证书更新监控
    configure_certificate_watcher

    # UFW 防火墙放行 UDP 端口
    if command -v ufw >/dev/null 2>&1; then
        if ufw status 2>/dev/null | grep -q "Status: active"; then
            ufw allow "$PORT/udp"
        fi
    fi

    echo
    echo "================================"
    echo "       Hysteria2 安装成功"
    echo "================================"
    echo "服务器 IP:$IP"
    echo "UDP 端口:$PORT"
    echo "连接密码:$PASSWORD"
    echo "SSL:宝塔 Let's Encrypt IP 证书"
    echo "证书更新检查:每 5 分钟"
    echo
    echo "客户端无需开启 insecure。"
    echo
    echo "请在云服务器安全组放行 UDP $PORT"
    echo "================================"
}

# 查看状态
show_status() {
    echo "========== Hysteria2 状态 =========="
    systemctl status hysteria-server --no-pager || true

    echo
    echo "========== 证书监控 =========="
    systemctl status hysteria-cert-watch.timer --no-pager || true

    echo
    echo "========== SSL 证书 =========="
    if [ -f "$CERT" ]; then
        openssl x509 -in "$CERT" -noout -subject -issuer -dates
    else
        echo "尚未配置证书路径,请使用安装时输入的 IP 检查。"
    fi

    echo
    echo "========== UDP 监听 =========="
    ss -ulnp | grep hysteria || true
}

# 卸载
uninstall_hysteria() {
    echo "即将卸载 Hysteria2。"
    read -r -p "确认卸载?(y/N): " CONFIRM

    if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
        echo "已取消。"
        exit 0
    fi

    systemctl disable --now hysteria-server 2>/dev/null || true
    systemctl disable --now hysteria-cert-watch.timer 2>/dev/null || true

    rm -f "$SERVICE"
    rm -f "$WATCH_SERVICE"
    rm -f "$WATCH_TIMER"
    rm -f "$WATCHER"

    systemctl daemon-reload
    systemctl reset-failed 2>/dev/null || true

    rm -rf /etc/hysteria

    if [ -x /usr/local/bin/hysteria ]; then
        rm -f /usr/local/bin/hysteria
    fi

    echo
    echo "Hysteria2 已卸载。"
    echo "宝塔 SSL 证书未删除。"
    echo "Nginx 和宝塔面板未修改。"
    echo "UFW 规则未自动删除。"
}

# 主程序
check_root

case "${1:-}" in
    install)
        install_hysteria
        ;;
    uninstall)
        uninstall_hysteria
        ;;
    status)
        show_status
        ;;
    *)
        echo "Hysteria2 一键管理脚本"
        echo
        echo "用法:"
        echo "  $0 install    安装配置"
        echo "  $0 uninstall  卸载"
        echo "  $0 status     查看状态"
        exit 1
        ;;
esac

使用方法

保存后执行:

chmod +x hysteria-manager.sh

安装:

sudo ./hysteria-manager.sh install

查看状态:

sudo ./hysteria-manager.sh status

卸载:

sudo ./hysteria-manager.sh uninstall

安装时的交互示例

服务器默认 IP:170.9.23.83
请输入连接 IP(直接回车使用默认 IP):

请输入 UDP 端口(默认 12568): 12345

请输入连接密码(留空自动生成): mypassword123

最终使用:

IP:170.9.23.83
端口:12345
密码:mypassword123

注意: 自定义 IP 必须是宝塔已经申请了对应 IP 证书的地址,否则脚本会提示找不到证书或 IP 不匹配。证书监控会检测宝塔续期后的证书变化,但不会代替宝塔执行续期。