<html>
	<head>
		<meta HTTP-EQUIV='Pragma' CONTENT='no-cache'>
      	<link rel="stylesheet" href='stylemain.css' type='text/css'>
      	<link rel="stylesheet" href='colors.css' type='text/css'>
        <script language="javascript">
<!-- hide
var sessionKey='<%ejGetOther(sessionKey)%>';
var filepath = '<%ejGetOther(tcpdumppath)%>';
var filename = '<%ejGet(capfile)%>';
var pagestate = '<%ejGet(capstate)%>';
var filesize = '<%ejGet(capsize)%>';
var count = '<%ejGet(capcount)%>';
var pro = '<%ejGet(capPro)%>';
var intf = '<%ejGet(capInterface)%>';
var usbstorage = '<%ejGet(usbstorage)%>';

function freshpage(){
	var curstate = '<%ejGet(capstate)%>';
	if(curstate == 2)
	{
		FileName.value= filename + ".pcap";
		FileName.disabled = 0;
	}
}

function sendRefresh() { 
	window.location.href="sniffer.html"; 
}    

function frmLoad(){
	with (document.forms[0]) {
		State.disabled =1;
		if(pagestate == 0){
			//init
			State.value = "Idle";			
			FileName.value="";
			FileName.disabled = 1;
		}
		if(pagestate == 1) {
			//start
			filePath.value = usbstorage;
			packetname.value = filename;
			State.value = "processing...";
			IntfName.value = intf;
			Filter.value = pro;
			packets.value = count;
			size.value = filesize;
			FileName.value=  "";
			FileName.disabled = 1;
			setTimeout('sendRefresh()' , 3000); 
		}
		if(pagestate == 2)
		{
			//stop
			filePath.value = usbstorage;
			packetname.value = filename;
			State.value = "Idle";
			IntfName.value = intf;
			Filter.value = pro;
			packets.value = count;
			size.value = filesize;
			FileName.value= filename + ".pcap";
			FileName.disabled = 0;
		}
		if(pagestate == 3)
		{
			//delete
			State.value = "Idle";			
			FileName.value="";
			FileName.disabled = 1;
		}				
	}	
}


function checkFileName(input) { 
	var re = /^(?!\.)[^\\\/:\*\?"<>\|]{1,255}$/;   
	if (re.test(input)) {
		return 0;
	}

	return 1;
}

function createFile(){
    with (document.forms[0]){
	 filename = document.getElementById("Name").value;
	 if(checkFileName(filename) == 1)
	 {
		alert("File Name is invalid, Please input again.");
		return;	 
	 }
     FileName.value = filename + ".pcap";
	var loc = 'sniffer.cmd?action=create';
	loc += '&file=' + filename;
	loc += '&state=1';
	loc += '&sessionKey=' + sessionKey;
	}
	var code = 'location ="' +  loc + '"';
	eval(code);
}

function checkNum(input) { 
	var re =/^[1-9]+[0-9]*]*$/;
	if (re.test(input)) {
		return 0;
	}

	return 1;
}

function StartCap(){
	with (document.forms[0]){
	var loc = "sniffer.cmd?action=start";

	if(filePath.value == 'nousb')
	{
		alert("Please plug in a USB storage.");
		return;
	}
	loc += '&usbstorage=' + filePath.value;

	filename = packetname.value;
  	if(filename == '')
	{
		alert("File Name is invalid.");
		return;
	}
	if(checkFileName(filename) == 1)
	{
		alert("File Name is invalid, it can only contain alphabet,number and underline, and start with a alphabet or an underline.");
		return;	 
	}	
	loc += '&file=' + filename;
  	loc += '&interface='+ IntfName.value;;
	loc += '&filter=' + Filter.value;
	if(packets.value == '')
	{
		alert("packet number is invalid.");
		return;
	}
	else
    {
		count= packets.value;

		if( checkNum(count) == 1)
		{
	  		alert("packets number is invalid.");
	  		return;
		}
		
		if(isNaN(count))
		{
			alert("packets number is invalid.");
			return;
		}

	  	if(count <=0)
	  	{
	  		alert("packets number is invalid.");
	  		return;
	  	}
		
	loc += '&count=' + count;	
    }
    if(size.value == '')
    {
  		alert("file size is invalid.");
  		return;
    }
    else
    {
  	 	filesize =size.value;
		if( checkNum(filesize) == 1)
		{
	  		alert("file size is invalid");
	  		return;
		}
		
  	 	if(isNaN(filesize))
  		{
  			alert("file size is invalid");
  			return;
  		}
	  	if(filesize <=0||filesize>100)
	  	{
	  		alert("file size is invalid");
	  		return;
	  	}
			
	  		loc +='&size=' + filesize;
	    }
    }
    loc += '&state=1';
  	loc += '&sessionKey=' + sessionKey;
  	var code = 'location="' + loc + '"';
  	eval(code);
}

function StopCap(){
	with (document.forms[0]){
 	var loc = "sniffer.cmd?action=stop";
 	loc += '&state=2';
	}
    loc += '&sessionKey=' + sessionKey;
  var code = 'location="' + loc + '"';
  eval(code);
}

