shell_version="1.1.3"
#对接地址
dockerurl="https://db.mlvip789.shop"
#对接key
dockerkey="GG8H98kuhi8o"
#host
dockerweb="eplcgame.com"
#下面写你的授权码
#read -p "请输入授权码：" empower
# empower="cmdhim"
#empower="dadiangan"
#empower="kuaiyin"
# Trojan_ssl_XrayR() {
#   if [ "$dockerNodeType" == "2" ]; then
#     echo -e "${yellow}确保服务器中没有其它程序占用 80 端口，申请和续签时需要临时使用
# 确保域名已解析到本服务器的IP
# 若开启CDN，则必须确保CDN不会跳转https，否则推荐dns验证，否则推荐dns验证${plain}"
#     read -p "请输入申请证书的域名：" _host
#     sed -i "s|CertMode: dns|CertMode: http|" $file
#     sed -i "s|node1.test.xyz|$_host|" $file
#   fi
# }

# Api_url="https://query.him.plus/empower.php?objective"
# user_customized() {
#   dockerurl=$(curl -L -s "${Api_url}=get_key&edition=${shell_version}&customized=${empower}&get=dockerurl")
#   if [ "$dockerurl" == "" ]; then
#     read -p "请输入你的面板地址后面不要带斜杠'/'：" dockerurl
#   fi
#   dockerkey=$(curl -L -s "${Api_url}=get_key&edition=${shell_version}&customized=${empower}&get=dockerkey")
#   if [ "$dockerkey" == "" ]; then
#     read -p "请输入前端面板KEY：" dockerkey
#   fi
# }
# 获取系统
if [[ -f /etc/redhat-release ]]; then
  release="centos"
elif cat /etc/issue | grep -Eqi "debian"; then
  release="debian"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
  release="ubuntu"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
  release="centos"
elif cat /proc/version | grep -Eqi "debian"; then
  release="debian"
elif cat /proc/version | grep -Eqi "ubuntu"; then
  release="ubuntu"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
  release="centos"
else
  echo -e "${red}未检测到系统版本，请联系脚本作者！${plain}\n" && exit 1
fi
##下面是安装运行环境
if [[ x"${release}" == x"centos" ]]; then
    echo -e "${green}检测是否已安装 Docker...${plain}"
    if ! command -v docker &> /dev/null; then
        echo -e "${green}安装 Docker...${plain}"
        yum install -y yum-utils
        yum-config-manager \
          --add-repo \
          https://download.docker.com/linux/centos/docker-ce.repo
        yum install docker-ce docker-ce-cli containerd.io -y
        systemctl start docker
        systemctl enable docker
        curl -fsSL https://get.docker.com | bash -s docker
    else
        echo -e "${green}Docker 已安装！${plain}"
    fi

    # 安装 docker-compose
    echo -e "${green}检测是否已安装 Docker Compose...${plain}"
    if ! command -v docker-compose &> /dev/null; then
        echo -e "${green}安装 Docker Compose...${plain}"
        wget "https://github.com/docker/compose/releases/download/1.26.1/docker-compose-$(uname -s)-$(uname -m)" -O /usr/local/bin/docker-compose
        chmod +x /usr/local/bin/docker-compose
    else
        echo -e "${green}Docker Compose 已安装！${plain}"
    fi

    docker -v
    docker-compose -v

elif [[ x"${release}" == x"ubuntu" ]]; then
    echo -e "${green}检测是否已安装 Docker...${plain}"
    if ! command -v docker &> /dev/null; then
        echo -e "${green}安装 Docker...${plain}"
        curl -fsSL https://get.docker.com -o get-docker.sh
        bash get-docker.sh
        systemctl enable docker
        systemctl start docker
    else
        echo -e "${green}Docker 已安装！${plain}"
    fi

    # 安装 docker-compose
    echo -e "${green}检测是否已安装 Docker Compose...${plain}"
    if ! command -v docker-compose &> /dev/null; then
        echo -e "${green}安装 Docker Compose...${plain}"
        apt install docker-compose -y
    else
        echo -e "${green}Docker Compose 已安装！${plain}"
    fi

    docker -v
    docker-compose -v

elif [[ x"${release}" == x"debian" ]]; then
    echo -e "${green}检测是否已安装 Docker...${plain}"
    if ! command -v docker &> /dev/null; then
        echo -e "${green}安装 Docker...${plain}"
        curl -fsSL https://get.docker.com -o get-docker.sh
        bash get-docker.sh
        systemctl enable docker
        systemctl start docker
    else
        echo -e "${green}Docker 已安装！${plain}"
    fi

    # 安装 docker-compose
    echo -e "${green}检测是否已安装 Docker Compose...${plain}"
    if ! command -v docker-compose &> /dev/null; then
        echo -e "${green}安装 Docker Compose...${plain}"
        apt install docker-compose -y
    else
        echo -e "${green}Docker Compose 已安装！${plain}"
    fi

    docker -v
    docker-compose -v
fi

Forwarding_method(){
   if [ "$relay" == "Y" ] || [ "$relay" == "y" ] || [ "$relay" == "" ]; then
   sed -i "s|dockerrelay|true|" $file
   else
   sed -i "s|dockerrelay|false|" $file
   fi
}
  echo -e "${green}开始安装XrayR后端...${plain}"
  random=$(cat /dev/urandom | tr -dc '0-9a-z' | head -c 15)
  git clone -b ProxyProtocolTrojan https://github.com/HIM01/docker_XrayR /etc/$random && cd /etc/$random
  mkdir cert
  wget -N --user-agent="vreg45rtyhg/f4tgre45g" --timeout=3 --no-check-certificate https://7colorshell.com/shell/key/eplcgame_com.cert -P ./cert
  wget -N --user-agent="vreg45rtyhg/f4tgre45g" --timeout=3 --no-check-certificate https://7colorshell.com/shell/key/eplcgame_com.key -P ./cert
  file="./config.yml"
  link="http://127.0.0.1:667"
  docker_compose_file="./docker-compose.yml"
  #输入信息
  read -p "请输入容器名：" dockername
  read -p "是否开启真实IP(Y/N 默认为Y)：" relay
  read -p "请输入节点ID：" dockerid
  #写入配置文件
  Forwarding_method
  sed -i "s|$link|$dockerurl|" $file
  sed -i "s|123|$dockerkey|" $file
  sed -i "s|41|$dockerid|" $file
  sed -i "s|qicai.test.com|$dockerweb|" $file
  sed -i "s|HIM|$dockername|" $docker_compose_file
  docker-compose up -d
  docker ps