• Stars
    star
    145
  • Rank 247,446 (Top 5 %)
  • Language
    C#
  • License
    MIT License
  • Created over 7 years ago
  • Updated 10 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

.NET public suffix domain parser

Nager.PublicSuffix

The TLD proliferation makes it difficult to check whether domain names are valid. This project uses the rules of publicsuffix.org, a list of known public domain suffixes (TLD) to validate and split domains into three the parts (TLD, domain, subdomain). The validation rules are loaded directly from https://publicsuffix.org.

A domain name has 3 major parts:

Example TLD Domain Subdomain
blog.google.com com google blog
www.wikipedia.org org wikipedia www
mail.yandex.ru ru yandex mail
www.amazon.co.uk co.uk amazon www

nuget

The package is available on nuget

PM> install-package Nager.PublicSuffix

Benefits

  • High performance
  • FileTldRuleProvider or WebTldRuleProvider
  • CacheProvider
  • Async support

Examples

Analyze domain

Without a custom config the WebTldRuleProvider has a default cache live time of 1 day, then you must refresh the cache with execute BuildAsync;

var domainParser = new DomainParser(new WebTldRuleProvider());

var domainInfo = domainParser.Parse("sub.test.co.uk");
//domainInfo.Domain = "test";
//domainInfo.Hostname = "sub.test.co.uk";
//domainInfo.RegistrableDomain = "test.co.uk";
//domainInfo.SubDomain = "sub";
//domainInfo.TLD = "co.uk";

Check is a valid domain

Without a custom config the WebTldRuleProvider has a default cache live time of 1 day, then you must refresh the cache with execute BuildAsync;

var domainParser = new DomainParser(new WebTldRuleProvider());

var isValid = domainParser.IsValidDomain("sub.test.co.uk");

Change the default cache time

//cache data for 10 hours
var cacheProvider = new FileCacheProvider(cacheTimeToLive: new TimeSpan(10, 0, 0));
var webTldRuleProvider = new WebTldRuleProvider(cacheProvider: cacheProvider);

var domainParser = new DomainParser(webTldRuleProvider);
for (var i = 0; i < 100; i++)
{
    var isValid = webTldRuleProvider.CacheProvider.IsCacheValid();
    if (!isValid)
    {
        webTldRuleProvider.BuildAsync().GetAwaiter().GetResult(); //Reload data
    }
	
    var domainInfo = domainParser.Parse($"sub{i}.test.co.uk");
}

Use a local publicsuffix data file

var domainParser = new DomainParser(new FileTldRuleProvider("effective_tld_names.dat"));

var domainInfo = domainParser.Parse("sub.test.co.uk");
//domainInfo.Domain = "test";
//domainInfo.Hostname = "sub.test.co.uk";
//domainInfo.RegistrableDomain = "test.co.uk";
//domainInfo.SubDomain = "sub";
//domainInfo.TLD = "co.uk";

More Repositories

1

Nager.Date

Worldwide holidays (REST API), NuGet or docker container 🌎
C#
1,051
star
2

Nager.AmazonProductAdvertising

.NET Amazon Product Advertising Client
C#
159
star
3

Nager.Country

Worldwide Country Informations (ISO-3166-1 Alpha2, ISO-3166-1 Alpha3, ISO 639-1)
C#
101
star
4

Nager.VideoStream

Get images from a network camera stream or webcam
C#
47
star
5

Nager.ArticleNumber

C# Validate Article Numbers ASIN, EAN8, EAN13, GTIN, ISBN, ISBN13, SKU, UPC
C#
31
star
6

Nager.TcpClient

A simple asynchronous TcpClient
C#
15
star
7

Nager.TemplateBuilder

Visual Studio Solution Builder
C#
10
star
8

Nager.FirewallManagement

Manage the default Windows Firewall over an WebAPI
C#
5
star
9

Nager.Holiday

Holiday Client for date.nager.at
C#
4
star
10

Nager.UdpDeviceDiscovery

Udp Broadcast device discovery
C#
3
star
11

Nager.TextValidation

Validate text via keyboard keys position
C#
2
star
12

Nager.CertificateManagement

Certificates management for Lets Encrypt
C#
2
star
13

Nager.ArduinoStepperMotor

.NET control a stepper motor via arduino
C++
2
star
14

Nager.AmazonSesNotification

Amazon Simple Email Service Notification Processing
C#
2
star
15

Nager.WindowsCalendarWeek

Show CalendarWeek in SystemTray of Microsoft Windows
C#
2
star
16

Nager.DataFragmentationHandler

Process fragmented bytes via a buffer to packages
C#
1
star
17

Nager.ConfigParser

.NET key value configuration parser
C#
1
star
18

Nager.BedLight

bed light with motion detector
C++
1
star
19

Nager.BathControl

BathControl is a smart fan control with humidity sensor and a ESP8266
Lua
1
star
20

Nager.Authentication

asp.net webapi headless authentication
C#
1
star