function LoadFile(){

   var loc =  filename + ".pcap";

   var code = 'location="' + loc + '"';
   eval(code);
}

function DeleteFile(){
	with (document.forms[0]){
	var loc = "sniffer.cmd?action=delete";
	loc += "&state=3";
	}
	loc += '&sessionKey=' + sessionKey;
  	var code = 'location="' + loc + '"';
	eval(code);
}
// done hiding -->
</script>
   </head>
   <body onLoad="frmLoad()">
   <blockquote>
   <form>
	<b>Sniffer</b>
	<br>
	<br>
	<p>The Sniffer is for capture packet. You need to set the "file name", "interface", "filter", "packet number" and "file size" then you can click "start" button to start capture.</p>
	<br>
 <table border="0" cellpadding="0" cellspacing="4">
 <tr>
	 <td width="150">USB storage: </td>
	 <td><select name ='filePath' onChange=''>
	<script language="javascript">
<!-- hide
{
        if(filepath == '')
        {
            document.write("  <option value='nousb'>Please plug in a USB storage</option>");
        }
        else
        {
            var i;
            var dirname = filepath.split('/');
            for(i=0; i<dirname.length; i++)
                document.write("  <option value='" + dirname[i] + "'>" + dirname[i] + "</option>");
        }
}
// done hiding -->
</script>
	 </select></td>
	 </tr>
 <tr>
	 <td width="150">File Name: </td>
	 <td><input type="text" id ="Name"  name="packetname" value="">.pcap</td>
	 </tr>
	 <tr>
	 <td><input type="button" value="Create" onClick="createFile()" style="visibility:hidden"></td>
	 </tr>
	<tr>
	<td width="150">Interface:</td>
	<td><select name ="IntfName" onChange="changeIntfName">
	<option disabled="disabled">WAN Interface</option>
	<script language="javascript">
<!-- hide
{
   var i = 0;
   var waninterfaceInfo = '<%ejGetOther(wanInterfaceInfo, all)%>';
   var waninterfaces = waninterfaceInfo.split('|');

  for ( i = 0; i < waninterfaces.length; i++ ) {
      var names = waninterfaces[i].split('/');
 
      document.write("  <option value='" + names[1] + "'>&nbsp;&nbsp;");
      document.writeln(waninterfaces[i]);
   }
  }
// done hiding -->
</script>
	<option disabled="disabled">LAN Interface</option>
	<script language="javascript">
<!-- hide
{
	var j = 0;
	var interfaceInfo = '<%ejGetOther(bridgeInterfaceInfo, all)%>';
	var interfaces = interfaceInfo.split('|');
	
    if (interfaces.length > 0) 
    {
       for (i = 0; i < interfaces.length; i++) 
       {                 
		 document.write("<option value='" + interfaces[i] + "'>&nbsp;&nbsp;" + interfaces[i]);
       }
    }
//done hiding -->	
}
</script>
   </select></td>
			   </tr>
			   <tr>
			   <td width="150">Protocol Filter:</td>
			   <td><select name="Filter" onChange="ChangeFilter">
			   <option value="ALL">ALL</option>
			   <option value="TCP">TCP</option>
			   <option value="UDP">UDP</option>
			   </select>
			   </td>
			   </tr>
			   <tr><td>Packet Number:</td>
			   <td><input type="text" name="packets" value="1000">(1~)</td>
			   </tr>
			   <tr>
			   <td>File Size(MB):</td>
			   <td><input type="text" name="size" value="1">(1~100)</td>
			   </tr>
			   <tr>
			   <td width="150">Status:</td>
			   <td><input type="text" name="State" style="border:none;background-color:#FFFFFF" value=""></td>
			   </tr>
			   <tr>
			   <td width="150">File: </td>
			   <td><input type="button" style="border:none;background:#FFFFFF;color:#0099FF" onClick="LoadFile()" name="FileName" value=""></td>
			   </tr>
			   </table>
			   <p>After complete,you can click the File to download the file.</p>
			   <br>
			<p align="center">
			<script language="javascript">
			<!-- hide
			if (pagestate == 0){
				//init
				document.writeln("<input type='button' name='start' value='Start' onClick='StartCap()' >");
			} else if (pagestate == 1){
				//start
				document.writeln("<input type='button' name='stop' value='Stop' onClick='StopCap()' >");
			} else if (pagestate == 2){
				//stop
				document.writeln("<input type='button' name='start' value='Start' onClick='StartCap()' >");
				document.writeln("<input type='button' name='deleteBtn' value='Clean' onClick='DeleteFile()' >");
			} else if (pagestate == 3){
				//delete
				document.writeln("<input type='button' name ='start' value='Start' onClick='StartCap()' >");
			}
			//done hiding -->	
			</script>
			<input type="button" name="Download" value="Download" style="visibility:hidden" disabled="disabled" onClick="LoadFile()">
			</p>
			</form>	
			</blockquote>
   </body>
</html>
