#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"			#ʧ
	LANG002="Success"		#ɹ
	LANG003="Block_Mobile"	#ƶն
	LANG004="Force_IP_account_offline"		#ǿIP˺
	LANG005="Set_intranet_IP_statistics "	#IPͳ
fi


load_ipobj_list()
{ 
	args=""

	[ "${CGI_page}" = "" ] && CGI_page=1
	[ "${CGI_limit}" = "" ] && CGI_limit=30
	[ "${CGI_bridge}" = "" ] && CGI_bridge=0
	[ "${CGI_sort}"   = "" ] && CGI_sort="all"
	[ "${CGI_type}" = "" ] && CGI_type="all"

	[ "${CGI_iprng}" != "" ] && args="${args} -ip ${CGI_iprng}"
	[ "${CGI_topn}" != "" ] && args="${args} -n ${CGI_topn}"
	[ "${CGI_ippxy}" != "" ] && args="${args} ippxy=${CGI_ippxy}"
	[ "${CGI_pool}" != "" ] && args="${args} pool=${CGI_pool}"
	[ "${CGI_tid}" != "" ] && args="${args} tid=${CGI_tid}"
	[ "${CGI_vpaid}" != "" ] && args="${args} vpaid=${CGI_vpaid}"
	
	if [ "${CGI_bridge}" = "9" ]; then
		CGI_bridge=0
		CGI_type=v6
	fi

	args="type=${CGI_type} ${args} bridge=${CGI_bridge}"

	if [ "${CGI_sort_type}" = "asc" ]; then
		args="${args} +s ${CGI_sort}"
	else
		args="${args} -s ${CGI_sort}"
	fi

	[ "${CGI_tail}" != "" ] && tail_tag=" | tail ${CGI_tail}"

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

	awk \
	-v start=${start} -v end=${end} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
		row++;
		if( row > end  || row < start ) next;

		gsub(/\\/, "", $17);

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"id\":\"%s\",", NR;
		printf "\"ipaddr\":\"%s\",", $(col++);
		printf "\"onlinesecs\":\"%s\",", $(col++);
		printf "\"ttl\":\"%s\",", $(col++);
		printf "\"flowcnt\":\"%s\",", $(col++);
		printf "\"mac\":\"%s\",", $(col++);
		printf "\"in\":\"%s\",", $(col++);
		printf "\"out\":\"%s\",", $(col++);
		printf "\"inbps\":\"%s\",", $(col++);
		printf "\"outbps\":\"%s\",", $(col++);
		printf "\"account\":\"%s\",", $(col++);
		printf "\"iecookies\":\"%s\",", $(col++);
		printf "\"chromecookies\":\"%s\",", $(col++);
		printf "\"natip\":\"%s\",", $(col++);
		printf "\"mstcnt\":\"%s\",", $(col++);
		printf "\"ratein\":\"%s\",", $(col++);
		printf "\"rateout\":\"%s\",", $(col++);
		printf "\"name\":\"%s\",", $(col++);
		printf "\"ippxy\":\"%s\",", $(col++);
		printf "\"blackip\":\"%s\",", $(col++);
		printf "\"vid\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":\"%s\"}", row;
	}' << EOF
`
if [ "${CGI_tail}" != "" ]; then
	${FLOWEYE} ipobj list ${args} ${CGI_tags} | tail -${CGI_tail} | grep -i "${CGI_keyword}"
else
	${FLOWEYE} ipobj list ${args} ${CGI_tags} -summary | grep -i "${CGI_keyword}"
fi
`	
EOF
}


