Add ExtractDomain to pkg/uri for eTLD+1 extraction
Uses golang.org/x/net/publicsuffix to extract the effective top-level domain plus one label from a URL. Used to populate detected_trackers.initiator_domain for domain-based tracker attribution. Signed-off-by: Émile Ré <emile@getprobo.com> Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
@@ -18,6 +18,9 @@ import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/publicsuffix"
|
||||
)
|
||||
|
||||
// URI is a validated absolute URI (scheme + host required).
|
||||
@@ -71,3 +74,28 @@ func (u *URI) Scan(value any) error {
|
||||
func (u URI) Value() (driver.Value, error) {
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
// ExtractDomain returns the eTLD+1 (effective top-level domain plus one
|
||||
// label) from a raw URL string. For example:
|
||||
//
|
||||
// "https://www.googletagmanager.com/gtag/js" → "googletagmanager.com"
|
||||
// "https://cdn.segment.io/v1/projects" → "segment.io"
|
||||
//
|
||||
// Returns an empty string when the URL cannot be parsed or has no valid
|
||||
// hostname (e.g. data: URIs, bare IP addresses without a public suffix).
|
||||
func ExtractDomain(rawURL string) string {
|
||||
u, err := Parse(rawURL)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
parsed, _ := url.Parse(string(u))
|
||||
hostname := strings.ToLower(parsed.Hostname())
|
||||
|
||||
domain, err := publicsuffix.EffectiveTLDPlusOne(hostname)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return domain
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user