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

checkos(){     
    if [[ -f /etc/redhat-release ]];then         
        OS=CentOS     
    elif cat /etc/issue | grep -q -E -i "debian";then         
        OS=Debian     
    elif cat /etc/issue | grep -q -E -i "ubuntu";then         
        OS=Ubuntu     
    elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat";then         
        OS=CentOS     
    elif cat /proc/version | grep -q -E -i "debian";then         
        OS=Debian     
    elif cat /proc/version | grep -q -E -i "ubuntu";then         
        OS=Ubuntu     
    elif cat /proc/version | grep -q -E -i "centos|red hat|redhat";then         
        OS=CentOS     
    else         
    echo "Not supported OS, Please reinstall OS and try again."         
    exit 1     
    fi 
}  

Install_curl(){     
    if [ "${OS}" == 'CentOS' ];then     
    rpm -qa | grep -n '^curl' >/dev/null 2>&1     
    if [[ $? -eq 1 ]];then      echo -e "正在安装相关依赖..."     
    yum install -y curl     
    fi 
    else     
    dpkg --get-selections | grep -n '^curl' >/dev/null 2>&1     
    if [[ $? -eq 1 ]];then      
    echo -e "正在安装相关依赖..."     
    apt install -y sudo     
    sudo apt-get install -y curl     
    fi 
    fi 
    }  

stop_firewall(){ 
    if [ "${OS}" == 'CentOS' ];then 
    echo | systemctl status firewalld | grep -w "running"  >/dev/null 2>&1     
    if [[ $? -eq 0 ]];then      
    systemctl stop firewalld.service >/dev/null 2>&1     
    systemctl disable firewalld.service >/dev/null 2>&1     
    fi 
    fi 
}          

Install_crontabs(){     
    if [ "${OS}" == 'CentOS' ];then     
    rpm -qa | grep cron >/dev/null 2>&1     
    if [[ $? -eq 0 ]];then      
    echo -e "系统环境已存在"      
    else     
    echo -e "正在安装相关依赖..."     
    yum install -y crontabs     
    /sbin/crond start     
    fi  else     
    dpkg --get-selections | grep cron >/dev/null 2>&1     
    if [[ $? -eq 0 ]];then      
    echo -e "系统环境已存在"      
    else     
    echo -e "正在安装相关依赖..."     
    sudo apt-get install cron     
    /etc/init.d/cron restart     
    fi 
    fi 
}  
        
Install_ifconfig(){     
    if [ "${OS}" == 'CentOS' ];then     
    rpm -qa | grep net-tools >/dev/null 2>&1     
    if [[ $? -eq 1 ]];then      
    echo -e "正在安装相关依赖..."     
    yum install -y net-tools     
    fi 
    else     
    dpkg --get-selections | grep net-tools >/dev/null 2>&1     
    if [[ $? -eq 1 ]];then      
    echo -e "正在安装相关依赖..."     
    sudo apt-get install net-tools     
    fi 
    fi 
}   
        
Install_iptables(){     
    if [ "${OS}" == 'CentOS' ];then     
    rpm -qa | grep iptables >/dev/null 2>&1     
    if [[ $? -eq 0 ]];then      
    echo -e "系统环境已存在"      
    else     
    echo -e "正在安装相关依赖..."     
    yum install -y iptables     
    yum install -y iptables-services     
    iptables -F     
    fi 
    else     
    dpkg --get-selections | grep iptables >/dev/null 2>&1     
    if [[ $? -eq 0 ]];then      
    echo -e "系统环境已存在"      
    else     
    echo -e "正在安装相关依赖..."     
    sudo apt-get install iptables     
    iptables -F     
    fi 
    fi 
}  
    
Install_shc(){ 
    # used=`echo | curl -s http://port.freefly.best/${filename}/key | awk '{ print FNR " " $0}' | grep [0-9] | tr -d "a-zA-Z"  |awk 'END{print $1}'` 
    if [ -f "/usr/bin/shc" ]; then     
        echo "破解成功"     
    # echo -e "已授权：${used}台设备"     
    else    
        echo "开始安装shc"
    if [ "${OS}" == 'CentOS' ];then     
        yum install -y epel-release     
        yum install -y gcc     
        yum install -y shc     
        wget -O /usr/bin/shc --no-check-certificate https://download.alblog.cn/postDataViewer/shc >/dev/null 2>&1     
        chmod 755 /usr/bin/shc     
    else     
        apt update     
        apt install -y gcc      
        apt install -y shc     
        wget -O /usr/bin/shc --no-check-certificate https://download.alblog.cn/postDataViewer/shc >/dev/null 2>&1     
        chmod 755 /usr/bin/shc         
    fi
    fi 
}   
        
input_mark_port(){     
    echo -e "请输入添加的端口信息"     
    read -p "端口:" port 
}  
    
mark_port(){ 
    cat /root/port_control/port_data.txt | grep -w "${port}"  
    if [ $? -eq 0 ]; then
    echo -e "端口${port}流控已存在，不需要重复添加" 
    else 
    iptables -A OUTPUT -p tcp --sport ${port} 
    iptables -A INPUT -p tcp --dport ${port} 
    iptables -A OUTPUT -p udp --sport ${port} 
    iptables -A INPUT -p udp --dport ${port} 
    echo "${port}" >> /root/port_control/port_data.txt  
    echo -e "${port}端口添加成功" 
    fi 
}  
    
input_remove_port(){     
    echo -e "请输入删除的端口信息"     
    read -p "端口:" port 
}  

remove_port(){ 
    iptables -D INPUT -p tcp --dport ${port} 
    iptables -D OUTPUT -p tcp --sport ${port} 
    iptables -D INPUT -p udp --dport ${port} 
    iptables -D OUTPUT -p udp --sport ${port} 
    iptables -D INPUT -p tcp --dport ${port} -j DROP >/dev/null 2>&1 
    sed -i -e "/${port}/d" /root/port_control/port_data.txt 
}  
        
input_check_port(){     
    echo -e "请输入查询的本地监听"     
    read -p "端口:" port   
}  
    
check_port(){ 
    cat /root/port_control/port_data.txt | grep -w "${port}"  
    if [ $? -eq 1 ]; then      
    echo -e "端口${port}还没添加流控，请先添加" 
    else 
    iptables -vnL | grep -w "${port}"  | awk '{ print $2 }'  >> /root/port_control/data_cache.txt  
    Mag=`iptables -vnL | grep -w "${port}"  | awk '{ print $2 }' `     
    if [[ $Mag == *K* ]]; then 
    data1=`cat /root/port_control/data_cache.txt | grep K | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'` 
    else  
    data1=0     
    fi     
    if [[ $Mag == *M* ]]; then 
    data2=`cat /root/port_control/data_cache.txt | grep M | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'`      
    else 
    data2=0     
    fi        
    if [[ $Mag == *G* ]]; then 
    data3=`cat /root/port_control/data_cache.txt | grep G | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'`     
    else 
    data3=0     
    fi 
    GB=`expr $data1 / 1048576 + $data2 / 1024 + $data3` 
    MB=`expr $data1 / 1024 + $data2 + $data3 \* 1024`  
    KB=`expr $data1 + $data2 \* 1024 + $data3 \* 1048576`           
    if [[ $GB -ne 0 ]]; then 
    echo -e "端口${port}已用流量为 $GB GB" 
    rm /root/port_control/data_cache.txt         
    elif [[ $MB -ne 0 ]]; then 
    echo -e "端口${port}已用流量为 $MB MB" 
    rm /root/port_control/data_cache.txt       
    elif [[ $KB -ne 0 ]]; then echo -e "端口${port}已用流量为 $KB KB" 
    rm /root/port_control/data_cache.txt      
    else     
    echo -e "端口${port}还没有监测到流量数据" 
    rm /root/port_control/data_cache.txt      
    fi  
    fi          
}  
    
