psl

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2023 License: MIT Imports: 6 Imported by: 0

README

psl (Public Suffix List)

This is a Go port of lupomontero/psl.

psl is a JavaScript Go domain name parser based on the Public Suffix List.

This implementation is tested against the test data hosted by Mozilla and kindly provided by Comodo.

What is the Public Suffix List?

The Public Suffix List is a cross-vendor initiative to provide an accurate list of domain name suffixes.

Initially created to meet the needs of browser manufacturers, it's now a community-maintained resource available for any software use. It lists all known public suffixes, which are domain parts where Internet users can directly register names. Examples include ".com", ".co.uk", and "pvt.k12.wy.us".

Source: http://publicsuffix.org

Installation

Go
go get github.com/Piitschy/psl

API

psl.Parse(domain string)

Parse a domain based on the Public Suffix List. Returns a struct with the following properties:

  • TLD: Top level domain (the public suffix).
  • SLD: Second level domain (the first private part of the domain name).
  • Domain: The combination of SLD and TLD.
  • Subdomain: Any optional parts left of the domain.
Example:
package main

import (
    "fmt"
    "github.com/Piitschy/psl"
)

func main() {
    parsed, _ := psl.Parse("www.google.com")
    fmt.Println(parsed.TLD) // 'com'
    fmt.Println(parsed.SLD) // 'google'
    fmt.Println(parsed.Domain) // 'google.com'
    fmt.Println(parsed.Subdomain) // 'www'
}
psl.Get(domain string)

Get the domain name, SLD + TLD. Returns an empty string if not valid.

Example:
package main

import (
    "fmt"
    "github.com/Piitschy/psl"
)

func main() {
    // null input.
    fmt.Println(psl.Get("")) // should print an empty string

    // Mixed case.
    fmt.Println(psl.Get("COM")) // should print an empty string
    fmt.Println(psl.Get("example.COM")) // 'example.com'
    fmt.Println(psl.Get("WwW.example.COM")) // 'example.com'

    // Unlisted TLD.
    fmt.Println(psl.Get("example")) // should print an empty string
    fmt.Println(psl.Get("example.example")) // 'example.example'
    fmt.Println(psl.Get("b.example.example")) // 'example.example'
    fmt.Println(psl.Get("a.b.example.example")) // 'example.example'

    // TLD with only 1 rule.
    fmt.Println(psl.Get("biz")) // should print an empty string
    fmt.Println(psl.Get("domain.biz")) // 'domain.biz'
    fmt.Println(psl.Get("b.domain.biz")) // 'domain.biz'
    fmt.Println(psl.Get("a.b.domain.biz")) // 'domain.biz'

    // TLD with some 2-level rules.
    fmt.Println(psl.Get("uk.com")) // should print an empty string
    fmt.Println(psl.Get("example.uk.com")) // 'example.uk.com'
    fmt.Println(psl.Get("b.example.uk.com")) // 'example.uk.com'

    // More complex TLD.
    fmt.Println(psl.Get("c.kobe.jp")) // should print an empty string
    fmt.Println(psl.Get("b.c.kobe.jp")) // 'b.c.kobe.jp'
    fmt.Println(psl.Get("a.b.c.kobe.jp")) // 'b.c.kobe.jp'
    fmt.Println(psl.Get("city.kobe.jp")) // 'city.kobe.jp'
    fmt.Println(psl.Get("www.city.kobe.jp")) // 'city.kobe.jp'

    // IDN labels.
    fmt.Println(psl.Get("食狮.com.cn")) // '食狮.com.cn'
    fmt.Println(psl.Get("食狮.公司.cn")) // '食狮.公司.cn'
    fmt.Println(psl.Get("www.食狮.公司.cn")) // '食狮.公司.cn'

    // Same as above, but punycoded.
    fmt.Println(psl.Get("xn--85x722f.com.cn")) // 'xn--85x722f.com.cn'
    fmt.Println(psl.Get("xn--85x722f.xn--55qx5d.cn")) // 'xn--85x722f.xn--55qx5d.cn'
    fmt.Println(psl.Get("www.xn--85x722f.xn--55qx5d.cn")) // 'xn--85x722f.xn--55qx5d.cn'
}

psl.IsValid(domain string)

Check whether a domain has a valid Public Suffix. Returns a bool indicating the validity.

Example
package main

import (
    "fmt"
    "github.com/Piitschy/psl"
)

func main() {
    fmt.Println(psl.IsValid("google.com"))       // true
    fmt.Println(psl.IsValid("www.google.com"))   // true
    fmt.Println(psl.IsValid("x.yz"))             // false
}

Testing and Building

Tests are written using Go's built-in testing framework. To run tests:

go test

Feel free to fork if you see possible improvements!

Acknowledgements

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDomainTooShort      = errors.New("Domain name too short")
	ErrDomainTooLong       = errors.New("Domain name too long. It should be no more than 255 chars")
	ErrLabelStartsWithDash = errors.New("Domain label starts with a dash")
	ErrLabelEndsWithDash   = errors.New("Domain label ends with a dash")
	ErrLabelTooLong        = errors.New("Domain name label should be at most 63 chars long")
	ErrLabelTooShort       = errors.New("Domain name label should be at least 1 character long")
	ErrLabelInvalidChar    = errors.New("Domain name label can only contain alphanumeric characters or dashes")
)

Functions

func Get

func Get(domain string) (string, error)

Get domain.

func IsValid

func IsValid(domain string) bool

Check whether domain belongs to a known public suffix.

Types

type Domain

type Domain struct {
	Input     string
	TLD       string
	SLD       string
	Domain    string
	Subdomain string
	Listed    bool
}

func Parse

func Parse(input string) (*Domain, error)

Parse domain.

type Rule

type Rule struct {
	Rule       string
	Suffix     string
	PunySuffix string
	Wildcard   bool
	Exception  bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL