jjy黑白名单同步脚本

jjy黑白名单同步脚本

#!/bin/bash
# Author    : royoy
# Desc       : Add, delete and query IP for jjy
# Version   : 0.0.2
# Usage:    : add_ip_for_jy.sh

# Source font file
. /etc/rc.d/init.d/font

salt_cmd="/usr/bin/salt"
node_group="JY_Web_Proxy"
conf_file_dir="/srv/salt/sources/jjy/nginx"
frontend_conf="${conf_file_dir}/frontend_allow.zone"
backoffice_conf="${conf_file_dir}/backoffice.zone"
log_file="/var/log/ip_rsync.log"
current_time="$(date +"%F %T")"
ip=""

stty erase ^H

# Format funtion
format_output_start(){
        echo -e "\n\n" >> $log_file
        echo "=============================== ${current_time} ==============================" >> $log_file
}

format_output_end(){
        wait
        result_char="`sed -n "/$current_time/,//p" $log_file |awk '/Failed:/{if($2 == 0)printf "\033[32mOK\t\033[0m";else printf "\033[31mERROR\t\033[0m"}'`"
        [ `echo "$result_char" | egrep -q "ERROR"` ] && {
            font -red "\nERROR" -reset -n -n ; font "ERROR" -n >> $log_file
        } || {
            font -green "\nOK" -reset -n -n ; font "OK" -n >> $log_file
        }
        echo "===================================== End ========================================" >> $log_file
}

# ---------------------
# Function functions
# ---------------------
ff1(){
        read -p "Please input IP : " ip
        echo
        [ "$ip" == "exit" -o "$ip" == "" -o "$ip" == "quit" -o "$ip" == "q" -o "$ip" == "x" ] && {
            font "[ No Operation ] 未操作,正常退出" -n >> $log_file
            format_output_end ; exit
        }
}

ff2(){
        [ "$IP" == "exit" -o "$IP" == "" -o "$IP" == "quit" -o "$IP" == "q" -o "$IP" == "x" ] && {
            font "[ " -green "No Operation" -reset " ] " "未操作,正常退出" -n >> $log_file
            format_output_end ; exit
        }
}

ff3(){
        check_ip_valid "$IP" "x"
        [ "`fgrep -o "$IP" "$1"`" == "$IP" ] && {
                font "查询结果: " -blue "$2" -reset -yellow " [ $IP ] " -reset -green "已存在" -reset -n
        } || {
                font "查询结果: " -blue "$2" -reset -yellow " [ $IP ] " -reset -purple "不存在" -reset -n
        }
}

ff4(){
        echo "输入需要查询的IP,多个IP以空格分开(exit退出脚本)"
        ff1
        for IP in $ip
        do
                ff3 "$1" "$2"
        done
}

ff5(){
        [ "$1" == "frontend" ] && tmp_v1="前" ;[ "$1" == "backoffice" ] && tmp_v1="后" ;[ "$1" == "all" ] && tmp_v1="前台和"
        format_output_start
        echo "输入需要添加的IP,多个IP以空格分开(exit退出脚本)"
        ff1
        echo -e "\n添加 [ $ip ] 到"${tmp_v1}"台白名单\n" >> $log_file
        for IP in $ip
        do
                check_ip_valid "$IP"
                [ "$1" == "frontend" ] && echo -e "allow $IP;\t\t#$current_time" >> $frontend_conf
                [ "$1" == "backoffice" ] && sed -i "/deny all/i allow $IP;\        #$current_time" $backoffice_conf
                [ "$1" == "all" ] && {
                        echo -e "allow $IP;\t\t#$current_time" >> $frontend_conf
                        sed -i "/deny all/i allow $IP;\        #$current_time" $backoffice_conf
                }
        done
        ff6
        format_output_end
}

ff6(){
        $salt_cmd -N "$node_group" state.sls saltenv="update" nginx_sls.nginx_file_manage |tee -a $log_file ; wait
}

ff7(){
        tmp_v3="$1" ; [ "$tmp_v3" == "frontend" ] && tmp_v1="前" ;[ "$tmp_v3" == "backoffice" ] && tmp_v1="后" ;[ "$tmp_v3" == "all" ] && tmp_v1="前台和"
        format_output_start
        echo "输入需要删除的IP,多个IP以空格分开(exit退出脚本)"
        ff1
        echo -e "\n删除 [ $ip ] 从"${tmp_v1}"台白名单\n" >> $log_file
        tmp_v2=1
        for IP in $ip
        do
                check_ip_valid "$IP"
                p1=$(ff8 1); p2=$(ff8 2); p3=$(ff8 3); p4=$(ff8 4)
                [ "$tmp_v3" == "frontend" ] && {
                        ff9 $p1 $p2 $p3 $p4 "$frontend_conf" || ff10 "前台"
                } || {
                        [ "$tmp_v3" == "backoffice" ] && {
                                ff9 $p1 $p2 $p3 $p4 "$backoffice_conf" || ff10 "后台"
                        } || {
                                [ "$tmp_v3" == "all" ] && {
                                      ff9 $p1 $p2 $p3 $p4 "$frontend_conf" || ff10 "前台"
                                      ff9 $p1 $p2 $p3 $p4 "$backoffice_conf" || ff10 "后台"
                                } 
                        }
                }
        done
        [ $tmp_v2 -eq 0 ] && ff6 ; format_output_end 
}

ff8(){
        echo $IP | cut -d. -f"$1"
}