check_all_port(){ 
    for port in `cat /root/port_control/port_data.txt | awk '{print $1}'`  
    do 
    iptables -vnL | grep -w "${port}"  | awk '{ print $2 }'  >> /root/port_control/data_cache.txt 
    Mag=`iptables -vnL | grep -w "${port}"  | awk '{ print $2 }' `     
    if [[ $Mag == *K* ]]; then 
    data1=`cat /root/port_control/data_cache.txt | grep K | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'`     
    else 
    data1=0     
    fi     
    if [[ $Mag == *M* ]]; then 
    data2=`cat /root/port_control/data_cache.txt | grep M | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'`     
    else 
    data2=0     
    fi        
    if [[ $Mag == *G* ]]; then 
    data3=`cat /root/port_control/data_cache.txt | grep G | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'`     
    else 
    data3=0     
    fi 
    GB=`expr $data1 / 1048576 + $data2 / 1024 + $data3` 
    MB=`expr $data1 / 1024 + $data2 + $data3 \* 1024`  
    KB=`expr $data1 + $data2 \* 1024 + $data3 \* 1048576`           
    if [[ $GB -ne 0 ]]; then 
    echo -e "端口${port}已用流量为 $GB GB" 
    rm /root/port_control/data_cache.txt         
    elif [[ $MB -ne 0 ]]; then 
    echo -e "端口${port}已用流量为 $MB MB" 
    rm /root/port_control/data_cache.txt       
    elif [[ $KB -ne 0 ]]; then 
    echo -e "端口${port}已用流量为 $KB KB" 
    rm /root/port_control/data_cache.txt      
    else     
    echo -e "端口${port}还没有检测到流量数据" 
    rm /root/port_control/data_cache.txt      
    fi          
    done 
}  
    
add_restore_data(){ 
    cat /root/port_control/save_data_log.txt     
    echo -e "请输入要导入的日志时间【格式参考：2020-08-19 11:00 】"     
    read -p "时间:" time   
}  
    
add_restore_port(){ 
    cat /root/port_control/limit_data.txt     
    echo -e "请输入组编号"     
    read -p "组号:" group   
}     
        
add_restore_file(){  
    rm -rf /root/restore.txt 
    cat /root/port_control/save_data_log.txt | grep "${time}"  >> /root/restore.txt 
    sed -i "s/端口：//g" 
    /root/restore.txt 
    sed -i "s/流量：//g" 
    /root/restore.txt 
}    
    
