-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.go
313 lines (272 loc) · 10.5 KB
/
parser.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package whoisparser
import (
"regexp"
"strings"
)
// Parser represents a structure with regular expressions for specific whois sections
type Parser struct {
errorRegex *ParseErrorRegex
registrarRegex *RegistrarRegex
registrantRegex *RegistrantRegex
adminRegex *RegistrantRegex
techRegex *RegistrantRegex
billRegex *RegistrantRegex
skipWordList []string
}
// Parse parses whois text
func (p *Parser) Parse(text string) *Record {
record := &Record{
ErrCode: p.getErrCode(text),
}
if record.ErrCode != ErrCodeNoError {
return record
}
record.Registrar = parseRegistrar(&text, p.registrarRegex, p.skipWordList)
record.Registrant = parseRegistrant(&text, p.registrantRegex, p.skipWordList)
record.Admin = parseRegistrant(&text, p.adminRegex, p.skipWordList)
record.Tech = parseRegistrant(&text, p.techRegex, p.skipWordList)
record.Bill = parseRegistrant(&text, p.billRegex, p.skipWordList)
return record
}
// getErrCode searches for request errors in provided text and returns request error flag
func (p *Parser) getErrCode(text string) ErrCode {
if len(text) == 0 {
return ErrCodeEmptyWhois
}
if strings.Contains(text, "This TLD has no whois server, but you can access the whois database at") {
return ErrCodeTldHasNoServer
}
if p.errorRegex == nil {
return ErrCodeNoErrorRegex
}
if p.errorRegex.MalformedRequest != nil && len(p.errorRegex.MalformedRequest.FindString(text)) > 0 {
return ErrCodeMalformedRequest
}
if p.errorRegex.NoSuchDomain != nil && len(p.errorRegex.NoSuchDomain.FindString(text)) > 0 {
return ErrCodeNoSuchDomain
}
if p.errorRegex.RateLimit != nil && len(p.errorRegex.RateLimit.FindString(text)) > 0 {
return ErrCodeRequestRateLimit
}
return ErrCodeNoError
}
func parseRegistrar(text *string, re *RegistrarRegex, skipWordList []string) *Registrar {
if re == nil {
return nil
}
registrar := &Registrar{}
fillIfFound(®istrar.CreatedDate, re.CreatedDate, text, skipWordList)
fillIfFound(®istrar.DomainDNSSEC, re.DomainDNSSEC, text, skipWordList)
fillIfFound(®istrar.DomainID, re.DomainID, text, skipWordList)
fillIfFound(®istrar.DomainName, re.DomainName, text, skipWordList)
fillIfFound(®istrar.DomainStatus, re.DomainStatus, text, skipWordList)
fillIfFound(®istrar.Emails, re.Emails, text, skipWordList)
fillIfFound(®istrar.ExpirationDate, re.ExpirationDate, text, skipWordList)
fillIfFound(®istrar.NameServers, re.NameServers, text, skipWordList)
fillIfFound(®istrar.ReferralURL, re.ReferralURL, text, skipWordList)
fillIfFound(®istrar.RegistrarID, re.RegistrarID, text, skipWordList)
fillIfFound(®istrar.RegistrarName, re.RegistrarName, text, skipWordList)
fillIfFound(®istrar.UpdatedDate, re.UpdatedDate, text, skipWordList)
fillIfFound(®istrar.WhoisServer, re.WhoisServer, text, skipWordList)
if *registrar == (Registrar{}) {
return nil
}
return registrar
}
func parseRegistrant(text *string, re *RegistrantRegex, skipWordList []string) *Registrant {
if re == nil {
return nil
}
registrant := &Registrant{}
fillIfFound(®istrant.ID, re.ID, text, skipWordList)
fillIfFound(®istrant.Name, re.Name, text, skipWordList)
fillIfFound(®istrant.Organization, re.Organization, text, skipWordList)
if re.Address == nil {
fillIfFound(®istrant.Street, re.Street, text, skipWordList)
fillIfFound(®istrant.StreetExt, re.StreetExt, text, skipWordList)
fillIfFound(®istrant.City, re.City, text, skipWordList)
fillIfFound(®istrant.Province, re.Province, text, skipWordList)
fillIfFound(®istrant.PostalCode, re.PostalCode, text, skipWordList)
fillIfFound(®istrant.Country, re.Country, text, skipWordList)
} else {
fillGeoAddress(registrant, re.Address, text, skipWordList)
}
fillIfFound(®istrant.Phone, re.Phone, text, skipWordList)
fillIfFound(®istrant.PhoneExt, re.PhoneExt, text, skipWordList)
fillIfFound(®istrant.Fax, re.Fax, text, skipWordList)
fillIfFound(®istrant.FaxExt, re.FaxExt, text, skipWordList)
fillIfFound(®istrant.Email, re.Email, text, skipWordList)
if *registrant == (Registrant{}) {
return nil
}
return registrant
}
func fillGeoAddress(registrant *Registrant, re *regexp.Regexp, text *string, skipWordList []string) {
if re != nil {
var (
i int
name string
skipWord string
skip = false
regexMatches = re.FindStringSubmatch(*text)
resultMap = make(map[string]string)
)
for i, name = range re.SubexpNames() {
if i == 0 || name == "" {
continue
}
for _, skipWord = range skipWordList {
if skip = skipWord == regexMatches[i]; skip {
break
}
}
if skip {
skip = false
continue
}
resultMap[name] = regexMatches[i]
}
registrant.Street = resultMap["street"]
registrant.City = resultMap["city"]
registrant.PostalCode = resultMap["postalCode"]
registrant.StreetExt = resultMap["streetExt"]
registrant.Province = resultMap["province"]
registrant.Country = resultMap["country"]
}
}
// Parse parses whois text for specified domain.
// Domain is used to identify the domain zone and
// to choose the parser should be used for this zone
func Parse(domain string, text string) *Record {
return parserFor(domain).Parse(text)
}
func parserFor(domain string) IParser {
for zone, parser := range parsers {
if strings.HasSuffix(domain, zone) {
return parser
}
}
return &DefaultParser
}
func fillIfFound(field *string, re *regexp.Regexp, text *string, skipWordList []string) {
if re != nil {
var (
found bool
skip = false
skipWord string
val string
)
if val, found = findAndJoinStrings(text, re); found {
for _, skipWord = range skipWordList {
if skip = skipWord == val; skip {
break
}
}
if skip {
return
}
*field = val
}
}
}
func findAndJoinStrings(text *string, re *regexp.Regexp) (string, bool) {
if re == nil {
return "", false
}
var (
keys []string
values = make(map[string]struct{})
res []string
k string
)
for _, res = range re.FindAllStringSubmatch(*text, -1) {
values[res[1]] = struct{}{}
}
for k = range values {
keys = append(keys, k)
}
if len(keys) == 0 {
return "", false
}
return strings.Join(keys[:], ","), true
}
var parsers = map[string]*Parser{}
// RegisterParser is used to register parsers in catalog which is used to select parser for specific domain
func RegisterParser(zone string, parser *Parser) {
parsers[zone] = parser
}
// DefaultParser is used in case if no parser for TLD is found
var DefaultParser = Parser{
errorRegex: &ParseErrorRegex{
NoSuchDomain: regexp.MustCompile(`^No match for domain "`),
RateLimit: nil,
MalformedRequest: regexp.MustCompile(`^No match for "`),
},
registrarRegex: &RegistrarRegex{
CreatedDate: regexp.MustCompile(`(?i)Creation Date: +(.+)`),
DomainDNSSEC: regexp.MustCompile(`(?i)dnssec: +([\S]+)`),
DomainID: regexp.MustCompile(`(?i)Registry Domain ID: +(.+)`),
DomainName: regexp.MustCompile(`(?i)Domain Name: +(.+)`),
DomainStatus: regexp.MustCompile(`(?i)Status: +(\w+).*`),
Emails: regexp.MustCompile(`(?i)Registrar Abuse Contact Email: +(` + EmailRegex + `)`),
ExpirationDate: regexp.MustCompile(`(?i)Expir\w+ Date: +(.+)`),
NameServers: regexp.MustCompile(`(?i)Name Server: +(.+)`),
ReferralURL: regexp.MustCompile(`(?i)Referral URL: +(.+)`),
RegistrarID: regexp.MustCompile(`(?i)Registrar IANA ID: +(.+)`),
RegistrarName: regexp.MustCompile(`(?i)Registrar: +(.+)`),
UpdatedDate: regexp.MustCompile(`(?i)Updated Date: +(.+)`),
WhoisServer: regexp.MustCompile(`(?i)Whois Server: +(.+)`),
},
registrantRegex: &RegistrantRegex{
ID: regexp.MustCompile(`(?i)Registry Registrant ID: +(.+)`),
Name: regexp.MustCompile(`(?i)Registrant Name: +(.+)`),
Organization: regexp.MustCompile(`(?i)Registrant\s*Organization: +(.+)`),
Street: regexp.MustCompile(`(?i)Registrant Street: +(.+)`),
StreetExt: nil,
City: regexp.MustCompile(`(?i)Registrant City: +(.+)`),
Province: regexp.MustCompile(`(?i)Registrant State/Province: +(.+)`),
PostalCode: regexp.MustCompile(`(?i)Registrant Postal Code: +(.+)`),
Country: regexp.MustCompile(`(?i)Registrant Country: +(.+)`),
Phone: regexp.MustCompile(`(?i)Registrant Phone: +(.+)`),
PhoneExt: regexp.MustCompile(`(?i)Registrant Phone Ext: +(.+)`),
Fax: regexp.MustCompile(`(?i)Registrant Fax: +(.+)`),
FaxExt: regexp.MustCompile(`(?i)Registrant Fax Ext: +(.+)`),
Email: regexp.MustCompile(`(?i)Registrant Email: +(.+)`),
},
adminRegex: &RegistrantRegex{
ID: regexp.MustCompile(`(?i)Registry Admin ID: +(.+)`),
Name: regexp.MustCompile(`(?i)Admin Name: +(.+)`),
Organization: regexp.MustCompile(`(?i)Admin\s*Organization: +(.+)`),
Street: regexp.MustCompile(`(?i)Admin Street: +(.+)`),
StreetExt: nil,
City: regexp.MustCompile(`(?i)Admin City: +(.+)`),
Province: regexp.MustCompile(`(?i)Admin State/Province: +(.+)`),
PostalCode: regexp.MustCompile(`(?i)Admin Postal Code: +(.+)`),
Country: regexp.MustCompile(`(?i)Admin Country: +(.+)`),
Phone: regexp.MustCompile(`(?i)Admin Phone: +(.+)`),
PhoneExt: regexp.MustCompile(`(?i)Admin Phone Ext: +(.+)`),
Fax: regexp.MustCompile(`(?i)Admin Fax: +(.+)`),
FaxExt: regexp.MustCompile(`(?i)Admin Fax Ext: +(.+)`),
Email: regexp.MustCompile(`(?i)Admin Email: +(.+)`),
},
techRegex: &RegistrantRegex{
ID: regexp.MustCompile(`(?i)Registry Tech ID: +(.+)`),
Name: regexp.MustCompile(`(?i)Tech Name: +(.+)`),
Organization: regexp.MustCompile(`(?i)Tech\s*Organization: +(.+)`),
Street: regexp.MustCompile(`(?i)Tech Street: +(.+)`),
StreetExt: nil,
City: regexp.MustCompile(`(?i)Tech City: +(.+)`),
Province: regexp.MustCompile(`(?i)Tech State/Province: +(.+)`),
PostalCode: regexp.MustCompile(`(?i)Tech Postal Code: +(.+)`),
Country: regexp.MustCompile(`(?i)Tech Country: +(.+)`),
Phone: regexp.MustCompile(`(?i)Tech Phone: +(.+)`),
PhoneExt: regexp.MustCompile(`(?i)Tech Phone Ext: +(.+)`),
Fax: regexp.MustCompile(`(?i)Tech Fax: +(.+)`),
FaxExt: regexp.MustCompile(`(?i)Tech Fax Ext: +(.+)`),
Email: regexp.MustCompile(`(?i)Tech Email: +(.+)`),
},
skipWordList: []string{
"REDACTED FOR PRIVACY",
"Please query the RDDS service of the Registrar of Record identified in this output for information on how to contact the Registrant, Admin, or Tech contact of the queried domain name.",
},
}