From 1194717a32a94630b600c8f5e12d620283534027 Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Sun, 29 Oct 2023 13:15:25 +0000 Subject: [PATCH] Detect no subdomains in GetTopFqdn --- utils/domain-utils.go | 20 +++++++------------- utils/domain-utils_test.go | 6 +++++- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/utils/domain-utils.go b/utils/domain-utils.go index 762a1ee..60a2914 100644 --- a/utils/domain-utils.go +++ b/utils/domain-utils.go @@ -65,23 +65,17 @@ func GetParentDomain(domain string) (string, bool) { // // hello.world.example.com => example.com func GetTopFqdn(domain string) (string, bool) { - var countDot int - n := strings.LastIndexFunc(domain, func(r rune) bool { - // return true if this is the second '.' - // otherwise counts one and continues - if r == '.' { - if countDot == 1 { - return true - } - countDot++ - } - return false - }) + n := strings.LastIndexByte(domain, '.') // if a valid index isn't found then return false if n == -1 { return "", false } - return domain[n+1:], true + // 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 diff --git a/utils/domain-utils_test.go b/utils/domain-utils_test.go index a058d7e..03a4ed8 100644 --- a/utils/domain-utils_test.go +++ b/utils/domain-utils_test.go @@ -52,7 +52,11 @@ func TestGetBaseDomain(t *testing.T) { } func TestGetTopFqdn(t *testing.T) { - domain, ok := GetTopFqdn("www.example.com") + domain, ok := GetTopFqdn("example.com") + assert.True(t, ok, "Output should be true") + assert.Equal(t, "example.com", domain) + + domain, ok = GetTopFqdn("www.example.com") assert.True(t, ok, "Output should be true") assert.Equal(t, "example.com", domain)