load_ip_detail()
{	
	cmdargs="${CGI_ip}"
	if [ "${NTM}" = "1" ]; then
		cmdargs="vpaid=${CGI_vpaid} ip=${CGI_ip}"
	fi

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


load_ip_app()
{	
	cmdargs=""
	if [ "${NTM}" = "1" ]; then
		cmdargs="vpaid=${CGI_vpaid}"
	fi

	# CITS 1922
	${FLOWEYE} apinfo list ${cmdargs} ip=${CGI_ip} monitor=1
	
	${FLOWEYE} ipobj listipa ${cmdargs} -ip ${CGI_ip} -s all shownum | awk \
	'BEGIN{
		dot = "";
		printf "["
	}{
		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"ip\":\"%s\",", $(col++);
		printf "\"appname\":\"%s\",", $(col++);
		printf "\"appcname\":\"%s\",", $(col++);
		printf "\"inbps\":\"%s\",", $(col++);
		printf "\"outbps\":\"%s\",", $(col++);
		printf "\"allbps\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}' 
	
}

load_ip_blocklog()
{
    total=0 dot=""
	[ "${CGI_page}" = "" ] && CGI_page=1
	[ "${CGI_limit}" = "" ] && CGI_limit=100
	end=$((${CGI_page} * ${CGI_limit}))
	start=$((${end} - ${CGI_limit}))
 
    result=""
    if [ "${CGI_type}" = "all_log" ]; then
	    result=`${FLOWEYE} denylog list | grep -i "${CGI_keyword}"`
    else
        result=`${FLOWEYE} denylog list ip=${CGI_ip} | grep -i "${CGI_keyword}"`
    fi

	if [ "${result}" = "" ]; then
		echo "{\"total\":0,\"data\":[]}"
		return 0
	fi
    
	printf "{\"data\":["
	   
        while read time type src dst sport dport proto appname appcname policy_id desc

        do
		total=$((${total}+1))
		[ ${total} -le ${start} -o ${total} -gt ${end} ] && continue

        printf "${dot}"
        printf "{"
        printf "\"time\":\"${time}\","
        printf "\"type\":\"${type}\","
        printf "\"src\":\"${src}\","
        printf "\"dst\":\"${dst}\","
        printf "\"sport\":\"${sport}\","
        printf "\"dport\":\"${dport}\","
        printf "\"proto\":\"${proto}\","
        printf "\"appname\":\"${appname}\","
        printf "\"appcname\":\"${appcname}\","
        printf "\"policy_id\":\"${policy_id}\","
        printf "\"desc\":\"${desc}\""
        printf "}"
		
		[ "${dot}" = "" ] && dot=","
		
	done << EOF
	${result}
EOF

	printf "],\"total\":%d}" ${total};

}


load_ip_session()
{
	[ "${CGI_sort}" = "" ] && CGI_sort="time"

	args="sort=${CGI_sort}"
	[ "${CGI_ip}" != "" ] && args="ip=${CGI_ip} ${args}"
	[ "${CGI_port}" != "" ] && args="${args} port=${CGI_port}"
	[ "${CGI_proto}" != "" ] && args="${args} proto=${CGI_proto}"	
	[ "${CGI_appname}" != "" ] && args="${args} app=${CGI_appname}"
	[ "${CGI_show_rexmit}" != "" ] && args="${args} show_rexmit=${CGI_show_rexmit}"

	[ "${PALANG}" = "en" ] && args="${args} lang=en"

	[ "${CGI_limit}" = "" ] && CGI_limit=100
	[ "${CGI_page}"  = "" ] && CGI_page=1
	end=$((${CGI_page} * ${CGI_limit}))
	start=$((${end} - ${CGI_limit}))

	${FLOWEYE} ipobj listflow ${args} showloc=1 | grep -i "${CGI_keyword}" | awk \
	-v wan="${CGI_wan}" -v time="${CGI_time}" -v clntdelay="${CGI_clntdelay}" \
	-v svrdelay="${CGI_svrdelay}" -v appdelay="${CGI_appdelay}" \
	-v end=${end} -v start=${start} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
		if(wan != "" && $14 != wan) next;
		if(time != "" && $1 < time) next;
		if(clntdelay != "" && $18 < clntdelay) next;
		if(svrdelay != "" && $19 < svrdelay) next;
		if(appdelay != "" && $20 < appdelay) next;

		row++;
		if(row > end || row < start) next;

		col = 1;

		printf "%s", dot;
		printf "{";
		printf "\"time\":\"%s\",", $(col++);
		printf "\"app_id\":\"%s\",", $(col++);
		printf "\"app_cname\":\"%s\",", $(col++);
		printf "\"proto\":\"%s\",", $(col++);
		printf "\"dst\":\"%s\",", $(col++);
		printf "\"bytes1\":\"%s\",", $(col++);
		printf "\"bytes2\":\"%s\",", $(col++);
		printf "\"ttl\":\"%s\",", $(col++);
		printf "\"allbytes\":\"%s\",", $(col++);
		printf "\"clpdeny\":\"%s\",", $(col++);
		printf "\"route\":\"%s\",", $(col++);
		printf "\"mss\":\"%s\",", $(col++);
		printf "\"tcproxy\":\"%s\",", $(col++);
		printf "\"wan\":\"%s\",", $(col++);

		gsub("\"", "", $col);
		printf "\"host\":\"%s\",", $(col++);
		
		printf "\"ifname\":\"%s\",", $(col++);
		printf "\"inpxyname\":\"%s\",", $(col++);
		printf "\"clntdelay\":\"%s\",", $(col++);
		printf "\"svrdelay\":\"%s\",", $(col++);
		printf "\"appdelay\":\"%s\",", $(col++);
		printf "\"pktmax0\":\"%s\",", $(col++);
		printf "\"pktmax1\":\"%s\",", $(col++);
		printf "\"rexup\":\"%s\",", $(col++);
		printf "\"rexdn\":\"%s\",", $(col++);
		printf "\"tcpstatus\":\"%s\",", $(col++);
		printf "\"loc\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":%s}", row;
	}'
}



