-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwhois.go
289 lines (263 loc) · 8.58 KB
/
whois.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
package udig
import (
"bufio"
"bytes"
"io"
"strings"
"github.com/domainr/whois"
)
// Expect to receive a reader to text with 3 parts:
// 1. Key-value pairs separated by colon (":")
// 2. A line `>>> Last update of WHOIS database: [date]<<<`
// 3. Follow by an empty line, then free text of the legal disclaimers.
func parseWhoisResponse(reader io.Reader) (contacts []WhoisContact) {
scanner := bufio.NewScanner(reader)
contact := WhoisContact{}
var lineNumber int
for lineNumber = 1; scanner.Scan(); lineNumber++ {
// Grab the line and clean it.
line := strings.Trim(scanner.Text(), " \n\r\t")
line = strings.ToLower(line)
if line == "" {
// Empty line usually separates contacts -> create a new one.
if !contact.IsEmpty() {
contacts = append(contacts, contact)
contact = WhoisContact{}
}
continue
} else if line[0] == '%' {
// Comment/disclaimer -> skip.
continue
} else if strings.Index(line, ">>> last update of whois database") == 0 {
// Last line -> break.
if !contact.IsEmpty() {
contacts = append(contacts, contact)
}
break
}
// Parse the individual parts.
parts := strings.SplitN(line, ":", 2)
if len(parts) != 2 {
// Invalid line -> skip.
continue
}
key := strings.Trim(parts[0], " \n\r\t")
value := strings.Trim(parts[1], " \n\r\t")
if key == "" || value == "" {
// No key/value -> skip.
continue
}
switch key {
case "registry domain id":
setOrAppendString(&contact.RegistryDomainId, value)
break
case "registrant":
setOrAppendString(&(contact.Registrant), value)
break
case "registrant organization":
setOrAppendString(&contact.RegistrantOrganization, value)
break
case "registrant state/province":
setOrAppendString(&contact.RegistrantStateProvince, value)
break
case "registrant country":
setOrAppendString(&contact.RegistrantCountry, value)
break
case "registrar":
setOrAppendString(&contact.Registrar, value)
break
case "registrar iana id":
setOrAppendString(&contact.RegistrarIanaId, value)
break
case "registrar whois server":
setOrAppendString(&contact.RegistrarWhoisServer, value)
break
case "registrar url":
setOrAppendString(&contact.RegistrarUrl, value)
break
case "creation date":
setOrAppendString(&contact.CreationDate, value)
break
case "updated date":
setOrAppendString(&contact.UpdatedDate, value)
break
case "registered":
setOrAppendString(&contact.Registered, value)
break
case "changed":
setOrAppendString(&contact.Changed, value)
break
case "expire":
setOrAppendString(&contact.Expire, value)
break
case "nsset":
setOrAppendString(&contact.NSSet, value)
break
case "contact":
setOrAppendString(&contact.Contact, value)
break
case "name":
setOrAppendString(&contact.Name, value)
break
case "address":
setOrAppendString(&contact.Address, value)
break
}
}
return contacts
}
func setOrAppendString(target *string, value string) {
if *target != "" {
value = *target + ", " + value
}
*target = value
}
/////////////////////////////////////////
// WHOIS RESOLVER
/////////////////////////////////////////
// NewWhoisResolver creates a new WhoisResolver instance provisioned
// with sensible defaults.
func NewWhoisResolver() *WhoisResolver {
return &WhoisResolver{
Client: whois.NewClient(DefaultTimeout),
}
}
// Type returns "WHOIS".
func (resolver *WhoisResolver) Type() ResolutionType {
return TypeWHOIS
}
// ResolveDomain attempts to resolve a given domain using WHOIS query
// yielding a list of WHOIS contacts.
func (resolver *WhoisResolver) ResolveDomain(domain string) Resolution {
resolution := &WhoisResolution{
ResolutionBase: &ResolutionBase{query: domain},
}
// Prepare a request.
request, err := whois.NewRequest(domain)
if err != nil {
LogErr("%s: %s -> %s", TypeWHOIS, domain, err.Error())
return resolution
}
response, err := resolver.Client.Fetch(request)
if err != nil {
LogErr("%s: %s -> %s", TypeWHOIS, domain, err.Error())
return resolution
}
contacts := parseWhoisResponse(bytes.NewReader(response.Body))
for _, contact := range contacts {
resolution.Contacts = append(resolution.Contacts, contact)
}
return resolution
}
/////////////////////////////////////////
// WHOIS RESOLUTION
/////////////////////////////////////////
// Type returns "WHOIS".
func (res *WhoisResolution) Type() ResolutionType {
return TypeWHOIS
}
// Domains returns a list of domains discovered in records within this Resolution.
func (res *WhoisResolution) Domains() (domains []string) {
for _, contact := range res.Contacts {
domains = append(domains, DissectDomainsFromString(contact.RegistryDomainId)...)
domains = append(domains, DissectDomainsFromString(contact.Registrant)...)
domains = append(domains, DissectDomainsFromString(contact.RegistrantOrganization)...)
domains = append(domains, DissectDomainsFromString(contact.RegistrantStateProvince)...)
domains = append(domains, DissectDomainsFromString(contact.RegistrantCountry)...)
domains = append(domains, DissectDomainsFromString(contact.Registrar)...)
domains = append(domains, DissectDomainsFromString(contact.RegistrarIanaId)...)
domains = append(domains, DissectDomainsFromString(contact.RegistrarWhoisServer)...)
domains = append(domains, DissectDomainsFromString(contact.RegistrarUrl)...)
domains = append(domains, DissectDomainsFromString(contact.CreationDate)...)
domains = append(domains, DissectDomainsFromString(contact.UpdatedDate)...)
domains = append(domains, DissectDomainsFromString(contact.Registered)...)
domains = append(domains, DissectDomainsFromString(contact.Changed)...)
domains = append(domains, DissectDomainsFromString(contact.Expire)...)
domains = append(domains, DissectDomainsFromString(contact.NSSet)...)
domains = append(domains, DissectDomainsFromString(contact.Contact)...)
domains = append(domains, DissectDomainsFromString(contact.Name)...)
domains = append(domains, DissectDomainsFromString(contact.Address)...)
}
return domains
}
/////////////////////////////////////////
// WHOIS CONTACT
/////////////////////////////////////////
func (contact *WhoisContact) IsEmpty() bool {
return contact.RegistryDomainId == "" &&
contact.Registrant == "" &&
contact.RegistrantOrganization == "" &&
contact.RegistrantStateProvince == "" &&
contact.RegistrantCountry == "" &&
contact.Registrar == "" &&
contact.RegistrarIanaId == "" &&
contact.RegistrarWhoisServer == "" &&
contact.RegistrarUrl == "" &&
contact.CreationDate == "" &&
contact.UpdatedDate == "" &&
contact.Registered == "" &&
contact.Changed == "" &&
contact.Expire == "" &&
contact.NSSet == "" &&
contact.Contact == "" &&
contact.Name == "" &&
contact.Address == ""
}
func (contact *WhoisContact) String() string {
var entries []string
if contact.RegistryDomainId != "" {
entries = append(entries, "registry domain id: "+contact.RegistryDomainId)
}
if contact.Registrant != "" {
entries = append(entries, "registrant: "+contact.Registrant)
}
if contact.RegistrantOrganization != "" {
entries = append(entries, "registrant organization: "+contact.RegistrantOrganization)
}
if contact.RegistrantStateProvince != "" {
entries = append(entries, "registrant state/province: "+contact.RegistrantStateProvince)
}
if contact.RegistrantCountry != "" {
entries = append(entries, "registrant country: "+contact.RegistrantCountry)
}
if contact.Registrar != "" {
entries = append(entries, "registrar: "+contact.Registrar)
}
if contact.RegistrarIanaId != "" {
entries = append(entries, "registrar iana id: "+contact.RegistrarIanaId)
}
if contact.RegistrarWhoisServer != "" {
entries = append(entries, "registrar whois server: "+contact.RegistrarWhoisServer)
}
if contact.RegistrarUrl != "" {
entries = append(entries, "registrar url: "+contact.RegistrarUrl)
}
if contact.CreationDate != "" {
entries = append(entries, "creation date: "+contact.CreationDate)
}
if contact.UpdatedDate != "" {
entries = append(entries, "updated date: "+contact.UpdatedDate)
}
if contact.Registered != "" {
entries = append(entries, "registered: "+contact.Registered)
}
if contact.Changed != "" {
entries = append(entries, "changed: "+contact.Changed)
}
if contact.Expire != "" {
entries = append(entries, "expire: "+contact.Expire)
}
if contact.NSSet != "" {
entries = append(entries, "nsset: "+contact.NSSet)
}
if contact.Contact != "" {
entries = append(entries, "contact: "+contact.Contact)
}
if contact.Name != "" {
entries = append(entries, "name: "+contact.Name)
}
if contact.Address != "" {
entries = append(entries, "address: "+contact.Address)
}
return strings.Join(entries, ", ")
}