#!/bin/bash

# 安装依赖
apt update -y && apt install -y wget curl tar git socat

nodeid=$1
domain=$2

if [ -z "${nodeid}" ]; then
    echo -e "error: 节点ID为空!!"
    exit 1
fi

# 下载并部署 XrayR 二进制
rm -rf /opt/XrayR
mkdir -p /opt/XrayR
cd /opt/XrayR

latest_url=$(curl -s https://api.github.com/repos/XrayR-project/XrayR/releases/latest | grep browser_download_url | grep linux-amd64.tar.gz | cut -d '"' -f 4)
wget -O XrayR.tar.gz "$latest_url"
tar -zxvf XrayR.tar.gz
chmod +x XrayR
rm XrayR.tar.gz

# 下载配置
mkdir -p config
wget --no-check-certificate -qO config/config.yml https://img.digac.icu/Files/LinuxShell/docker_XrayR_vless_reality_config.yml

# 替换配置内容
sed -i 's/API节点/023021.xyz/' config/config.yml
sed -i 's/API秘钥/michaeltan057720/' config/config.yml
sed -i 's/节点序号/'"${nodeid}"'/' config/config.yml
sed -i 's/NodeType: V2ray/NodeType: Vless/' config/config.yml
sed -i 's/EnableVless: false/EnableVless: true/' config/config.yml
sed -i 's/EnableREALITY: false/EnableREALITY: true/' config/config.yml
sed -i 's/DisableLocalREALITYConfig: false/DisableLocalREALITYConfig: true/' config/config.yml
sed -i 's/Show: true/Show: false/' config/config.yml

# 注释掉证书申请部分（CertMode 保留 none）
sed -i 's/CertMode: .*/CertMode: none/' config/config.yml
#sed -i 's/^ *CertDomain:/# &/' config/config.yml
#sed -i 's/^ *CertFile:/# &/' config/config.yml
#sed -i 's/^ *KeyFile:/# &/' config/config.yml
#sed -i 's/^ *Provider:/# &/' config/config.yml
#sed -i 's/^ *Email:/# &/' config/config.yml
#sed -i 's/^ *CLOUDFLARE_EMAIL:/# &/' config/config.yml
#sed -i 's/^ *CLOUDFLARE_API_KEY:/# &/' config/config.yml

# 显示配置文件
echo "当前配置："
cat config/config.yml

# 创建 systemd 服务
cat > /etc/systemd/system/xrayr.service <<EOF
[Unit]
Description=XrayR Service
After=network.target

[Service]
ExecStart=/opt/XrayR/XrayR -c /opt/XrayR/config/config.yml
WorkingDirectory=/opt/XrayR
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF