forked from cerc-io/ipld-eth-server
36533f7c3f
Fixes for new geth version
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package isdomain
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
//go:generate bash regenerate-tlds.sh
|
|
|
|
// IsICANNTLD returns whether the given string is a TLD (Top Level Domain),
|
|
// according to ICANN. Well, really according to the TLDs listed in this
|
|
// package.
|
|
func IsICANNTLD(s string) bool {
|
|
s = strings.ToUpper(s)
|
|
_, found := TLDs[s]
|
|
return found
|
|
}
|
|
|
|
// IsExtendedTLD returns whether the given string is a TLD (Top Level Domain),
|
|
// extended with a few other "TLDs": .bit, .onion
|
|
func IsExtendedTLD(s string) bool {
|
|
s = strings.ToUpper(s)
|
|
_, found := ExtendedTLDs[s]
|
|
return found
|
|
}
|
|
|
|
// IsTLD returns whether the given string is a TLD (according to ICANN, or
|
|
// in the set of ExtendedTLDs listed in this package.
|
|
func IsTLD(s string) bool {
|
|
return IsICANNTLD(s) || IsExtendedTLD(s)
|
|
}
|
|
|
|
// IsDomain returns whether given string is a domain.
|
|
// It first checks the TLD, and then uses a regular expression.
|
|
func IsDomain(s string) bool {
|
|
if strings.HasSuffix(s, ".") {
|
|
s = s[:len(s)-1]
|
|
}
|
|
split := strings.Split(s, ".")
|
|
|
|
// Need a TLD and a domain.
|
|
if len(split) < 2 {
|
|
return false
|
|
}
|
|
|
|
// Check the TLD
|
|
tld := split[len(split)-1]
|
|
if !IsTLD(tld) {
|
|
return false
|
|
}
|
|
|
|
// Check the domain.
|
|
s = strings.ToLower(s)
|
|
return domainRegexp.MatchString(s)
|
|
}
|