load_ip_session_branch()
{
	[ "${CGI_sort}" = "" ] && CGI_sort="time"

	args="sort=${CGI_sort}"
	[ "${CGI_ip}" != "" ] && args="ip=${CGI_ip} ${args}"
	[ "${CGI_isipv6}" != "" ] && args="${args} isipv6=${CGI_isipv6}"
	[ "${CGI_port}" != "" ] && args="${args} port=${CGI_port}"
	# [ "${CGI_proto}" != "" ] && args="${args} proto=${CGI_proto}"	
	[ "${CGI_appname}" != "" ] && args="${args} app=${CGI_appname}"
	[ "${CGI_seq}" != "" ] && args="${args} seq=${CGI_seq}"
	[ "${CGI_ipid}" != "" ] && args="${args} ipid=${CGI_ipid}"
	[ "${CGI_polid}" != "" ] && args="${args} polid=${CGI_polid}"
	[ "${CGI_vpaid}" != "" ] && args="${args} vpaid=${CGI_vpaid}"
	[ "${CGI_show_rexmit}" != "" ] && args="${args} show_rexmit=${CGI_show_rexmit}"

	[ "${PALANG}" = "en" ] && args="${args} lang=en"

	[ "${CGI_limit}" = "" ] && CGI_limit=100
	[ "${CGI_page}"  = "" ] && CGI_page=1
	end=$((${CGI_page} * ${CGI_limit}))
	start=$((${end} - ${CGI_limit}))

	
	${NTMEYE} ipobj listbussflow ${args} listflow=1 | awk \
	-v end=${end} -v start=${start} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
		if(wan != "" && $14 != wan) next;
		if(time != "" && $1 < time) next;
		if(clntdelay != "" && $18 < clntdelay) next;
		if(svrdelay != "" && $19 < svrdelay) next;
		if(appdelay != "" && $20 < appdelay) next;

		if ($1 == "total"){
			 row = $2;
			 exit
		}

		col = 1;

		printf "%s", dot;
		printf "{";
		printf "\"time\":\"%s\",", $(col++);
		printf "\"app_id\":\"%s\",", $(col++);
		printf "\"app_cname\":\"%s\",", $(col++);
		printf "\"proto\":\"%s\",", $(col++);
		printf "\"dst\":\"%s\",", $(col++);
		printf "\"bytes1\":\"%s\",", $(col++);
		printf "\"bytes2\":\"%s\",", $(col++);
		printf "\"ttl\":\"%s\",", $(col++);
		printf "\"allbytes\":\"%s\",", $(col++);
		printf "\"clpdeny\":\"%s\",", $(col++);
		printf "\"route\":\"%s\",", $(col++);
		printf "\"mss\":\"%s\",", $(col++);
		printf "\"tcproxy\":\"%s\",", $(col++);
		printf "\"wan\":\"%s\",", $(col++);
		printf "\"host\":\"%s\",", $(col++);
		printf "\"ifname\":\"%s\",", $(col++);
		printf "\"inpxyname\":\"%s\",", $(col++);
		printf "\"clntdelay\":\"%s\",", $(col++);
		printf "\"svrdelay\":\"%s\",", $(col++);
		printf "\"appdelay\":\"%s\",", $(col++);
		printf "\"pktmax0\":\"%s\",", $(col++);
		printf "\"pktmax1\":\"%s\",", $(col++);
		printf "\"rexup\":\"%s\",", $(col++);
		printf "\"rexdn\":\"%s\",", $(col++);
		printf "\"tcpstatus\":\"%s\",", $(col++);
		printf "\"loc\":\"%s\",", $(col++);
		printf "\"vpaid\":\"%s\",", $(col++);
		printf "\"ipid\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":%s}", row;
	}'
}