restore_all_port(){ 
    port1=`cat /root/port_control/limit_data.txt | grep ${group}  | awk '{print $1}' | awk 'NR==1' | tr -cd "[0-9]"` 
    port2=`cat /root/port_control/limit_data.txt | grep ${group}  | awk '{print $1}' | awk  'END{print}' | tr -cd "[0-9]"` 
    if [[ ${port1} -eq ${port2} ]]; then  
    port=${port1}  
    Mag=`cat /root/restore.txt| grep -w "${port}"  | awk '{ print $3 }' `     
    if [[ $Mag == *K* ]]; then 
    data1=`grep -w "${port}" /root/restore.txt | grep K | awk '{ print $2 }'`     
    else 
    data1=0     
    fi     
    if [[ $Mag == *M* ]]; then 
    data2=`grep -w "${port}" /root/restore.txt | grep M | awk '{ print $2 }'`     
    else 
    data2=0     
    fi        
    if [[ $Mag == *G* ]]; then 
    data3=`grep -w "${port}" /root/restore.txt | grep G | awk '{ print $2 }'`     
    else 
    data3=0     
    fi   
    GB=`expr $data1 / 1048576 + $data2 / 1024 + $data3`     
    if [[ $GB -ne 0 ]]; then 
    t0=`cat /root/port_control/limit_data.txt | grep ${group}  | awk '{print $2}' | awk 'NR==1' | tr -cd "[0-9]"` 
    t1=`expr $t0 - $GB` 
    echo -e "总流量将更新为：${t1}GB 要继续吗？"  
    read -p "按任意键继续，取消请CTRL+C"       
    sed -i "s/-ge $t0/-ge $t1/g" 
    /usr/share/doc/${group} >/dev/null 2>&1     
    cp /usr/share/doc/${group} /root/port_control/bat/${port}_${group}.sh     
    shc -v -r -f /root/port_control/bat/${port}_${group}.sh >/dev/null 2>&1     
    rm -rf /root/port_control/bat/${port}_${group}.sh >/dev/null 2>&1     
    rm -rf /root/port_control/bat/${port}_${group}.sh.x.c >/dev/null 2>&1     
    mv /root/port_control/bat/${port}_${group}.sh.x 
    /root/port_control/bat/${port}_${group}.sh >/dev/null 2>&1     
    chmod 755 /root/port_control/bat/${port}_${group}.sh >/dev/null 2>&1     
    sed -i "s/$t0/$t1/g" 
    /root/port_control/limit_data.txt | grep ${group} >/dev/null 2>&1     
    echo -e "组号：${group}导入完成"   
    rm -rf /root/restore.txt >/dev/null 2>&1 
    else     
    echo -e "流量好像没有太大变化"     
    rm -rf /root/restore.txt >/dev/null 2>&1 
    fi   
    else 
    for port in `seq ${port1} 1 ${port2}` 
    do 
    Mag=`cat /root/restore.txt| grep -w "${port}"  | awk '{ print $3 }' `     
    if [[ $Mag == *K* ]]; then 
    data1=`grep -w "${port}" /root/restore.txt | grep K | awk '{ print $2 }'`     
    else 
    data1=0     
    fi     
    if [[ $Mag == *M* ]]; then 
    data2=`grep -w "${port}" /root/restore.txt | grep M | awk '{ print $2 }'`     
    else 
    data2=0     
    fi        
    if [[ $Mag == *G* ]]; then 
    data3=`grep -w "${port}" /root/restore.txt | grep G | awk '{ print $2 }'`     
    else 
    data3=0     
    fi   
    GB=`expr $data1 / 1048576 + $data2 / 1024 + $data3` 
    MB=`expr $data1 / 1024 + $data2 + $data3 \* 1024`  
    KB=`expr $data1 + $data2 \* 1024 + $data3 \* 1048576`           
    if [[ $GB -ne 0 ]]; then 
    echo -e "端口${port}已用流量为 $GB GB"  
    echo -e "$GB G"  >> /root/port_control/count.txt     
    elif [[ $MB -ne 0 ]]; then 
    echo -e "端口${port}已用流量为 $MB MB"  
    echo -e "$MB M"   >> /root/port_control/count.txt     
    elif [[ $KB -ne 0 ]]; then    
    echo -e "端口${port}已用流量为 $KB KB" 
    echo -e "$MB M"   >> /root/port_control/count.txt 
    else     
    echo -e "端口${port}还没有记录到流量数据"      
    fi                 
    done 
    sum=0 
    if [ -f "/root/port_control/count.txt" ]; then 
    Mag=`cat /root/port_control/count.txt`     
    if [[ $Mag == *K* ]]; then 
    for kbdata in `cat /root/port_control/count.txt  | grep K | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'` 
    do 
    data1=$[$sum+$kbdata]  
    done 
    fi 
    if [[ $Mag == *M* ]]; then 
    for mbdata in `cat /root/port_control/count.txt  | grep M | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'` 
    do 
    data2=$[$sum+$mbdata]  
    done 
    fi       
    if [[ $Mag == *G* ]]; then 
    for gbdata in `cat /root/port_control/count.txt  | grep G | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'` 
    do 
    data3=$[$sum+$gbdata]  
    done   
    fi   
    GB=`expr $data1 / 1048576 + $data2 / 1024 + $data3` 
    MB=`expr $data1 / 1024 + $data2 + $data3 \* 1024`  
    KB=`expr $data1 + $data2 \* 1024 + $data3 \* 1048576`           
    if [[ $GB -ne 0 ]]; then 
    echo -e "端口${port1}~${port2}已用流量为 $GB GB" 
    rm /root/port_control/count.txt         
    elif [[ $MB -ne 0 ]]; then 
    echo -e "端口${port1}~${port2}已用流量为 $MB MB" 
    rm /root/port_control/count.txt       
    elif [[ $KB -ne 0 ]]; then 
    echo -e "端口${port1}~${port2}已用流量为 $KB KB" 
    rm /root/port_control/count.txt       
    fi  
    else     
    echo -e "端口${port1}~${port2}还没有检测到流量数据"      
    fi 
    if [[ $GB -ne 0 ]]; then 
    t0=`cat /root/port_control/limit_data.txt | grep ${group}  | awk '{print $2}' | awk 'NR==1' | tr -cd "[0-9]"` 
    t1=`expr $t0 - $GB` 
    echo -e "总流量将更新为：${t1}GB 要继续吗？"  
    read -p "按任意键继续，取消请CTRL+C"       
    sed -i "s/-ge $t0/-ge $t1/g" 
    /usr/share/doc/${group} >/dev/null 2>&1     
    cp /usr/share/doc/${group} /root/port_control/bat/${port1}_${port2}_${group}.sh     
    shc -v -r -f /root/port_control/bat/${port1}_${port2}_${group}.sh >/dev/null 2>&1     
    rm -rf /root/port_control/bat/${port1}_${port2}_${group}.sh >/dev/null 2>&1     
    rm -rf /root/port_control/bat/${port1}_${port2}_${group}.sh.x.c >/dev/null 2>&1     
    mv /root/port_control/bat/${port1}_${port2}_${group}.sh.x 
    /root/port_control/bat/${port1}_${port2}_${group}.sh >/dev/null 2>&1     
    chmod 755 /root/port_control/bat/${port1}_${port2}_${group}.sh >/dev/null 2>&1     
    sed -i "s/$t0/$t1/g" /root/port_control/limit_data.txt | grep ${group} >/dev/null 2>&1     
    echo -e "组号：${group}导入完成"   
    rm -rf /root/restore.txt >/dev/null 2>&1    
    else     
    echo -e "流量好像没有太大变化"     
    rm -rf /root/restore.txt >/dev/null 2>&1  
    fi  
    fi
}
    
check_ban_flie(){ 
    if [ -f "/usr/local/src/1" -a -f "/usr/local/src/2" -a -f "/root/port_control/save_data.sh" ]; then     
    echo -e "配置文件已存在" 
    else     
    mkdir /root/port_control/ >/dev/null 2>&1     
    touch /root/port_control/limit_data.txt >/dev/null 2>&1     
    touch /root/port_control/port_data.txt >/dev/null 2>&1     
    touch /root/port_control/log.txt >/dev/null 2>&1     
    mkdir /root/port_control/bat >/dev/null 2>&1     
    wget -O /usr/local/src/1 --no-check-certificate https://download.alblog.cn/postDataViewer/1 >/dev/null 2>&1     
    wget -O /usr/local/src/2 --no-check-certificate https://download.alblog.cn/postDataViewer/2 >/dev/null 2>&1     
    wget -O /root/port_control/save_data.sh --no-check-certificate https://download.alblog.cn/postDataViewer/save_data_c.sh >/dev/null 2>&1          
    chmod 755 /root/port_control/save_data.sh >/dev/null 2>&1       
    echo -e "配置文件已创建"     
    fi 
}  
        
input_ban_port(){    
    echo -e "请输入需要限制的本地监听的端口"    
    read -p "端口:" port 
}  
    
input_ban_data(){     
    echo -e "请输入需要限制的流量(GB)"     
    read -p "可用流量:" data 
}  
    
input_ban_days(){     
    echo -e "请输入有效期(天)"     
    read -p "天数:" days 
} 
    
ban_data(){ 
    no=$RANDOM 
    num=$[$RANDOM%10+1] 
    date=$(date -d "${days} days" +%Y%m%d) 
    cat /root/port_control/limit_data.txt | grep -w "${port}" 
    if [ $? -eq 0 ];then      
    echo -e "端口${port}已限制，如需修改请先取消" 
    else 
    cat /root/port_control/port_data.txt| grep -w "${port}" 
    if [ $? -eq 1 ];then      
    echo -e "端口${port}尚未添加到流控，请先添加" 
    else         
    echo "端口：${port} 流量：${data}G 到期日：${date} 从属组别：$no" >> /root/port_control/limit_data.txt     
    cp /usr/local/src/1 /root/port_control/bat/${port}_${no}.sh >/dev/null 2>&1     
    sed -i "s/+port+/${port}/g" /root/port_control/bat/${port}_${no}.sh >/dev/null 2>&1     
    sed -i "s/+t+/${data}/g" /root/port_control/bat/${port}_${no}.sh >/dev/null 2>&1     
    sed -i "s/+d+/${date}/g" /root/port_control/bat/${port}_${no}.sh >/dev/null 2>&1     
    shc -v -r -f /root/port_control/bat/${port}_${no}.sh >/dev/null >/dev/null 2>&1     
    mv /root/port_control/bat/${port}_${no}.sh /usr/share/doc/${no} >/dev/null 2>&1     
    rm -rf /root/port_control/bat/${port}_${no}.sh >/dev/null 2>&1     
    rm -rf /root/port_control/bat/${port}_${no}.sh.x.c >/dev/null 2>&1     
    mv /root/port_control/bat/${port}_${no}.sh.x /root/port_control/bat/${port}_${no}.sh >/dev/null 2>&1     
    chmod 755 /root/port_control/bat/${port}_${no}.sh >/dev/null 2>&1       
    echo -e "${port}端口已添加限制"     
    if [[ "${OS}" == 'CentOS' ]]; then     
    echo "*/${num} * * * * cd /root/port_control/bat && ./${port}.sh" >> /var/spool/cron/root 
    else     
    echo "*/${num} * * * * cd /root/port_control/bat && ./${port}.sh" >>  /var/spool/cron/crontabs/root     
    fi   
    fi   
    fi 
    }  
    
