#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
    LANG001="Error"		#ʧ
    LANG002="Success"	#ɹ
fi


get_dnat46_stat()
{
    ${FLOWEYE} dnat46 stat | awk -F"=" \
    'BEGIN{
        dot = "";
        printf "{";
    }{
        printf "%s\"%s\":\"%s\"", dot, $1, $2;
        if(dot == "") dot = ",";
    }END{
        printf "}";
    }'
}


get_dnssnif_stat()
{
    ${FLOWEYE} dnssnif stat | awk -F"=" \
    'BEGIN{
        dot = "";
        printf "{";
    }{
        printf "%s\"%s\":\"%s\"", dot, $1, $2;
        if(dot == "") dot = ",";
    }END{
        printf "}";
    }'
}

get_dnnat_host_stat(){
    ${FLOWEYE} dnat46_host stat | awk -F"=" \
    'BEGIN{
        dot = "";
        printf "{";
    }{
        printf "%s\"%s\":\"%s\"", dot, $1, $2;
        if(dot == "") dot = ",";
    }END{
        printf "}";
    }'
}

get_set_stat_env()
{
    . ../common/ajax_object_lib

    printf "{"
    printf "\"proxy\":`lib_proxy_list wan`"
    printf ",\"ipgrp\":`lib_iptable_list`"
    printf ",\"dnsgrp\":`lib_dnsgrp_list`"
    printf ",\"dnssnif\":`get_dnssnif_stat`"
    printf ",\"dnnat_host\":`get_dnnat_host_stat`"
    printf ",\"dnnat\":`get_dnat46_stat`"
    printf "}"
}


set_dnat46_stat()
{
    [ "${CGI_enable}" = "on" ] && CGI_enable=1 || CGI_enable=0

    cmdargs="enable=${CGI_enable} proxy=${CGI_pxyid} pxy4=${CGI_pxy4id} prefix=${CGI_prefix} pfxlen=${CGI_pfxlen} allowipgrp=${CGI_allowipgrp}"
    errmsg=`${FLOWEYE} dnat46 config ${cmdargs}`
    [ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}${errmsg}"
    sync_floweye "dnat46 config enable=${CGI_enable}"
    
    cmdargs="blackgrp=${CGI_blackgrp} whitegrp=${CGI_whitegrp} svraddr=${CGI_svraddr} pxy=${CGI_proxy}"
    errmsg=`${FLOWEYE} dnssnif config ${cmdargs}`
    [ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}${errmsg}"
    errmsg=`${FLOWEYE} dnat46_host config ipstart=${CGI_ipstart} ipend=${CGI_ipend}`
    [ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}${errmsg}"
    sync_floweye "dnssnif config ${cmdargs}"

    WEB_LOGGER "v4/v6ת" "${cmdargs}"
    retjson 0 "${LANG002:=ɹ}"
}

load_dnat46_list()
{
	case "${CGI_sort}" in
		"ipnum")
			sort_key="-nk10"
			;;

		"hit")
			sort_key="-nk4"
			;;

		*)
			sort_key="-nk1"
			;;
	esac

	[ "${CGI_sort_type}" = "desc" ] && sort_type="-r"

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

	end=$(( ${CGI_page} * ${CGI_limit} ))
	start=$(( ${end} - ${CGI_limit} ))

    ${FLOWEYE} dnat46 list | grep -i "${CGI_keyword}" | sort ${sort_type} ${sort_key} | awk -v stat=${CGI_stat} \
    -v start=${start} -v end=${end} \
    'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
    }{
        row++;
		if( row > end  || row < start ) next;
        
        col = 1;
        printf "%s{", dot;
        printf "\"id\":\"%s\"", $(col++);
        printf ",\"ok\":\"%s\"", $(col++);
        printf ",\"syn\":\"%s\"", $(col++);
        printf ",\"hits\":\"%s\"", $(col++);
        printf ",\"domain\":\"%s\"", $(col++);
        printf ",\"len\":\"%s\"", $(col++);
        printf ",\"birth\":\"%s\"", $(col++);
        printf ",\"last80\":\"%s\"", $(col++);
        printf ",\"last443\":\"%s\"", $(col++);
        printf ",\"exp4\":\"%s\"", $(col++);
        printf ",\"exp6\":\"%s\"", $(col++);
        printf ",\"fip\":\"%s\"", $(col++);
        printf ",\"rip\":\"%s\"", $(col++);
        printf ",\"ipv6\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
		printf "],\"total\":\"%s\"}", row;
    }'
}


get_dnssnif_info()
{
    ${FLOWEYE} dnat46 get host=${CGI_domain} all=1 | grep -v "^addr" | awk -F"=" \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        printf "%s\"%s\"", dot, $0;
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'
}



list_blackip()
{
    ${FLOWEYE} dnat46_host list blackip=1 | grep -i "${CGI_keyword}" | awk -F"=" \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        printf "%s{", dot;
        printf "\"ip\":\"%s\"", $1;
        printf "}";
        
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'
}


add_blackip()
{
    errmsg=`${FLOWEYE} dnat46_host add blackip=${CGI_ip}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        sync_floweye "dnat46_host add blackip=${CGI_ip}"
        WEB_LOGGER "DNAT46IP" "ip=${CGI_ip}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


rmv_blackip()
{
    errmsg=`${FLOWEYE} dnat46_host remove blackip=${CGI_ip}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        sync_floweye "dnat46_host remove blackip=${CGI_ip}"
        WEB_LOGGER "ɾDNAT46IP" "ip=${CGI_ip}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


btnrmv_blackip()
{
    for ip in ${CGI_ips}
    do
        ${FLOWEYE} dnat46_host remove blackip=${ip}
        sync_floweye "dnat46_host remove blackip=${ip}"
    done

    WEB_LOGGER "ɾDNAT46IP" ip="${CGI_ips}"
    retjson 0 "${LANG002:=ɹ}"
}


clear_blackip()
{
    for ip in `${FLOWEYE} dnat46_host list blackip=1`
    do
        ${FLOWEYE} dnat46_host remove blackip=${ip}
        sync_floweye "dnat46_host remove blackip=${ip}"
    done

    WEB_LOGGER "DNAT46IP"
    retjson 0 "${LANG002:=ɹ}"
}


case "${CGI_action}" in
    "get_dnat46_stat")
        retjson 0 "OK" "`get_dnat46_stat`"
        ;;
        
    "get_set_stat_env")
        retjson 0 "OK" "`get_set_stat_env`"
        ;;

    "set_dnat46_stat")
        action_check
        set_dnat46_stat
        ;;

    "load_dnat46_list")
        retjson 0 "OK" "`load_dnat46_list`"
        ;;

    "get_dnssnif_info")
        retjson 0 "OK" "`get_dnssnif_info`"
        ;;
        
    "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
        ;;

    "clear_blackip")
        action_check
        clear_blackip
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
