Recently in the operation of the crawler, the content of today's study is about the production of DNS resolution module. Using the library for ARSoft.Tools.Net, it is a very powerful library of open source DNS controls, including. Net SPF validation, SenderID validation, and DNS Client, DNS server interface. This interface makes it easy to implement DNS client-side and server-side resolution. Project Address: Http://arsofttoolsnet.codeplex.com/,Nuget Package Address: https://www.nuget.org/packages/ARSoft.Tools.Net/.
First, the NuGet package is introduced:
Install-package ARSoft.Tools.Net
Here's how it starts:
////// DNS parsing /////DNS server IP///Parsing Time-out///Parsing URLs///Whether the resolution was successful///
IP information resolved to
public static IPAddress Dnsresolver (string dnsserver, int timeOut, string URL, out bool issuccess) {//Initialize dnsclient, first parameter The number is the IP of the DNS server, the second parameter is the time-out var dnsclient = new Dnsclient (Ipaddress.parse (dnsserver), timeout); Resolves the domain name. The domain request is sent to the DNS server for resolution, the first parameter is the domain name that needs to be resolved, the second parameter is//resolution type, RECORDTYPE.A is IPV4 type//dnsmessage dnsmessage = Dnsclient.resolve ("Www.s Ina.com ", recordtype.a); var s = new Stopwatch (); S.start (); var dnsmessage = dnsclient.resolve (domainname.parse (URL)); S.stop (); Console.WriteLine (S.elapsed.milliseconds); If the returned result is empty, or if there is an error, the request fails. if (Dnsmessage = = NULL | | (Dnsmessage.returncode! = Returncode.noerror && Dnsmessage.returncode! = returncode.nxdomain)) {issuccess= false; }//Loops through the returned results and adds the returned IPV4 records to the result set list. if (dnsmessage! = null) foreach (var dnsrecord in dnsmessage.answerrecords) {var Arecord = Dnsre Cord as Arecord; if (Arecord = = null) continue; Issuccess = true; Return arecord.address; } issuccess= false; return null;}
Call Method:
BOOL Issuccess;ipaddress IP = dnsresolver ("223.5.5.5", $, "shaoweicloud.cn", out issuccess); if (issuccess) Console.WriteLine (IP);
Understand the use of the method after we can do it further encapsulation, get Dnsresolver class:
Using system;using system.collections.generic;using system.diagnostics;using system.net;using ARSoft.Tools.Net; Using Arsoft.tools.net.dns;namespace crawler.protocol{public class Dnsresolver {public TimeSpan TimeSpan { Get Set public string Url {get; set;} Public List Record {get; set;} public string DNSServer {get; set;} public int TimeOut {get; set;} Public ReturnCode ReturnCode {get; set;} public bool Issuccess {get; private set;} Public dnsresolver (string url, string dnsserver = "223.5.5.5", int timeOut = $) {url = URL; DNSServer = DNSServer; Timeout = timeout; Record=new List (); Dig (); } public void Dig () {//Initialize dnsclient, first parameter is IP for DNS server, second parameter is time-out var dnsclient = new Dn Sclient (Ipaddress.parse (dnsserver), TimeOut); var s = new Stopwatch (); S.start (); Resolves the domain name. Send a domain requestTo DNS server resolution, the parameter is the domain name that needs to be parsed var dnsmessage = Dnsclient.resolve (Domainname.parse (URL)); S.stop (); TimeSpan = s.elapsed; If the returned result is empty, or if there is an error, the request fails. if (Dnsmessage = = NULL | | (Dnsmessage.returncode! = Returncode.noerror && Dnsmessage.returncode! = returncode.nxdomain)) Issuccess = false; Loops through the returned results, adding the returned IPV4 records to the result set list. if (dnsmessage! = null) foreach (var dnsrecord in dnsmessage.answerrecords) { var Arecord = Dnsrecord as Arecord; if (Arecord = = null) continue; Issuccess = true; Record.add (Arecord); } if (dnsmessage! = null) ReturnCode = Dnsmessage.returncode; } }}
Call Method:
Dnsresolver DNS = new Dnsresolver ("shaoweicloud.cn"), if (DNS. issuccess) Console.WriteLine (DNS. Record[0]);
At this point, the DNS resolution module is basically finished, as to why the title of the semi-finished, because I want to base on the basis of DNS resolution to the DNS information based on the TTL to do a set of information caching mechanism, reduce unnecessary duplication of queries, is still considering the method used, the subsequent implementation will be updated.
[Crawler Learning Notes] C # ARSoft.Tools.Net-based DNS resolution module (semi-finished)