예제 #1
0
        /// <summary>
        /// Attempts to parse the given value into a <see cref="HostName"/>.  Returns
        /// true if successful.
        /// </summary>
        public static bool TryParse(string value, out HostName hostName)
        {
            try
            {
                hostName = new HostName(value);

                return(true);
            }
            catch (ArgumentNullException)
            {
                hostName = null;

                return(false);
            }
            catch (FormatException)
            {
                hostName = null;

                return(false);
            }
        }
예제 #2
0
        private bool SeenServer(HostName whoisServer, int depth)
        {
            // Referral limit
            if (depth > 255)
            {
                return(true);
            }

            // Ignore top level request
            if (depth == 0)
            {
                return(Referrer?.SeenServer(whoisServer, 1) ?? false);
            }


            if (WhoisServer.IsEqualTo(whoisServer))
            {
                return(true);
            }

            return(Referrer != null && Referrer.SeenServer(whoisServer, depth + 1));
        }
예제 #3
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);
        }
예제 #4
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);
        }
예제 #5
0
 /// <summary>
 /// Determines if the given WHOIS server URL has been visited in this lookup chain
 /// </summary>
 internal bool SeenServer(HostName whoisServer)
 {
     return(SeenServer(whoisServer, 0));
 }