#!/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}"

	args="type=${CGI_type} ${args} -s ${CGI_sort} bridge=${CGI_bridge}"

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

	${FLOWEYE} ipobj list ${args} -summary | grep -i "${CGI_keyword}" | awk \
	-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 "{";
		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 "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":\"%s\"}", row;
	}'
}


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


load_ip_app()
{
	${FLOWEYE} ipobj listipa -ip ${CGI_ip} -s all | 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_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"

	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 != "" && $13 != wan) next;
		if(time != "" && $1 < time) next;
		if(clntdelay != "" && $17 < clntdelay) next;
		if(svrdelay != "" && $18 < svrdelay) next;
		if(appdelay != "" && $19 < appdelay) next;

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

		col = 1;

        printf "%s", dot;
        printf "{";
        printf "\"time\":\"%s\",", $(col++);
        printf "\"app\":\"%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 "}";
        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()
{
	${FLOWEYE} appobj useridlist ip=${CGI_ip} | awk \
	'BEGIN{
		row = 0;
		dot = "";
		printf "[";
	}{
		if(NF != 7) next;

		col = 1;

        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;

        printf "%s", dot;
        printf "{";
        printf "\"id\":\"%s\",", $(col++);
        printf "\"devid\":\"%s\",", $(col++);
		printf "\"ipaddr\":\"%s\",", $(col++);
		printf "\"lastime\":\"%s\",", $(col++);
		printf "\"name\":\"%s\"", $(col++);
        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++);
		printf "\"account\":\"%s\",", $(col++);
        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 "}";
        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
}


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

	"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
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
