예제 #1
0
        public async Task TestLookupDomain()
        {
            var request = new WhoisRequest("google.com");

            var rootServer = new WhoisResponse
            {
                DomainName = new HostName("com"),
                Registrar  = new Registrar {
                    WhoisServer = new HostName("whois.markmonitor.com")
                }
            };

            whoisServerLookup
            .Setup(call => call.LookupAsync(request))
            .Returns(Task.FromResult(rootServer));

            tcpReader
            .Setup(call => call.Read("whois.markmonitor.com", 43, "google.com", Encoding.UTF8, 10))
            .Returns(Task.FromResult(sampleReader.Read("whois.markmonitor.com", "com", "found.txt")));

            var result = await lookup.LookupAsync(request);

            Assert.AreEqual("google.com", result.DomainName.ToString());
            Assert.AreEqual(WhoisStatus.Found, result.Status);
        }
예제 #2
0
        public async Task TestLookupDomainDontFollowReferrer()
        {
            var request = new WhoisRequest {
                Query = "google.com", FollowReferrer = false
            };
            var intermediateResult = sampleReader.Read("whois.verisign-grs.com", "com", "found_status_registered.txt");

            var rootServer = new WhoisResponse
            {
                DomainName = new HostName("com"),
                Registrar  = new Registrar {
                    WhoisServer = new HostName("whois.verisign-grs.com")
                }
            };

            whoisServerLookup
            .Setup(call => call.LookupAsync(request))
            .Returns(Task.FromResult(rootServer));

            tcpReader
            .Setup(call => call.Read("whois.verisign-grs.com", 43, "google.com", Encoding.UTF8, 10))
            .Returns(Task.FromResult(intermediateResult));

            var result = await lookup.LookupAsync(request);

            Assert.AreEqual("google.com", result.DomainName.ToString());
            Assert.AreEqual(WhoisStatus.Found, result.Status);

            Assert.AreEqual(intermediateResult, result.Content);
            Assert.AreEqual(rootServer, result.Referrer);
        }
예제 #3
0
        private async Task <string> Download(string url, WhoisRequest request)
        {
            // TODO: Expose this & extend for other TLDs
            var query = request.Query;

            if (query.EndsWith("jp"))
            {
                query += "/e";                          // Return English .jp results
            }
            var content = await TcpReader.Read(url, 43, query, request.Encoding, request.TimeoutSeconds);

            Log.Debug("Lookup {0}: Downloaded {1:###,###,##0} byte(s) from {2}.", request.Query, content.Length, url);

            return(content);
        }
예제 #4
0
        public async Task TestLookupDomainUseBestResponse()
        {
            // Setup our initial request
            var request = new WhoisRequest {
                Query = "fark.co", FollowReferrer = true
            };

            // Setup the inital root server response
            var rootServer = new WhoisResponse
            {
                DomainName = new HostName("co"),
                Registrar  = new Registrar {
                    WhoisServer = new HostName("whois.nic.co")
                }
            };

            whoisServerLookup
            .Setup(call => call.LookupAsync(request))
            .Returns(Task.FromResult(rootServer));

            // Setup the intermediate server response
            var intermediateResult = sampleReader.Read("whois.nic.co", "co", "fark.co.txt");

            tcpReader
            .Setup(call => call.Read("whois.nic.co", 43, "fark.co", Encoding.UTF8, 10))
            .Returns(Task.FromResult(intermediateResult));

            // Setup the authoritative server response
            // Note: this contains less data than the intermediate response, so should be ignored
            var authoritativeResult = sampleReader.Read("whois.dynadot.com", "co", "fark.co.txt");

            tcpReader
            .Setup(call => call.Read("whois.dynadot.com", 43, "fark.co", Encoding.UTF8, 10))
            .Returns(Task.FromResult(authoritativeResult));

            var result = await lookup.LookupAsync(request);

            Assert.AreEqual("fark.co", result.DomainName.ToString());
            Assert.AreEqual(WhoisStatus.Found, result.Status);

            Assert.AreEqual(intermediateResult, result.Content);
            Assert.AreEqual(rootServer, result.Referrer);
        }
예제 #5
0
        public async Task TestLookupTld()
        {
            var request = new WhoisRequest(".com");

            var rootServer = new WhoisResponse
            {
                DomainName = new HostName("com"),
                Registrar  = new Registrar {
                    WhoisServer = new HostName("whois.markmonitor.com")
                }
            };

            whoisServerLookup
            .Setup(call => call.LookupAsync(request))
            .Returns(Task.FromResult(rootServer));

            var result = await lookup.LookupAsync(request);

            Assert.AreEqual(rootServer, result);
        }