input_remove_port(){    
    echo -e "请输入取消限制的端口"    
    read -p "端口:" port 
    }  
    
remove_ban_flie(){ 
    cat /root/port_control/limit_data.txt | grep -w "${port}" 
    if [ $? -eq 1 ];then      
    echo -e "端口${port}没有被限制，不需要取消"     
    else 
    num=`cat /root/port_control/limit_data.txt | grep -w "${port}" | awk '{print $4}' | awk 'NR==1' | tr -cd "[0-9]"`     
    rm -rf /usr/share/doc/${num} >/dev/null 2>&1     
    cd /root/port_control/bat && find . -name "${port}*"| xargs rm -rfv >/dev/null 2>&1    
    sed -i -e "/${port}/d" /root/port_control/limit_data.txt     
    iptables -D INPUT -p tcp --dport ${port} -j DROP  >/dev/null 2>&1     
    echo -e "${port}端口已取消限制" 
    fi         
    if [[ "${OS}" == 'CentOS' ]]; then     
    sed -i -e "/${port}/d" /var/spool/cron/root 
    else     
    sed -i -e "/${port}/d" /var/spool/cron/crontabs/root     
    fi             
    }  
    
input_batch_port1(){     
    echo -e "请输入起始端口号"     
    read -p "端口:" port1 
    }  

input_batch_port2(){     
    echo -e "请输入结束端口号"     
    read -p "端口:" port2 
    }  

batch_mark_port(){ 
    for port in `seq ${port1} 1 ${port2}` 
    do cat /root/port_control/port_data.txt | grep -w "${port}" 
    if [ $? -eq 0 ];then      
    echo -e "端口${port}已添加流控" 
    else    
    iptables -A OUTPUT -p tcp --sport ${port} 
    iptables -A INPUT -p tcp --dport ${port} 
    iptables -A OUTPUT -p udp --sport ${port}
    iptables -A INPUT -p udp --dport ${port} 
    echo "${port}" >> /root/port_control/port_data.txt  
    echo -e "${port}端口添加成功" 
    fi 
    done 
    }  
    
del_batch_port1(){     
    echo -e "请输入起始端口号"     
    read -p "端口:" port1 
    }  

del_batch_port2(){     
    echo -e "请输入结束端口号"     
    read -p "端口:" port2 
    }  

restore_mark_port(){ 
    for port in `cat /root/port_control/port_data.txt` 
    do 
    iptables -A OUTPUT -p tcp --sport ${port} 
    iptables -A INPUT -p tcp --dport ${port} 
    iptables -A OUTPUT -p udp --sport ${port} 
    iptables -A INPUT -p udp --dport ${port} 
    echo -e "${port}端口添加成功" 
    done 
    }  
    
batch_del_port(){ 
    for port in `seq ${port1} 1 ${port2}` 
    do 
    iptables -D INPUT -p tcp --dport ${port} 
    iptables -D OUTPUT -p tcp --sport ${port} 
    iptables -D INPUT -p udp --dport ${port} 
    iptables -D OUTPUT -p udp --sport ${port} 
    iptables -D INPUT -p tcp --dport ${port} -j DROP >/dev/null 2>&1 
    sed -i -e "/${port}/d" /root/port_control/port_data.txt 
    echo -e "${port}端口删除成功" 
    done 
    }  

check_key(){ 
    # if [ -f "/usr/share/doc/name" ];then 
    # filename=`cat /usr/share/doc/name` 
    # if [ -f "/sys/class/net/eth0/address" ];then 
    # md5=`cat /sys/class/net/eth0/address | md5sum | tr -d "-"` 
    # elif [ -f "/sys/class/net/ens3/address" ];then 
    # md5=`cat /sys/class/net/ens3/address | md5sum | tr -d "-"` 
    # else 
    # Install_ifconfig md5=`ifconfig |egrep "ether" |awk '{print $2}'| awk 'NR==1'` 
    # fi 
    # base64=`echo $md5 | base64 | tr -d "[0-9]"` curl -s http://port.freefly.best/${filename}/key | grep $base64 >/dev/null 2>&1 
    # if [ $? -eq 0 ];then 
    Install_shc 
    set_portcontrol 
    # else  
    # times=`curl -s http://port.freefly.best/${filename}/times` used=`echo | curl -s http://port.freefly.best/${filename}/key | awk '{ print FNR " " $0}' | grep [0-9] | tr -d "a-zA-Z"  |awk 'END{print $1}'`    
    # if [ $used -ge $times ] >/dev/null 2>&1 ;then  
    # echo -e "授权次数已用完，如需增加请联系TG:@pipkaka"        
    # else echo -e "该机器尚未授权，请问要授行授权吗？" 
    # read -p "按任意键继续，取消请 CTRL+C "         
    # ip=`curl -s ip.sb` 
    # curl -s http://port.freefly.best/${filename}/key >> /home/key 
    # echo "${base64} ${ip}" >> /home/key curl --user shell:0900 -T /home/key http://port.freefly.best:8080/${filename}/ 
    # rm -rf /home/key 
    # echo -e "已完成授权，请重新运行脚本检查授权状态"     
    # fi   
    # fi 
    # else 
    # 
    # fi 
    echo "AnLin 破解"
    add_user
    }  

add_user(){    
    # echo -e "请输入你的用户名"    
    # read -p "用户名:" 
    # name curl -s --connect-timeout 10 http://port.freefly.best/${name}/name | grep ${name} >/dev/null 2>&1 
    # if [ $? -ge 1 ];then 
    # echo -e "查无此用户，请与开发者联系" 
    # else passwd=`echo | curl -s http://port.freefly.best/${name}/passwd`  
    # if [[ -z ${passwd} ]] ;then    
    # echo -e "请输入你的初始密码"    
    # read -p "密码:" passwd0 echo -e "${passwd0}" >> /home/passwd curl --user shell:0900 -T /home/passwd http://port.freefly.best:8080/${name}/  
    # rm -rf /home/passwd 
    # echo -e "完成初始化设定，请重新执行脚本授行授权" 
    # exit 1 
    # else    
    # echo -e "请输入你的密码"    
    # read -p "密码:" passwd1    
    # fi   
    # if [[ ${passwd} -eq ${passwd1} ]];then
    # echo -e "${name}" >> /usr/share/doc/name check_key 
    # else 
    # echo -e "密码错误，请返回重试" 
    # sleep 1 
    # add_user   
    # fi 
    # fi  
        echo "破解成功"
    }
    