load_ip_peersum()
{
	[ "${CGI_clntdelay}" != "" ] && CGI_clntdelay=$((${CGI_clntdelay} * 1000))
	[ "${CGI_svrdelay}" != "" ] && CGI_svrdelay=$((${CGI_svrdelay} * 1000))
	[ "${CGI_appdelay}" != "" ] && CGI_appdelay=$((${CGI_appdelay} * 1000))

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

	[ "${CGI_sort}" = "" ] && CGI_sort="-nrk2"

	${FLOWEYE} flow peersummary ip=${CGI_ip} | sort ${CGI_sort} | grep -i "${CGI_keyword}" | awk \
	-v clntdelay="${CGI_clntdelay}" -v svrdelay="${CGI_svrdelay}" -v appdelay="${CGI_appdelay}" \
	-v end=${end} -v start=${start} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
		if(clntdelay != "" && $3 < clntdelay) next;
		if(svrdelay != "" && $6 < svrdelay) next;
		if(appdelay != "" && $9 < appdelay) next;

		row++;
		if(row > end || row < start) next;

		col = 1;

		printf "%s", dot;
		printf "{";
		printf "\"peer\":\"%s\",", $(col++);
		printf "\"connect\":\"%s\",", $(col++);
		printf "\"cli_min\":\"%s\",", sprintf("%.2f", $(col++)/1000);
		printf "\"cli_max\":\"%s\",", sprintf("%.2f", $(col++)/1000);
		printf "\"cli_avg\":\"%s\",", sprintf("%.2f", $(col++)/1000);
		printf "\"svr_min\":\"%s\",", sprintf("%.2f", $(col++)/1000);
		printf "\"svr_max\":\"%s\",", sprintf("%.2f", $(col++)/1000);
		printf "\"svr_avg\":\"%s\",", sprintf("%.2f", $(col++)/1000);
		printf "\"app_min\":\"%s\",", sprintf("%.2f", $(col++)/1000);
		printf "\"app_max\":\"%s\",", sprintf("%.2f", $(col++)/1000);
		printf "\"app_avg\":\"%s\",", sprintf("%.2f", $(col++)/1000);
		printf "\"host\":\"%s\",", $(col++);
		printf "\"loc\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":%s}", row;
	}'
}


