Looking for a comprehensive introduction to creating networking applications with AJAX? Then look no further, because you’ve come to the right place! Welcome to the concluding installment of the series “Creating a Network Processor with AJAX.” As you may have guessed, this series leads you through the development of a highly expansible networking application which uses the capacity of AJAX for sending queries in the background.
Completing a Network Processor with AJAX - Completing the networking application: listing the full source code (Page 5 of 5 )
As I stated a few lines before, here is the complete source code for this networking application. I split up the program in two well-differentiated files: the first one, called "network_proc.htm," corresponds to the client module, while the second one, "query_processor.php," belongs obviously to the server layer.
Having clarified this concept, here are the files in question:
// definition for "network_proc.htm" file <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>AJAX-based Networking Processor</title> <style type="text/css"> body{ padding: 0; margin: 0; background: #fff; } h1{ font: bold 24px Arial, Helvetica, sans-serif; color: #000; text-align: center; margin: 10px; } #maincontainer{ width: 500px; height: 400px; background: #eee; padding: 5px; margin-left: auto; margin-right: auto; border: 1px solid #000; } #paramcontainer{ padding: 5px; margin-bottom: 5px; background: #f5ebb1; font: bold 12px Arial, Helvetica, sans-serif; color: #000; border: 1px solid #999; } #leftpanel{ float: left; width: 100px; height: 350px; padding: 5px; background: #f5ebb1; font: bold 12px Arial, Helvetica, sans-serif; color: #000; border: 1px solid #999; } #centerpanel{ float: left; width: 254px; height: 350px; padding: 5px; margin-left: 5px; background: #ccc; overflow: auto; font: bold 12px Arial, Helvetica, sans-serif; color: #000; border: 1px solid #999; } #rightpanel{ float: right; width: 100px; height: 350px; padding: 5px; background: #f5ebb1; font: bold 12px Arial, Helvetica, sans-serif; color: #000; border: 1px solid #999; } .databox{ width: 348px; font: normal 12px Arial, Helvetica, sans-serif; color: #000; } .controlbutton{ width: 100px; margin: 3px 0 3px 0; font: normal 12px Arial, Helvetica, sans-serif; color: #000; text-align: center; } </style> <script language="javascript"> // send http requests function sendHttpRequest(url,callbackFunc,respXml){ var xmlobj=null; try{ xmlobj=new XMLHttpRequest(); } catch(e){ try{ xmlobj=new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){ alert('AJAX is not supported by your browser!'); return false; } } xmlobj.onreadystatechange=function(){ if(xmlobj.readyState==4){ if(xmlobj.status==200){ respXml?eval(callbackFunc+ '(xmlobj.responseXML)'):eval(callbackFunc+ '(xmlobj.responseText)'); } } } // open socket connection xmlobj.open('GET',url,true); // send http header xmlobj.setRequestHeader('Content-Type','text/html; charset=UTF-8'); // send http request xmlobj.send(null); } // display command results function displayCommandResults(results){ var centpanel=document.getElementById('centerpanel'); if(!centpanel){return}; centpanel.innerHTML=''; centpanel.innerHTML=results; } // initialize control panel function initializeControlPanel(){ var form=document.getElementsByTagName('form')[0]; if(!form){return}; // assign 'onclick' event handlers to control buttons if(!form.elements[1]){return}; form.elements[1].onclick=function(){sendHttpRequest('query_processor.php?data='+document.getElementsByTagName('form')[0].elements[0].value+'&command=host','displayCommandResults')}; if(!form.elements[2]){return}; form.elements[2].onclick=function(){sendHttpRequest('query_processor.php?data='+document.getElementsByTagName('form')[0].elements[0].value+'&command=ip','displayCommandResults')}; if(!form.elements[3]){return}; form.elements[3].onclick=function(){sendHttpRequest('query_processor.php?data='+document.getElementsByTagName('form')[0].elements[0].value+'&command=iplist','displayCommandResults')}; if(!form.elements[4]){return}; form.elements[4].onclick=function(){sendHttpRequest('query_processor.php?data='+document.getElementsByTagName('form')[0].elements[0].value+'&command=ping','displayCommandResults')}; if(!form.elements[5]){return}; form.elements[5].onclick=function(){sendHttpRequest('query_processor.php?data='+document.getElementsByTagName('form')[0].elements[0].value+'&command=ipconfig','displayCommandResults')}; if(!form.elements[6]){return}; form.elements[6].onclick=function(){sendHttpRequest('query_processor.php?data='+document.getElementsByTagName('form')[0].elements[0].value+'&command=netstat','displayCommandResults')}; if(!form.elements[7]){return}; form.elements[7].onclick=function(){sendHttpRequest('query_processor.php?data='+document.getElementsByTagName('form')[0].elements[0].value+'&command=mxrecords','displayCommandResults')}; if(!form.elements[8]){return}; form.elements[8].onclick=function(){sendHttpRequest('query_processor.php?data='+document.getElementsByTagName('form')[0].elements[0].value+'&command=serviceports','displayCommandResults')}; if(!form.elements[9]){return}; form.elements[9].onclick=function(){sendHttpRequest('query_processor.php?data='+document.getElementsByTagName('form')[0].elements[0].value+'&command=servicenames','displayCommandResults')}; if(!form.elements[10]){return}; form.elements[10].onclick=function(){sendHttpRequest('query_processor.php?data='+document.getElementsByTagName('form')[0].elements[0].value+'&command=scanport','displayCommandResults')}; if(!form.elements[11]){return}; form.elements[11].onclick=function(){sendHttpRequest('query_processor.php?data='+document.getElementsByTagName('form')[0].elements[0].value+'&command=nsrecords','displayCommandResults')}; if(!form.elements[12]){return}; form.elements[12].onclick=function(){ var centpanel=document.getElementById('centerpanel'); if(!centpanel){return}; centpanel.innerHTML=''; } } // execute 'initializeControlPanel()' function when web // page is loaded window.onload=function(){ if(document.getElementById && document.getElementsByTagName && document.createElement){ initializeControlPanel(); } } </script> </head> <body> <h1>AJAX-BASED NETWORKING PROCESSOR</h1> <div id="maincontainer"> <form> <div id="paramcontainer"> Host Name/ IP Address <input type="text" name="data" class="databox"></input> </div> <div id="leftpanel"> <input type="button" name="host" value="Host to IP" class="controlbutton" title="CovertHostname to IP address"></input> <input type="button" name="ip" value="IP to Host" class="controlbutton" title="Convert IP address to Hostname"></input> <input type="button" name="iplist" value="IP List" class="controlbutton" title="Retrieve IP list"></input> <input type="button" name="ping" value="Ping" class="controlbutton" title="Execute ping command"></input> <input type="button" name="ipconfig" value="IP Config" class="controlbutton" title="Execute ipconfig command"></input> <input type="button" name="netstat" value="Netstat" class="controlbutton" title="Execute netstat command"></input> </div> <div id="centerpanel"></div> <div id="rightpanel"> <input type="button" name="mxrec" value="MX Records" class="controlbutton" title="Retrieve MX records"></input> <input type="button" name="servports" value="Service Ports" class="controlbutton" title="Retrieve service ports"></input> <input type="button" name="servnames" value="Service Names" class="controlbutton" title="Retrieve service names"></input> <input type="button" name="scanport" value="Scan Port 80" class="controlbutton" title="Scan port 80"></input> <input type="button" name="whois" value="NS Records" class="controlbutton" title="Retrieve NS records"></input> <input type="button" name="reset" value="Clear Panel" class="controlbutton" title="Clear display panel"></input> </div> </form> </div> </body> </html> // definition for "query_processor.php" file <?php class QueryProcessor{ private $host; private $services=array('http','https','ftp','telnet','imap', 'smtp','nicname','gopher','finger','pop3','www'); private $ports=array(21,23,25,43,70,79,80,110,143,443); private $validRecTypes=array('A','MX','NS','SOA','PTR', 'CNAME','AAAA','A6','SRV','NAPTR','ANY'); public function __construct($host='myhost.com'){ $this->host=$host; } // get IP address public function getIp(){ if(!$ip=gethostbyname($this->host)){ return 'Error resolving host IP address.'; } return $ip; } // get list of IP addresses public function getIpList(){ if(!$ips=implode(' - ',gethostbynamel($this->host))){ return 'Error getting list of IP addresses for the provided hostname.'; } return $ips; } // get host name public function getHost(){ if(!$host=gethostbyaddr($this->getIp())){ return 'Error resolving host name.'; } return $host; } // get TCP ports of Internet services public function getServicePorts(){ $output='Retrieving services ports...Please wait.<br />'; foreach($this->services as $service){ if(!$port=getservbyname($service,'tcp')){ $output.='Error retrieving port of service '.$service.'<br />'; } else{ $output.='Service '.$service. ' runs on TCP port: '. $port.'<br />'; } } return $output; } // get Services by TCP ports public function getServiceNames(){ $output='Retrieving services names...Please wait.<br />'; foreach($this->ports as $port){ if(!$service=getservbyport($port,'tcp')){ $output.='Error retrieving service name on port '.$port.'<br />'; } else{ $output.='TCP Port '.$port. ' is used by service: '. $service.'<br />'; } } return $output; } // execute 'ipconfig' command on Windows systems public function IpConfig(){ $output='Running ipconfig command...Please wait.<br />'; exec('ipconfig',$lines); foreach($lines as $line){ $output.=$line.'<br />'; } return $output; } // execute 'ping' command on Windows systems public function Ping(){ $output='Running ping command...Please wait.<br />'; exec('ping '.$this->host,$lines); foreach($lines as $line){ $output.=$line.'<br />'; } return $output; } // execute 'netstat' command on Windows systems public function Netstat(){ $output='Running netstat command...Please wait.<br />'; exec('netstat',$lines); foreach($lines as $line){ $output.=$line.'<br />'; } return $output; } // get MX records on Windows systems public function getMXRecordsWin(){ $output='Retrieving MX Records...please wait.<br />'; exec("nslookup -type=mx $this->host",$mxhosts); foreach($mxhosts as $mxhost){ $output.=$mxhost.'<br />'; } return $output; } // check for DNS records public function checkDNSRecords($recType='MX'){ if(!in_array($recType,$this->validRecTypes)){ return 'Invalid DNS record type.'; } if(!checkdnsrr($this->host,$recType)){ return 'No '.$recType.' records were found.'; } return 'DNS records where found.'; } // check for DNS records on Windows systems public function checkDNSRecordsWin($recType='MX'){ if(!in_array($recType,$this->validRecTypes)){ return 'Invalid DNS record type.'; } exec('nslookup -type='.$recType.' '.$this->host,$result); foreach($result as $line){ if(preg_match("/^$this->host/",$line)){ return 'DNS records were found.'; } } return 'No '.$recType.' records were found.'; } // scan TCP ports public function scanPort($port=80){ if($port<1||$port>65535){ return 'Invalid TCP port number.'; } $output='Scanning port '.$port.'...please wait.<br />'; if(!$fp=@fsockopen($this->host,$port,$errno,$errstr,30)){ return 'Unable to open connection on port '.$port; } fclose($fp); return 'Port '.$port.' is open to connections.'; } // get basic WHOIS info public function getWhoIs(){ $output='Retrieving WHOIS information...please wait.<br />'; if(!$fp=fsockopen("whois.opensrs.net",43,$errno, $errstr,30)){ return 'Error opening socket connection to WHOIS server.'; } sleep(2); fputs($fp,"$this->hostrn"); while(!feof($fp)){ $output.=fread($fp,128).'<br />'; } fclose($fp); return $output; } } $data=get_magic_quotes_gpc()?$_GET['data']:addslashes($_GET['data']); $command=get_magic_quotes_gpc()?$_GET['command']:addslashes($_GET['command']); $queryProc=new QueryProcessor($data); switch($command){ case 'host': echo $queryProc->getIp(); break; case 'ip': echo $queryProc->getHost(); break; case 'iplist': echo $queryProc->getIpList(); break; case 'ping': echo $queryProc->Ping(); break; case 'ipconfig': echo $queryProc->IpConfig(); break; case 'netstat': echo $queryProc->Netstat(); break; case 'mxrecords': echo $queryProc->getMXRecordsWin(); break; case 'serviceports': echo $queryProc->getServicePorts(); break; case 'servicenames': echo $queryProc->getServiceNames(); break; case 'scanport': echo $queryProc->scanPort(); break; case 'nsrecords': echo $queryProc->checkDNSRecordsWin('NS'); break; } ?>
Here you have them. The two files listed above turn this AJAX networking processor into a fully-functional piece of software. I hope you enjoy using it!
Final thoughts
In this three-part series, I walked you through the creation of an extensible networking application that uses AJAX for sending queries in the background. Naturally, you have the liberty of incorporating your own modifications to the program to suit your personal requirements. See you in the next PHP tutorial!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.