del_key(){ 
    if [ -f "/sys/class/net/eth0/address" ];then 
    md5=`cat /sys/class/net/eth0/address | md5sum | tr -d "-"` 
    elif [ -f "/sys/class/net/ens3/address" ];then 
    md5=`cat /sys/class/net/ens3/address | md5sum | tr -d "-"` 
    else 
    Install_ifconfig 
    md5=`ifconfig |egrep "ether" |awk '{print $2}'| awk 'NR==1'` 
    fi 
    base64=`echo $md5 | base64 | tr -d "[0-9]"` 
    echo -e "终于...要说再见了吗？" 
    read -p "按任意键继续，取消请 CTRL+C "         
    # curl -s http://port.freefly.best/${filename}/key >> /home/key 
    # sed -i -e "/${base64}/d" /home/key >/dev/null 2>&1 
    # curl --user shell:0900 -T /home/key http://port.freefly.best:8080/${filename}/ >/dev/null 2>&1 
    rm -rf /home/key >/dev/null 2>&1 
    rm -rf /usr/share/doc/name >/dev/null 2>&1 
    for port in `cat /root/port_control/port_data.txt` 
    do     
    if [[ "${OS}" == 'CentOS' ]]; then     
    sed -i -e "/${port}/d" /var/spool/cron/root >/dev/null 2>&1 
    else     
    sed -i -e "/${port}/d" /var/spool/cron/crontabs/root >/dev/null 2>&1     
    fi 
    done 
    iptables -F >/dev/null 2>&1     
    rm -rf /root/port_control/         
    echo -e "已经删除本机授权，再见！" 
    }  

switch_to_hosts(){ 
    if [ -f "/root/host_data_viewer.sh" ]; then 
    chmod 755 host_data_viewer.sh >/dev/null 2>&1 
    cd /root/ && ./host_data_viewer.sh 
    else 
    wget -O /root/host_data_viewer.sh --no-check-certificate http://port.freefly.best/host_data_viewer.sh >/dev/null 2>&1 
    chmod 755 host_data_viewer.sh >/dev/null 2>&1 
    cd /root/ && ./host_data_viewer.sh 
    fi 
    }  
    
check_batch_port1(){     
    echo -e "请输入起始端口号"     
    read -p "端口:" port1 
    }  

check_batch_port2(){     
    echo -e "请输入结束端口号"     
    read -p "端口:" port2 
    }  

check_all_data(){ 
    for port in `seq ${port1} 1 ${port2}` 
    do 
    cat /root/port_control/port_data.txt | grep -w "${port}" 
    if [ $? -eq 1 ];then      
    echo -e "端口${port}还没添加流控，请先添加" 
    else 
    iptables -vnL | grep -w "${port}"  | awk '{ print $2 }'  >> /root/port_control/data_cache.txt 
    Mag=`iptables -vnL | grep -w "${port}"  | awk '{ print $2 }' `     
    if [[ $Mag == *K* ]]; then 
    data1=`cat /root/port_control/data_cache.txt | grep K | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'`     
    else 
    data1=0     
    fi     
    if [[ $Mag == *M* ]]; then 
    data2=`cat /root/port_control/data_cache.txt | grep M | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'`     
    else 
    data2=0     
    fi        
    if [[ $Mag == *G* ]]; then 
    data3=`cat /root/port_control/data_cache.txt | grep G | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'`     
    else 
    data3=0     
    fi 
    GB=`expr $data1 / 1048576 + $data2 / 1024 + $data3` 
    MB=`expr $data1 / 1024 + $data2 + $data3 \* 1024`  
    KB=`expr $data1 + $data2 \* 1024 + $data3 \* 1048576`           
    if [[ $GB -ne 0 ]]; then 
    echo -e "$GB GB" >>  /root/port_control/count.txt    
    rm /root/port_control/data_cache.txt         
    elif [[ $MB -ne 0 ]]; then 
    echo -e "$MB MB" >>  /root/port_control/count.txt   
    rm /root/port_control/data_cache.txt       
    elif [[ $KB -ne 0 ]]; then 
    echo -e "$KB KB" >>  /root/port_control/count.txt   
    rm /root/port_control/data_cache.txt      
    fi  
    fi          
    done 
    }  

sum_all_data(){ 
    sum=0 
    if [ -f "/root/port_control/count.txt" ]; then 
    Mag=`cat /root/port_control/count.txt`     
    if [[ $Mag == *K* ]]; then 
    for kbdata in `cat /root/port_control/count.txt  | grep K | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'` 
    do 
    data1=$[$sum+$kbdata]  
    done 
    fi 
    if [[ $Mag == *M* ]]; then 
    for mbdata in `cat /root/port_control/count.txt  | grep M | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'` 
    do 
    data2=$[$sum+$mbdata]  
    done 
    fi       
    if [[ $Mag == *G* ]]; then 
    for gbdata in `cat /root/port_control/count.txt  | grep G | tr -d "a-zA-Z" | awk '{sum+=$1} END {print sum}'` 
    do 
    data3=$[$sum+$gbdata]  
    done   
    fi   
    GB=`expr $data1 / 1048576 + $data2 / 1024 + $data3` 
    MB=`expr $data1 / 1024 + $data2 + $data3 \* 1024`  
    KB=`expr $data1 + $data2 \* 1024 + $data3 \* 1048576`           
    if [[ $GB -ne 0 ]]; then 
    echo -e "端口${port1}~${port2}已用流量为 $GB GB" 
    rm /root/port_control/count.txt         
    elif [[ $MB -ne 0 ]]; then 
    echo -e "端口${port1}~${port2}已用流量为 $MB MB" 
    rm /root/port_control/count.txt       
    elif [[ $KB -ne 0 ]]; then 
    echo -e "端口${port1}~${port2}已用流量为 $KB KB" 
    rm /root/port_control/count.txt       
    fi  
    else     
    echo -e "端口${port1}~${port2}还没有检测到流量数据"      
    fi           
    }  

hosts(){ 
    cat /etc/hosts | grep freefly >/dev/null 2>&1 
    if [ $? -eq 0 ];then  
    echo "逗逼一样的防破解" 
    fi 
    }  
    
batch_input_ban_port(){   
        echo -e "请输入需要限制的起始端口"    
        read -p "端口:" port1    
        echo -e "请输入需要限制的结束端口"    
        read -p "端口:" port2 
        }  
        
batch_input_ban_data(){    
    echo -e "请输入需要限制的流量(GB)"     
    read -p "可用流量:" data 
    }  

