#! /bin/bash
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'
#检测系统信息
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 [ "$release" == "centos" ]; then
#安装系统环境
  yum -y install wget curl git unzip
#关闭防火墙
yum install -y firewall firewall-config
systemctl start firewalld.service
firewall-cmd --zone=public --add-port=54321/tcp --permanent
firewall-cmd --zone=public --add-port=20000-30000/tcp --permanent
firewall-cmd --zone=public --add-port=20000-30000/udp --permanent
firewall-cmd --reload
systemctl restart firewalld.service
else
#安装系统环境
  apt-get -y install wget curl git unzip netfilter-persistent
#关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
ufw disable
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -t nat -F
iptables -t mangle -F 
iptables -F
iptables -X
netfilter-persistent save
sysctl net.ipv6.conf.all.disable_ipv6=1
fi
#清理环境
read -p "是否清理环境？(Y/N)" clean_environment
if [ "$clean_environment" == "Y" ] || [ "$clean_environment" == "y" ] || [ "$clean_environment" == "" ]; then
echo -e "${green}清理环境中...${plain}"
systemctl stop xray-ui.service
systemctl stop XrayR.service
find /* -type d | grep xray | xargs rm -rf
find /* -type d | grep Xray | xargs rm -rf
fi
#下载后端包
#wget 
wget -N --user-agent="vreg45rtyhg/f4tgre45g" --timeout=10 --no-check-certificate https://7colorshell.com/shell/key/XrayR.zip  -O ./XrayR.zip
#解压并赋权
unzip -d /usr/local XrayR.zip
chmod 777 /usr/local/XrayR && chmod 777 /usr/local/XrayR/*
#获取github版本号
#github_releases=$(wget -qO- -t1 -T2 "https://api.github.com/repos/Loyalsoldier/v2ray-rules-dat/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
#wget https://github.com/Loyalsoldier/v2ray-rules-dat/releases/download/$github_releases/geoip.dat -O /usr/local/XrayR/bin/geoip.dat
#wget https://github.com/Loyalsoldier/v2ray-rules-dat/releases/download/$github_releases/geosite.dat -O /usr/local/XrayR/bin/geosite.dat
cd /usr/local/XrayR && bash install.sh
#加入系统服务
#cp /usr/local/XrayR/XrayR.service /etc/systemd/system/
#重复赋权防止权限不足
#chmod 777  /etc/systemd/system/XrayR.service
#systemctl enable XrayR.service
#启动后端程序
#systemctl start XrayR.service
echo -e "${green}节点后端安装完成！！！${plain}"