-
Notifications
You must be signed in to change notification settings - Fork 4
/
domain.go
78 lines (75 loc) · 2.58 KB
/
domain.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
72
73
74
75
76
77
78
package utils
import (
"errors"
"fmt"
"strings"
"unicode/utf8"
)
// GetDomain 检查并返回清除前后空白的域名
func GetDomain(name string) string {
name = strings.TrimSpace(name)
if CheckDomain(name) != nil {
return ""
}
return name
}
// CheckDomain returns an error if the host name is not valid.
// See https://tools.ietf.org/html/rfc1034#section-3.5 and
// https://tools.ietf.org/html/rfc1123#section-2.
// Ref: chmike/domain
func CheckDomain(name string) error {
if len(name) == 0 {
return errors.New("domain name is empty")
}
if name[len(name)-1] == '.' {
if len(name) > 254 {
return fmt.Errorf("domain name length is %d, can't exceed 254 with a trailing dot", len(name))
}
name = name[:len(name)-1] // drop valid trailing dot
if len(name) == 0 {
return errors.New("domain name is a single dot")
}
} else if len(name) > 253 {
return fmt.Errorf("domain name length is %d, can't exceed 253 without a trailing dot", len(name))
}
var l int
for i := 0; i < len(name); i++ {
b := name[i]
if b == '.' {
// check domain labels validity
switch {
case i == l:
return fmt.Errorf("domain has an empty label at offset %d", l)
case i-l > 63:
return fmt.Errorf("domain byte length of label '%s' is %d, can't exceed 63", name[l:i], i-l)
case name[l] == '-':
return fmt.Errorf("domain label '%s' at offset %d begins with a hyphen", name[l:i], l)
case name[i-1] == '-':
return fmt.Errorf("domain label '%s' at offset %d ends with a hyphen", name[l:i], l)
}
l = i + 1
continue
}
// test label character validity, note: tests are ordered by decreasing validity frequency
if !(b >= 'a' && b <= 'z' || b >= '0' && b <= '9' || b == '-' || b >= 'A' && b <= 'Z') {
// show the printable unicode character starting at byte offset i
c, _ := utf8.DecodeRuneInString(name[i:])
if c == utf8.RuneError {
return fmt.Errorf("domain has invalid rune at offset %d", i)
}
return fmt.Errorf("domain has invalid character '%c' at offset %d", c, i)
}
}
// check top level domain validity
switch {
case len(name)-l > 63:
return fmt.Errorf("domain's top level domain '%s' has byte length %d, can't exceed 63", name[l:], len(name)-l)
case name[l] == '-':
return fmt.Errorf("domain's top level domain '%s' at offset %d begin with a hyphen", name[l:], l)
case name[len(name)-1] == '-':
return fmt.Errorf("domain's top level domain '%s' at offset %d ends with a hyphen", name[l:], l)
case name[l] >= '0' && name[l] <= '9':
return fmt.Errorf("domain's top level domain '%s' at offset %d begins with a digit", name[l:], l)
}
return nil
}