PublicSufx

CILicenseVersionHex Docs

PublicSufx is an Elixir library to operate on domain names using the public suffix rules provided by https://publicsuffix.org/:

A “public suffix” is one under which Internet users can (or historically could) directly register names. Some examples of public suffixes are .com, .co.uk and pvt.k12.ma.us. The Public Suffix List is a list of all known public suffixes.

This Elixir library provides a means to get the public suffix from any domain:

iex(1)> PublicSufx.public_suffix("mysite.foo.bar.com")
"com"
iex(2)> PublicSufx.public_suffix("mysite.foo.bar.co.uk")
"co.uk"

… and a way to check if a domain is a public suffix:

iex(1)> PublicSufx.public_suffix?("mysite.foo.bar.co.uk")
false
iex(2)> PublicSufx.public_suffix?("co.uk")
true

The publicsuffix.org data file contains both official ICANN records and private records:

ICANN domains are those delegated by ICANN or part of the IANA root zone database. The authorized registry may express further policies on how they operate the TLD, such as subdivisions within it. Updates to this section can be submitted by anyone, but if they are not an authorized representative of the registry then they will need to back up their claims of error with documentation from the registry’s website.

PRIVATE domains are amendments submitted by the domain holder, as an expression of how they operate their domain security policy. Updates to this section are only accepted from authorized representatives of the domain registrant. This is so we can be certain they know what they are getting into.

Working with Rules

You can also gain access to the prevailing rule for a particular domain:

iex(1)> PublicSufx.prevailing_rule("mysite.foo.bar.com")
"com"
iex(2)> PublicSufx.prevailing_rule("mysite.example")
"*"

Installation

The package can be installed by adding public_sufx to your list of dependencies in mix.exs:

def deps do
  [
    {:public_sufx, "~> 0.6.0"}
  ]
end

Configuration

PublicSufx is bundled with a cached copy of the public suffix list from publicsuffix.org and can be configured to download a fresh copy of the list on compilation by adding this in your config.exs:

config :public_sufx, download_data_on_compile: true

There are pros and cons to both approaches; which you choose will depend on the needs of your project: