-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
70 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,87 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using Nager.PublicSuffix; | ||
using Nager.PublicSuffix.RuleProviders; | ||
|
||
#region CachedHttpRuleProvider | ||
Console.WriteLine("Run - DemoCachedHttpRuleProviderAsync"); | ||
await DemoCachedHttpRuleProviderAsync(); | ||
Console.WriteLine("Run - DemoSimpleHttpRuleProviderAsync"); | ||
await DemoSimpleHttpRuleProviderAsync(); | ||
Console.WriteLine("Run - DemoLocalFileRuleProviderAsync"); | ||
await DemoLocalFileRuleProviderAsync(); | ||
|
||
//using var loggerFactory = LoggerFactory.Create(builder => | ||
//{ | ||
// builder.AddConsole(); | ||
//}); | ||
|
||
//var ruleProviderLogger = loggerFactory.CreateLogger<CachedHttpRuleProvider>(); | ||
|
||
//IConfiguration configuration = new ConfigurationBuilder() | ||
// .AddInMemoryCollection(new List<KeyValuePair<string, string?>> | ||
// { | ||
// new("Nager:PublicSuffix:DataUrl", "https://publicsuffix.org/list/public_suffix_list1.dat") | ||
// }) | ||
// .Build(); | ||
|
||
//var httpClient = new HttpClient(); | ||
//var cacheProvider = new Nager.PublicSuffix.RuleProviders.CacheProviders.LocalFileSystemCacheProvider(); | ||
|
||
//var ruleProvider = new CachedHttpRuleProvider(ruleProviderLogger, configuration, cacheProvider, httpClient); | ||
|
||
#endregion | ||
|
||
#region SimpleHttpRuleProvider | ||
|
||
var ruleProvider = new SimpleHttpRuleProvider(); | ||
|
||
#endregion | ||
async Task DemoCachedHttpRuleProviderAsync() | ||
{ | ||
using var loggerFactory = LoggerFactory.Create(builder => | ||
{ | ||
builder.AddConsole(); | ||
}); | ||
|
||
#region LocalFileRuleProvider | ||
var ruleProviderLogger = loggerFactory.CreateLogger<CachedHttpRuleProvider>(); | ||
|
||
//var ruleProvider = new LocalFileRuleProvider("public_suffix_list.dat"); | ||
IConfiguration configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(new List<KeyValuePair<string, string?>> | ||
{ | ||
new("Nager:PublicSuffix:DataUrl", "https://publicsuffix.org/list/public_suffix_list.dat") | ||
}) | ||
.Build(); | ||
|
||
#endregion | ||
using var httpClient = new HttpClient(); | ||
var cacheProvider = new Nager.PublicSuffix.RuleProviders.CacheProviders.LocalFileSystemCacheProvider(); | ||
|
||
await ruleProvider.BuildAsync(ignoreCache: true); | ||
var ruleProvider = new CachedHttpRuleProvider(ruleProviderLogger, configuration, cacheProvider, httpClient); | ||
|
||
var domainParser = new DomainParser(ruleProvider); | ||
var domainInfo = domainParser.Parse("www.google.com"); | ||
await CheckAsync(ruleProvider); | ||
} | ||
|
||
if (domainInfo != null) | ||
async Task DemoSimpleHttpRuleProviderAsync() | ||
{ | ||
Console.WriteLine("------------------------------------------------"); | ||
Console.WriteLine($"{"TLD:", 20} {domainInfo.TopLevelDomain}"); | ||
Console.WriteLine($"{"FQDN:", 20} { domainInfo.FullyQualifiedDomainName}"); | ||
Console.WriteLine($"{"RegistrableDomain:", 20} {domainInfo.RegistrableDomain}"); | ||
Console.WriteLine($"{"Subdomain:",20} {domainInfo.Subdomain}"); | ||
Console.WriteLine("------------------------------------------------"); | ||
} | ||
using var ruleProvider = new SimpleHttpRuleProvider(); | ||
|
||
var validDomains = new[] { "www.google.com", "amazon.com", "microsoft.de", "mail.google.com", "microsoft.de." }; | ||
var invalidDomains = new[] { "www", "uk", "co.uk", ".", "[email protected]" }; | ||
await CheckAsync(ruleProvider); | ||
} | ||
|
||
foreach (var validDomain in validDomains) | ||
async Task DemoLocalFileRuleProviderAsync() | ||
{ | ||
Console.WriteLine($"{validDomain} -> {domainParser.IsValidDomain(validDomain)}"); | ||
var ruleProvider = new LocalFileRuleProvider("public_suffix_list.dat"); | ||
|
||
await CheckAsync(ruleProvider); | ||
} | ||
|
||
foreach (var invalidDomain in invalidDomains) | ||
async Task CheckAsync(IRuleProvider ruleProvider) | ||
{ | ||
Console.WriteLine($"{invalidDomain} -> {domainParser.IsValidDomain(invalidDomain)}"); | ||
} | ||
if (!await ruleProvider.BuildAsync(ignoreCache: true)) | ||
{ | ||
Console.BackgroundColor = ConsoleColor.Red; | ||
Console.ForegroundColor = ConsoleColor.White; | ||
Console.WriteLine("Build error"); | ||
Console.ResetColor(); | ||
return; | ||
} | ||
|
||
var domainParser = new DomainParser(ruleProvider); | ||
var domainInfo = domainParser.Parse("www.google.com"); | ||
|
||
if (domainInfo != null) | ||
{ | ||
Console.WriteLine("------------------------------------------------"); | ||
Console.WriteLine($"{"TLD:",20} {domainInfo.TopLevelDomain}"); | ||
Console.WriteLine($"{"FQDN:",20} {domainInfo.FullyQualifiedDomainName}"); | ||
Console.WriteLine($"{"RegistrableDomain:",20} {domainInfo.RegistrableDomain}"); | ||
Console.WriteLine($"{"Subdomain:",20} {domainInfo.Subdomain}"); | ||
Console.WriteLine("------------------------------------------------"); | ||
} | ||
|
||
var validDomains = new[] { "www.google.com", "amazon.com", "microsoft.de", "mail.google.com", "microsoft.de." }; | ||
var invalidDomains = new[] { "www", "uk", "co.uk", ".", "[email protected]" }; | ||
|
||
foreach (var validDomain in validDomains) | ||
{ | ||
Console.WriteLine($"{validDomain} -> {domainParser.IsValidDomain(validDomain)}"); | ||
} | ||
|
||
foreach (var invalidDomain in invalidDomains) | ||
{ | ||
Console.WriteLine($"{invalidDomain} -> {domainParser.IsValidDomain(invalidDomain)}"); | ||
} | ||
} |