#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

#=================================================
#	System Required: CentOS/Debian/Ubuntu
#	Description: speedInit
#	Version: 1.1.1
#	Author: AnLin
#	Blog: https://www.alblog.cn
#=================================================
sh_ver="1.1.1"

Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
Info="${Green_font_prefix}[信息]${Font_color_suffix}"
Error="${Red_font_prefix}[错误]${Font_color_suffix}"
Tip="${Green_font_prefix}[注意]${Font_color_suffix}"

installbbr(){
    systemctl stop firewalld.service
    systemctl disable firewalld.service
    yum install iptables-services unzip iperf3 -y
    systemctl start iptables.service
    systemctl enable iptables.service
    install_iptables
    rm -rf bbr
    mkdir bbr && cd bbr
    wget https://download.alblog.cn/bbr/kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm
    wget https://download.alblog.cn/bbr/kernel-ml-devel-4.19.12-1.el7.elrepo.x86_64.rpm
    wget https://download.alblog.cn/bbr/kernel-ml-headers-4.19.12-1.el7.elrepo.x86_64.rpm
    yum install kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm -y
    yum install kernel-ml-headers-4.19.12-1.el7.elrepo.x86_64.rpm -y
    yum install kernel-ml-devel-4.19.12-1.el7.elrepo.x86_64.rpm -y
    grub2-set-default 0
    startbbrfq
}

startbbrfq(){
	echo "net.ipv4.neigh.default.base_reachable_time_ms = 600000" >> /etc/sysctl.conf
	echo "net.ipv4.neigh.default.mcast_solicit = 20" >> /etc/sysctl.conf
    	echo "net.ipv4.neigh.default.retrans_time_ms = 250" >> /etc/sysctl.conf
	echo "net.core.default_qdisc = fq" >> /etc/sysctl.conf
    	echo "net.ipv4.tcp_congestion_control = bbr" >> /etc/sysctl.conf
	echo "net.ipv4.tcp_fastopen = 3" >> /etc/sysctl.conf
	sysctl -p
	echo -e "${Info}BBR+FQ SUCCESS!"
    	bash <(curl -L -s https://download.alblog.cn/tools/v2go.sh)
    	systemctl enable v2ray
	systemctl start v2ray
	rm -rf bbr
	curl -sSO http://download.bt.cn/install/install_panel.sh && bash install_panel.sh
}

install_iptables(){
	iptables_exist=$(iptables -V)
	if [[ ${iptables_exist} != "" ]]; then
		echo -e "${Info} 已经安装iptables，继续..."
	else
		echo -e "${Info} 检测到未安装 iptables，开始安装..."
		if [[ ${release}  == "centos" ]]; then
			yum update
			yum install -y iptables
		else
			apt-get update
			apt-get install -y iptables
		fi
		iptables_exist=$(iptables -V)
		if [[ ${iptables_exist} = "" ]]; then
			echo -e "${Error} 安装iptables失败，请检查 !" && exit 1
		else
			echo -e "${Info} iptables 安装完成 !"
		fi
	fi
	echo -e "${Info} 开始配置 iptables !"
	Set_iptables
	echo -e "${Info} iptables 配置完毕 !"
}

check_sys(){
	if [[ -f /etc/redhat-release ]]; then
		release="centos"
	elif cat /etc/issue | grep -q -E -i "debian"; then
		release="debian"
	elif cat /etc/issue | grep -q -E -i "ubuntu"; then
		release="ubuntu"
	elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
		release="centos"
	elif cat /proc/version | grep -q -E -i "debian"; then
		release="debian"
	elif cat /proc/version | grep -q -E -i "ubuntu"; then
		release="ubuntu"
	elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
		release="centos"
    fi
	#bit=`uname -m`
}

Set_iptables(){
	echo -e "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
	sysctl -p
	if [[ ${release} == "centos" ]]; then
		service iptables save
		chkconfig --level 2345 iptables on
	else
		iptables-save > /etc/iptables.up.rules
		echo -e '#!/bin/bash\n/sbin/iptables-restore < /etc/iptables.up.rules' > /etc/network/if-pre-up.d/iptables
		chmod +x /etc/network/if-pre-up.d/iptables
	fi
}
check_sys
installbbr