#!/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()
{
	[ "${CGI_gid}" != "" ] && gidargs="gid=${CGI_gid}"

	printf "["
	${FLOWEYE} route list ${gidargs} json=1
	printf "]"
}


rmv_route_policy()
{
	[ "${CGI_gid}" != "" ] && gidargs="gid=${CGI_gid}"

	errmsg=`${FLOWEYE} route remove ${gidargs} id=${CGI_id}`

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


disable_route_policy()
{
	[ "${CGI_gid}" != "" ] && gidargs="gid=${CGI_gid}"

	errmsg=`${FLOWEYE} route disable ${gidargs} id=${CGI_id}`

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


enable_route_policy()
{
	[ "${CGI_gid}" != "" ] && gidargs="gid=${CGI_gid}"

	errmsg=`${FLOWEYE} route enable ${gidargs} id=${CGI_id}`

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


btndisable_route_policy()
{
	[ "${CGI_gid}" != "" ] && gidargs="gid=${CGI_gid}"

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

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


btnenable_route_policy()
{
	[ "${CGI_gid}" != "" ] && gidargs="gid=${CGI_gid}"

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

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


btnremove_route_policy()
{
	[ "${CGI_gid}" != "" ] && gidargs="gid=${CGI_gid}"

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

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


get_nat_policy()
{
	[ "${CGI_gid}" != "" ] && gidargs="gid=${CGI_gid}"

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

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


lib_cgnat_list()
{
	${FLOWEYE} cgnat list cgnatid=-1 | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s{", dot;
		printf "\"id\":\"%s\",", $1;
		printf "\"name\":\"%s\"", $2;
		printf "}";
		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_list\":`lib_ippool_list`"
	printf ",\"proxy\":`lib_proxy_list`"
	printf ",\"rtptime_list\":`lib_rtptime_list`"
	printf ",\"cgnat_list\":`lib_cgnat_list`"

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

	printf "}"
}


add_route_policy()
{
	[ "${CGI_gid}" != "" ] && gidargs="gid=${CGI_gid}"

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

	[ "${CGI_dscp}" = "" ] && CGI_dscp="0"
	[ "${CGI_vlan}" = "" ] && CGI_vlan="0-0"
	[ "${CGI_submit_srcip}" = "" ] && CGI_submit_srcip=""
	[ "${CGI_submit_dstip}" = "" ] && CGI_submit_dstip=""
	
	cmdargs="${cmdargs} sport=${CGI_sport} dport=${CGI_dport}"
	cmdargs="${cmdargs} src=${CGI_submit_srcip} dst=${CGI_submit_dstip}"
	cmdargs="${cmdargs} proto=${CGI_proto} app=${CGI_appid} vlan=${CGI_vlan} dscp=${CGI_dscp}"

	case "${CGI_act}" in
		"nat"|"cgnat"|"dnat")
			[ "${CGI_actpxyname}" = "" ] && retjson 1 "INV_PXY"
			[ "${CGI_nexthop}" = "" ] && CGI_nexthop="_NULL_"
			cmdargs="${cmdargs} action=${CGI_act}-${CGI_actpxyname}"
			cmdargs="${cmdargs} nexthop=${CGI_nexthop}"
			if [ "${CGI_act}" = "nat" ]; then
				[ "${CGI_fullconenat}" = "on" ] && fullconenat=1 || fullconenat=0
				cmdargs="${cmdargs} fullconenat=${fullconenat}"
			fi
			if [ "${CGI_act}" = "dnat" ]; then
				[ "${CGI_nosnat}" = "on" ] && nosnat=1 || nosnat=0
				cmdargs="${cmdargs} nosnat=${nosnat}"
			fi
			if [ "${CGI_act}" = "cgnat" ]; then
				cmdargs="${cmdargs} cgnatid=${CGI_cgnatid}"
			fi
		;;

		"ippxy")
			cmdargs="${cmdargs} action=ippxy"
		;;

		"route")
			#·ɶDNATַǿΪ
			#û༭ԣDNAT ĳ ·ɣDNATַΪյ²ʧЧ
			CGI_newdstip="0.0.0.0"

			[ "${CGI_route_nexthop}" = "" ] && CGI_route_nexthop="0.0.0.0"
			cmdargs="${cmdargs} action=route-${CGI_route_pxyname}"
			cmdargs="${cmdargs} nexthop=${CGI_route_nexthop}"
		;;
		
	esac

	[ "${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

	cmdargs="${cmdargs} wanbw=${CGI_wanbw} wanbwout=${CGI_wanbwout} natip=${CGI_natip} disable=${CGI_disable}"

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

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		sync_floweye "route ${cmdargs}"
		WEB_LOGGER "${LOG_MSG}" "${cmdargs}"
		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()
{
	import_stat="${RAMDISK}/tmp/import_route.stat"

	[ -f ${import_stat} ] && rm -rf ${import_stat}

	for pid in `ps -axwww | grep "policy_import" | grep "module=route" | grep -v grep | awk '{print $1}'`
	do
		kill -9 ${pid}
	done

	cmdargs=""
	cmdargs="${cmdargs} file=${CGI_file}"
	cmdargs="${cmdargs} stat=${import_stat}"
	cmdargs="${cmdargs} cover=${CGI_type}"
	cmdargs="${cmdargs} module=route"

	${RAMDISK}/bin/policy_import ${cmdargs} >/dev/null 2>&1 &

	WEB_LOGGER "${LANG012:=·}"
	retjson 0 "ɹ"
}


query_import_stat()
{
	import_stat="${RAMDISK}/tmp/import_route.stat"

	if [ ! -f "${import_stat}" ]; then
		printf "{}"
	else
		cat ${import_stat}
	fi
}


clear_import_stat()
{
	import_stat="${RAMDISK}/tmp/import_route.stat"

	[ -f ${import_stat} ] && rm -rf ${import_stat}
}


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


set_segrt_stat()
{
	errmsg=`${FLOWEYE} segrt config enable=${CGI_enable}`

	if [ $? -ne 0 ]; then
		retjson 1 "${errmsg}"
	else
		sync_floweye "segrt config enable=${CGI_enable}"
		WEB_LOGGER "÷ֶ·" "enable=${CGI_enable}"
		retjson 0 "ɹ"
	fi
}


list_editip()
{
	. ../common/ajax_object_lib

	printf "{"
	# MAC
	printf "\"ippool_list\":`lib_ippool_list`"
	# IPȺ
	printf ",\"iptab_list\":`lib_iptable_list`"
	# Ⱥ
	printf ",\"dnsgrp_list\":`lib_dnsgrp_list`"
	printf "}"
}


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

	"query_import_stat")
		retjson 0 "OK" "`query_import_stat`"
		;;

	"clear_import_stat")
		action_check
		clear_import_stat
		;;

	"get_segrt_stat")
		retjson 0 "OK" "`get_segrt_stat`"
		;;

	"set_segrt_stat")
		action_check
		set_segrt_stat
		;;

	"list_editip")
		retjson 0 "OK" "`list_editip`"
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