load_ip_acct()
{
	[ "${CGI_ip}" = "" ] && retjson 1 "INV_IP"
	
	${FLOWEYE} appobj useridlist ip=${CGI_ip} | awk \
	'BEGIN{
		row = 0;
		dot = "";
		printf "[";
	}{
		if(NF < 7) next;

		col = 1;

		gsub("\"", "", $3);
		
		printf "%s", dot;
		printf "{";
		printf "\"et\":\"%s\",", $(col++);
		printf "\"type\":\"%s\",", $(col++);
		printf "\"account\":\"%s\",", $(col++);
		printf "\"ipaddr\":\"%s\",", $(col++);
		printf "\"refcnt\":\"%s\",", $(col++);
		printf "\"lastdate\":\"%s\",", $(col++);
		printf "\"lasttime\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


load_ip_share()
{
	${FLOWEYE} appobj cookielist ip=${CGI_ip} | awk \
	'BEGIN{
		row = 0;
		dot = "";
		printf "[";
	}{
		if(NF < 8) next;

		col = 1;

		printf "%s", dot;
		printf "{";
		printf "\"tag\":\"%s\",", $(col++);
		printf "\"browser\":\"%s\",", $(col++);
		printf "\"cookie\":\"%s\",", $(col++);
		printf "\"ipaddr\":\"%s\",", $(col++);
		printf "\"refcnt\":\"%s\",", $(col++);
		printf "\"lastdate\":\"%s\",", $(col++);
		printf "\"lasttime\":\"%s\",", $(col++);
		printf "\"winver\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


load_ip_mobile()
{
	${FLOWEYE} mobile list ip=${CGI_ip} | awk \
	'BEGIN{
		row = 0;
		dot = "";
		printf "[";
	}{
		if(NF < 5) next;

		col = 1;

		devname = "";
		for(i = 5; i <= NF; i++)
			devname = sprintf("%s %s", devname, $i);

		printf "%s", dot;
		printf "{";
		printf "\"id\":\"%s\",", $(col++);
		printf "\"devid\":\"%s\",", $(col++);
		printf "\"ipaddr\":\"%s\",", $(col++);
		printf "\"lastime\":\"%s\",", $(col++);
		printf "\"devname\":\"%s\"", devname;
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


load_ip_online()
{
	${FLOWEYE} ipobj get ip=${CGI_ip} getuids=1 | awk \
	'BEGIN{
		row = 0;
		dot = "";
		printf "[";
	}{
		if($0 == "NEXIST") next;

		col = 1;

		printf "%s", dot;
		printf "{";
		printf "\"tname\":\"%s\",", $(col++);
		printf "\"tcname\":\"%s\",", $(col++);
		printf "\"state\":\"%s\",", $(col++);
		
		cmd = sprintf("floweye iconv utf8togb2312 %s", $(col++));
		cmd | getline d;
		
		printf "\"account\":\"%s\",", d;

		printf "\"birth\":\"%s\",", $(col++);
		printf "\"poolname\":\"%s\",", $(col++);
		printf "\"vlan0\":\"%s\",", $(col++);
		printf "\"vlan1\":\"%s\",", $(col++);
		printf "\"ratein\":\"%s\",", $(col++);
		printf "\"rateout\":\"%s\",", $(col++);
		printf "\"name\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


set_mobile_black()
{
	errmsg=`${FLOWEYE} mobile set id=${CGI_id} devid=${CGI_devid}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		WEB_LOGGER "${LANG003:=ƶն}" "id=${CGI_id} devid=${CGI_devid}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


kickoff_ip_account()
{
	errmsg=`${FLOWEYE} ipobj remove ip=${CGI_ip} uid=${CGI_tname}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		WEB_LOGGER "${LANG004:=ǿIP˺}" "ip=${CGI_ip} uid=${CGI_tname} account=${CGI_account}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


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


set_ipobj_stat()
{
	enable=0
	[ "${CGI_enable}" = "on" ] && enable=1

	errmsg=`${FLOWEYE} ipobj config enable=${enable} ttl=${CGI_ttl}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		WEB_LOGGER "${LANG005:=IPͳ}" "enable=${enable} ttl=${CGI_ttl}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


rmv_ipobj_flow()
{
	if [ "${CGI_rmv_type}" = "all" ]; then
		src_ip=${CGI_ip}
		errmsg=`${FLOWEYE} flow remove ip=${src_ip}`
	else
		src=`echo ${CGI_obj} | cut -d"-" -f1`
		dst=`echo ${CGI_obj} | cut -d"-" -f2`
		
		is_ipv6=`echo "${src}" | grep "/"`

		if [ "${is_ipv6}" != "" ]; then
			src_ip=`echo ${src} | cut -d"/" -f1`
			src_port=`echo ${src} | cut -d"/" -f2`
			dst_ip=`echo ${dst} | cut -d"/" -f1`
			dst_port=`echo ${dst} | cut -d"/" -f2`
		else
			src_ip=`echo ${src} | cut -d":" -f1`
			src_port=`echo ${src} | cut -d":" -f2`
			dst_ip=`echo ${dst} | cut -d":" -f1`
			dst_port=`echo ${dst} | cut -d":" -f2`
		fi

		${FLOWEYE} flow remove src=${src_ip}  sport=${src_port} dst=${dst_ip} dport=${dst_port} proto=${CGI_proto}
	fi

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		WEB_LOGGER "ɾIP" "ip=${src_ip}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


get_ipobj_listflow()
{	
	args=""
	[ "${CGI_srcip}" != "" ] && args="srcip=${CGI_srcip} ${args}"
	[ "${CGI_sport}" != "" ] && args="${args} sport=${CGI_sport}"
	[ "${CGI_dstip}" != "" ] && args="dstip=${CGI_dstip} ${args}"
	[ "${CGI_dport}" != "" ] && args="${args} dport=${CGI_dport}"
	[ "${CGI_proto}" != "" ] && args="${args} proto=${CGI_proto}"	
	[ "${CGI_appname}" != "" ] && args="${args} app=${CGI_appname}"	
	[ "${CGI_topn}" != "" ] && args="${args} topn=${CGI_topn}"
	[ "${CGI_show_sport}" != "" ] && args="${args} show_sport=${CGI_show_sport}"
	[ "${CGI_show_dport}" != "" ] && args="${args} show_dport=${CGI_show_dport}"
	[ "${CGI_host}" != "" ] && args="${args} host=${CGI_host}"
	[ "${CGI_isipv6}" != "" ] && args="${args} isipv6=${CGI_isipv6}"
	[ "${CGI_type}" != "" ] && args="${args} type=${CGI_type}"

	${NTMEYE} ipobj listflow show_rela=1 ${args}
}

export_blocklog_csv()
{
    
    mkdir -p ${WEB_DOWNLOAD}

    file_name="־_`date "+%Y%m%d_%H%M%S"`.csv"
    download_path="${WEB_DOWNLOAD}/${file_name}"

    echo ",ʱ,Э,,Դַ,Դ˿,Ŀַ,Ŀ˿,Ӧ,Ա," > ${download_path}
    
    result=""
    if [ ${CGI_type} = "all_log" ];then
        result=`${FLOWEYE} denylog list`
    else
        result=`${FLOWEYE} denylog list ip=${CGI_ip}`
    fi
    counter=1

    while read time type src dst sport dport proto appname appcname policy_id desc
	do
        printf "%s,%s,%s,%s,%s,%s,%s\n" "${counter}","${time}","${proto}","${type}","${src}","${sport}","${dst}","${dport}","${appcname}","${policy_id}","${desc}" >> ${download_path}
		counter=$((counter + 1))
	done << EOF
	${result}
EOF

    WEB_LOGGER "־־б"
    retjson 0 "OK" "\"${file_name}\""
}


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

	"load_ip_detail")
		retjson 0 "OK" "`load_ip_detail`"
		;;

	"load_ip_app")
		retjson 0 "OK" "`load_ip_app`"
		;;

    "load_ip_blocklog")
		retjson 0 "OK" "`load_ip_blocklog`"
		;;

	"load_ip_session")
		retjson 0 "OK" "`load_ip_session`"
		;;

	"load_ip_session_branch")
		retjson 0 "OK" "`load_ip_session_branch`"
		;;
		
	"load_ip_peersum")
		retjson 0 "OK" "`load_ip_peersum`"
		;;

	"load_ip_acct")
		retjson 0 "OK" "`load_ip_acct`"
		;;

	"load_ip_share")
		retjson 0 "OK" "`load_ip_share`"
		;;

	"load_ip_mobile")
		retjson 0 "OK" "`load_ip_mobile`"
		;;

	"load_ip_online")
		retjson 0 "OK" "`load_ip_online`"
		;;

	"set_mobile_black")
		action_check
		set_mobile_black
		;;

	"kickoff_ip_account")
		action_check
		kickoff_ip_account
		;;

	"load_ipobj_stat")
		retjson 0 "OK" "`load_ipobj_stat`"
		;;

	"set_ipobj_stat")
		action_check
		set_ipobj_stat
		;;

	"rmv_ipobj_flow")
		action_check
		rmv_ipobj_flow
		;;

	"get_ipobj_listflow")
		retjson 0 "OK" "`get_ipobj_listflow`"
		;;

    "export_blocklog_csv")
		action_check
		export_blocklog_csv
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
