#!/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 ·ɵַ#/cgi-bin/Setup/if_rtmain_route"

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 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;
	}
}
</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}" = "addip" ]; then
	ipexist=0
	exec 0<${PGETC}/tbl_main.conf
	while read ipaddr; do
		if [ "${ipaddr}" = "${CGI_ipaddr}" ]; then
			ipexist=1
			break
		fi
	done
	if [ ${ipexist} -eq 0 ]; then
		errmsg=`${PGBIN}/pfctl -t tbl_main -Tadd ${CGI_ipaddr} 2>/dev/null`
		if [ "$?" = "0" ]; then
			echo "${CGI_ipaddr}" >> ${PGETC}/tbl_main.conf
			afm_dialog_msg "ɹ!"
		else
			afm_dialog_msg "صַʧ!"
		fi
	else
		afm_dialog_msg "ַѾ!"
	fi
fi
if [ "${CGI_action}" = "rmvip" ]; then
	tmpfile=/var/tmp/tbl_main.conf
	[ -e ${tmpfile} ] && rm -f ${tmpfile}
	touch ${tmpfile}
	ipexist=0
	exec 0<${PGETC}/tbl_main.conf
	while read ipaddr; do
		if [ "${ipaddr}" = "${CGI_ipaddr}" ]; then
			ipexist=1
			break
		else
			echo "${ipaddr}" >> ${tmpfile}
		fi
	done
	mv ${tmpfile} ${PGETC}/tbl_main.conf
	if [ ${ipexist} -eq 1 ]; then
		errmsg=`${PGBIN}/pfctl -t tbl_main -Tdelete ${CGI_ipaddr} 2>/dev/null`
		if [ "$?" = "0" ]; then
			afm_dialog_msg "ɹ!"
		else
			afm_dialog_msg "жصַʧ!"
		fi
	else
		afm_dialog_msg "ַ!"
	fi
fi
if [ "${CGI_action}" = "loadip" ]; then
	errmsg=`cat ${CGI_ipfile} | ${PGBIN}/pfctl -t tbl_main -Treplace -f- 2>/dev/null`
	if [ "$?" = "0" ]; then
		mv ${CGI_ipfile} ${PGETC}/tbl_main.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></td>
</tr>
</table>
</form>
<br>
"; cgi_print_mod_header "·ɵַ" 
echo -n "
<br>
<table class=bgurl width=600 border=0 cellspacing=1 cellpadding=1 bgcolor=\"#ffffff\">
<tr id=row1 height=230>
	<td width=120 align=center>ĿĵַΪڵַݰߴ˳ڳȥ</td>
	<td width=250><select style=\"width:100%;height:100%;\" name=iptable size=50 onchange=\"onIPChanged(this)\"> ";
		if [ -e ${PGETC}/tbl_main.conf ]; then
			exec 0<${PGETC}/tbl_main.conf
			while read ipaddr; do
				echo "<option value=\"${ipaddr}\">${ipaddr}</option>"
			done
		fi 
echo -n "</select>
	</td>
	<td></td>
</tr>
<tr id=row2>
<form method=post onsubmit=\"return ValidateIPAddr(this)\" action=\"${myself}\">
	<td></td>
	<td><input type=text style=\"width:100%\" name=ipaddr></input></td>
	<td width=* align=right>
		<input type=hidden name=action value=\"addip\"></input>
		<input type=submit style=\"width:90;height:22px\" value=\"ӵַ\"></input>&nbsp;&nbsp;
		<input type=button name=rmvip disabled style=\"width:90;height:22px\" value=\"ɾַ\" onclick=\"onRmvIP()\"></input>
	</td>
</form>
</tr>
</table>
<table width=600 border=0 cellspacing=1 cellpadding=1 bgcolor=\"#ffffff\">
<form method=post action=\"${myself}\" enctype=\"multipart/form-data\">
<tr height=22>
        <td width=500 style=\"font:14px bold\" align=left>
		<b>ļ,·</b>&nbsp;
                <input type=file name=ipfile style=\"width:300\">
        </td>
        <td align=right width=*>
                <input type=hidden name=action value=\"loadip\"></input>
                <input type=submit style=\"width:90\" value=\"ύбļ\"></input>
        </td>
</tr>
</form>
</table>
</body>
</html>
";