#!/bin/sh

. ../common/ajax_common


get_blackip_num()
{
    ${FLOWEYE} blackip list | awk 'END{print NR}'
}


load_blackip_list()
{
    [ "${CGI_page}"  = "" ] && CGI_page=1
    [ "${CGI_limit}" = "" ] && CGI_limit=100

    end=$((${CGI_page} * ${CGI_limit}))
    start=$((${end} - ${CGI_limit}))
    
    ${FLOWEYE} blackip list | grep -i "${CGI_keyword}" | awk \
    -v start=${start} -v end=${end} \
    'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
		row++;
		if( row > end  || row < start ) next;

		printf "%s", dot;
		printf "{";
		printf "\"id\":\"%s\",", NR;
		printf "\"ipaddr\":\"%s\"", $1;
		printf ",\"time\":\"%s\"", $2;
		printf "}";
        
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":%s}", row;
	}'
}


load_blackip_desc()
{
    ${FLOWEYE} ipobj list blackip=1 | awk \
    'BEGIN{
        dot = "";
        printf "{";
    }{
        desc = $(NF - 2) == "none" ? "" : $(NF - 2);
        mac  = $5;

        printf "%s\"%s\":{", dot, $1;
        printf "\"desc\":\"%s\"", desc;
        printf ",\"mac\":\"%s\"", mac;
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "}";
    }'
}


list_blackip()
{
    printf "{"
    printf "\"list\":`load_blackip_list`,"
    printf "\"desc\":`load_blackip_desc`"
    printf "}"
}


add_blackip()
{
    [ "${CGI_ipaddr}" = "" ] && retjson 1 "NO_IP"

    if [ "${CGI_time}" != "0" ]; then
        now=`date +%s`
        dur=$((${CGI_time} + ${now}))
        CGI_ipaddr="${CGI_ipaddr}/${dur}"
    fi

    cmdargs="blackip add ip=${CGI_ipaddr}"
    errmsg=`${FLOWEYE} ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}" 
    else
        sync_floweye "${cmdargs}"
        WEB_LOGGER "IP" "ip=${CGI_ipaddr}"
        retjson 0 "ɹ"
    fi
}


rmv_blackip()
{
    cmdargs="blackip remove ip=${CGI_ipaddr}"
    errmsg=`${FLOWEYE} ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}" 
    else
        sync_floweye "${cmdargs}"
        WEB_LOGGER "ɾIP" "ip=${CGI_ipaddr}"
        retjson 0 "ɹ"
    fi
}


btnrmv_blackip()
{
    for ipaddr in ${CGI_items}
    do
        cmdargs="blackip remove ip=${ipaddr}"
        errmsg=`${FLOWEYE} ${cmdargs}`
        [ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
        sync_floweye ${cmdargs}
    done

    WEB_DOWNLOAD "ɾIP" "${CGI_items}"
    retjson 0 "ɹ"
}


export_blackip()
{
    file_name="IP.conf"
    file_path="${WEB_DOWNLOAD}/${file_name}"

    mkdir -p ${WEB_DOWNLOAD}

    now=`date +%s`

    ${FLOWEYE} blackip list | awk \
    -v now=${now} \
    '{
        ip=$1;
        
        if($2 != 0)
            time = $2 - now;
        else
            time = 0;

        print ip,time;
    }' > ${file_path}

    WEB_LOGGER "IP"
    retjson 0 "OK" "{\"file_name\":\"${file_name}\"}"
}


import_blackip()
{
    if [ -f "${CGI_file}" ]; then
        ok=0
        cmdargs=""

        while read ip time
        do
            if [ "${time}" != "0" ]; then
                now=`date +%s`
                dur=$((${time} + ${now}))
                ip="${ip}/${dur}"
            fi

            cmdargs="${cmdargs} ip=${ip}"
            ok=$((${ok} + 1))
        done <<EOF
            `cat ${CGI_file} | tr -ds '\r' ''`
EOF

        errmsg=`${FLOWEYE} blackip add ${cmdargs}`

        if [ $? -ne 0 ]; then
            retjson 1 "ʧܣ${errmsg}" 
        else
            sync_floweye " blackip add ${cmdargs}"
            WEB_LOGGER "IP" "${ok}"
            retjson 0 "ɹ,${ok}"
        fi
    fi
}


case "${CGI_action}" in
    "get_blackip_num")
        retjson 0 "OK" "`get_blackip_num`"
        ;;
        
    "list_blackip")
		retjson 0 "OK" "`list_blackip`"
		;;
        
    "add_blackip")
        action_check
        add_blackip
		;;

    "rmv_blackip")
        action_check
		rmv_blackip
		;;

    "btnrmv_blackip")
        action_check
        btnrmv_blackip
        ;;

    "export_blackip")
        action_check
		export_blackip
		;;

    "import_blackip")
        action_check
		import_blackip
		;;
    
    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
