#!/bin/bash
echo "=== 一键安装 XrayR + 接入 XBoard (VLESS+Reality) ==="

# 检查依赖
echo "👉 检查依赖..."
for cmd in git wget sed yq unzip jq; do
    if ! command -v $cmd >/dev/null 2>&1; then
        echo "安装 $cmd ..."
        apt update -y && apt install -y $cmd
    fi
done

# 安装 XrayR-release
echo "👉 删除旧 XrayR-release 并拉取最新版本..."
rm -rf /opt/XrayR-release
cd /opt
git clone https://github.com/XrayR-project/XrayR-release

# 输入 NodeID
read -p "请输入 NodeID (整数): " NODE_ID

# 下载默认 config.yml
echo "👉 下载默认 config.yml..."
wget --no-check-certificate -qO /opt/XrayR-release/config/config.yml https://sh.digac.icu/shell/docker_XrayR_vless_reality_config.yml

# 修改 config.yml
echo "👉 修改 config.yml..."
sed -i "s|节点序号|$NODE_ID|" /opt/XrayR-release/config/config.yml
sed -i "s|NodeType: V2ray|NodeType: Vless|" /opt/XrayR-release/config/config.yml
sed -i "s|EnableVless: false|EnableVless: true|" /opt/XrayR-release/config/config.yml
sed -i "s|EnableREALITY: false|EnableREALITY: true|" /opt/XrayR-release/config/config.yml
sed -i "s|DisableLocalREALITYConfig: false|DisableLocalREALITYConfig: true|" /opt/XrayR-release/config/config.yml
sed -i "s|Show: true|Show: false|" /opt/XrayR-release/config/config.yml

# 设置 systemd 服务
if [ ! -f /etc/systemd/system/XrayR.service ]; then
cat >/etc/systemd/system/XrayR.service <<EOL
[Unit]
Description=XrayR Service
After=network.target

[Service]
Type=simple
ExecStart=/opt/XrayR-release/XrayR -c /opt/XrayR-release/config/config.yml
Restart=on-failure
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target
EOL
    systemctl daemon-reload
    systemctl enable XrayR
fi

# 启动服务
echo "👉 启动 XrayR 服务..."
systemctl restart XrayR
systemctl status XrayR -l --no-pager

echo "✅ XrayR 安装完成，已接入 XBoard 节点 (VLESS+Reality)"
