violet/utils/domain-utils.go

107 lines
2.5 KiB
Go
Raw Permalink Normal View History

2023-04-21 03:21:46 +01:00
package utils
import (
2023-11-03 08:24:17 +00:00
"golang.org/x/net/publicsuffix"
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-11-03 08:24:17 +00:00
out, err := publicsuffix.EffectiveTLDPlusOne(domain)
return out, err == nil
2023-04-21 03:21:46 +01:00
}
// SplitHostPath extracts the host/path from the input
func SplitHostPath(a string) (host, path string) {
// check if source has path
n := strings.IndexByte(a, '/')
if n == -1 {
// set host then path to /
host = a
path = "/"
} else {
// set host then custom path
host = a[:n]
path = a[n:] // this required to keep / at the start of the path
}
return
}
// SplitHostPathQuery extracts the host/path?query from the input
func SplitHostPathQuery(a string) (host, path, query string) {
host, path = SplitHostPath(a)
if path == "/" {
n := strings.IndexByte(host, '?')
if n != -1 {
query = host[n+1:]
host = host[:n]
}
return
}
n := strings.IndexByte(path, '?')
if n != -1 {
query = path[n+1:]
path = path[:n] // reassign happens after
}
return
}