#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"					#ʧ
	LANG002="Success"				#ɹ
	LANG003="Delete_Route_Policy"	#ɾ·ɲ
	LANG004="Disable_Route_Policy"	#·ɲ
	LANG005="Enable_Route_Policy"	#·ɲ
	LANG006="Disable_Route_Policy"	#ò·
	LANG007="Enable_Route_Policy"	#ò·
	LANG008="Delete_Route_Policy"	#ɾ·
	LANG009="-"
	LANG010="Set_Route_Policy"		#༭·
	LANG011="Add_Route_Policy"		#Ӳ·
	LANG012="Import_Route_Policy"	#·
	LANG013="num"					#
	LANG014="Success"				#
	LANG015="ok"					#ɹ
	LANG016="fail"					#ʧ
fi

list_route_policy()
{
	${FLOWEYE} route list | grep -i "${CGI_keyword}" | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		gsub("%20", " ", $32);

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"polno\":\"%s\",", $(col++);
		printf "\"iniftype\":\"%s\",", $(col++);
		printf "\"inif\":\"%s\",", $(col++);
		printf "\"proto\":\"%s\",", $(col++);
		printf "\"appid\":\"%s\",", $(col++);
		printf "\"appname\":\"%s\",", $(col++);
		printf "\"srctype\":\"%s\",", $(col++);
		printf "\"srcip\":\"%s\",", $(col++);
		printf "\"srcport\":\"%s\",", $(col++);
		printf "\"dsttype\":\"%s\",", $(col++);
		printf "\"dstip\":\"%s\",", $(col++);
		printf "\"dstport\":\"%s\",", $(col++);
		printf "\"dscp\":\"%s\",", $(col++);
		printf "\"disabled\":\"%s\",", $(col++);
		printf "\"vlan\":\"%s\",", $(col++);
		printf "\"ttl\":\"%s\",", $(col++);
		printf "\"action\":\"%s\",", $(col++);
		printf "\"nhtype\":\"%s\",", $(col++);
		printf "\"nexthop\":\"%s\",", $(col++);
		printf "\"gwtype\":\"%s\",", $(col++);
		printf "\"gateway\":\"%s\",", $(col++);
		printf "\"state\":\"%s\",", $(col++);
		printf "\"pkts\":\"%s\",", $(col++);
		printf "\"rtptimeid\":\"%s\",", $(col++);
		printf "\"rtptimename\":\"%s\",", $(col++);
		printf "\"active\":\"%s\",", $(col++);
		printf "\"standby\":\"%s\",", $(col++);
		printf "\"usrtype\":\"%s\",", $(col++);
		printf "\"pid\":\"%s\",", $(col++);
		printf "\"pname\":\"%s\",", $(col++);
		printf "\"newdstip\":\"%s\",", $(col++);
		printf "\"desc\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


rmv_route_policy()
{
	errmsg=`${FLOWEYE} route remove id=${CGI_id}`

	if [ "$?" != "0" ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		sync_floweye "route remove id=${CGI_id}"
		WEB_LOGGER "${LANG003:=ɾ·ɲ}" "id=${CGI_id}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


disable_route_policy()
{
	errmsg=`${FLOWEYE} route disable id=${CGI_id}`

	if [ "$?" != "0" ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		sync_floweye "route disable id=${CGI_id}"
		WEB_LOGGER "${LANG004:=·ɲ}" "id=${CGI_id}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


enable_route_policy()
{
	errmsg=`${FLOWEYE} route enable id=${CGI_id}`

	if [ "$?" != "0" ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		sync_floweye "route enable id=${CGI_id}"
		WEB_LOGGER "${LANG005:=·ɲ}" "id=${CGI_id}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


btndisable_route_policy()
{
	for id in `echo ${CGI_items} | tr ";" " "`
	do
		errmsg=`${FLOWEYE} route disable id=${id}`
		[ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}:${errmsg}"
		sync_floweye "route disable id=${id}"
	done

	WEB_LOGGER "${LANG006:=ò·}" "ids=${CGI_items}"
	retjson 0 "${LANG002:=ɹ}"
}


btnenable_route_policy()
{
	for id in `echo ${CGI_items} | tr ";" " "`
	do
		errmsg=`${FLOWEYE} route enable id=${id}`
		[ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}:${errmsg}"
		sync_floweye "route enable id=${id}"
	done

	WEB_LOGGER "${LANG007:=ò·}" "ids=${CGI_items}"
	retjson 0 "${LANG002:=ɹ}"
}


btnremove_route_policy()
{
	for id in `echo ${CGI_items} | tr ";" " "`
	do
		errmsg=`${FLOWEYE} route remove id=${id}`
		[ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}:${errmsg}"
		sync_floweye "route remove id=${id}"
	done

	WEB_LOGGER "${LANG008:=ɾ·}" "ids=${CGI_items}"
	retjson 0 "${LANG002:=ɹ}"
}


get_nat_policy()
{
	${FLOWEYE} route get id=${CGI_id} | awk -F "=" \
	'BEGIN{
		dot = "";
		printf "{";
	}{
		gsub("%20", " ", $2);

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


load_addrule_env()
{
	. ../common/ajax_object_lib

	printf "{"
	printf "\"interface\":`lib_interface_list`"
	printf ",\"wangroup\":`lib_wangroup_list`"
	printf ",\"ippool\":`lib_ippool_list`"
	printf ",\"proxy\":`lib_proxy_list`"
	printf ",\"iptab_list\":`lib_iptable_list`"
	printf ",\"rtptime_list\":`lib_rtptime_list`"
	printf ",\"bridge_list\":`lib_bridge_list`"
	printf ",\"dnsgrp_list\":`lib_dnsgrp_list`"

	if [ "${CGI_id}" != "" ]; then
		printf ",\"policy\":`get_nat_policy`"
	fi

	printf "}"
}


add_route_policy()
{
	if [ "${CGI_action}" != "add_route_policy" ]; then
		LOG_MSG="${LANG010:=༭·}"
		cmdargs="set id=${CGI_old_id} newid=${CGI_id} inif=${CGI_inif}"
	else
		LOG_MSG="${LANG011:=Ӳ·}"
		cmdargs="add id=${CGI_id} inif=${CGI_inif}"
	fi

	case "${CGI_srctype}" in
		"net")
			CGI_inip="${CGI_inipnet}"
			;;

		"iprange")
			CGI_inip="${CGI_iniprng}"
			;;

		"table")
			CGI_inip="${CGI_iniptbl}"
			;;
		*)
			CGI_inip="any"
	esac

	if [ "${CGI_srcport}" != "" -a "${CGI_srcport}" != "0" ]; then
		CGI_inip="${CGI_inip}:${CGI_srcport}"
	fi

	cmdargs="${cmdargs} src=${CGI_inip}"

	case "${CGI_dsttype}" in
		"net")
			CGI_outip="${CGI_outipnet}"
			;;

		"iprange")
			CGI_outip="${CGI_outiprng}"
			;;

		"table")
			CGI_outip="${CGI_outiptbl}"
			;;
		*)
			CGI_outip="any"
			;;
	esac

	if [ "${CGI_dstport}" != "" -a "${CGI_dstport}" != "0" ]; then
		CGI_outip="${CGI_outip}:${CGI_dstport}"
	fi

	cmdargs="${cmdargs} dst=${CGI_outip}"

	if [ "${CGI_dscp}" != "" -a "${CGI_dscp}" != "0" ]; then
		cmdargs="${cmdargs} dscp=${CGI_dscp}"
	fi

	cmdargs="${cmdargs}  proto=${CGI_proto} app=${CGI_appid}"

	if [ "${CGI_act}" = "nat" -o "${CGI_act}" = "cgnat" -o "${CGI_act}" = "dnat" ]; then
		cmdargs="${cmdargs} action=${CGI_act}-${CGI_actpxyname}"
		cmdargs="${cmdargs} nexthop=${CGI_nexthop}"
	elif [ "${CGI_act}" = "ippxy" ]; then
		cmdargs="${cmdargs} action=ippxy"
	else 
		cmdargs="${cmdargs} action=route-${CGI_route_pxyname}"
		[ "${CGI_route_nexthop}" = "" ] && CGI_route_nexthop="0.0.0.0"
		cmdargs="${cmdargs} nexthop=${CGI_route_nexthop}"
	fi

	if [ "${CGI_vlan}" != "" -a "${CGI_vlan}" != "0-0" -a "${CGI_vlan}" != "0" ]; then
		cmdargs="${cmdargs} vlan=${CGI_vlan}"
	fi
	
	[ "${CGI_ttl}" != "" ] && cmdargs="${cmdargs} ttl=${CGI_ttl}"
	
	cmdargs="${cmdargs} usrtype=${CGI_usrtype}"

	if [ "${CGI_desc}" = "" ]; then
		cmdargs="${cmdargs} desc=NULL"
	else
		CGI_desc=`echo ${CGI_desc} | sed -r 's/ /\%20/g'`
		cmdargs="${cmdargs} desc=${CGI_desc}"
	fi

	[ "${CGI_schtime}" = "" ] && CGI_schtime=0
	cmdargs="${cmdargs} schtime=${CGI_schtime}"

	[ "${CGI_pool}" = "" ] && CGI_pool=0
	cmdargs="${cmdargs} pool=${CGI_pool}"

	if [ "${CGI_newdstip}" != "" ]; then
		cmdargs="${cmdargs} newdstip=${CGI_newdstip}"
	else
		cmdargs="${cmdargs} newdstip=0.0.0.0"
	fi

	errmsg=`${FLOWEYE} route ${cmdargs}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		sync_floweye "route ${cmdargs}"
		WEB_LOGGER "${LOG_MSG}" "id=${CGI_id}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


export_route_policy()
{
	file_name="pa_route.conf"
	file_path="${WEB_DOWNLOAD}/${file_name}"
	
	mkdir -p ${WEB_DOWNLOAD}

	grep "^rtp " "${PGETC}/panabit.conf" | cut -d" " -f2- > ${file_path}

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


import_route_policy()
{
	errmsg=`grep "^id=" ${CGI_file}`
	if [ "${errmsg}" = "" ]; then
		rm -rf "${CGI_file}"
		retjson 1 "INV_FILE"
	fi

	errmsg=`grep " action=" ${CGI_file}`
	if [ "${errmsg}" = "" ]; then
		rm -rf "${CGI_file}"
		retjson 1 "INV_FILE"
	fi

	ok=0
	err=0

	while read line
	do
		errmsg=`${FLOWEYE} route add ${line}`
		
		if [ $? -ne 0 ]; then
			if [ "${CGI_type}" = "cover" ]; then
				errmsg=`${FLOWEYE} route set ${line}`
				if [ $? -ne 0 ]; then
					err=$((${err}+1))
				else
					ok=$((${ok}+1))
				fi
			else
				err=$((${err}+1))
			fi
		else
			ok=$((${ok}+1))
		fi
		
	done << EOF
`cat ${CGI_file}`
EOF

	rm -rf ${CGI_file}

	WEB_LOGGER "${LANG012:=·}" "${LANG013:=}=${ok}"
	retjson 0 "${LANG014:=}${LANG015:=ɹ}:${ok},${LANG016:=ʧ}:${err}"
}


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

	"rmv_route_policy")
		action_check
		rmv_route_policy
		;;
	
	"disable_route_policy")
		action_check
		disable_route_policy
		;;

	"enable_route_policy")
		action_check
		enable_route_policy
		;;
	
	"btndisable_route_policy")
		action_check
		btndisable_route_policy
		;;

	"btnenable_route_policy")
		action_check
		btnenable_route_policy
		;;

	"btnremove_route_policy")
		action_check
		btnremove_route_policy
		;;

	"load_addrule_env")
		retjson 0 "OK" "`load_addrule_env`"
		;;

	"add_route_policy"|"edit_route_policy")
		action_check
		add_route_policy
		;;

	"export_route_policy")
		action_check
		export_route_policy
		;;

	"import_route_policy")
		action_check
		import_route_policy
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
