#!/bin/bash

# 常量：API 地址和密钥
API_HOST="https://v2ray.023021.xyz"
API_KEY="michaeltan057720"

# 获取用户输入
read -rp "请输入节点 ID（Node ID）: " NODE_ID

# 安装 XrayR
install_xrayr() {
  echo "🟡 安装 XrayR..."
  bash <(curl -Ls https://raw.githubusercontent.com/XrayR-project/XrayR-release/master/install.sh)
}

# 创建配置文件
create_config() {
  echo "🟢 写入配置到 /etc/XrayR/config.yml"

  cat <<EOF > /etc/XrayR/config.yml
Log:
  Level: warning
  AccessPath: ""
  ErrorPath: ""

DnsConfigPath: ""

ConnectionConfig:
  Handshake: 4
  ConnIdle: 300
  UplinkOnly: 2
  DownlinkOnly: 5
  BufferSize: 64

Nodes:
  - PanelType: "V2board"
    ApiConfig:
      ApiHost: "${API_HOST}"
      ApiKey: "${API_KEY}"
      NodeID: ${NODE_ID}
      NodeType: V2ray
      Timeout: 30
      EnableVlessReality: true
    ControllerConfig:
      ListenIP: 0.0.0.0
      SendIP: 0.0.0.0
      UpdatePeriodic: 60
      EnableDNS: false
      DisableSniffing: true
      EnableProxyProtocol: false
      FallBackInsecure: false
      CertConfig:
        CertMode: none
EOF
}

# 启动服务
start_xrayr() {
  echo "🟢 启动 XrayR 服务..."
  systemctl restart XrayR
  systemctl enable XrayR
}

main() {
  install_xrayr
  create_config
  start_xrayr
  echo -e "\n✅ 搭建完成！请在主控面板中查看节点是否上线。\n"
}

main
