#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"			#ʧ
	LANG002="Success"		#ɹ
	LANG003="Add_CGNAT"		#CGNAT
	LANG004="Add_CGNAT"		#CGNAT
	LANG005="Edit_CGNAT"	#༭CGNAT
	LANG006="Import_CGNAT"	#CGNAT
	LANG007="Del_CGNAT"		#ɾCGNAT
	LANG008="Del_CGNAT"		#ɾCGNAT
	LANG009="num"			#ɾ
fi


dyn_cgnat_set()
{
	[ "${CGI_natip}" = "" ] && CGI_natip="0.0.0.0"
	[ "${CGI_portnum}" = "" ] && CGI_portnum="0"
	
	cmdargs="desc=${CGI_name} iplist=${CGI_natip} portnum=${CGI_portnum}"
	[ "${CGI_id}" != "0" ] && cmdargs="${cmdargs} id=${CGI_id}"
	
	errmsg=`${FLOWEYE} cgnat config ${cmdargs}`

	if [ $? -ne 0 ]; then
		retjson 1 "ʧܣ${errmsg}"
	else
		sync_floweye "cgnat config ${cmdargs}"
		WEB_LOGGER "ö̬CGNAT" "${cmdargs}"
		retjson 0 "ɹ"
	fi
}


list_cgnat_dyn_pool()
{
	${FLOWEYE} cgnat list cgnatid=-1 | grep -i "${CGI_keyword}" | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"id\":\"%s\"", $(col++);
		printf ",\"name\":\"%s\"", $(col++);
		printf ",\"natip\":\"%s\"", $(col++);
		printf ",\"portnum\":\"%s\"", $(col++);
		printf ",\"used\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


list_cgnat_dyn_session()
{
	${FLOWEYE} cgnat list cgnatid=${CGI_id} | grep -i "${CGI_keyword}" | awk \
	'BEGIN{
		dot = "";
		printf "[";
	} {
		col = 1;

		printf "%s", dot;
		printf "{";
		printf "\"srcip\":\"%s\",", $(col++);
		printf "\"birth\":\"%s\",", $(col++);
		printf "\"natip\":\"%s\",", $(col++);
		printf "\"port\":\"%s\",", $(col++);
		printf "\"count\":\"%s\",", $(col++);
		printf "\"fail\":\"%s\"", $(col++);
		printf "}";

		if(dot == "") dot = ",";
	} END{
		printf "]";
	}'
}


list_cgnat_stc()
{
	${FLOWEYE} cgnat list cgnatid=-2 | grep -i "${CGI_keyword}" | awk \
	'BEGIN{
		dot = "";
		printf "[";
	} {
		printf "%s", dot;
		printf "{";
		printf "\"srcip\":\"%s\",", $1;
		printf "\"natport\":\"%s\",", $2;
		printf "\"natip\":\"%s\"", $3;
		printf "}";
		
		if(dot == "") dot = ",";
	} END{
		printf "]";
	}'
}


