https://raw.githubusercontent.com/whois-server-list/whois-server-list/master/whois-server-list.xml
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; }
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; }
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); } }
Source: https://habr.com/ru/post/302494/
All Articles