Growing collection of network utilities for Go (Golang), such as domain names, IPs and URLs.
To install n5, use go get
:
go get github.com/detectify/n5
The domain
package provides functions for managing domain names, which is represented as a sequence of labels
in the type domain.Name
. Supports various levels of names (up to TLD), internationalized names and names not on the
public suffix list.
To create a domain name, parse the string representation, or extract it from a string containing the domain name, such
as a URL.
import (
"fmt"
"github.com/detectify/n5/domain"
)
func main() {
name, err := domain.Parse("www.example.com")
fmt.Println("apex = " + name.Apex()) // prints "apex = example.com"
fmt.Println("tld = " + name.EffectiveTLD()) // prints "tld = com"
fmt.Println("FQDN = " + name.FQDN()) // prints "tld = www.example.com."
name, err = domain.Extract("https://пример.мкд/index.html")
fmt.Println("domain (punycoded) = " + name.String())
// prints "domain (punycoded) = xn--e1afmkfd.xn--d1alf"
fmt.Println("domain (original) = " + name.Unicode())
// prints "domain (original) = пример.мкд"
}
- Domain name validation is based on the domain name definition specified in RFC 1034,
following the recommended domain name syntax (reaffirmed
in RFC 2181), which is matching the host name
definition in RFC 952, extended in
RFC 1123. Exception is allowing usage of '_' in labels as
there are various such names in existence. The name validation is taken from a
Gist by chmike
with added support for
_
character. - For checking against the public suffix list the github.com/weppos/publicsuffix-go package is used with the default list.
The http
package provides a function for validating the HTTP method (http.ValidateMethod
).
The ip
package provides functions for validating IP v4/v6 addresses, including cross-checking against reserved IPs.
Separate functions are available for all, v4 and v6 IPs, for example ip.IsIP
, ip.IsIPv4
, ip.IsIPv6
.
The url
package provides functions for validating absolute URLs (url.IsAbsolute
) and extracting hostname from URL (url.Host
).
- Host extraction aims to support non-standard URL formats for which the
url.URL
type returns error.
Please feel free to submit issues, fork the repository and send pull requests. In addition to fixes, new features are also welcome if you feel they are within the scope of the package. Feel free to reach out and discuss if you have any questions.
This project is licensed under the terms of the MIT license.