예제 #6
0
        public async Task TestLookupDomainSpecifyRootServer()
        {
            var request = new WhoisRequest {
                Query = "google.com", WhoisServer = "whois.markmonitor.com"
            };
            var authoritativeResult = sampleReader.Read("whois.markmonitor.com", "com", "found.txt");

            tcpReader
            .Setup(call => call.Read("whois.markmonitor.com", 43, "google.com", Encoding.UTF8, 10))
            .Returns(Task.FromResult(authoritativeResult));

            var result = await lookup.LookupAsync(request);

            Assert.AreEqual("google.com", result.DomainName.ToString());
            Assert.AreEqual(WhoisStatus.Found, result.Status);

            Assert.AreEqual(authoritativeResult, result.Content);
            Assert.AreEqual("whois.markmonitor.com", result.Referrer.WhoisServer.Value);

            whoisServerLookup
            .Verify(call => call.LookupAsync(request), Times.Never());
        }
예제 #7
0
 /// <summary>
 /// Performs a WHOIS lookup for the given request.
 /// </summary>
 public WhoisResponse Lookup(WhoisRequest request)
 {
     return(AsyncHelper.RunSync(() => LookupAsync(request)));
 }
예제 #8
0
        /// <summary>
        /// Performs a WHOIS lookup for the given request.
        /// </summary>
        public async Task <WhoisResponse> LookupAsync(WhoisRequest request)
        {
            if (string.IsNullOrEmpty(request.Query))
            {
                throw new ArgumentNullException("domain");
            }

            // Trim leading '.'
            if (request.Query.StartsWith("."))
            {
                request.Query = request.Query.Substring(1);
            }

            // Validate domain name
            if (HostName.TryParse(request.Query, out var hostName) == false)
            {
                throw new WhoisException($"WHOIS Query Format Error: {request.Query}");
            }

            Log.Debug("Looking up WHOIS response for: {0}", hostName.Value);

            // Set our starting point
            WhoisResponse response;

            if (string.IsNullOrEmpty(request.WhoisServerUrl))
            {
                // Lookup root WHOIS server for the TLD
                response = await ServerLookup.LookupAsync(request);
            }
            else
            {
                // Use the given WHOIS server
                response = WhoisResponse.WithServerUrl(request.WhoisServerUrl);
            }

            // Main loop: download & parse WHOIS data and follow the referrer chain
            var whoisServerUrl = response?.WhoisServerUrl;

            while (string.IsNullOrEmpty(whoisServerUrl) == false && !hostName.IsTld)
            {
                // Download
                var content = await Download(whoisServerUrl, request);

                // Parse result
                var parsed = whoisParser.Parse(whoisServerUrl, content);

                // Build referrer chain
                response = response.Chain(parsed);

                // Check for referral loop
                if (request.FollowReferrer == false)
                {
                    break;
                }
                if (response.SeenServer(response.WhoisServerUrl))
                {
                    break;
                }

                // Lookup result in referral server
                whoisServerUrl = response.WhoisServerUrl;
            }

            return(response);
        }
예제 #9
0
        /// <summary>
        /// Performs a WHOIS lookup for the given request.
        /// </summary>
        public async Task <WhoisResponse> LookupAsync(WhoisRequest request)
        {
            if (string.IsNullOrEmpty(request.Query))
            {
                throw new ArgumentNullException($"{nameof(request)}.{nameof(request.Query)}");
            }

            // Trim leading '.'
            if (request.Query.StartsWith("."))
            {
                request.Query = request.Query.Substring(1);
            }

            // Validate domain name
            if (HostName.TryParse(request.Query, out var hostName) == false)
            {
                throw new WhoisException($"WHOIS Query Format Error: {request.Query}");
            }

            Log.Debug("Looking up WHOIS response for: {0}", hostName.Value);

            // Set our starting point
            WhoisResponse response;

            if (string.IsNullOrEmpty(request.WhoisServer))
            {
                // Lookup root WHOIS server for the TLD
                response = await ServerLookup.LookupAsync(request);
            }
            else
            {
                // Use the given WHOIS server
                response = WhoisResponse.WithServerUrl(request.WhoisServer);
            }

            // If query is for a top level domain, we're finished
            if (hostName.IsTld)
            {
                return(response);
            }

            // Main loop: download & parse WHOIS data and follow the referrer chain
            var whoisServer = response?.WhoisServer;

            while (whoisServer != null)
            {
                // Download
                var content = await Download(whoisServer.Value, request);

                // Parse result
                var parsed = Parser.Parse(whoisServer.Value, content);

                // Sanity check: ensure the last response has some data
                if (parsed.FieldsParsed == 0 && response.FieldsParsed > 0)
                {
                    break;
                }

                // Build referrer chain
                response = response.Chain(parsed);

                // Check for referral loop
                if (request.FollowReferrer == false)
                {
                    break;
                }
                if (response.SeenServer(response.WhoisServer))
                {
                    break;
                }

                // Lookup result in referral server
                whoisServer = response.WhoisServer;
            }

            return(response);
        }