-
Notifications
You must be signed in to change notification settings - Fork 0
/
fqdn.go
71 lines (55 loc) · 1.45 KB
/
fqdn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package checker
import (
"reflect"
"regexp"
"strings"
)
// CheckerFqdn is the name of the checker.
const CheckerFqdn = "fqdn"
// ResultNotFqdn indicates that the given string is not a valid FQDN.
const ResultNotFqdn = "NOT_FQDN"
// Valid characters excluding full-width characters.
var fqdnValidChars = regexp.MustCompile("^[a-z0-9\u00a1-\uff00\uff06-\uffff\\-]+$")
// IsFqdn checks if the given string is a fully qualified domain name.
func IsFqdn(domain string) Result {
parts := strings.Split(domain, ".")
// Require TLD
if len(parts) < 2 {
return ResultNotFqdn
}
tld := parts[len(parts)-1]
// Should be all numeric TLD
if IsDigits(tld) == ResultValid {
return ResultNotFqdn
}
// Short TLD
if len(tld) < 2 {
return ResultNotFqdn
}
for _, part := range parts {
// Cannot be more than 63 characters
if len(part) > 63 {
return ResultNotFqdn
}
// Check for valid characters
if !fqdnValidChars.MatchString(part) {
return ResultNotFqdn
}
// Should not start or end with a hyphen (-) character.
if part[0] == '-' || part[len(part)-1] == '-' {
return ResultNotFqdn
}
}
return ResultValid
}
// makeFqdn makes a checker function for the fqdn checker.
func makeFqdn(_ string) CheckFunc {
return checkFqdn
}
// checkFqdn checks if the given string is a fully qualified domain name.
func checkFqdn(value, _ reflect.Value) Result {
if value.Kind() != reflect.String {
panic("string expected")
}
return IsFqdn(value.String())
}