#!/bin/bash


v2ray_config_file="/usr/local/etc/v2ray/config.json"
v2ray_tls_file="/usr/local/etc/v2ray/cert"

# 默认的uuid
V2rayUUID=""
DomainName=""

RED="31m"    ## 姨妈红
GREEN="32m"  ## 水鸭青
YELLOW="33m" ## 鸭屎黄
PURPLE="35m" ## 基佬紫
BLUE="36m"   ## 天依蓝

######## 颜色函数方法很精妙 ############
colorEcho() {
  echo -e "\033[${1}${@:2}\033[0m" 1>&2
}

check_root() {
  if [[ $EUID != 0 ]]; then
    colorEcho ${RED} "当前非root账号(或没有root权限)，无法继续操作，请更换root账号!"
    colorEcho ${YELLOW} "使用sudo -命令获取临时root权限（执行后可能会提示输入root密码）"
    exit 1
  fi
}
# 判断命令是否存在
command_exists() {
	command -v "$@" >/dev/null 2>&1
}

downloadSoftwares(){
    colorEcho ${PURPLE} "---------------------------------------------------------------------------------"
    colorEcho ${BLUE} "开始下载安装V2ray所需要的所有软件…………"
    
    command_exists curl wget
    if [[ $? -nq 0]]
    then
        colorEcho ${RED} "未检测到强依赖----------------------------------------------"
        colorEcho ${RED} "请确保您已经安装wget,curl, unzip等工具！！"
    fi
    echo ""

    colorEcho ${BLUE} "创建v2ray的安装目录 ./v2ray_install"
    pwd
    mkdir v2ray_install
    cd ./v2ray_install
    pwd
    echo ""

    colorEcho ${BLUE} "正在下载v2ray的安装脚本…………"
    wget https://objectstorage.ap-seoul-1.oraclecloud.com/n/cnk8d6fazu16/b/seoul/o/v2ray-install.sh
    chmod +x v2ray-install.sh
    colorEcho ${GREEN} "      下载完成      "
    echo ""
    
    colorEcho ${BLUE} "正在下载v2ray-linux-adm64-v4.32.1的安装包文件…………"
    wget https://objectstorage.ap-seoul-1.oraclecloud.com/n/cnk8d6fazu16/b/seoul/o/v2ray-linux-64_v4.32.1.zip
    colorEcho ${GREEN} "      下载完成      "
    echo ""
    
    colorEcho ${BLUE} "正在下载envsubst-Linux-x86_64文件…………"
    wget https://objectstorage.ap-seoul-1.oraclecloud.com/n/cnk8d6fazu16/b/seoul/o/envsubst-Linux-x86_64 -O -o envsubst
    chmod + envsubst
    mv envsubst /usr/local/bin
    colorEcho ${GREEN} "      下载完成      "
    echo ""
}

installV2ray(){
    colorEcho ${PURPLE} "---------------------------------------------------------------------------------"
    colorEcho ${BLUE} "开始下载安装V2ray所需要的所有软件…………"
    echo ""
    
    colorEcho ${BLUE}"开始离线安装v2ray……"
    echo "
    " | ./v2ray-install.sh --local v2ray-linux-64_v4.32.1.zip

    echo ""
    cd ..
    colorEcho ${GREEN} "v2ray v4.32.1已经安装成功！"
    echo ""
    colorEcho ${BLUE} "正在开启v2ray的服务程序……"
    systemctl start v2ray
    colorEcho ${GREEN} "      启动完成      "
    echo ""
    colorEcho ${BLUE} "正在设置v2ray的开机自启动……"
    systemctl enable v2ray
    colorEcho ${GREEN} "      设置开启自启动完成      "
    echo ""
}

modifyV2rayConfig(){
    colorEcho ${PURPLE} "---------------------------------------------------------------------------------"
    colorEcho ${BLUE} "开始配置V2ray，默认采用Vless_Over_TCP+XTLS…………"
    colorEcho ${BLUE} "本脚本暂不负责证书生成，请手动将域名的证书文件上传至 /usr/local/etc/v2ray/cert目录下"
    colorEcho ${PURPLE} "---------------------------------------------------------------------------------"
    colorEcho ${BLUE} "证书文件命名规则： <DomainName>.pem <DomainName>.key"
    echo ""

    echo ""
    mkdir -p ${v2ray_tls_file}

    colorEcho ${RED} "-------请输入解析到此服务器的域名(前面不带"http://"或"https://")-------"
    colorEcho ${RED} "----------------------------------------------------------"
    read -r -p "输入域名地址：" DomainName
    echo ""
    colorEcho ${BLUE} "----------------------------------------------------------"
    colorEcho ${GREEN} "您输入的域名为：${DomainName}"

    colorEcho ${GREEN} "----------------------------------------------------------"
    echo ""

    while true
    do
      colorEcho ${RED} "请确保您已经将证书文件上传至 ${v2ray_tls_file} 目录下！！"
      read -r -p "请输入yes进行确认，脚本才可继续运行！！" input
      case $input in
          yes)
          colorEcho ${GREEN} "您已确认上传了证书文件！ 开始检测……"
          echo ""
          if [[ -f ${v2ray_tls_file}/${DomainName}.pem && -f ${v2ray_tls_file}/${DomainName}.key]]
          then
            colorEcho ${GREEN} "检测到TLS域名的证书文件！"
            colorEcho ${GREEN} "----------------------------------------------------------"
            echo ""     
            break
          else
            colorEcho ${RED} "你欺骗了善良的自己！！  没有检测到TLS域名的证书文件！"
            colorEcho ${RED} "请手动将域名的证书文件上传至 ${v2ray_tls_file} 目录下"
            colorEcho ${BLUE} "证书文件命名规则：${DomainName}.pem ${DomainName}.key"
            continue
          ;;
          *)
          echo ""
          colorEcho ${RED} "输入有误！！！ 请输入 >> yes << 进行确认"
          colorEcho ${RED} "-----------------------------------------------------"
          echo ""
          ;;
      esac
    done

    colorEcho ${BLUE} "开始配置v2ray的config文件…………"
    V2rayUUID=$(cat /proc/sys/kernel/random/uuid)
    echo ""
    colorEcho ${BLUE} "生成的UUID为： ${V2rayUUID}"
    colorEcho ${GREEN} "-----------------------------------------------------"

    colorEcho ${BLUE} "正在下载v2ray的config文件…………"
    wget https://objectstorage.ap-seoul-1.oraclecloud.com/n/cnk8d6fazu16/b/seoul/o/v2ray_vless-xtls-tcp.json
    colorEcho ${GREEN} "      下载完成      "
    echo ""
    colorEcho ${BLUE} "正在生成v2ray的config文件…………"
    envsubst < v2ray_vless-xtls-tcp.json > ${v2ray_config_file}
    colorEcho ${GREEN} "      生成完成      "
    echo ""
}

printV2rayInfo(){
  
}

main(){
  check_root
  downloadSoftwares
  installV2ray
  modifyV2rayConfig
  printV2rayInfo
}