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 definition for the QueryProcessor class: coding some additional methods (Page 4 of 5 )
In order to provide the previous "QueryProcessor" class with the capacity for searching DNS records, scanning TCP ports, and so forth, it's necessary obviously to code some additional methods which will perform the aforementioned tasks.
Having said that, here is the complete signature for this class, this time including the extra methods that I mentioned before. Have a look at it, please:
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; } }
In this case, the previous class now presents some additional and useful features, like searching for DNS and MX records, scanning TCP ports, and finding basic WHOIS information on a selected Internet host. Indeed, at this point, the query processor class offers a good level of functionality, doesn't it?
Okay, now that you have seen the complete signature for the prior class, it's time to put all the pieces together, that is the corresponding client and server modules of this networking application, and see how they can work in conjunction. Taking this final step, the application will be completely finished. Don't you feel a bit happier?
To see the full source code that corresponds to this AJAX-driven networking application, click on the link below and keep reading.