Skip to content

Commit

Permalink
Add support for TLD WHOIS lookups
Browse files Browse the repository at this point in the history
- Add query support for ".{tld}" format lookups to just download top
  level TLD data from IANA.  E.g. ".com"
  • Loading branch information
flipbit committed Oct 20, 2019
1 parent 0affff4 commit c516dcf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
20 changes: 20 additions & 0 deletions Whois.Tests/WhoisLookupTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ public async Task TestLookupDomainSpecifyRootServer()
.Verify(call => call.LookupAsync(request), Times.Never());
}

[Test]
public async Task TestLookupTld()
{
var request = new WhoisRequest(".com");

var rootServer = new WhoisResponse
{
DomainName = "com",
Registrar = new Registrar { WhoisServerUrl = "whois.markmonitor.com" }
};

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

var result = await lookup.LookupAsync(request);

Assert.AreEqual(rootServer, result);
}

[Test]
public void TestLookupDomainWithEmptyQuery()
{
Expand Down
9 changes: 8 additions & 1 deletion Whois/Servers/IanaServerLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,17 @@ public async Task<WhoisResponse> LookupAsync(WhoisRequest request)
{
var match = result.BestMatch.Value;

match.Content = content;

return match;
}

return new WhoisResponse { DomainName = tld, Status = WhoisStatus.Unknown };
return new WhoisResponse
{
Content = content,
DomainName = tld,
Status = WhoisStatus.Unknown
};
}

private async Task<string> Download(string tld, WhoisRequest request)
Expand Down
26 changes: 24 additions & 2 deletions Whois/WhoisLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Tokens.Extensions;
using Whois.Logging;
using Whois.Net;
using Whois.Parsers;
Expand Down Expand Up @@ -107,7 +108,13 @@ public async Task<WhoisResponse> LookupAsync(WhoisRequest request)
throw new ArgumentNullException("domain");
}

if (IsValidDomainName(request.Query) == false)
var isTldQuery = false;
if (IsTldQuery(request.Query))
{
isTldQuery = true;
request.Query = request.Query.SubstringAfterString(".");
}
else if (IsValidDomainName(request.Query) == false)
{
throw new WhoisException($"Domain Name is invalid: {request.Query}");
}
Expand All @@ -129,7 +136,7 @@ public async Task<WhoisResponse> LookupAsync(WhoisRequest request)

// Main loop: download & parse WHOIS data and follow the referrer chain
var whoisServerUrl = response?.WhoisServerUrl;
while (string.IsNullOrEmpty(whoisServerUrl) == false)
while (string.IsNullOrEmpty(whoisServerUrl) == false && !isTldQuery)
{
// Download
var content = await Download(whoisServerUrl, request);
Expand Down Expand Up @@ -165,6 +172,21 @@ public bool IsValidDomainName(string domain)
return valid;
}

internal bool IsTldQuery(string query)
{
var isTldQuery = false;

if (!string.IsNullOrEmpty(query))
{
var regex = new Regex(@"^\.([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)");

isTldQuery = regex.Match(query).Success;
}


return isTldQuery;
}

private async Task<string> Download(string url, WhoisRequest request)
{
// TODO: Expose this & extend for other TLDs
Expand Down

0 comments on commit c516dcf

Please sign in to comment.