📜 ⬆️ ⬇️

We make our own service to determine the WHOIS of any domain



The WHOIS service is one of the main tools for people who constantly work with domain names. It is needed as any person who wants to choose a beautiful domain name, and a hosting provider who, among other services, can provide the ability to register a domain. Both those and others are looking for automation of their work.

So let's see how this works.

Each domain zone, be it RU, COM or HOST, has at least one center (whois server) that has information about the domain located in its zone. For zone RU, for example, it is whois.ripn.net and whois.tcinet.ru
')
All whois servers of all domain zones provide information on a strictly unified protocol that listens for connections and requests on port 43.

The very request to the whois server is just sending the domain name of interest to this port, after which we just read the answer.

In order to develop a system for automated obtaining WHOIS information on domains, it is first necessary to get a list of whois servers for all existing domain zones. The correct Google request in the first line gives a link to the project in GitHub, where a full-fledged XML is laid out, containing all the information we need:
https://raw.githubusercontent.com/whois-server-list/whois-server-list/master/whois-server-list.xml 

We save this file to ourselves for the subsequent opening in our application.

If it seems to someone that constantly updating the XML file and parsing its business is not very convenient, then you can use a simpler method - a third-party online service whois-servers.net. Simply glue the root zone name with the tail “.whois-servers.net” and get a ready address to send a request to the WHOIS data (for example, for the COM zone you will get the address “com.whois-servers.net”). This service has no relation to WHOIS; it simply refers to the correct addresses of the working WHOIS servers using its third-level domains.

The example was developed in C # in the usual WinForms: only 2 text fields and 1 button.

To obtain a list of WHOIS servers by domain zone from the downloaded XML file, the following function was written:

 public static List<string> GetWhoisServers(string domainZone){ if (_serverList == null){ _serverList = new XmlDocument(); // XML       _serverList.Load("whois-server-list.xml"); } List<string> result = new List<string>(); //     XML Action<XmlNodeList> find = null; find = new Action<XmlNodeList>((nodes) =>{ foreach (XmlNode node in nodes) if (node.Name == "domain"){ //  XML     if (node.Attributes["name"] != null && node.Attributes["name"].Value.ToLower() == domainZone){ foreach (XmlNode n in node.ChildNodes) //   ,           if (n.Name == "whoisServer"){ XmlAttribute host = n.Attributes["host"]; if (host != null && host.Value.Length > 0 && !result.Contains(host.Value)) result.Add(host.Value); } } find(node.ChildNodes); } }); find(_serverList["domainList"].ChildNodes); return result; } 

The function for obtaining WHOIS information from an already known server looks like this:

 public static string Lookup(string whoisServer, string domainName){ try{ if (string.IsNullOrEmpty(whoisServer) || string.IsNullOrEmpty(domainName)) return null; //Punycode- ( ) Func<string, string> formatDomainName = delegate(string name){ return name.ToLower() //                , //, "."     XN--H1ALFFA9F.XN--P1AI .Any(v => !"abcdefghijklmnopqrstuvdxyz0123456789.-".Contains(v)) ? new IdnMapping().GetAscii(name) ://  Punycode name;//   }; StringBuilder result = new StringBuilder(); result.AppendLine("  " + whoisServer + ": ------------------------------------------"); using (TcpClient tcpClient = new TcpClient()){ //    WHOIS tcpClient.Connect(whoisServer.Trim(), 43); byte[] domainQueryBytes = Encoding.ASCII.GetBytes(formatDomainName(domainName) + "\r\n"); using (Stream stream = tcpClient.GetStream()){ //    WHOIS stream.Write(domainQueryBytes, 0, domainQueryBytes.Length); //    UTF8,           using (StreamReader sr = new StreamReader(tcpClient.GetStream(), Encoding.UTF8)){ string row; while ((row = sr.ReadLine()) != null) result.AppendLine(row); } } } result.AppendLine("---------------------------------------------------------------------\r\n"); return result.ToString(); }catch{} return "      " + whoisServer; } 

The function is able to automatically convert domains in Cyrillic (or any other language), thanks to which the query works perfectly with domains in the classical zones in the Latin alphabet, as well as with any national ones. It is especially nice that in .NET this conversion is implemented by a single line of code using the System.Globalization.IdnMapping class .

These 2 functions created give us everything that is required and it remains only to process the pressing of the “Get data” button on the form.

Having a domain name at the entrance to check the WHOIS, we first need to isolate the zone in which it is located. In view of the fact that the domain can be in the zone of any level (it is not at all necessary that it is always in the second!), I wrote a simple cycle, which for each level, starting from the highest, will check for the existence of WHOIS servers.

 private void get_BTN_Click(object sender, EventArgs e){ List<string> whoisServers = null; //    string[] domainLevels = domainName_TB.Text.Trim().Split('.'); //    WHOIS-          for (int a = 1; a < domainLevels.Length; a++){ /* *      test.some-name.ru.com, *     WHOIS-  some-name.ru.com, *   ru.com      ,   com */ string zone = string.Join(".", domainLevels, a, domainLevels.Length - a); whoisServers = WhoisService.GetWhoisServers(zone); //  WHOIS-,    if (whoisServers.Count > 0) break; } if (whoisServers == null || whoisServers.Count == 0) result_TB.Text = domainName_TB.Text + "\r\n----------------\r\n  "; else{ result_TB.Text = ""; foreach (string whoisServer in whoisServers) result_TB.Text += WhoisService.Lookup(whoisServer, domainName_TB.Text); } } 

Next, we ask the domain information for each of the servers found and write it into the output window.



In order not to collect in parts from the code in the article, the project in finished form uploaded here .

Source: https://habr.com/ru/post/302494/


All Articles