add_cgnat()
{
	errmsg=`${FLOWEYE} cgnat add src=${CGI_srcip} port=${CGI_port1}-${CGI_port2} natip=${CGI_natip}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		sync_floweye "cgnat add src=${CGI_srcip} port=${CGI_port1}-${CGI_port2} natip=${CGI_natip}"
		WEB_LOGGER "${LANG003:=CGNAT}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


btn_add_cgnat()
{
	port_range=${CGI_port_range}

	start_port=${CGI_start_port}
	end_port=$((${start_port} + ${port_range}))

	for ip in `echo ${CGI_srcip} | tr "," " "`
	do
		port="${start_port}-${end_port}"

		errmsg=`${FLOWEYE} cgnat add src=${ip} port=${port}`
		[ $? -ne 0 ] && retjson 1 "${ip}:${errmsg}"

		sync_floweye "cgnat add src=${ip} port=${port}"
		start_port=${end_port}
		end_port=$((${start_port} + ${port_range}))
		start_port=$((${start_port} + 1))
	done

	WEB_LOGGER "${LANG004:=CGNAT}" "port_range=${CGI_port_range} start_port=${CGI_start_port} end_port=${end_port}"
	retjson 0 "${LANG002:=ɹ}"
}


set_cgnat()
{
	cmdargs="src=${CGI_srcip} port=${CGI_port1}-${CGI_port2} natip=${CGI_natip}"
	errmsg=`${FLOWEYE} cgnat set ${cmdargs}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		sync_floweye "cgnat set ${cmdargs}"
		WEB_LOGGER "${LANG005:=༭CGNAT}" "${cmdargs}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


remove_cgnat()
{
	errmsg=`${FLOWEYE} cgnat remove src=${CGI_srcip}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		sync_floweye "cgnat remove src=${CGI_srcip}"
		WEB_LOGGER "${LANG007:=ɾCGNAT}" "src=${CGI_srcip}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


btn_remove_cgnat()
{
	num=0

	for ip in `echo ${CGI_items} | tr ";" " "`
	do
		${FLOWEYE} cgnat remove src=${ip}
		sync_floweye "cgnat remove src=${ip}"
		num=$((${num}+1))
	done

	WEB_LOGGER "${LANG008:=ɾCGNAT}" "${LANG009:=ɾ}=${num}"
	retjson 0 "${LANG002:=ɹ}"
}


export_dyn_cgnat()
{
	file_name="dyn_cgnat.csv"
	file_path="${WEB_DOWNLOAD}/${file_name}"

	mkdir -p ${WEB_DOWNLOAD}

	echo "ַ,NATַ,NAT˿" > ${file_path}

	${FLOWEYE} cgnat list cgnatid=${CGI_id} | awk \
	'{
		printf "%s,%s,%s\n", $1, $3, $4;
	}' >> ${file_path}

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

export_cgnat_dyn()
{
	file_name="cgnat_dyn.conf"
	file_path="${WEB_DOWNLOAD}/${file_name}"

	mkdir -p ${WEB_DOWNLOAD}

	echo -n "" > ${file_path}

	${FLOWEYE} cgnat list cgnatid=-1 | awk \
	'{
		printf "%s %s %s %s\n", $1, $2, $3, $4;
	}' >> ${file_path}

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

export_cgnat_stc()
{
	file_name="cgnat.conf"
	file_path="${WEB_DOWNLOAD}/${file_name}"

	mkdir -p ${WEB_DOWNLOAD}

	echo -n "" > ${file_path}

	${FLOWEYE} cgnat list cgnatid=-2 | awk \
	'{
		printf "%s %s %s\n", $1, $2, $3;
	}' >> ${file_path}

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

import_cgnat_dyn()
{
	cols=`awk '{print NF}' ${CGI_file} | tail -1`

	if [ "${cols}" != "4" ]; then
		rm -rf ${CGI_file}
		retjson 1 "INV_FILE"
	fi

	cat ${CGI_file} | while read line
	do	
		id=`echo ${line} | cut -d" " -f1`
		desc=`echo ${line} | cut -d" " -f2`
		iplist=`echo ${line} | cut -d" " -f3`
		portnum=`echo ${line} | cut -d" " -f4`

		${FLOWEYE} cgnat config id=${id} desc=${desc} iplist=${iplist} portnum=${portnum}
	done

	rm -rf "${CGI_file}"
	WEB_LOGGER "${LANG006:=CGNAT}"
	retjson 0 "${LANG002:=ɹ}"
}

import_cgnat_stc()
{
	cols=`awk '{print NF}' ${CGI_file} | tail -1`
	
	if [ "${cols}" != "3" ]; then
		rm -rf ${CGI_file}
		retjson 1 "INV_FILE"
	fi

	cat ${CGI_file} | while read line
	do	
		srcip=`echo ${line} | cut -d" " -f1`
		port=`echo ${line} | cut -d" " -f2`
		natip=`echo ${line} | cut -d" " -f3`

		errmsg=`${FLOWEYE} cgnat add src=${srcip} port=${port} natip=${natip}`
		if [ "${errmsg}" = "EXIST" ]; then
			${FLOWEYE} cgnat set src=${srcip} port=${port} natip=${natip}
		fi
	done

	rm -rf "${CGI_file}"
	WEB_LOGGER "${LANG006:=CGNAT}"
	retjson 0 "${LANG002:=ɹ}"
}

case "${CGI_action}" in

	"dyn_cgnat_set")
		action_check
		dyn_cgnat_set
		;;

	"list_cgnat_dyn_pool")
		retjson 0 "OK" "`list_cgnat_dyn_pool`"
		;;

	"list_cgnat_dyn_session")
		retjson 0 "OK" "`list_cgnat_dyn_session`"
		;;

	"list_cgnat_stc")
		retjson 0 "OK" "`list_cgnat_stc`"
		;;
	
	"add_cgnat")
		action_check
		add_cgnat
		;;
	
	"btn_add_cgnat")
		action_check
		btn_add_cgnat
		;;

	"set_cgnat")
		action_check
		set_cgnat
		;;

	"remove_cgnat")
		action_check
		remove_cgnat
		;;

	"btn_remove_cgnat")
		action_check
		btn_remove_cgnat
		;;

	"export_dyn_cgnat")
		action_check
		export_dyn_cgnat
		;;

	"export_cgnat_stc")
		action_check
		export_cgnat_stc
		;;

	"export_cgnat_dyn")
		action_check
		export_cgnat_dyn
		;;

	"import_cgnat_stc")
		action_check
		import_cgnat_stc
		;;

	"import_cgnat_dyn")
		action_check
		import_cgnat_dyn
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