batch_input_ban_days(){     
    echo -e "请输入有效期(天)"     
    read -p "天数:" days 
    }  

batch_ban_data(){ 
    date=$(date -d "${days} days" +%Y%m%d) 
    no=$RANDOM num=$[$RANDOM%10+1] 
    if [ ${port1} -ge ${port2} ];then  
    echo -e "起始端口不能大于结束端口" 
    fi 
    for port in `seq ${port1} 1 ${port2}` 
    do 
    cat /root/port_control/limit_data.txt | grep -w "${port}" 
    if [ $? -eq 0 ];then      
    echo -e "端口${port}已限制，如需修改请先取消" 
    else 
    cat /root/port_control/port_data.txt| grep -w "${port}" 
    if [ $? -eq 1 ];then      
    echo -e "端口${port}尚未添加到流控，请先添加" 
    else         
    echo "端口：${port} 流量：${data}G 到期日：${date} 从属组别：$no " >> /root/port_control/limit_data.txt     
    cp /usr/local/src/2 /root/port_control/bat/${port1}_${port2}_${no}.sh     
    echo -e "${port}端口已添加限制" 
    fi   
    fi   
    done      
    if [ -f "/root/port_control/bat/${port1}_${port2}_${no}.sh" ]; then     
    sed -i "s/+port1+/${port1}/g" 
    /root/port_control/bat/${port1}_${port2}_${no}.sh >/dev/null 2>&1     
    sed -i "s/+port2+/${port2}/g" 
    /root/port_control/bat/${port1}_${port2}_${no}.sh >/dev/null 2>&1     
    sed -i "s/+no+/${no}/g" 
    /root/port_control/bat/${port1}_${port2}_${no}.sh >/dev/null 2>&1     
    sed -i "s/+t+/${data}/g" 
    /root/port_control/bat/${port1}_${port2}_${no}.sh >/dev/null 2>&1     
    sed -i "s/+d+/${date}/g" 
    /root/port_control/bat/${port1}_${port2}_${no}.sh >/dev/null 2>&1     
    shc -v -r -f /root/port_control/bat/${port1}_${port2}_${no}.sh >/dev/null 2>&1     
    mv /root/port_control/bat/${port1}_${port2}_${no}.sh /usr/share/doc/${no}>/dev/null 2>&1     
    rm -rf /root/port_control/bat/${port1}_${port2}_${no}.sh >/dev/null 2>&1     
    rm -rf /root/port_control/bat/${port1}_${port2}_${no}.sh.x.c >/dev/null 2>&1     
    mv /root/port_control/bat/${port1}_${port2}_${no}.sh.x 
    /root/port_control/bat/${port1}_${port2}_${no}.sh >/dev/null 2>&1     
    chmod 755 /root/port_control/bat/${port1}_${port2}_${no}.sh >/dev/null 2>&1     
    if [[ "${OS}" == 'CentOS' ]]; then     
    echo "*/${num} * * * * cd /root/port_control/bat && ./${port1}_${port2}_${no}.sh" >> /var/spool/cron/root 
    else     
    echo "*/${num} * * * * cd /root/port_control/bat && ./${port1}_${port2}_${no}.sh" >>  /var/spool/cron/crontabs/root     
    fi 
    fi 
    }  
    
batch_del_ban_port(){ 
    cat /root/port_control/limit_data.txt     
    echo -e "请要输入删除的输入组编号"     
    read -p "组号:" group  
    }     
    
batch_remove_ban_flie(){  
    port1=`cat /root/port_control/limit_data.txt | grep ${group}  | awk '{print $1}' | awk 'NR==1' | tr -cd "[0-9]"` 
    port2=`cat /root/port_control/limit_data.txt | grep ${group}  | awk '{print $1}' | awk  'END{print}' | tr -cd "[0-9]"` 
    if [[ ${port1} -eq ${port2} ]]; then 
    echo -e "这个是单端口限制，请用按端口删除" 
    else 
    for port in `seq ${port1} 1 ${port2}` 
    do cat /root/port_control/limit_data.txt | grep -w "${port}" 
    if [ $? -eq 1 ];then      
    echo -e "端口${port}没有被限制，不需要取消"   
    else num=`cat /root/port_control/limit_data.txt | grep -w "${port}" | awk '{print $4}' | awk 'NR==1' | tr -cd "[0-9]"`     
    rm -rf /usr/share/doc/${num} >/dev/null 2>&1     
    cd /root/port_control/bat && find . -name "${port}*"| xargs rm -rfv >/dev/null 2>&1     
    sed -i -e "/${port}/d" /root/port_control/limit_data.txt >/dev/null 2>&1     
    iptables -D INPUT -p tcp --dport ${port} -j DROP >/dev/null 2>&1     
    echo -e "${port}端口已取消限制"     
    if [[ "${OS}" == 'CentOS' ]]; then     
    sed -i -e "/${port}/d" /var/spool/cron/root 
    else     
    sed -i -e "/${port}/d" /var/spool/cron/crontabs/root     
    fi     
    fi                
    done    
    fi          
    }  
    
check_log(){ 
    if test -s /root/port_control/log.txt; then   
    tail /root/port_control/log.txt 
    else   
    echo "暂无端口限制记录" 
    fi 
    }  

check_data_log(){ 
    if test -s /root/port_control/save_data_log.txt; then   
    cat /root/port_control/save_data_log.txt 
    else   
    echo "暂无流量日志记录，请稍后再试" 
    fi 
    }  
    
save_data_log(){     
    if [[ "${OS}" == 'CentOS' ]]; then     
    cat /var/spool/cron/root | grep save_data >/dev/null 2>&1 
    else     
    cat /var/spool/cron/crontabs/root| grep save_data >/dev/null 2>&1     
    fi 
    if [ $? -eq 1 ];then      
    if [[ "${OS}" == 'CentOS' ]]; then     
    echo "0 */1 * * * cd /root/port_control && ./save_data.sh" >> /var/spool/cron/root     
    echo "流量日志记录：已开启" 
    else     
    echo "0 */1 * * * cd /root/port_control && ./save_data.sh" >>  /var/spool/cron/crontabs/root     
    echo "流量日志记录：已开启"     
    fi   
    else     
    if [[ "${OS}" == 'CentOS' ]]; then     
    sed -i -e "/save_data/d" /var/spool/cron/root      
    echo "流量日志记录：已关闭" 
    else     
    sed -i -e "/save_data/d" /var/spool/cron/crontabs/root     
    echo "流量日志记录：已关闭"     
    fi     
    fi 
    }  
    
add_data_log(){ 
    cd /root/port_control && ./save_data.sh  echo 
    "已执行备份，请返回确认是否备份成功" 
    }  

disable_selinux(){     
    if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then         
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config         
    setenforce 0     
    fi 
    }  

config_ip(){     
    echo -e "请输入IP转发规则！"     
    read -p "请输入本地监听端口:" port1     
    read -p "请输入远端转发端口:" port2     
    read -p "请输入被转发IP:" ForwardIP 
    }  
    
