#!/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/Setup/`basename $0`
MOD_TAB_LIST="˿ӳ#/cgi-bin/Setup/if_rtmain_portmap"

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script languate=javascript>
function beforeConfig(frm)
{
	var ifaddr = document.getElementsByName(\"ipaddr\")[0];
	var ifmask = document.getElementsByName(\"netmask\")[0];
	var ifgw   = document.getElementsByName(\"gateway\")[0];
	ifaddr.value = TrimAll(ifaddr.value);
	ifmask.value = TrimAll(ifmask.value);
	ifgw.value   = TrimAll(ifgw.value);
	if (!IsIPAddr(ifaddr.value)) {
		alert(\"ȷIPַ!\");
		return false;
	}
	if (!IsNetmask(ifmask.value)) {
		alert(\"ȷ!\");
		return false;
	}
	
	if (ifgw.value != \"\" && !IsIPAddr(ifgw.value)) {
		alert(\"ȷصַ!\");
		return false;
	}
	
	return true;
}
function ValidateRDR(frm)
{
	var port   = document.getElementsByName(\"port\")[0];
	var inip   = document.getElementsByName(\"inip\")[0];
	var inport = document.getElementsByName(\"inport\")[0];
	port.value = TrimAll(port.value);
	inip.value = TrimAll(inip.value);
	inport.value = TrimAll(inport.value);
	if (!IsDigitIn(port.value, 1, 65535)) {
		alert(\"ȷĶ˿ں!\");
		port.select();
	}
	if (!IsIPAddr(inip.value)) {
		alert(\"ȷIPַ!\");
		inip.select();
		return false;
	}
	if (!IsDigitIn(inport.value, 1, 65535)) {
		alert(\"ȷĶ˿ں!\");
		inport.select();
		return false;
	}
	return true;
}
function ValidateIPAddr(frm)
{
	var ipaddr = document.getElementsByName(\"ipaddr\")[0];
	ipaddr.value = TrimAll(ipaddr.value);
	if (!IsNet(ipaddr.value) && !IsIPAddr(ipaddr.value)) {
		alert(\"ЧIPַ!\");
		ipaddr.select();
		return false;
	}
	return true;
}
function onIPChanged(iptbl)
{
	if (iptbl.value != \"\") 
		document.getElementsByName(\"rmvip\")[0].disabled = false;
	else
		document.getElementsByName(\"rmvip\")[0].disabled = true;
}
function onRmvIP()
{
        var iptbl;
        iptbl = document.getElementsByName(\"iptable\")[0];
	if (iptbl.value != \"\") {
		window.location.href = \"${myself}?action=rmvip&ipaddr=\" + iptbl.value;
	}
}
function deleteRDR(proto, port)
{
	if (confirm(\"ȷҪɾö˿ӳ?\")) {
		window.location.href = \"${myself}?action=rmvrdr&proto=\" + proto + \"&port=\" + port;
	}
}
</script>
";
if [ "${CGI_action}" = "setif" ]; then
        . ${PGETC}/if_main.conf
        if [ "${if_ipaddr}"  != "${CGI_ipaddr}" -o \
             "${if_netmask}" != "${CGI_netmask}" -o \
             "${if_gateway}" != "${CGI_gateway}" ]; then
                echo "if_ipaddr=${CGI_ipaddr}
if_netmask=${CGI_netmask}
if_gateway=${CGI_gateway}" > ${PGETC}/if_main.conf
                ifconfig ${MAIN_RTPORT} ${CGI_ipaddr} netmask ${CGI_netmask} 2>/dev/null
                gateway=`netstat -rn | grep "^default" | awk '{print $2}' 2>/dev/null`
                if [ "${gateway}" != "${CGI_gateway}" ]; then
                        ermsg=`route delete default 2>&1`
                        route add default ${CGI_gateway} >/dev/null
                fi
                ${PGPATH}/bin/natcfg
                ${PGPATH}/bin/pfctl -f /var/tmp/pf.conf
                afm_dialog_msg "ɹ!"
        fi
fi
if [ "${CGI_action}" = "addrdr" ]; then
	portexist=0
	exec 0<${PGETC}/rdr_main.conf
	while read proto port inip inport others; do
		if [ "${proto}" = "${CGI_proto}" -a "${port}" = "${CGI_port}" ]; then
			portexist=1
			break
		fi
	done
	if [ ${portexist} -eq 0 ]; then
		echo "${CGI_proto} ${CGI_port} ${CGI_inip} ${CGI_inport}" >> ${PGETC}/rdr_main.conf
                ${PGPATH}/bin/natcfg
                ${PGPATH}/bin/pfctl -f /var/tmp/pf.conf
		afm_dialog_msg "ɹ!"
	else
		afm_dialog_msg "˿Ѿӳ䣬ӳ䣬ɾӳ!"
	fi
fi
if [ "${CGI_action}" = "rmvrdr" ]; then
	tmpfile=/var/tmp/rdr_main.conf
	[ -e ${tmpfile} ] && rm -f ${tmpfile}
	touch ${tmpfile}
	portexist=0
	exec 0<${PGETC}/rdr_main.conf
	while read proto port inip inport others; do
		if [ "${proto}" = "${CGI_proto}" -a "${port}" = "${CGI_port}" ]; then
			portexist=1
		else
			echo "${proto} ${port} ${inip} ${inport}" >> ${tmpfile}
		fi
	done
	mv ${tmpfile} ${PGETC}/rdr_main.conf
	if [ ${portexist} -eq 1 ]; then
                ${PGPATH}/bin/natcfg
                ${PGPATH}/bin/pfctl -f /var/tmp/pf.conf
		afm_dialog_msg "ɹ!"
	else
		afm_dialog_msg "ӳ!"
	fi
fi
. ${PGETC}/if_main.conf

echo -n "
<body>
"; cgi_show_title "·->1" 
echo -n "
<br>
<form method=post onsubmit=\"return beforeConfig(this)\" action=\"${myself}\">
<table style=\"font:14px\" width=600 border=0 cellspacing=0 cellpadding=3 bgcolor=\"#ffffff\">
<tr id=row1>            
        <td width=120 align=center>ӿ</td>
        <td width=240 align=left><b>${MAIN_RTPORT}(ΪϵͳĬϳ)</b></td>
	<td width=*></td>
</tr>
<tr id=row1>            
        <td align=center>IPַ</td>
        <td align=left>
                <input type=text name=ipaddr style=\"width:120px\" value=\"${if_ipaddr}\"></input>
        </td>
	<td width=*></td>
</tr>
<tr id=row1>
        <td align=center></td>
        <td align=left>
                <input type=text name=netmask style=\"width:120px\" value=\"${if_netmask}\"></input>
	</td>
	<td width=*></td>
</tr>
<tr id=row1>            
        <td align=center>صַ</td>
        <td align=left>
                <input type=text name=gateway style=\"width:120px\" value=\"${if_gateway}\"></input>
                <input type=hidden name=action value=\"setif\"></input>
        </td>
       	<td width=* align=right><input type=submit style=\"width:90\" value=\"ύ\"></input>&nbsp;&nbsp;</td>
</tr>
</table>
</form>
<br>
"; cgi_print_mod_header "˿ӳ" 
echo -n "
<br>
<table class=bgurl width=600 left=20 border=0 cellspacing=1 cellpadding=1 bgcolor=\"#ffffff\">
<tr id=tblhdr height=22>
	<td width=120 align=center>Э</td>
	<td width=120 align=center>˿</td>
	<td width=120 align=center>IP</td>
	<td width=120 align=center>˿</td>
        <td width=*   align=center></td>
</tr>
";
rowid="row1"
exec 0<${PGETC}/rdr_main.conf
while read proto port inner_ip inner_port theothers; do
	echo "<tr id=${rowid}>"
	echo "<td align=center>${proto}</td>"
	echo "<td align=center>${port}</td>"
	echo "<td align=center>${inner_ip}</td>"
	echo "<td align=center>${inner_port}</td>"
	echo "<td align=center>"
	echo "<a href=\"javascript:deleteRDR('${proto}', '${port}')\" style=\"#0000ff\">ɾ</a></td>"
	echo "</tr>"
	if [ "${rowid}" = "row1" ]; then
		rowid="row2"
	else
		rowid="row1"
	fi
done

echo -n "
<tr id=row2>
<form method=post onsubmit=\"return ValidateRDR(this)\" action=\"${myself}\">
	<td><select name=proto align=center style=\"width:100%\">
		<option value=\"tcp\">tcp</option><option value=\"udp\">udp</option>
	     </select>
	</td>
	<td>
		<input type=hidden name=action value=\"addrdr\"></input>
		<input type=text style=\"width:100%\" name=port></input>
	</td>
	<td><input type=text style=\"width:100%\" name=inip></input></td>
	<td><input type=text style=\"width:100%\" name=inport></input></td>
	<td width=* align=center>
		<input type=submit style=\"width:90;height:22px\" value=\"ӳ\"></input>
	</td>
</form>
</tr>
</table>
</body>
</html>
";