ff9(){
        [ "`fgrep -o "$IP" "$5"`" == "$IP" ] && {
                sed -i "/$1\.$2\.$3\.$4/d" $5 ; tmp_v2=0
        }
}

ff10(){
        font -blue "$1" -reset -yellow " [ $IP ] " -reset -purple "不存在, 删除未执行" -reset -n ; font "$1 [ $IP ] 不存在, 删除未执行" -n >> $log_file
}

ff11(){
        font -n -blue "$2最近5条记录如下:" -reset -n ; tail -5 "$1"
}

ff12(){
        salt 'JY_XG_Proxy_1-44' cmd.run '/usr/local/bin/jy_update' &>/dev/null
}

ff13(){
        wget http://jy.update.org/frontend_allow.zone -O /opt/update/frontend_allow.zone &>/dev/null
        wait
        wget http://jy.update.org/backoffice.zone -O /opt/update/backoffice.zone  &>/dev/null
        wait
}

ff14(){
        ff12 || ff12 || {
                echo "File rsysc failed" | tee -a $log_file
                exit
        }
        ff13
}

ff15(){
       ff14 && {
                /bin/cp -f /opt/update/{frontend_allow.zone,backoffice.zone} /srv/salt/sources/jjy/nginx/
       } 
}

ff16(){
      font "rsyscing file,please wait a few seconds..." -n
      ff15 && font "Finished" -n
}

#
check_ip_valid(){
        IP="$1"
        while  :
        do
                echo "$IP" | egrep -q '^([0-9]{1,3}(\.\b|$)){4}$'
                [ $? -eq 0 ] && {
                        valid=1
                        for number in ${IP//./ }
                        do
                                [ $number -gt 255 ] && valid=0 &&  break
                        done
                        [ $valid -eq 1 ] && break
                }
                echo -n "IP格式错误[ " ; font -red "$IP" -reset ; echo " ],不用重新输入所有的IP,只需要修改错误的IP即可"
                tmp_ip="$IP"
                echo ; read -p "请输入正确的IP: " IP ; echo ;ff2
                [ "$2" != "x" ] && echo -e "IP【$tmp_ip】格式错误,已修正为[$IP]\n" >> $log_file
        done
}

# ---------------------
# Query module
# ---------------------
query_frontend_ip(){
        ff4 "${frontend_conf}" "前台白名单"
}

query_backoffice_ip(){
        ff4 "${backoffice_conf}" "后台白名单"
}

query_frontend_backoffice_ip(){
        echo "输入需要查询的IP,多个IP以空格分开(exit退出脚本)"
        ff1
        for IP in $ip
        do
                ff3 "${frontend_conf}" "前台白名单"
                ff3 "${backoffice_conf}" "后台白名单"
        done
}

# ---------------------
# Add module
# ---------------------
add_frontend_ip(){
        ff5 "frontend"
}

add_backoffice_ip(){
        ff5 "backoffice"
}

add_frontend_backoffice_ip(){
        ff5 "all"
}

# ---------------------
# Delete module
# ---------------------
del_frontend_ip(){
        ff7 "frontend"
}

del_backoffice_ip(){
        ff7 "backoffice"
}

del_frontend_backoffice_ip(){
        ff7 "all"
}

# ---------------------
# Display module
# ---------------------
display_recent_frontend_entries(){
        ff11 "$frontend_conf" "前台"
}
display_recent_backoffice_entries(){
        ff11 "$backoffice_conf" "后台"
}
display_recent_all_entries(){
        ff11 "$frontend_conf" "前台"
        ff11 "$backoffice_conf" "后台"
}


# ---------------------
# Main module
# ---------------------
ff16
font -yellow "请选择一项操作(1|2|3|4|5|6|7|8|9|10|11|12|13):" -n -reset
select i in 添加到前台 添加到后台 添加到前台和后台 从前台删除 从后台删除 从前台和后台删除 从前台查询 从后台查询 从前后台查询 显示前台5条 显示后台5条 显示前后台5条 更新本地配置文件 退出脚本
do
        case $i in
        1|添加到前台)
            add_frontend_ip
            ;;
        2|添加到后台)
            add_backoffice_ip
            ;;
        3|添加到前台和后台)
            add_frontend_backoffice_ip
            ;;
        4|从前台删除)
            del_frontend_ip
            ;;
        5|从后台删除)
            del_backoffice_ip    
            ;;
        6|从前台和后台删除)
            del_frontend_backoffice_ip
            ;;
        7|从前台查询)
            query_frontend_ip
            ;;
        8|从后台查询)
            query_backoffice_ip
            ;;
        9|从前后台查询)
            query_frontend_backoffice_ip
            ;;
        10|显示前台5条)
            display_recent_frontend_entries
            ;;
        11|显示后台5条)
            display_recent_backoffice_entries
            ;;
        12|显示前后台5条)
            display_recent_all_entries 
            ;;
        13|更新本地配置文件)
            ff16
            ;;
        *)
            exit
        esac
        font -n -yellow "请选择一项操作(1|2|3|4|5|6|7|8|9|10|11|12|13):" -n -reset
        font "1) 添加到前台\t\t8) 从后台查询\n2) 添加到后台\t\t9) 从前后台查询\n3) 添加到前台和后台    10) 显示前台5条\n4) 从前台删除\t       11) 显示后台5条\n5) 从后台删除\t       12) 显示前后台5条\n6) 从前台和后台删除    13) 更新本地配置文件\n7) 从前台查询 \t14) 退出脚本" -n
done