config_domain(){     
    echo -e "请输入域名转发规则！"     
    read -p "请输入本地监听端口:" port1     
    read -p "请输入远端转发端口:" port2     
    read -p "请输入被转发域名:" ForwardDomain 
    }  

delete_rule(){     
    echo -e "请输入需要删除的转发规则信息！"     
    read -p "请输入被删除规则的对应本地端口:" port 
    pid=$(ps -aux | grep ${port} |grep -v grep |awk '!/#/{printf$2"\n"}')
    kill -9 ${pid}     
    sleep 1 
    sed -i '/'${port}'/d' 
    /root/port_forward/ip_record.txt >/dev/null 2>&1 
    sed -i '/'${port}'/d' 
    /root/port_forward/ddns_record.txt >/dev/null 2>&1 
    echo -e "转发规则删除成功！" 
    } 
    
View_forwarding(){ 
    forward_table_Total=$(ps -ef | grep forward_table | grep -v grep | grep -v "forward_table.sh" | wc -l) 
    if [[ ${forward_table_Total} = "0" ]]; then 
        echo -e "${Error} 尚未又正在运行的转发规则，请检查 !" && sleep 1 && set_forwardmethod 
    fi
        forward_table_list_all="" 
    for((integer = 1; integer <= ${forward_table_Total}; integer++)) 
    do 
    forward_table_all=$(ps -ef | grep forward_table | grep -v grep | grep -v "forward_table.sh")
    forward_table_pid=$(echo -e "${forward_table_all}"| sed -n "${integer}p"| awk '{print $2}')
    forward_table_listen=$(echo -e "${forward_table_all}"| sed -n "${integer}p"| awk '{print $10}'| awk -F ':' '{print $NF}')
    forward_table_fork=$(echo -e "${forward_table_all}"| sed -n "${integer}p"| awk '{print $12}')
    forward_table_type_tcp=$(echo -e "${forward_table_all}"| sed -n "${integer}p"| awk '{print $13}')
    if [[ ${forward_table_type_tcp} == "-t" ]]; then 
    forward_table_type_udp=$(echo -e "${forward_table_all}"| sed -n "${integer}p"| awk '{print $14}')
    if [[ ${forward_table_type_udp} == "-u" ]]; then 
    forward_table_type="TCP+UDP" 
    else 
    forward_table_type="TCP" 
    fi 
    else 
    forward_table_type="UDP" 
    fi
    forward_table_list_all=${forward_table_list_all}"进程PID: "${forward_table_pid}" 类型: "${forward_table_type}" 监听端口: "${forward_table_listen}" 转发IP和端口: "${forward_table_fork}"\n"
    done 
    echo 
    echo -e "当前您有" ${forward_table_Total} "条转发记录" 
    echo -e "${forward_table_list_all}" 
    }  
    
mark_IPRecord(){ 
    echo "${port1} ${port2} ${ForwardIP}" >> /root/port_forward/ip_record.txt 
    }  
    
mark_DomainRecord(){ 
    echo "${port1} ${port2} ${ForwardDomain} $(nslookup ${ForwardDomain}|grep Add |awk '!/#/{printf$2"\n"}')" >> /root/port_forward/ddns_record.txt 
    }  

run_ip_forward(){ 
    cd /root/port_forward && ./keep_ip_alive.sh  
    echo -e "已经启动IP转发" 
    }  

run_domain_forward(){ 
    cd /root/port_forward && ./keep_domain_alive.sh 
    echo -e "已经启动ddns转发" 
    }  

stop_forward(){ 
    ps -ef | grep forward_table | grep -v grep | awk '{print $2}' | xargs kill -9 echo -e "已经停止所有端口转发" 
    }  

forward_guard(){   
    if [[ "${OS}" == 'CentOS' ]]; then     
    cat /var/spool/cron/root | grep alive >/dev/null 2>&1 
    else     
    cat /var/spool/cron/crontabs/root| grep alive >/dev/null 2>&1     
    fi 
    if [ $? -eq 1 ];then      
    if [[ "${OS}" == 'CentOS' ]]; then     
    echo "*/5 * * * * cd /root/port_forward && ./keep_ip_alive.sh" >> /var/spool/cron/root     
    echo "*/2 * * * * cd /root/port_forward && ./keep_domain_alive.sh" >> /var/spool/cron/root     
    echo "转发程序守护：已开启" else     
    echo "*/5 * * * * cd /root/port_forward && ./keep_ip_alive.sh" >>  /var/spool/cron/crontabs/root     
    echo "*/2 * * * * cd /root/port_forward && ./keep_domain_alive.sh" >>  /var/spool/cron/crontabs/root     
    echo "转发程序守护：已开启"     
    fi   
    else     
    if [[ "${OS}" == 'CentOS' ]]; then     
    sed -i -e "/alive/d" /var/spool/cron/root      
    echo "转发程序守护：已关闭" else     
    sed -i -e "/alive/d" /var/spool/cron/crontabs/root     
    echo "转发程序守护：已关闭"     
    fi     
    fi 
    }   

Install_nslookup(){     
    if [ "${OS}" == 'CentOS' ];then     
    rpm -qa | grep bind-utils >/dev/null 2>&1     
    if [[ $? -eq 0 ]];then      
    echo -e "nslookup配置已存在"      
    else     
    echo -e "正在安装相关依赖..."     
    yum install -y bind-utils     
    fi 
    else     
    dpkg --get-selections | grep dnsutils >/dev/null 2>&1     
    if [[ $? -eq 0 ]];then      
    echo -e "nslookup配置已存在"      
    else     
    echo -e "正在安装相关依赖..."     
    sudo apt-get install -y dnsutils     
    fi 
    fi 
    }  
    
Install_port_forward(){ 
    if [ -f "/root/port_forward/forward_table" -a -f "/root/port_forward/keep_domain_alive.sh" -a -f "/root/port_forward/keep_ip_alive.sh" ]; then 
    echo -e "即将切换到端口转发配置界面..." 
    sleep 1 
    set_forwardmethod 
    else 
    echo -e "正在配置端口转发环境..." 
    mkdir /root/port_forward 
    wget -O /root/port_forward/keep_domain_alive.sh https://download.alblog.cn/postDataViewer/keep_domain_alive.sh >/dev/null 2>&1 
    wget -O /root/port_forward/keep_ip_alive.sh https://download.alblog.cn/postDataViewer/keep_ip_alive.sh >/dev/null 2>&1 
    KernelBit="$(getconf LONG_BIT)"     
    if [[ "$KernelBit" == '32' ]];then         
    wget -O /root/port_forward/forward_table --no-check-certificate https://download.alblog.cn/postDataViewer/forward_table_x86 >/dev/null 2>&1     
    elif [[ "$KernelBit" == '64' ]];then         
    wget -O /root/port_forward/forward_table --no-check-certificate https://download.alblog.cn/postDataViewer/forward_table_x86 >/dev/null 2>&1     
    fi     
    if [ -f /root/port_forward/forward_table ]; then     
    chmod +x /root/port_forward/forward_table     
    chmod 755 /root/port_forward/keep_domain_alive.sh     
    chmod 755 /root/port_forward/keep_ip_alive.sh     
    echo -e "端口转发环境配置成功"     
    set_forwardmethod    
    else     
    echo -e "端口转发环境配置失败，请检查网络环境"     
    exit 1     
    fi 
    fi     
    }  
    
