2023-04-21 03:21:46 +01:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2023-04-22 18:11:21 +01:00
|
|
|
"strconv"
|
2023-04-21 03:21:46 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-04-24 01:35:23 +01:00
|
|
|
// SplitDomainPort takes an input host and default port then outputs the domain,
|
|
|
|
// port and true or empty values and false if the split failed
|
2023-04-22 18:11:21 +01:00
|
|
|
func SplitDomainPort(host string, defaultPort int) (domain string, port int, ok bool) {
|
2023-04-21 03:21:46 +01:00
|
|
|
a := strings.SplitN(host, ":", 2)
|
|
|
|
switch len(a) {
|
|
|
|
case 2:
|
|
|
|
domain = a[0]
|
2023-04-22 18:11:21 +01:00
|
|
|
p, err := strconv.Atoi(a[1])
|
|
|
|
port = p
|
2023-04-21 03:21:46 +01:00
|
|
|
ok = err == nil
|
|
|
|
case 1:
|
|
|
|
domain = a[0]
|
|
|
|
port = defaultPort
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-24 01:35:23 +01:00
|
|
|
// GetDomainWithoutPort takes an input domain + port and outputs the domain
|
|
|
|
// without the port.
|
|
|
|
//
|
|
|
|
// example.com:443 => example.com
|
2023-06-04 22:28:48 +01:00
|
|
|
func GetDomainWithoutPort(domain string) string {
|
2023-04-24 01:35:23 +01:00
|
|
|
// if a valid index isn't found then return false
|
|
|
|
n := strings.LastIndexByte(domain, ':')
|
|
|
|
if n == -1 {
|
2023-06-04 22:28:48 +01:00
|
|
|
return domain
|
2023-04-21 03:21:46 +01:00
|
|
|
}
|
2023-06-04 22:28:48 +01:00
|
|
|
return domain[:n]
|
2023-04-21 03:21:46 +01:00
|
|
|
}
|
|
|
|
|
2023-04-24 01:35:23 +01:00
|
|
|
// ReplaceSubdomainWithWildcard returns the domain with the subdomain replaced
|
|
|
|
// with a wildcard '*' character.
|
|
|
|
//
|
|
|
|
// www.example.com => *.example.com
|
2023-04-21 03:21:46 +01:00
|
|
|
func ReplaceSubdomainWithWildcard(domain string) (string, bool) {
|
2023-04-24 01:35:23 +01:00
|
|
|
// if a valid index isn't found then return false
|
|
|
|
n := strings.IndexByte(domain, '.')
|
|
|
|
if n == -1 {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
return "*" + domain[n:], true
|
2023-04-21 03:21:46 +01:00
|
|
|
}
|
|
|
|
|
2023-04-24 01:35:23 +01:00
|
|
|
// GetParentDomain returns the parent domain stripping off the subdomain.
|
|
|
|
//
|
|
|
|
// www.example.com => example.com
|
|
|
|
func GetParentDomain(domain string) (string, bool) {
|
|
|
|
// if a valid index isn't found then return false
|
|
|
|
n := strings.IndexByte(domain, '.')
|
|
|
|
if n == -1 {
|
|
|
|
return "", false
|
2023-04-21 03:21:46 +01:00
|
|
|
}
|
2023-04-24 01:35:23 +01:00
|
|
|
return domain[n+1:], true
|
2023-04-21 03:21:46 +01:00
|
|
|
}
|
|
|
|
|
2023-04-24 01:35:23 +01:00
|
|
|
// GetTopFqdn returns the top domain stripping off multiple layers of subdomains.
|
|
|
|
//
|
|
|
|
// hello.world.example.com => example.com
|
2023-04-21 03:21:46 +01:00
|
|
|
func GetTopFqdn(domain string) (string, bool) {
|
2023-04-24 01:35:23 +01:00
|
|
|
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
|
|
|
|
})
|
|
|
|
// if a valid index isn't found then return false
|
|
|
|
if n == -1 {
|
|
|
|
return "", false
|
2023-04-21 03:21:46 +01:00
|
|
|
}
|
2023-04-24 01:35:23 +01:00
|
|
|
return domain[n+1:], true
|
2023-04-21 03:21:46 +01:00
|
|
|
}
|