Git Product home page Git Product logo

nager.publicsuffix's Introduction

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

Online test tool

You can try the logic right here publicsuffix test tool

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";

nager.publicsuffix's People

Contributors

fcallejon avatar louislouw avatar nazgaul avatar phil-dobson-fh avatar phildobsonongithub avatar ronnykarlsson avatar tinohager avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.