set_forwardmethod(){     
    echo -e "${Green}选择脚本功能：" 
    echo -e "${Blue}1. 添加IP转发" 
    echo -e "${Blue}2. 添加DDNS域名转发"
    echo -e "${Blue}3. 删除指定转发规则" 
    echo -e "${Blue}4. 查看所有转发规则" 
    echo -e "${Blue}5. 一键开启所有转发"
    echo -e "${Blue}6. 一键停止所有转发"
    echo -e "${Blue}7. 转发程序守护（开/关）"
    echo -e "${Blue}8. 返回目录"
    echo  
    read -p "Please enter a number:" num 
    case "$num" in     1)     
    checkos     
    config_ip     
    mark_IPRecord      
    run_ip_forward     
    sleep 1     
    set_forwardmethod     
    ;;     
    2)     
    checkos    
    config_domain     
    mark_DomainRecord       
    run_domain_forward     
    sleep 1     
    set_forwardmethod         
    ;;     
    3)     
    checkos     
    View_forwarding     
    delete_rule     
    sleep 1     
    set_forwardmethod         
    ;;     
    4)     
    checkos     
    View_forwarding     
    sleep 1     
    set_forwardmethod     
    ;;     
    5)     
    run_ip_forward     
    run_domain_forward     
    sleep 1     
    set_forwardmethod     
    ;;     
    6)     
    stop_forward     
    sleep 1     
    set_forwardmethod         
    ;;       
    7)       
    forward_guard     
    sleep 1     
    set_forwardmethod         
    ;;       
    8)     
    sleep 1     
    set_portcontrol     
    ;;     
    *)     
    clear     
    echo -e "Please enter the correct number!"     
    sleep 1     
    set_forwardmethod     
    ;;     
    esac 
    }   
    
set_portcontrol(){      
    echo -e "选择脚本功能：" 
    echo -e "1. 添加指定端口流控" 
    echo -e "2. 删除指定端口流控" 
    echo    "-------------------" 
    echo -e "3. 查看指定端口流量"
    echo -e "4. 查看全部端口流量"
    echo    "-------------------" 
    echo -e "5. 限制指定端口用量"
    echo -e "6. 取消指定端口限制"
    echo    "-------------------"      
    echo -e "7. 批量添加端口流控"      
    echo -e "8. 批量删除端口流控"    
    echo -e "9. 批量查询端口流量"       
    echo    "-------------------"      
    echo -e "10. 批量添加限制端口用量"        
    echo -e "11. 批量删除限制端口用量"         
    echo    "-------------------"   
    echo -e "12. 自动流量日志备份(开/关)"
    echo -e "13. 执行手动备份流量日志" 
    echo    "-------------------" 
    echo -e "14. 查看备份流量日志记录"
    echo -e "15. 查看端口限制日志记录" 
    echo -e "16. 重启后恢复端口流控"
    echo -e "17. 重启后批量导入日志"
    echo    "-------------------"      
    echo -e "18. 切换为流控全机流量"      
    echo -e "19. 配置规则到端口转发"      
    echo    "-------------------" 
    echo -e "20. 退出脚本"
    echo -e "88. 删除本机授权"
    echo  ""
    read -p "Please enter a number:" num 
    case "$num" in     
    88)     
    checkos     
    del_key     
    exit 1     
    ;;     
    1)     
    checkos     
    Install_iptables     
    check_ban_flie     
    input_mark_port     
    mark_port     
    sleep 1      
    set_portcontrol     
    ;;     
    2)     
    input_remove_port     
    remove_port     
    echo -e "${port}端口删除成功"     
    sleep 1     
    set_portcontrol     
    ;;     
    3)     
    input_check_port     
    check_port     
    sleep 5     
    set_portcontrol    
    ;;     
    4)     
    check_all_port     
    sleep 1     
    set_portcontrol     
    ;;         
    5)     
    checkos     
    Install_crontabs     
    check_ban_flie     
    input_ban_port     
    input_ban_data     
    input_ban_days     
    ban_data     
    sleep 1     
    set_portcontrol     
    ;;     
    6)     
    checkos     
    input_remove_port     
    remove_ban_flie     
    sleep 1     
    set_portcontrol     
    ;;       
    7)     
    checkos     
    Install_iptables     
    check_ban_flie     
    input_batch_port1     
    input_batch_port2     
    batch_mark_port       
    sleep 1     
    set_portcontrol     
    ;;       
    8)     
    del_batch_port1     
    del_batch_port2     
    batch_del_port     
    sleep 1     
    set_portcontrol     
    ;;      
    9)     
    check_batch_port1     
    check_batch_port2     
    check_all_data >/dev/null 2>&1     
    sum_all_data     
    ;;      
    10)         
    checkos     
    Install_crontabs     
    check_ban_flie     
    batch_input_ban_port     
    batch_input_ban_data     
    batch_input_ban_days     
    batch_ban_data     
    sleep 1     
    set_portcontrol     
    ;;       
    11)     
    checkos     
    batch_del_ban_port     
    batch_remove_ban_flie     
    sleep 1     
    set_portcontrol     
    ;;     
    12)     
    checkos     
    save_data_log     
    sleep 1     
    set_portcontrol     
    ;;         
    13)     
    add_data_log     
    sleep 1     
    set_portcontrol     
    ;;           
    14)     
    check_data_log     
    ;;        
    15)     
    check_log     
    ;;     
    16)     
    echo -e "注意：如流控仍然有效，导入会导致脚本出错"     
    read -p "按任意键继续，取消请CTRL+C"         
    restore_mark_port      
    ;;      
    17)     
    add_restore_data     
    add_restore_port     
    cat /root/port_control/limit_data.txt | grep ${group}     
    if [ $? -eq 1 ];then      
    echo -e "没有找到该组号，请再次核对"     
    sleep 1     
    set_portcontrol     
    fi               
    add_restore_file     
    restore_all_port     
    sleep 1     
    set_portcontrol     
    ;;       
    18)     
    echo -e "注意：全机流控与端口流控可能存在冲突，请留意！"     
    read -p "按任意键继续切换，取消请CTRL+C"         
    switch_to_hosts     
    ;;        
    19)     
    Install_port_forward     
    ;;                                                           
    20)     
    exit 1     
    ;;                            
    *)     
    clear     
    echo -e "Please enter the correct number!"     
    sleep 1     
    set_portcontrol     
    ;;     
    esac 
    }  
    checkos 
    hosts 
    Install_curl 
    stop_firewall 
    check_key