-
Notifications
You must be signed in to change notification settings - Fork 41
/
util.go
270 lines (220 loc) · 9.02 KB
/
util.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package util
import "strings"
import "gopkg.in/hlandau/madns.v2/merr"
import "fmt"
import "regexp"
import "net/mail"
// Split a domain name a.b.c.d.e into parts e (the head) and a.b.c.d (the rest).
func SplitDomainHead(name string) (head, rest string) {
if len(name) > 0 && name[len(name)-1] == '.' {
name = name[0 : len(name)-1]
}
parts := strings.Split(name, ".")
head = parts[len(parts)-1]
if len(parts) >= 2 {
rest = strings.Join(parts[0:len(parts)-1], ".")
}
return
}
// Split a domain name a.b.c.d.e into parts a (the tail) and b.c.d.e (the rest).
func SplitDomainTail(name string) (tail, rest string) {
s := strings.SplitN(name, ".", 2)
if len(s) == 1 {
return s[0], ""
}
return s[0], s[1]
}
// For domains of the form "Y1.Y2...Yn.ANCHOR.X1.X2...Xn.", returns the following values:
//
// basename is the name appearing directly beneath the anchor (Yn).
//
// subname contains any additional labels appearing beneath the anchor (Y1 through Y(n-1)),
// separated by dots.
//
// rootname contains ANCHOR through Xn inclusive, separated by dots.
//
// If no label corresponds to ANCHOR, an error is returned.
// If ANCHOR is the first label, basename is an empty string.
//
// Examples, where anchor="bit":
//
// "a.b.c.d." -> merr.ErrNotInZone
// "a.b.c.d.bit." -> subname="a.b.c", basename="d", rootname="bit"
// "d.bit." -> subname="", basename="d", rootname="bit"
// "bit." -> subname="", basename="", rootname="bit"
// "bit.x.y.z." -> subname="", basename="", rootname="bit.x.y.z"
// "d.bit.x.y.z." -> subname="", basename="d", rootname="bit.x.y.z"
// "c.d.bit.x.y.z." -> subname="c", basename="d", rootname="bit.x.y.z"
// "a.b.c.d.bit.x.y.z." -> subname="a.b.c", basename="d", rootname="bit.x.y.z"
func SplitDomainByFloatingAnchor(qname, anchor string) (subname, basename, rootname string, err error) {
qname = strings.TrimRight(qname, ".")
parts := strings.Split(qname, ".")
if len(parts) < 2 {
if parts[0] != anchor {
err = merr.ErrNotInZone
return
}
rootname = parts[0]
return
}
for partIndex := len(parts) - 1; partIndex >= 0; partIndex-- {
v := parts[partIndex]
// scanning for rootname
if v == anchor {
if partIndex == 0 {
// partIndex is already zero, so we have something like bit.x.y.z.
rootname = qname
return
}
rootname = strings.Join(parts[partIndex:], ".")
basename = parts[partIndex-1]
subname = strings.Join(parts[0:partIndex-1], ".")
return
}
}
err = merr.ErrNotInZone
return
}
// Convert a domain name basename (e.g. "example") to a Namecoin domain name
// key name ("d/example").
func BasenameToNamecoinKey(basename string) (string, error) {
if !ValidateDomainLabel(basename) {
return "", fmt.Errorf("invalid domain name")
}
return basenameToNamecoinKey(basename), nil
}
func basenameToNamecoinKey(basename string) string {
return "d/" + basename
}
// Convert a Namecoin domain name key name (e.g. "d/example") to a domain name
// basename ("example").
func NamecoinKeyToBasename(key string) (string, error) {
if !strings.HasPrefix(key, "d/") {
return "", fmt.Errorf("not a valid domain name key")
}
key = key[2:]
if !ValidateDomainLabel(key) {
return "", fmt.Errorf("not a valid domain name key")
}
return key, nil
}
// An owner name is any technically valid DNS name. RFC 2181 permits binary
// data in DNS labels (!), but this is ridiculous. The conventions which appear
// to be enforced by web browsers are used.
const sre_ownerLabel = `([a-z0-9_]|[a-z0-9_][a-z0-9_-]{0,61}[a-z0-9_])`
const sre_ownerName = `(` + sre_ownerLabel + `\.)*` + sre_ownerLabel + `\.?` // must check length
const sre_relOwnerName = `(|@|(` + sre_ownerLabel + `\.)*` + sre_ownerLabel + `(\.@?)?)` // must check length
var re_ownerLabel = regexp.MustCompilePOSIX(`^` + sre_ownerLabel + `$`)
var re_ownerName = regexp.MustCompilePOSIX(`^` + sre_ownerName + `$`)
var re_relOwnerName = regexp.MustCompilePOSIX(`^` + sre_relOwnerName + `$`)
const sre_domainLabel = `(xn--)?([a-z0-9]+-)*[a-z0-9]+` // must check length
const sre_domainName = `(` + sre_domainLabel + `\.)*` + sre_domainLabel + `\.?` // must check length
const sre_relDomainName = `(|@|(` + sre_domainLabel + `\.)*` + sre_domainLabel + `(\.@?)?)` // must check length
var re_domainLabel = regexp.MustCompilePOSIX(`^` + sre_domainLabel + `$`)
var re_domainName = regexp.MustCompilePOSIX(`^` + sre_domainName + `$`)
var re_relDomainName = regexp.MustCompilePOSIX(`^` + sre_relDomainName + `$`)
const sre_hostLabel = `([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])` // must check length
const sre_hostName = sre_hostLabel + `(\.` + sre_hostLabel + `)*\.?` // must check length
const sre_relHostName = `(|@|(` + sre_hostLabel + `\.)*` + sre_hostLabel + `(\.@?)?)` // must check length
var re_hostLabel = regexp.MustCompilePOSIX(`^` + sre_hostLabel + `$`)
var re_hostName = regexp.MustCompilePOSIX(`^` + sre_hostName + `$`)
var re_relHostName = regexp.MustCompilePOSIX(`^` + sre_relHostName + `$`)
func ValidateLabelLength(label string) bool {
return len(label) <= 63
}
func ValidateNameLength(name string) bool {
return len(name) <= 255 || (name[len(name)-1] == '.' && len(name) <= 256)
}
func ValidateOwnerLabel(label string) bool {
return ValidateLabelLength(label) && re_ownerLabel.MatchString(label)
}
func ValidateServiceName(label string) bool {
return len(label) <= 62 && ValidateOwnerLabel(label)
}
func ValidateOwnerName(name string) bool {
return ValidateNameLength(name) && re_ownerName.MatchString(name)
}
func ValidateRelOwnerName(name string) bool {
return ValidateNameLength(name) && re_relOwnerName.MatchString(name)
}
func ValidateDomainLabel(label string) bool {
return ValidateLabelLength(label) && re_domainLabel.MatchString(label)
}
func ValidateDomainName(name string) bool {
return ValidateNameLength(name) && re_domainName.MatchString(name)
}
func ValidateRelDomainName(name string) bool {
return ValidateNameLength(name) && re_relDomainName.MatchString(name)
}
func ValidateHostLabel(label string) bool {
return ValidateLabelLength(label) && re_hostLabel.MatchString(label)
}
func ValidateHostName(name string) bool {
return ValidateNameLength(name) && re_hostName.MatchString(name)
}
func ValidateRelHostName(name string) bool {
return ValidateNameLength(name) && re_relHostName.MatchString(name)
}
/*
var re_ownerName = regexp.MustCompilePOSIX(`^([a-z0-9_][a-z0-9_-]{0,62}\.)*[a-z0-9_][a-z0-9_-]{0,62}\.?$`)
var re_ownerLabel = regexp.MustCompilePOSIX(`^([a-z0-9_]{1,2}|[a-z0-9_][a-z0-9_-]{0,61}[a-z0-9_])$`)
// A domain name is an owner name complying with the standard rules applied to
// domain names: No consecutive hyphens except as allowed by IDN, no underscores.
var re_domainName = regexp.MustCompilePOSIX(`^(([a-z0-9]+-)*[a-z0-9]+\.)*$`)
// This is used to validate NS records, targets in SRV records, etc. In these cases
// an IP address is not allowed. Therefore this regex must exclude all-numeric domain names.
// This is done by requiring the final part to start with an alphabetic character.
var re_hostName = regexp.MustCompilePOSIX(`^(([a-z0-9_][a-z0-9_-]{0,62}\.)*[a-z_][a-z0-9_-]{0,62}\.?|\.)$`)
var re_label = regexp.MustCompilePOSIX(`^[a-z_][a-z0-9_-]*$`)
var re_serviceName = regexp.MustCompilePOSIX(`^[a-z_][a-z0-9_-]*$`)
var re_domainNameLabel = regexp.MustCompilePOSIX(`^(xn--)?[a-z0-9]+(-[a-z0-9]+)*$`)
var re_ownerName = regexp.MustCompilePOSIX(`^(|@|([a-z0-9_-]{1,63}\.)*[a-z0-9_-]{1,63}(\.@?)?)$`)
func ValidateHostName(name string) bool {
name = dns.Fqdn(name)
return len(name) <= 255 && re_hostName.MatchString(name)
}
func ValidateLabel(name string) bool {
return len(name) <= 63 && re_label.MatchString(name)
}
func ValidateServiceName(name string) bool {
return len(name) < 63 && re_serviceName.MatchString(name)
}
func ValidateDomainNameLabel(name string) bool {
return len(name) <= 63 && re_domainNameLabel.MatchString(name)
}
func ValidateOwnerName(name string) bool {
return len(name) <= 255 && re_ownerName.MatchString(name)
}*/
func ValidateEmail(email string) bool {
addr, err := mail.ParseAddress(email)
if addr == nil || err != nil {
return false
}
return addr.Name == ""
}
// Takes a name in the form "d/example" or "example.bit" and converts it to the
// bareword "example". Returns an error if the input is in neither form.
func ParseFuzzyDomainName(name string) (string, error) {
if strings.HasPrefix(name, "d/") {
return NamecoinKeyToBasename(name)
}
if len(name) > 0 && name[len(name)-1] == '.' {
name = name[0 : len(name)-1]
}
if strings.HasSuffix(name, ".bit") {
name = name[0 : len(name)-4]
if !ValidateDomainLabel(name) {
return "", fmt.Errorf("invalid domain name")
}
return name, nil
}
return "", fmt.Errorf("invalid domain name")
}
func ParseFuzzyDomainNameNC(name string) (bareName string, namecoinKey string, err error) {
name, err = ParseFuzzyDomainName(name)
if err != nil {
return "", "", err
}
return name, basenameToNamecoinKey(name), nil
}
// © 2014 Hugo Landau <hlandau@devever.net> GPLv3 or later