Replace bodged GetTopFqdn with eTLD+1

This commit is contained in:
Melon 2023-11-03 08:24:17 +00:00
parent 37b0617e78
commit fc2f3d5b7b
Signed by: melon
GPG Key ID: 6C9D970C50D26A25

View File

@ -1,6 +1,7 @@
package utils package utils
import ( import (
"golang.org/x/net/publicsuffix"
"strconv" "strconv"
"strings" "strings"
) )
@ -65,17 +66,8 @@ func GetParentDomain(domain string) (string, bool) {
// //
// hello.world.example.com => example.com // hello.world.example.com => example.com
func GetTopFqdn(domain string) (string, bool) { func GetTopFqdn(domain string) (string, bool) {
n := strings.LastIndexByte(domain, '.') out, err := publicsuffix.EffectiveTLDPlusOne(domain)
// if a valid index isn't found then return false return out, err == nil
if n == -1 {
return "", false
}
// if a valid index isn't found then this is the fqdn
n2 := strings.LastIndexByte(domain[:n], '.')
if n2 == -1 {
return domain, true
}
return domain[n2+1:], true
} }
// SplitHostPath extracts the host/path from the input // SplitHostPath extracts the host/path from the input