#!/bin/sh
#This script is created by ssparser automatically. The parser first created by MaoShouyan
printf "Content-type: text/html
Cache-Control: no-cache

"
echo -n "";
. ../common/common.sh 
myself="/cgi-bin/Route/`basename $0`"

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
function beforeAddRule(frm)
{
	var rngs;
	var polno   = document.getElementsByName(\"polno\")[0];
	var srctype  = document.getElementsByName(\"srctype\")[0];
	var dsttype = document.getElementsByName(\"dsttype\")[0];
	var srcipnet = document.getElementsByName(\"srcipnet\")[0];
	var srciprng = document.getElementsByName(\"srciprng\")[0];
	var dstipnet= document.getElementsByName(\"dstipnet\")[0];
	var dstiprng= document.getElementsByName(\"dstiprng\")[0];
	var srcport  = document.getElementsByName(\"srcport\")[0];
	var dstport = document.getelementsByName(\"dstport\")[0];
	var apid    = document.getElementsByName(\"appid\")[0];
	var dscp    = document.getElementsByName(\"dscp\")[0];
	var nexthop = document.getElementsByName(\"nexthop\")[0];
	polno.value = TrimAll(polno.value);
	if (!IsDigitIn(polno.value, 1, 65535)) {
		alert(\"űǽ165535֮һ!\");
		polno.select();
		return false;
	}
	srcipnet.value = TrimAll(srcipnet.value);
	srciprng.value = TrimAll(srciprng.value);
	if (srctype.value == \"net\") {
		if (!IsIPAddr(srcipnet.value)) {
			alert(\"ȷIPַʽΪx.x.x.x/n\");
			srcipnet.select();
			return false;
		}
	}
	else
	if (srctype.value == \"range\") {
		rngs = srciprng.value.split(\"-\");
		if (rngs.length != 2 || !IsIPAddr(rngs[0]) || !IsIPAddr(rngs[1])) {
			alert(\"ȷIPΣʽΪx.x.x.x-y.y.y.y\");
			srciprng.select();
			return false;
		}
	}
	srcport.value = TrimAll(srcport.value);
	if (srcport.value == \"\") srcport.value = \"0\";
	rngs = srcport.value.split(\"-\");
	if (rngs.length == 2) {
		if (!IsDigitIn(rngs[0], 0, 65535) || !IsDigitIn(rngs[1], 0, 65535)) {
			alert(\"˿!\");
			srcport.select();
			return false;
		}
	}
	else {
		if (!IsDigitIn(srcport.value, 0, 65535)) {
			alert(\"˿!\");
			srcport.select();
			return false;
		}
	}
	dstipnet.value = TrimAll(dstipnet.value);
	dstiprng.value = TrimAll(dstiprng.value);
	if (dsttype.value == \"net\") {
		if (!IsIPAddr(dstipnet.value)) {
			alert(\"ȷIPַʽΪx.x.x.x/n\");
			dstipnet.select();
			return false;
		}
	}
	else
	if (dsttype.value == \"range\") {
		rngs = dstiprng.value.split(\"-\");
		if (rngs.length != 2 || !IsIPAddr(rngs[0]) || !IsIPAddr(rngs[1])) {
			alert(\"ȷIPΣʽΪx.x.x.x-y.y.y.y\");
			dstiprng.select();
			return false;
		}
	}
	dstport.value = TrimAll(dstport.value);
	if (dstport.value == \"\") dstport.value = \"0\";
	rngs = dstport.value.split(\"-\");
	if (rngs.length == 2) {
		if (!IsDigitIn(rngs[0], 0, 65535) || !IsDigitIn(rngs[1], 0, 65535)) {
			alert(\"˿!\");
			dstport.select();
			return false;
		}
	}
	else {
		if (!IsDigitIn(dstport.value, 0, 65535)) {
			alert(\"˿!\");
			dstport.select();
			return false;
		}
	}
		
	dscp.value = TrimAll(dscp.value);
	if (dscp.value == \"\") dscp.value = \"0\";
	if (!IsDigitIn(dscp.value, 0, 63)) {
		alert(\"0~63!\");
		dscp.select();
		return false;
	}
	if (nexthop.value == \"\") {
		alert(\"ѡһ·!\");
		nexthop.select();
		return false;
	}
	
	return true;
}
function onCancel()
{
	window.location.href = \"/cgi-bin/Route/policy_list\";
}
function onSrctypeChanged(obj) 
{
	var net = document.getElementsByName(\"srcipnet\")[0];
	var rng = document.getElementsByName(\"srciprng\")[0];
	var tbl = document.getElementsByName(\"srciptbl\")[0];
	if (obj.value == \"any\") {
		net.style.display = \"none\";
		rng.style.display = \"none\";
		tbl.style.display = \"none\";
	}
	else
	if (obj.value == \"net\") {
		net.style.display = \"block\";
		rng.style.display = \"none\";
		tbl.style.display = \"none\";
	}
	else
	if (obj.value == \"range\") {
		net.style.display = \"none\";
		rng.style.display = \"block\";
		tbl.style.display = \"none\";
	}
	else
	if (obj.value == \"table\") {
		net.style.display = \"none\";
		rng.style.display = \"none\";
		tbl.style.display = \"block\";
	}
}
function onDsttypeChanged(obj) 
{
	var net = document.getElementsByName(\"dstipnet\")[0];
	var rng = document.getElementsByName(\"dstiprng\")[0];
	var tbl = document.getElementsByName(\"dstiptbl\")[0];
	if (obj.value == \"any\") {
		net.style.display = \"none\";
		rng.style.display = \"none\";
		tbl.style.display = \"none\";
	}
	else
	if (obj.value == \"net\") {
		net.style.display = \"block\";
		rng.style.display = \"none\";
		tbl.style.display = \"none\";
	}
	else
	if (obj.value == \"range\") {
		net.style.display = \"none\";
		rng.style.display = \"block\";
		tbl.style.display = \"none\";
	}
	else
	if (obj.value == \"table\") {
		net.style.display = \"none\";
		rng.style.display = \"none\";
		tbl.style.display = \"block\";
	}
}
function selectApp(appid)
{
	var url = \"/cgi-bin/Setup/app_select\";
	var retval = ShowDialog(url, \"\", 400, 600);
	if (retval == \"NONE\") return;
	var ar = retval.split(\",\");
	if (ar.length != 2) {
		alert(\"Чķֵ\");
		return;
	}
	var appid = document.getElementsByName(\"appid\")[0];
	var appname = document.getElementsByName(\"appname\")[0];
	appid.value = ar[0];
	appname.value = ar[1];
}
</script>
";
if [ "${REQUEST_METHOD}" = "POST" ]; then
	operator_check "/cgi-bin/Route/policy_list"
	cmdargs="id=${CGI_polno}"
	[ "${CGI_inifname}" != "any" ] && cmdargs="${cmdargs} inif=${CGI_inifname}"
	case "${CGI_srctype}" in
	"any")
		CGI_srcip="any"
		;;
	"net")
		CGI_srcip="${CGI_srcipnet}"
		;;
	"range")
		CGI_srcip="${CGI_srciprng}"
		;;
	"table")
		CGI_srcip="${CGI_srciptbl}"
		;;
	esac
	if [ "${CGI_srcport}" != "" -a "${CGI_srcport}" != "0" ]; then
		CGI_srcip="${CGI_srcip}:${CGI_srcport}"
	fi
	cmdargs="${cmdargs} src=${CGI_srcip}"
	case "${CGI_dsttype}" in
	"any")
		CGI_dstip="any"
		;;
	"net")
		CGI_dstip="${CGI_dstipnet}"
		;;
	"range")
		CGI_dstip="${CGI_dstiprng}"
		;;
	"table")
		CGI_dstip="${CGI_dstiptbl}"
		;;
	esac
	if [ "${CGI_dstport}" != "" -a "${CGI_dstport}" != "0" ]; then
		CGI_dstip="${CGI_dstip}:${CGI_dstport}"
	fi
	cmdargs="${cmdargs} dst=${CGI_dstip}"
	if [ "${CGI_dscp}" != "" -a "${CGI_dscp}" != "0" ]; then
		cmdargs="${cmdargs} dscp=${CGI_dscp}"
	fi
	[ "${CGI_proto}" != "any" ] && cmdargs="${cmdargs} proto=${CGI_proto}"
	cmdargs="${cmdargs} app=${CGI_appid}"
	cmdargs="${cmdargs} action=${CGI_action}-${CGI_nexthop}"
	errmsg=`${FLOWEYE} route add ${cmdargs}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_load_page 0 "/cgi-bin/Route/policy_list"
		exit 0
	fi
else
	[ "${CGI_dscp}" = "" ] && CGI_dscp=0
	[ "${CGI_srcport}" = "" ] && CGI_srcport=0
	[ "${CGI_dstport}" = "" ] && CGI_dstport=0
fi

echo -n "
<body>
<script type=\"text/javascript\" src=\"/img/wz_tooltip.js\"></script>
<span id=\"hlpno\" style=\"display:none\">ŷΧΪ1~65535,СĹƥ</span>
<span id=\"hlpdir\" style=\"display:none\">\"\"ʾ,\"\"ʾ</span>
<span id=\"hlprate\" style=\"display:none\">IPֻIPЧ,0ʾ</span>
<span id=\"hlpnext\" style=\"display:none\">(1)\"ƥ\"ʾһƥƥĲ<br>(2)\"ֹͣƥ\"ʾƥĲ<br>(3)Ϊ\"\"ʱѡЧ</span>
"; 
cgi_show_title "->·->Ӳ"
tblexists=`${FLOWEYE} table list`

echo -n "
<br>
<form method=post onsubmit=\"return beforeAddRule(this)\" action=\"${myself}\">
<table width=600 border=0 cellspacing=0 cellpadding=3 bgcolor=\"#ffffff\">
<tr id=row1>
	<td width=80></td>
        <td width=120 align=left>Աʶ</td>
	<td width=160 align=left><input align=right type=text name=polno style=\"width:100%\" value=\"${CGI_polno}\" /></td>
	<td width=* align=left><a style=\"color:#0000ff\" onmouseover=\"TagToTip('hlpno')\" onmouseout=\"UnTip()\">(1~65535)</a></td>
</tr>
</table>
<br>
<table style=\"width:600; border-bottom:1px #787882 solid; color:#000000\">
<tr><td align=left style=\"font-size:11px\"><b>ƥ</b></td></tr>
</table>
<br>
<table width=600 border=0 cellspacing=0 cellpadding=3 bgcolor=\"#ffffff\">
<tr id=row1>
	<td width=80></td>
        <td width=120 align=left>Դӿ</td>
	<td width=160 align=left>
		<select name=inifname style=\"width:100%\" value=\"${CGI_iniftype}.${CGI_inif}\">
		";
			${FLOWEYE} nat listproxy type=routerif | while read type id name theothers
			do
				if [ "${CGI_iniftype}" = "proxy" -a "${CGI_inif}" = "${name}" ]; then
					echo "<option value=\"proxy.${name}\" selected>${name}</option>"
				else
					echo "<option value=\"proxy.${name}\">${name}</option>"
				fi
			done
			${FLOWEYE} if list | while read ifname ifmode ifzone theothers
			do
				[ "${ifzone}" != "inside" ] && continue
				if [ "${CGI_inif}" = "${ifname}"  -a "${CGI_iniftype}" = "if" ]; then
					echo "<option value=\"if.${ifname}\" selected>${ifname}</option>"
				else
					echo "<option value=\"if.${ifname}\">${ifname}</option>"
				fi
			done
			if [ "${CGI_inif}" = "any" ]; then
				echo "<option value=any selected>ӿ</option>"
			else
				echo "<option value=any>ӿ</option>"
			fi
		
echo -n "
		</select>
	</td>
	<td width=* align=left></td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>Դַ</td>
        <td align=left>
		<select name=srctype style=\"width:100%\" value=\"any\" onchange=\"onSrctypeChanged(this)\">
			<option value=\"any\" selected>ַ</option>
			<option value=\"net\">xxx.xxx.xxx.xxx/nn</option>
			<option value=\"range\">n.n.n.n-m.m.m.m</option>
		";
			if [ "${tblexists}" != "" ]; then
				echo "<option value=\"table\">IPȺ</option>"
			fi
		
echo -n "
		</select>
	</td>
	<td width=* align=left>
		<input type=text name=srcipnet style=\"width:210px;display:none\"></input>
		<input type=text name=srciprng style=\"width:210px;display:none\"></input>
		<select name=srciptbl style=\"width:210px;display:none\">
		";
			${FLOWEYE} table list | while read tblid tblname
			do
				echo "<option value=\"${tblid}\">${tblname}</option>"
			done
		
echo -n "
		</select>
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>Դ˿</td>
        <td><input type=text name=srcport value=\"${CGI_srcport}\" style=\"width:100%\"></input></td>
	<td width=* align=left><a style=\"color:#0000ff\">(808000-81000ʾ˿)</a></td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>Ŀַ</td>
        <td align=left>
		<select name=dsttype style=\"width:100%\" value=\"any\" onchange=\"onDsttypeChanged(this)\">
			<option value=\"any\" selected>ַ</option>
			<option value=\"net\">xxx.xxx.xxx.xxx/nn</option>
			<option value=\"range\">n.n.n.n-m.m.m.m</option>
		";
			if [ "${tblexists}" != "" ]; then
				echo "<option value=\"table\">IPȺ</option>"
			fi
		
echo -n "
		</select>
	</td>
	<td width=* align=left>
		<input type=text name=dstipnet style=\"width:210px;display:none\"></input>
		<input type=text name=dstiprng style=\"width:210px;display:none\"></input>
		<select name=dstiptbl style=\"width:210px;display:none\">
		";
			${FLOWEYE} table list | while read tblid tblname
			do
				echo "<option value=\"${tblid}\">${tblname}</option>"
			done
		
echo -n "
		</select>
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>˿</td>
        <td align=left><input type=text name=dstport value=\"${CGI_dstport}\" style=\"width:100%\"></input></td>
	<td width=* align=left><a style=\"color:#0000ff\">(808000-81000ʾ˿)</a></td>
</tr>
";
[ "${CGI_appid}" = "" ] && CGI_appid="any"
appname=`${FLOWEYE} app name ${CGI_appid}`

echo -n "
<tr id=row1>
	<td></td>
        <td align=left>Э</td>
        <td><select name=proto value=\"${CGI_proto}\" style=\"width:100%\">
		";
			case "${CGI_proto}" in
			"tcp|TCP")
				echo "<option value=any></option>"
				echo "<option value=TCP selected>TCP</option>"
				echo "<option value=UDP>UDP</option>"
				echo "<option value=ICMP>ICMP</option>"
				;;
			"udp|UDP")
				echo "<option value=any></option>"
				echo "<option value=TCP>TCP</option>"
				echo "<option value=UDP selected>UDP</option>"
				echo "<option value=ICMP>ICMP</option>"
				;;
			"icmp|ICMP")
				echo "<option value=any></option>"
				echo "<option value=TCP>TCP</option>"
				echo "<option value=UDP>UDP</option>"
				echo "<option value=ICMP selected>ICMP</option>"
				;;
			*)
				echo "<option value=any selected></option>"
				echo "<option value=TCP>TCP</option>"
				echo "<option value=UDP>UDP</option>"
				echo "<option value=ICMP>ICMP</option>"
				;;
			esac
		
echo -n "
		</select>
	</td>
        <td width=* align=left></td>
</tr>
"; if [ ${JOS_RELEASE} -ne 0 ]; then 
echo -n "
<tr id=row1>
	<td></td>
        <td align=left>ӦЭ</td>
        <td align=left>
		<input type=hidden name=appid value=\"${CGI_appid}\"></input>
		<input type=text name=appname value=\"${appname}\" disabled style=\"width:100%\">
	</td>
	<td width=* class=cssTDArial align=left>
	  <input type=button name=tmp value=\"ѡӦ...\" style=\"width:80px\" onclick=\"selectApp('${CGI_appid}')\"></input>
	</td>
</tr>
"; else 
echo -n "
<tr id=row1>
	<td></td>
        <td align=left>ӦЭ</td>
        <td align=left>
		<input type=hidden name=appid value=\"any\"></input>
		<input type=text name=appname value=\"Э\" disabled style=\"width:100%\">
	</td>
	<td width=* class=cssTDArial style=\"color:#ff0000\" align=left>Ǳ׼棬֧Ӧ·</td>
</tr>
"; fi 
echo -n "
<tr id=row1>
	<td></td>
        <td align=left>DSCP</td>
        <td align=left><input type=text name=dscp value=\"${CGI_dscp}\" style=\"width:100%\"></input></td>
	<td width=* align=left></td>
</tr>
</table>
<br>
<table style=\"width:600; border-bottom:1px #787882 solid; color:#000000\">
<tr><td align=left style=\"font-size:11px\"><b>·ɶ</b></td></tr>
</table>
<br>
<table width=600 border=0 cellspacing=0 cellpadding=3 bgcolor=\"#ffffff\">
<tr id=row1>
	<td width=80></td>
        <td width=120 align=left>ִж</td>
        <td width=160>
		<select name=action value=\"${CGI_action}\" style=\"width:100%\">
		";
			if [ "${CGI_action}" = "nat" ]; then
				echo "<option value=\"nat\" selected>NAT</option>"
			else
				echo "<option value=\"nat\">NAT</option>"
			fi
			if [ "${CGI_action}" = "route" ]; then
				echo "<option value=\"route\" selected>FWD</option>"
			else
				echo "<option value=\"route\">FWD</option>"
			fi
		
echo -n "
		</select>
	</td>
        <td width=* align=left></td>
</tr>
<tr id=row1>
	<td width=80></td>
        <td width=120 align=left>WAN·</td>
        <td width=160>
		<select name=nexthop value=\"${CGI_nexthop}\" style=\"width:100%\">
		";
			${FLOWEYE} nat listproxy type=proxy.pppoe | while read type id name theothers
			do
				if [ "${CGI_nexthop}" = "${name}" ]; then
					echo "<option value=\"${name}\" selected>${name}</option>"
				else
					echo "<option value=\"${name}\">${name}</option>"
				fi
			done
			${FLOWEYE} nat listwg | while read id name pxycnt theothers
			do
				[ ${pxycnt} -eq 0 ] && continue
				if [ "${CGI_nexthop}" = "_wg.${name}" ]; then
					echo "<option value=\"_wg.${name}\" selected>${name}</option>"
				else
					echo "<option value=\"_wg.${name}\">${name}</option>"
				fi
			done
			if [ "${CGI_nexthop}" = "_NULL_" ]; then
				echo "<option value=\"_NULL_\" selected>·</option>"
			else
				echo "<option value=\"_NULL_\">·</option>"
			fi
		
echo -n "
		</select>
	</td>
        <td width=* align=left></td>
</tr>
</table>
<table style=\"width:600; border-bottom:1px #787882 solid; color:#0000ff\">
<tr><td align=right>&nbsp;</td></tr>
</table>
<table style=\"width:600\"> 
<tr>
        <td align=right>
		<input type=submit id=newbtn style=\"width:80\" value=\"ύ\"></input>
		<input type=button id=newbtn style=\"width:80\" value=\"ȡ\" onclick=\"onCancel()\"></input>
	</td>
</tr>
</table>
</form>
</body>
</html>
";