-
Notifications
You must be signed in to change notification settings - Fork 45
/
domain.go
60 lines (56 loc) · 1.43 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
package scheduler
import (
"regexp"
"strings"
)
var regexpForIP = regexp.MustCompile(`((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))`)
var regexpForDomains = []*regexp.Regexp{
// *.xx or *.xxx.xx
regexp.MustCompile(`\.(com|com\.\w{2})$`),
regexp.MustCompile(`\.(gov|gov\.\w{2})$`),
regexp.MustCompile(`\.(net|net\.\w{2})$`),
regexp.MustCompile(`\.(org|org\.\w{2})$`),
// *.xx
regexp.MustCompile(`\.me$`),
regexp.MustCompile(`\.biz$`),
regexp.MustCompile(`\.info$`),
regexp.MustCompile(`\.name$`),
regexp.MustCompile(`\.mobi$`),
regexp.MustCompile(`\.so$`),
regexp.MustCompile(`\.asia$`),
regexp.MustCompile(`\.tel$`),
regexp.MustCompile(`\.tv$`),
regexp.MustCompile(`\.cc$`),
regexp.MustCompile(`\.co$`),
regexp.MustCompile(`\.\w{2}$`),
}
// getPrimaryDomain 用于获取给定主机名的主域名。
func getPrimaryDomain(host string) (string, error) {
host = strings.TrimSpace(host)
if host == "" {
return "", genError("empty host")
}
if regexpForIP.MatchString(host) {
return host, nil
}
var suffixIndex int
for _, re := range regexpForDomains {
pos := re.FindStringIndex(host)
if pos != nil {
suffixIndex = pos[0]
break
}
}
if suffixIndex > 0 {
var pdIndex int
firstPart := host[:suffixIndex]
index := strings.LastIndex(firstPart, ".")
if index < 0 {
pdIndex = 0
} else {
pdIndex = index + 1
}
return host[pdIndex:], nil
}
return "", genError("unrecognized host")
}