forked from Nixius/authelia
1
0
Fork 0
ATLAS/docker/ss-atlas/internal/validation/domain.go

43 lines
845 B
Go

package validation
import (
"context"
"net"
"net/url"
"strings"
"time"
)
// DomainResolves checks that the given domain or URL has a DNS record (A or AAAA).
// Input can be "git.example.com" or "https://git.example.com".
func DomainResolves(input string, timeout time.Duration) bool {
host := extractHost(input)
if host == "" {
return false
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
var r net.Resolver
addrs, err := r.LookupHost(ctx, host)
return err == nil && len(addrs) > 0
}
func extractHost(input string) string {
s := strings.TrimSpace(strings.ToLower(input))
if s == "" {
return ""
}
if !strings.Contains(s, "//") {
s = "//" + s
}
u, err := url.Parse(s)
if err != nil {
return ""
}
host := u.Hostname()
if host == "" || host == "." {
return ""
}
return host
}